var j = jQuery.noConflict();

j(document).ready(run);

var cta_email = null;

//prep the site
function run() {
    
	j.preloadCssImages();
	cta_email=j("#cta_email, #cta_email_icon");
	cta_email.bind("click", open_email_form);
	cta_button = j("#cta_email_submit");
	cta_button.bind("click", send_form);
	
	//j("a.closebox").show();
	
	Shadowbox.init({
        // let's skip the automatic setup because we don't have any
        // properly configured link elements on the page
        skipSetup: true
    });

    
    
}

function open_email_form()
{
	j("#cta_email, #cta_livechat").fadeOut(function(){
		j("#cta_email_form").fadeIn();
	});
	return false;
}

function send_form(){
	formdata = j("#cta_email_form input").serializeArray();

	if(formdata[0]["value"] == ""){
		window.alert("Please enter your first name.");
		return false;
	}

	if(formdata[1]["value"] == ""){
		window.alert("Please enter your last name.");
		return false;
	}
	
	if(checkemail(formdata[2]["value"]) == false){
		window.alert("Please enter a valid email address.");
		return false;
	}
	
//	j.get("/Newsletter.aspx", function(data){
//		pagedata = j(data).serializeArray();
//		for(var i in pagedata){
//			if(pagedata[i]["name"] == "__VIEWSTATE"){
//				viewstate = pagedata[i]["value"];
//			}
//			if(pagedata[i]["name"] == "__EVENTVALIDATION"){
//				eventval = pagedata[i]["value"]
//			}
//		}
//		
//		fielddata = {"TextBox1":formdata[0]["value"],"TextBox2":formdata[1]["value"],"TextBox3":formdata[2]["value"],"__VIEWSTATE":viewstate,"__EVENTVALIDATION":eventval,"Button1":"Signup"};
//		 j.ajax({
//		   url: "/Newsletter.aspx",
//		   processData: true,
//		   data: fielddata,
//		   error: ctaform_fail,
//		   success: ctaform_success,
//		   type: "POST"
//		 });			
//	});
	//fielddata = {"emailaddress":formdata[2]["value"],"firstname":formdata[0]["value"],"lastName":formdata[1]["value"],"sourceCode":getCookie("IDENTIFIER"),"sourceTypeId":"13","titleCode":"Y"}
	fielddata = "<NewsletterSignup><emailAddress>"+formdata[2]["value"]+"</emailAddress>"
	fielddata = fielddata+"<firstName>"+formdata[0]["value"]+"</firstName>"
	fielddata = fielddata+"<lastName>"+formdata[1]["value"]+"</lastName>"
	fielddata = fielddata+"<sourceCode>"+getCookie("IDENTIFIER")+"</sourceCode>"
	fielddata = fielddata+"<sourceTypeId>13</sourceTypeId>"
	fielddata = fielddata+"<titleCode>Y</titleCode>"
	fielddata = fielddata+"</NewsletterSignup>"
	 j.ajax({
	   url: "http://culturious.com/ServicesV3/Public/REST/Marketing.svc/NewsletterSignup/Invoke",
	   processData: true,
	   data: fielddata,
	   error: ctaform_fail,
	   success: ctaform_success,
	   type: "POST",
	   contentType: "application/xml"
	 });
	return false;
}

function ctaform_fail(){
	j("#cta_email_form").fadeOut(function(){
		j("#cta_email").text("Sorry, there was a problem. Please call us.");
		j("#cta_email, #cta_livechat").fadeIn();
	});
	return false;
}

function ctaform_success(){
	j("#cta_email_form").fadeOut(function(){
		j("#cta_email").text("Thank you for subscribing.");
		j("#cta_email, #cta_livechat").fadeIn();
	});
	
	_rts_set_conversion('1.01','Email Update Request');
	
	return false;
}

jQuery.preloadImages = function()
{
  for(var i = 0; i<arguments.length; i++)
  {
    jQuery("<img>").attr("src", arguments[i]);
  }
}

jQuery.preloadContent = function(){
  for(var i = 0; i<arguments.length; i++)
  {
    jQuery("<div>").html(arguments[i]);
  }
}

//function nav_mouseover()
//{
//	console.log(j(this).width() + " :: " + j(this).siblings().width());
//	j(this).siblings().css({"margin-left":"-100px"})
//	j(this).fadeOut();
//	j(this).siblings().fadeIn();
//}
//
//function nav_mouseout()
//{
//	j(this).fadeOut();
//	j(this).siblings().fadeIn();
//}
//

function checkemail(str){
	var filter=/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i
	if (filter.test(str)){
		return true;
	} else{
		return false;
	}
}

function getCookie( check_name ) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f

	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );


		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}

function redirParent(url, isanchor){
	if(isanchor && isanchor==true){
	    if(parent.location.href.indexOf(url)<0) // i.e. if anchor isn't already part of the url
	    	parent.location.href = url;
	}
	else
	    parent.location.href = url;
	try{    
	    parent.Shadowbox.close();
	    }
	catch(e){ }	
}

function invokeSoDoIt() {
    
    var js = document.createElement("script");
    js.src = "http://tauck.trk.sodoit.com/rts.js";
    var head = document.getElementsByTagName("head")[0];
    head.appendChild(js);
    
}