function isdefined( variable)
{
    return (typeof(window[variable]) == "undefined")?  false: true;
}

$(document).ready( function(){
	//roll over function
	$(".roll-image").hover(
		function () {
			var img_orig = $(this).attr("src");
			var img_replace = img_orig.replace("_off.gif", "_on.gif");
			$(this).attr( "src", img_replace )
		},
		function () {
			var img_orig = $(this).attr("src");
			var img_replace = img_orig.replace("_on.gif", "_off.gif");
			$(this).attr( "src", img_replace )
		}
	);



	//Used to clear a form
	//eg $('#FormIdHere').clearForm();
	$.fn.clearForm = function() {
		this.each(function() {
			var type = this.type, tag = this.tagName.toLowerCase();

			if (tag == 'form')
				return $(':input',this).clearForm();
			if (type == 'text' || type == 'password' || tag == 'textarea')
				this.value = '';
			else if (type == 'checkbox' || type == 'radio')
				this.checked = false;
			else if (tag == 'select')
				this.selectedIndex = '';
		});
	};

	//Used to submit into ajax popup
	//eg $('#FormIdHere').submit_popup();
	$.fn.submit_popup = function() {
		this.each(function() {
			var type = this.type, tag = this.tagName.toLowerCase();

			if (tag == 'form')
				return $(':input',this).clearForm();
			if (type == 'text' || type == 'password' || tag == 'textarea')
				this.value = '';
			else if (type == 'checkbox' || type == 'radio')
				this.checked = false;
			else if (tag == 'select')
				this.selectedIndex = '';
		});
	};

	$(".thumb_cntr").hover(
    	function(){
      		$(this).children('p').show();
      	},
      	function(){
      		$(this).children('p').hide();
      	}
    );

    $("#nav li").hover(
    	function(){
    		var disp_nav = $(this).children('a').attr("id");
    		$("#sub" + disp_nav).show();
      	},
      	function(){
      		var disp_nav = $(this).children('a').attr("id");
      		$("#sub" + disp_nav).hide();
      	}
    );

    $("#subnav div").hover(
    	function(){
    		var main_nav = ($(this).attr("id").substr(3));
    		$("#" + main_nav).addClass("on");
    		$(this).show();
      	},
      	function(){
      		var main_nav = ($(this).attr("id").substr(3));
    		$("#" + main_nav).removeClass("on");
      		$(this).hide();
      	}
    );

    //tb_show("MY CAPTION", "/store/basket/ajax/?height=300&width=300", "");

    $.get("/store/basket/ajax/", {  },
	  function(data){
	  	$("#AJ_Basket").html(data);
	});

	$.get("/store/wishlist/ajax/", {  },
	  function(data){
	  	$("#AJ_Wishlist").html(data);
	});

// Decided to include the template rather to minimize ajax calls done on each page load.
// See ss_right_col.tpl
//	$.get("/newsletter/ajax/", {  },
//	  function(data){
//	  	$("#AJ_Newsletter").html(data);
//	});


	/* top login fields */
	$("#top_username").focus(function () {
		if($(this).val() == "Email"){
			$(this).val('');
		}
	});
	$("#top_username").blur(function () {
		if($(this).val() == ""){
			$(this).val('Email');
		}
	});
	$("#top_password").focus(function () {
			$(this).hide();
			$("#top_password2").show().focus();
	});
	$("#top_password2").blur(function () {
		if ($(this).val() == ''){
			$(this).hide();
			$("#top_password").show();
		}
	});
});


/* ------------ STORE FUNCTIONS ---------------*/
function pop_basket(){
	$.get("/store/basket/ajax/", {  },
	  function(data){
	    alert("Data Loaded: " + data);
	  });
}

function add_basket(prod_id){

	$("#AJ_Basket").html('Loading...');

	$.get("/store/basket/ajax/", { action: "add", p: prod_id },
	  function(data){
	    	$("#AJ_Basket").html(data);
	  }
	);
}

function add_wishlist(prod_id){

	$("#AJ_Wishlist").html('Loading...');

	$.get("/store/wishlist/ajax/", { action: "wish_add", p: prod_id },
	  function(data){
	    	$("#AJ_Wishlist").html(data);
	  }
	);
}

/* ------------ STORE FUNCTIONS END ---------------*/

/* ------------ NEWSLETTER SUBSCRIBE ----------------*/

function post_newsletter(){

	var post_name = $("#newsletter_name").val();
	var post_email = $("#newsletter_email").val();

	$("#AJ_Newsletter").html('Loading...');

	$.get("/newsletter/ajax/", { name: post_name, email: post_email, subscribe: "Yes" },
	  function(data){
	    	$("#AJ_Newsletter").html(data);
	  }
	);
}

/* ------------ NEWSLETTER SUBSCRIBE END ----------------*/

/* ------------ STORE PRODUCT COMMENT ----------------*/

function post_product_comment(post_id){

	var post_comment = $('textarea[name="product_comment"]').attr('value');
	var post_comment_anon = $("#product_comment_anonymous").attr('checked');

	$("#AJ_ProductComment").html('Loading...');

	$.get("/store/product/comment/ajax/", { id: post_id, comment: post_comment, anon: post_comment_anon, submit_data: "1" },
	  function(data){
	    	$("#AJ_ProductComment").html(data);
	  }
	);
}

/* ------------ STORE PRODUCT COMMENT END ----------------*/


/* ------------ REGISTER FUNCTIONS ----------------*/


function populate(thisform){

	if(thisform.elements["asabove"].checked){
		thisform.elements["data[users][0][del_addr_01]"].value = thisform.elements["data[users][0][phy_addr_01]"].value;
		thisform.elements["data[users][0][del_addr_02]"].value = thisform.elements["data[users][0][phy_addr_02]"].value;
		thisform.elements["data[users][0][del_city]"].value = thisform.elements["data[users][0][phy_city]"].value;
		thisform.elements["data[users][0][del_state]"].value = thisform.elements["data[users][0][phy_state]"].value;
		thisform.elements["data[users][0][del_code]"].value = thisform.elements["data[users][0][phy_code]"].value;
		thisform.elements["data[users][0][del_country_id]"].value = thisform.elements["data[users][0][phy_country_id]"].value;
	}else{
		thisform.elements["data[users][0][del_addr_01]"].value = "";
		thisform.elements["data[users][0][del_addr_02]"].value = "";
		thisform.elements["data[users][0][del_city]"].value = "";
		thisform.elements["data[users][0][del_state]"].value = "";
		thisform.elements["data[users][0][del_code]"].value = "";
		thisform.elements["data[users][0][del_country_id]"].value = "-1";
	}
}

/* ------------ REGISTER FUNCTIONS END----------------*/


/* ------------ MISC ----------------*/

function submitForm(formid, theaction){
	$('#'+formid).attr('action',theaction);
	$('#'+formid).submit();
}

function disable_button(id){
	$('#'+id).attr('disabled','true');
}

function submitEnter(myfield,e){
	var keycode;
	if (window.event) keycode = window.event.keyCode;
	else if (e) keycode = e.which;
	else return true;

	if (keycode == 13){
	   myfield.form.submit();
	   return false;
	}else{
	   return true;
	}
}


function browser(){
	this.dom = (document.getElementById) ? true : false;
	this.ns4 = (document.layers) ? true : false;
	this.ie = (document.all) ? true : false;
	this.ns6 = this.dom && !this.ie
	this.ie4 = this.ie && !this.dom;
	this.opera = (navigator.userAgent.indexOf("Opera") != -1);
	this.mac = (navigator.appVersion.indexOf("Mac") != -1);
	this.macie4 = this.ie4 || this.mac;
	this.dhtml = (this.dom || this.ns4 || this.ie || this.ie4 || this.opera || !this.mac) ? true : false;
	this.dhtml = (this.mac)?false:true;
	return this;
}

var bw = new browser();

function toggleTable(el,st) {
	var myelement = document.getElementById(el);

	if(st == 1){
		if(bw.ie){
			myelement.style.display = "inline"
		}else{
			myelement.style.display = "table-cell"
		}
	}else{
		myelement.style.display = "none"
	}
}
