$(document).ready(function(){	
	var fullBgPath,thePath,COOKIE_NAME,COOKIE_VALUE,COOKIE_OPTIONS,contact_referral_val,winOptions; // globals
	
	$('img[src$=.png]').ifixpng('http://www.mywealth.com/media/img/ui/pixel.gif');// png correction w/ custom pixel.gif location
	
	$("#site-container #header #social-bar div img, #site-container #header #nav-bar div").not("#enroll_image, #btn_sharethis").addClass('btn');// nav bar & social bar

	// logo click
	$("#logo").click(function () {
		goToURL("index.php");
	});

	// every odd footer div apply classes: copy-11 & color-16 & btn
	$("#site-container #footer div:nth-child(odd)").addClass('btn copy-11 color-666');
	
	// every even (divider) footer apply .divider
	$("#site-container #footer div:nth-child(even)").addClass('divider copy-11 color-666');
	
	// swap login & social bar bg images
	$("#login-bar #login-module div #student-login-form #student-form-btn img, #site-container #header #social-bar div img").not("#enroll_image, #btn_sharethis").mouseover(function(){
		$(this).attr("src", "http://www.mywealth.com/media/img/ui/btns/" + this.id + "-on.png");
	}).mouseout(function(){
		$(this).attr("src", "http://www.mywealth.com/media/img/ui/btns/" + this.id + "-off.png");
	});	
	
	// chat bubble over,swap bg
	$("#site-container #content-container .four-col-feature #chat-copy div").mouseover(function(){
		$(this).parent().css("background-image","url('http://www.mywealth.com/media/img/ui/btns/btn-chat-on.png')");
	}).mouseout(function(){
		$(this).parent().css("background-image","url('http://www.mywealth.com/media/img/ui/btns/btn-chat-off.png')");
	});	

	// chat bubble click
	$("#site-container #content-container #chat-copy, #site-container #content-container #three-one-cols #one-col #chat-module").click(function () {
		winOptions = {target:"_blank",name:"myWealthChat",urlpath:"https://hostedusa2.whoson.com/chat/chatstart.htm?domain=www.mywealth.com"};
		openNewWin(winOptions);
	});
	
	// twitter click
	$("#site-container #header #social-bar div img#btn-twitter").click(function () {
		winOptions = {target:"_blank",name:"mywealthTwiiter",urlpath:"https://twitter.com/mywealth"};
		openNewWin(winOptions);
	});
	
	// facebook click
	$("#site-container #header #social-bar div img#btn-facebook").click(function () {
		winOptions = {target:"_blank",name:"mywealthFacebook",urlpath:"http://www.facebook.com/home.php?#/group.php?gid=40671545167"};
		openNewWin(winOptions);
	});
	
	// blog click
	$("#site-container #header #social-bar div img#btn-blog, #site-container #content-container #three-one-cols #three-cols #contact-details #intro span.copy-underline").click(function () {
		winOptions = {target:"_blank",name:"mywealthBlog",urlpath:"http://www.mywealth.com/blog/"};
		openNewWin(winOptions);
	});
	
	// nav bg swap
	$("#wrapper #site-container #header #nav-bar div").hover(function () {
		fullBgPath = "url('http://www.mywealth.com/media/img/ui/nav/" + this.id + "-on.png')";
		$(this).css({"background-image":fullBgPath,"color":"#fff"});
	}, 
	function () {
		$(this).css({"background-image":"none","color":"#424242"});
		setNav();// make sure we turn on current page
	});

	// nav clicks
 $("#site-container #header #nav-bar div").click(function () {
		theID = this.id;
		thePath = theID.substring(theID.indexOf("-")+1,theID.length) + ".php"; // clean up id by removing 'nav-'
		thePath == "http://www.mywealth.com/home.php" ? thePath = "http://www.mywealth.com/index.php" : doNothing(); // home.php
		thePath == "http://www.mywealth.com/investing-101.php" ? thePath = "http://www.mywealth.com/investing.php" : doNothing(); // investing.php == beginners nav :|
		goToURL(thePath);
	});
	
	// footer clicks
 $("#site-container #footer div:nth-child(odd)").click(function () {
		theID = this.id;
		thePath = theID.substring(theID.indexOf("-")+1,theID.length) + ".php"; // clean up id by removing 'footer-'
		thePath == "http://www.mywealth.com/investing-101.php" ? thePath = "http://www.mywealth.com/investing.php" : doNothing();// investing.php == beginners nav :|
		goToURL(thePath);
	});

	// footer mouseovers
	$("#site-container #footer div:nth-child(odd)").mouseover(function(){
		$(this).css({"text-decoration":"underline","color":"#000000"});
	}).mouseout(function(){
		$(this).css({"text-decoration":"none","color":"#666666"});
	});	
	

	// newsletter signup btn over
	$("#newsletter #newsletter-form-container #newsletter-form #newsletter-form-btn img").mouseover(function(){
		$(this).attr("src", "http://www.mywealth.com/media/img/ui/btns/" + this.id + "-on.png");
	}).mouseout(function(){
		$(this).attr("src", "http://www.mywealth.com/media/img/ui/btns/" + this.id + "-off.png");
	});	

	// newsletter signup btn click
	$("#newsletter #newsletter-form-container #newsletter-form #newsletter-form-btn img").click(function () {
		newsletterFormSubmit();
	});

	
  // newsletter form validation
	function validateNewsletterForm(){
		var hasError = hasEmailError = false; // start from scratch
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/; //regex
		var emailVal = $("#newsletter #newsletter-form-container #newsletter-form #newsletter-email #from").val();
		$("#newsletter-email-error").empty(); // clear errors for regcheck

		//  validates either email value or phone number
		if(emailVal == '') {
			$("#newsletter-email-error").empty().append('Please enter your email address.');
			hasError = true;
		} else if ((!emailReg.test(emailVal))){
			// valid email?
			$("#newsletter-email-error").empty().append('A valid email is required.');
			hasError = true;
		}

		return hasError;	
	}
	
	// newsletter form submit controls
	function newsletterFormSubmit(){
		var hasError = validateNewsletterForm(); // validate form
		if(!hasError){
			$("#newsletter #newsletter-form-container #newsletter-form").submit();
	}
		return false; // don't submit if invalid
	}; 
		

 	$("#login-bar #login-module div #student-login-form #student-form-btn").click(function () {
		//alert("this getting triggered");//debug
		studentFormSubmit();
	});	
	

    init(); // init site...
});

function doNothing(){
	return false;
}


function init(){
	setNav(); // set navigation ON
}


function setCookie(){
	// set/create cookie
	COOKIE_NAME = 'contact_referral';
	COOKIE_VALUE = $("#current-pg").html();
	COOKIE_OPTIONS = { path: '/staging/',domain:'mywealth.com'};
	$.cookie(COOKIE_NAME, COOKIE_VALUE, COOKIE_OPTIONS);
	return false;
}

function getCookie(){
	// set/create cookie
	contact_referral_val = $.cookie('contact_referral');
	return contact_referral_val;
}

function setNav(){
	var currentNav = $("#current-pg").html();
	// set nav div On for currentNav
	currentNav == "activetraders" || currentNav == "stock-market" || currentNav == "investing" || currentNav == "etf" || currentNav == "currency-trading" ? currentNav = "active-traders" : doNothing();
	$("#nav-" + currentNav).css({"background-image":"url('http://www.mywealth.com/media/img/ui/nav/nav-" + currentNav + "-on.png')","color":"#fff"}); 
}

function openNewWin(opts){
	!opts.target ? opts.target = "_blank" : doNothing(); // if target not defined, default to blank
	!opts.name ? opts.name = "myWealthWin" : doNothing();// if name not defined, use 'myWealthWin' as default
	
	// if no url is specified, don't do anything.
	if (opts.urlpath){
		window.open(opts.urlpath,opts.target,opts.name,"resizable=1,scrollbars=1"); 
		return false;
	}
}

function goToURL(url){
	window.location.href = url;	
}
