$(document).ready(function () {

    $('.silhouettes img').hover(
        function () { $(this).attr('src', 'images/2silhouettes_hover.png'); },
        function () { $(this).attr('src', 'images/2silhouettes.png'); }
    );

    $('#gallery a').lightBox();

    if (window.location.hash.indexOf("#video") > -1) {
        changeClass();
    }

    $('.intertrigger').click(function () {
        url = "http://www.nuvigil.com/nuvigil-and-swd/?utm_source=WakeUpSquad.com&utm_medium=banner&utm_content=top&utm_campaign=WUS_Banners";
        $('#homebanner').css("display", "none");
        $('#folder').before("<div class='interwrap'></div>");
        $('.interwrap').load('/inter.aspx', function () {
            // get value of URL and inject it into the link's href attribute
            $("#continue").attr("href", url);
        });
        _gaq.push(['_trackEvent', 'top_banners', 'click', 'homepage']);
        return false;
    });

    $('.sidetrigger').click(function () {
        url = "http://www.nuvigil.com/nuvigil-and-swd/?utm_source=WakeUpSquad.com&utm_medium=banner&utm_content=left&utm_campaign=WUS_Banners";
        $('#homebanner').css("display", "none");
        $('#folder').before("<div class='interwrap'></div>");
        $('.interwrap').load('/inter.aspx', function () {
            // get value of URL and inject it into the link's href attribute
            $("#continue").attr("href", url);
        });
        _gaq.push(['_trackEvent', 'left_banners', 'click']);
        return false;
    });

    $('.goback').live("click", function () {
        $('.interwrap').remove();
        $('#homebanner').css("display", "block");
    });

    $('#coffeead').flash({
        id: 'coffeead',
        swf: '/media/coffee/WakeUpCup.swf',
        width: '350',
        height: '550',
        wmode: 'transparent',
        flashvars: { clickTag1: 'http://www.nuvigil.com/home-consumer/?utm_source=WakeUpSquad.com&utm_medium=banner&utm_content=left&utm_campaign=WUS_Banners' }
    });

    $('.coffeeclick').click(function () {
        _gaq.push(['_trackEvent', 'left_banners', 'click']);
    });


    $('.cta').click(function () {
        $('body').append('<div class="backdrop"></div>');
        $('.backdrop').css('filter', 'alpha(opacity=50)');
        $('.backdrop').fadeIn(250, function () {
            $('#swdvideo').show();
        });
        $('#swdvideo').flash(
            {
                id: 'swdvideo',
                swf: '/media/swd/WakePlayerF9v3.swf',
                width: '660',
                height: '447',
                wmode: 'transparent',
                flashvars: { url: '/media/swd/CPH_Shift_Work_Disorder_R1_13_640_360.flv', emailservice: '/video_email_friend.php' }
            });

    });

    $("#wus_video").click(function () {
        _gaq.push(['_trackEvent', 'WakeUpSquad_VideoUpdate', 'click', 'launch_video_player']);
    });

    $('.screen').live("click", function () {
        var imgID = $(this).attr("id").split("img_")[1];
        if (imgID == 1 || imgID == 2) {
            var newid = parseInt(imgID) + 1;
            $(this).attr("src", "/images/cta-video-screen" + newid + ".jpg").attr("id", "img_" + newid);
        }
        if (imgID == 3) {
            $('.backdrop').fadeOut(250);
            $('.videoplayer').fadeOut(250);
        }
    });

    if ($(location).attr('href').search('#video') > 0) {
        /* fire off google tag if link coming from email */
        _gaq.push(['_trackEvent', 'WakeUpSquad_VideoUpdate', 'arrive']);
        /* clicked on email link */

        if ($(location).attr('href').search('_source=swd_wakeupsquad_video_update&utm_medium=email&utm_campaign=SWD') > 0) {
            _gaq.push(['_trackEvent', 'WakeUpSquad_VideoUpdate', 'arrive', 'email_visit']);
        }

        $('body').append('<div class="backdrop"></div>');
        $('.backdrop').css('filter', 'alpha(opacity=50)');
        $('.backdrop').fadeIn(250, function () {
            $('#swdvideo').show();
        });
        $('#swdvideo').flash(
            {
                id: 'swdvideo',
                swf: '/media/swd/WakePlayerF9v3.swf',
                width: '660',
                height: '447',
                wmode: 'transparent',
                flashvars: { url: '/media/swd/CPH_Shift_Work_Disorder_R1_13_640_360.flv', emailservice: '/video_email_friend.php' }
            });
    }

});



function closeWakeVid() {
    $('.backdrop').fadeOut(250);
    $('.videoplayer').fadeOut(250);
}

function validate_email(field,errormsg,divname){
with (field){
	apos=value.indexOf("@");
	dotpos=value.lastIndexOf(".");
	if (apos<1||dotpos-apos<2){
		if (document.getElementById) { // DOM3 = IE5, NS6
			document.getElementById(divname).style.visibility = 'visible';
		}else {
			if (document.layers) { // Netscape 4
				document.divname.visibility = 'visible';
			}else { // IE 4
				document.all.divname.style.visibility = 'visible';
			}
		}
		var mydiv=document.getElementById(divname);
		mydiv.innerHTML=errormsg;
		return false;
	} else {
		return true;
	}
  }
}

function validate_required(field,errormsg,divname){
with (field)  {
	if (value==null||value==""){
		if (document.getElementById) { // DOM3 = IE5, NS6
			document.getElementById(divname).style.visibility = 'visible';
		}else {
			if (document.layers) { // Netscape 4
				document.divname.visibility = 'visible';
			}else { // IE 4
				document.all.divname.style.visibility = 'visible';
			}
		}
		var mydiv=document.getElementById(divname);
		mydiv.innerHTML=errormsg;
		return false;
	} else {
		return true;
    }
  }
}

function validate_address(field1,field2,errormsg,divname){
	if (field1.value==field2.value){
		document.getElementById(divname).style.visibility = 'hidden';
		return true;
	}else{
		document.getElementById(divname).style.visibility = 'visible';
		var mydiv=document.getElementById(divname);
		mydiv.innerHTML=errormsg;
		return false;
	}
}
function validate_zip(field,errormsg,divname) {
	with (field) {
		if (value==null||value=="") {
			document.getElementById(divname).style.visibility = 'visible';
			var mydiv=document.getElementById(divname);
			mydiv.innerHTML=errormsg;
			return false;
		} else {
			if (value.length == 5 && is_numeric(value)) {
				document.getElementById(divname).style.visibility = 'hidden';
				return true;
			} else {
				document.getElementById(divname).style.visibility = 'visible';
				var mydiv=document.getElementById(divname);
				mydiv.innerHTML=errormsg;
				return false;
			}
		}
	}
}
function is_numeric(value) {
	var valid = "0123456789";
	for (i=0; i<5; i++) {
		if (valid.indexOf(value.substr(i,1)) == -1) {
			return false;
		}
	}
	return true;
}
function checkcheckbox(field,errormsg,divname){
	if (field.checked == false ){
		if (document.getElementById) { // DOM3 = IE5, NS6
			document.getElementById(divname).style.visibility = 'visible';
		}else {
			if (document.layers) { // Netscape 4
				document.divname.visibility = 'visible';
			}else { // IE 4
				document.all.divname.style.visibility = 'visible';
			}
		}
		var mydiv=document.getElementById(divname);
		mydiv.innerHTML=errormsg;			
		return false;
	}else{
		return true;
	} 
}
function validate_form_signup(thisform){
	with (thisform){
		if (validate_email(email,"Please enter a valid e-mail address.","errmsg")==false) {
			email.focus();return false;
		} else if (validate_address(email,email2,"The e-mail addresses provided do not match.","errmsg")==false) {
			email2.focus();return false;
		} else if (checkcheckbox(agree,"Please select checkbox in order to proceed.","errmsg")==false) {
			agree.focus();return false;
		} else if (validate_zip(zip,"Please enter a valid zip code.","errmsg")==false) {
			zip.focus();return false;
		} else {
			spotlight('thewa408','wakeu301','667753',null);
			
			return true;
		}
	}
}

function validate_smallform_signup(thisform){
	with (thisform){
		if (validate_email(emailAddr,"Please enter a valid e-mail address.","errmsg")==false){
			email.focus();return false;
		}else if (validate_zip(zipCode,"Please enter a valid zip code.","errmsg")==false) {
			zip.focus();return false;
		}else if (checkcheckbox(agreeTo,"Please select checkbox in order to proceed.","errmsg")==false){
			agree.focus();return false;
		}else{
			return true;
		}
	}
}

function validate_form_unsignup(thisform){
with (thisform){
	if (validate_email(email,"Please enter a valid e-mail address to unsubscribe.","errmsg")==false){
		email.focus();return false;
	}else{
		return true;
	}
  }
}

function validate_send_friend(thisform){
with (thisform){
	if(validate_required(recipient_name,"Please fill in all required information.","errmsg2")==false){
		recipient_name.focus();return false;
	}else if (validate_email(recipient_email,"Please enter a valid e-mail address.","errmsg2")==false){
		recipient_email.focus();return false;
	}else if(validate_required(sender_name,"Please fill in all required information.","errmsg2")==false){
		sender_name.focus();return false;
	}else if (validate_email(sender_email,"Please enter a valid e-mail address.","errmsg2")==false){
		sender_email.focus();return false;
	}else{
			countme();
		return true;
	}
  }
}


function popupform(myform, windowname){
if (! window.focus)return true;
if (windowname==""){
	windowname="Self-Assessment Results - The Wake-Up Squad";
}
	window.open('', windowname, 'height=600,width=800,scrollbars=yes');
	myform.target=windowname;
	return true;
}

function submitform(formname)
{
  document.forms[formname].submit();
}


function countme(){

  var _gaq =_gaq || [];
_gaq.push(['_setAccount', 'UA-9022327-4']);
_gaq.push(['_trackPageview']);

  (function() {
      var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(ga);
  })();

}

/*
	Function: spotlight
	Parameters: type, cat, ord
	Description: global Spotlight click event handler; num should be random to prevent caching of tag
*/
function spotlight(type, cat, ord, url) {
	var axel = Math.random() + "";
	var num = axel * 1000000000000000000;
	var tag_url = new Image();
	tag_url.src = 'http://ad.doubleclick.net/activity;src=2644892;type='+type+';cat='+cat+';ord='+ord+';num='+num+'?';
	if (url != null) { location.href = url; }
}
