//Global Variables
var theMagsMainLink = "https://www.ostomysecrets.com/";

$(function(){
	var redirectToken;
	
	$(".shop_button:not(.btnFeature, .formSubmit, #osStyleEnclosingItemBox .shop_button, #addToCartButton .shop_button)").bind("click", function(){
		redirectToken =	returnTheRedirectToken($(this));
		redirectTheLink("mags", redirectToken);
	});
	
	$("#advanced_search_button").bind("click", function(){ 
		window.location = "http://ostomysecrets.myshopify.com/search";
	});
	
	$(".formSubmit").bind("click", function() {
		whichFormToSubmit($(this));
	});
});


/**
 * Setups the listeners for the Contact Forms.  
 * @todo change the listeners to be .... better :) 
 */

function whichFormToSubmit(element){
	
	var currentFormSubmit
	var action;
	
	$("#form_error").hide();
		
	currentFormSubmit = element.attr("id");
	
	if(currentFormSubmit === "tearpad_submit")
	    submitTheTearPadRequest();
	else if(currentFormSubmit ==="testimonial_submit")
		submitTheTestimonialForm();
	else if(currentFormSubmit ==="contact_submit")
		submitTheContactForm();
}


function returnTheRedirectToken(obj){
	
	var theClass = obj.attr("class");
	var splicedString = theClass.split(" ");
	var theToken;
	
	for(var i=0; i < splicedString.length; i++){
		
		if(splicedString[i].substring(0, 7) === "shopBtn"){
			theToken = splicedString[i].substring(8, splicedString[i].length);
			break;
		}		
	}
	
	return theToken;
}


function redirectTheLink(which, redirect){
	
	var link;
	
	switch(which){
		default :
			link = theMagsMainLink + redirect + ".php";
	}	
	
	window.location = link;
}

