//array of preloaded images (navigation)
var $hostname = "http://"+location.hostname+"/";
var image_urls = new Array($hostname+"images_home/home_sel.gif",$hostname+"images_home/personality_sel.gif",$hostname+"images_home/fun_sel.gif",$hostname+"images_home/health_sel.gif",$hostname+"images_home/love-sex_sel.gif",$hostname+"images_home/beauty-style_sel.gif",$hostname+"images_home/parenting_sel.gif",$hostname+"images_home/create-your-own_sel.gif",$hostname+"images_home/horoscopes_sel.gif");

//this executes after the page is loaded
$(window).bind('load', function() {
	//list of preloaded images
	preloadImages(image_urls);
});

//this executes when page is ready to be loaded
$(document).ready(function() {
	$(".nav_img").hover(
		function () { //hover over 
			$cur_img = $(this).attr("src");
			$new_img = $cur_img.replace(/.gif/,"_sel.gif");
			$(this).attr("src",$new_img);
		},
		function () { //hover out
			$cur_img = $(this).attr("src");
			$new_img = $cur_img.replace(/_sel.gif/,".gif");
			$(this).attr("src",$new_img);
		}
	);
    $("#american_heart").click( function () {
    	popUnder("http://www.americanheart.org", 700, 500);
	});
	
    $("#cb_try_again").click(function () {
         $("#mycb_loginform").show();
         $("#login_msg").hide();
		 return false;
    });

    $('.poll_form').submit(function () {
    	$form_id = this.id;
    	$vote = $("#"+$form_id+" input:radio:checked").val();
    	if(!$vote) {
    		alert("Please select an option before submitting.")
    		return false;
    	}
    	$poll_id = $("#"+$form_id+" input#poll_id:hidden").val();
    	var $post_url = "/submit_poll/"+$poll_id+"/"+$vote;
		//$post_url = "/submit_poll/2/7";

		$("#"+$form_id+"_answer").load($post_url);		
    	$("#"+$form_id).fadeOut("slow", function () {
		$("#"+$form_id+"_answer").show();
    	});

    	return false;
    });

    $('.poll_form2').submit(function () {
    	$form_id = this.id;
    	$vote = $("#"+$form_id+" input:radio:checked").val();
    	$poll_id = $("#"+$form_id+" input#poll_id:hidden").val();
    	var $post_url = "/submit_poll2/"+$poll_id+"/"+$vote;
        
    	$.getJSON($post_url, '', function(json) {
    	    $('#poll_header').html(json.header);
    	    $("#"+$form_id+"_answer").html(json.results);
    	    $("#"+$form_id).fadeOut("slow", function() {
	        $("#"+$form_id+"_answer").show();
    	    });
    	});
    	
    	return false;
    });
	
    $("#expand_collapse").click(function () {
    	$icon = $(this).attr("src");
    	$new_icon = ($icon.match("collapse"))?$icon.replace(/collapse.gif/,"expand.gif"):$icon.replace(/expand.gif/,"collapse.gif");
    	$(this).attr("src",$new_icon);
    	$("#gen_health_topics").slideToggle("normal");
    });
    
    $('a[href$=/recipes/]').addClass('local');
});

//preload images
function preloadImages(image_array) { 
	for(i=0; i<image_array.length; i++) {
		$("html").append("<img src=\""+image_array[i]+"\" class=\"preload_images\">");
	}
}


//function to print the current document
function printit() {
    window.print();
}


/****************************************************************************************/

/* default js for quiz template */

	function skipOffer() {
		document.location = jsNextPage;//next result
	}
	
	function showForm(scrollTo) {
		var formDiv = document.getElementById('with_form');
		if (formDiv) formDiv.style.display='block';
		if (jsScroll) window.scroll(0, jsScrollTo);//roughly top of form
	}

/*	function openLink(url, width, height) {
		if (!width) width=800;
		if (!height) height=600;
		popUp(url, width, height); 
		skipOffer();
	}*/
   
	function killSubmit() {
		return false; 
	}
   
	function popUpNoResize( URL, width, height ) {
		var popwin = popUp( URL, width, height, "status=no,location=no,menubar=no,toolbar=no,directories=no,resizable=no,scrollbars=no");    
	}
/****************************************************/
    function openOffer(url, nextOffer){
        width = 800;
        height = 600;
		properties = 'status=yes,location=yes,menubar=yes,toolbar=yes,directories=no,resizable=yes,scrollbars=yes';
        popUp(url, width, height, properties);

		return goNextOffer(nextOffer);
	}

    function popUp( URL, width, height, props ){
        if (!props) props = "status=yes,location=yes,menubar=yes,toolbar=yes,directories=no,resizable=yes,scrollbars=yes";
        if (!width) width=800;
        if (!height) height=600;
        var popup_window=window.open( URL, "_blank", props + ",width=" + width + ",height=" + height + ",left=50,top=10");
        window.onerror = null;
        return popup_window;
    }

    function goNextOffer(goTo) {
        self.location= goTo;

		return false;
	}
/****************************************************/
	function popUpSimple( URL, width, height ) {
		if (!height) height=650;
		var popwin = popUp( URL, width, height, "status=no,location=no,menubar=no,toolbar=no,directories=no,resizable=yes,scrollbars=yes");    
	}

	function popUnder( URL, width, height ) {
		popup_window = popUp( URL, width, height );
		popup_window.blur();
	}

	function popUnderNoResize( URL, width, height ) {
		var popup_window = popUpNoResize( URL, width, height );
		popup_window.blur();
	}
   
/*	function popUp( URL, width, height, props ) {
		if (!props) props = "status=yes,location=yes,menubar=yes,toolbar=yes,directories=no,resizable=yes,scrollbars=yes";
		if (!width) width=800;
		if (!height) height=500;
		var popup_window=window.open( URL, "_blank", props + ",width=" + width + ",height=" + height + ",left=50,top=10");
		window.onerror = null;
		return popup_window;
	}*/

	function submitform() {
		document.myForm.submit();
		return false;
	}

	function showHint(str) { 
		if (str==110) { 
			document.getElementById("quiz18").className='urlshow';
			document.getElementById("quiz3").className='urlhide';
		}
		else { 
			document.getElementById("quiz3").className='urlshow';
			document.getElementById("quiz18").className='urlhide';
		}
	}