// JavaScript Document

function standard_on_load()
{
		var config = {    
			 sensitivity: 2, // number = sensitivity threshold (must be 1 or higher)    
			 interval: 200, // number = milliseconds for onMouseOver polling interval    
			 over: showContent, // function = onMouseOver callback (REQUIRED)    
			 timeout: 200, // number = milliseconds delay before onMouseOut    
			 out: hideContent // function = onMouseOut callback (REQUIRED)    
		};
		
		jQuery("#ssi_nav li.main_nav_sec").hoverIntent( config );
		jQuery("#ssi_nav li .nav_link").append('<img src="/store/skin/frontend/default/ssi/images/nav-arrow.png" class="nav_arrow" />');
		jQuery("#ssi_nav li .add_triangle").append('<img src="/store/skin/frontend/default/ssi/images/triangles_03_short.gif" />');
		
		jQuery("#nav_cart").bind("click", function() { toggleCart(); }).attr("href", "javascript:void(0)");
		
		//jQuery(".mask_this").prepend('<div class="mask"></div>');
		jQuery(".mask_this").prepend('<div class="mask_bg"></div>');
		
		jQuery('.boxgrid.caption').hover(function(){
				if (jQuery('.boxcaption P', this).length)
					jQuery(".cover", this).stop().animate({top:'0px'},{queue:false,duration:260});
			}, function() {
				if (jQuery('.boxcaption P', this).length)
					jQuery(".cover", this).stop().animate({top:'65px'},{queue:false,duration:260});
			});
}


function contactFormValidate(data)
{
	var valid = true;
	jQuery("#nav-contact .invalid").removeClass("invalid");
	
	if (data.name == "") {
		valid = false;
		jQuery("#contact-name").addClass("invalid");
	}
	
	if (data.emailreal == "") {
		valid = false;
		jQuery("#contact-email-real").addClass("invalid");
	}
	
	if (data.message == "") {
		valid = false;
		jQuery("#contact-message").addClass("invalid");
	}

	if (valid == false)
		contactShowError("Opps! Some of the required fields are missing.");

	return valid;
	
}

function contactFormSend()
{
	jQuery("#nav-contact .msg_error").remove();
	
	
	// assmble the data
	data = {
			reason: jQuery("#contact-reason").val(),
			name: jQuery("#contact-name").val(),
			company: jQuery("#contact-company").val(),
			emailreal: jQuery("#contact-email-real").val(),
			email: jQuery("#contact-email").val(),
			phone: jQuery("#contact-phone").val(),
			onpage: jQuery("#contact-onpage").val(),
			message: jQuery("#contact-message").val()
		};
	
	// validate
	if (contactFormValidate(data)) {
		
		jQuery("#nav-contact .nav_contact_submit").hide();
		jQuery("#nav-contact .nav_contact_loading").show();
	
		jQuery.post("/ajax/email/contact/", data, function(res){
		
			
		
			if (res.status == "error") {
				contactShowError(res.message);
			}
			else {
				contactShowSuccess();
			}
			
		}, "json");
	}

	
	return false;
	
}

function formSend(formId)
{
	jQuery("#" + formId + " .msg_error").remove();
	
	data = {
			reason: jQuery("#" + formId + "-reason").val(),
			name: jQuery("#" + formId + "-name").val(),
			emailreal: jQuery("#" + formId + "-email-real").val(),
			email: jQuery("#" + formId + "-email").val(),
			phone: jQuery("#" + formId + "-phone").val(),
			onpage: jQuery("#" + formId + "-onpage").val(),
			message: jQuery("#" + formId + "-message").val()
		};
	
	if (validateForm("#" + formId))
	{
		jQuery("#" + formId + " .nav_contact_submit").hide();
		jQuery("#" + formId + " .nav_contact_loading").show();
	
		jQuery.post("/ajax/email/contact/", data, function(res){
	
			if (res.status == "error") {
				jQuery("#" + formId + " .nav_contact_submit").show();
				jQuery("#" + formId + " .nav_contact_loading").hide();
				
				jQuery("#" + formId).prepend('<div class="msg_error">' + res.message + '</div>');
			}
			else {
				jQuery("#" + formId).fadeOut(function(){ 
													  jQuery("#" + formId +"-success").fadeIn();
													  });
			}
			
		}, "json");
	}
}

function contactShowError(msg)
{
	jQuery("#nav-contact .nav_contact_submit").show();
	jQuery("#nav-contact .nav_contact_loading").hide();
	
	jQuery("#nav-contact .nav_contact_form").prepend('<div class="msg_error">' + msg + '</div>');
}

function contactShowSuccess()
{
	jQuery.get("/_includes/nav_contact_success/", function(res){
			// hide existing form
			jQuery("#nav-contact .nav_contact_col2").fadeOut();
			jQuery("#nav-contact .nav_contact_col1").fadeOut(function(){
				// show success message
				jQuery("#nav-contact .nav_contact_submit").show();
				jQuery("#nav-contact .nav_contact_loading").hide();
			
				jQuery("#nav-contact .nav_contact_form").prepend(res);
			});
	});	
}

function contactShowForm()
{
	jQuery("#nav-contact-success").hide();
	jQuery("#nav-contact FORM").show();
}

function validateForm(elId)
{
    var isValid = true;
	
	jQuery(elId + " .req").each(function(i){
		if (jQuery(this).val() == "") {
			isValid = false;
			jQuery(this).addClass("invalid");
		}
	});
	
	if (isValid == false)
		jQuery(elId).prepend('<div class="msg_error msg">Some required fields are missing.  See Below.</div>');
	
	return isValid;
}

function expand(section)
{
	jQuery('#exp_' + section).slideToggle('slow', function()
		{
			if (jQuery('#exp_' + section + ':visible').length)
			{
				jQuery('#exp_' + section + '_link').html('Collapse Section').toggleClass('more').toggleClass('less');
			}
			else
			{
				jQuery('#exp_' + section + '_link').html('Expand Section').toggleClass('more').toggleClass('less');
			}
		});
}

function addValueArrayRow(){
	jQuery(".value_array TBODY").append("<tr>" + jQuery(".value_array TR:last").html() + "</tr>");
}

function countryCheck() {
	// first do a cookie check
	if (getCookie("ssi_country_stayput") != "yes") {
	
		jQuery.getJSON("/getipinfo.php", function(res) {
												   
			if (res.country_val == "GB" || res.country_val == "FR" || res.country_val == "DE")
			{
				// now lazy load the color box info
				jQuery("head").prepend('<link rel="stylesheet" type="text/css" href="/assets/threed-js-css-images/colorbox.css"><script type="text/javascript" src="/assets/threed-js-css-images/jquery.colorbox-min.js"></script>');
				
				jQuery.fn.colorbox({href:'/ajax/country_popup_' + res.country_val.toLowerCase() + '/'});
				
			}
			else if (res.country_val == "US" || res.country_val == "CA")
			{
				// set the cookies so we don't have to do an ajax call on every page.
				setCookie("ssi_country_stayput", "yes", 365);
			}
		});
	
	}
}

function countryStayput()
{
	// set a cookie 	
	setCookie("ssi_country_stayput", "yes", 365);
	jQuery.fn.colorbox.close()
}

// cookie functions
				
function setCookie(c_name,value,expiredays)
{
	var path = "/";
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+
	((expiredays==null) ? "" : ";expires="+exdate.toGMTString())+
	((path==null) ? "" : ";path=" + path);
}

function getCookie(c_name)
{
	if (document.cookie.length>0)
	  {
	  c_start=document.cookie.indexOf(c_name + "=");
	  if (c_start!=-1)
		{
		c_start=c_start + c_name.length+1;
		c_end=document.cookie.indexOf(";",c_start);
		if (c_end==-1) c_end=document.cookie.length;
		return unescape(document.cookie.substring(c_start,c_end));
		}
	  }
	return "";
}
