$(document).ready(function(){ 

	$(document).pngFix();
	
	if (datetimecheck() == 1) {
		$('#enter-to-win-div').load("include-enter-to-win.php",entertowin);
	} else {
		$('#enter-to-win-div').load("include-enter-to-win-ended.php",setupDemos);
	}
		
	//alert(datetime());
	
	//if ($.cookie('entered') == undefined) {
					
		//$('#enter-to-win-div').load("include-enter-to-win.php",entertowin);

	//} else {
	
		//$('#enter-to-win-div').load("include-entered.php",setupDemos);
	//}

	$('#enter-to-win').click(
		function(e) {
			e.preventDefault();
			
			changetab('enter-to-win');
		}
	);
	
	 $('#rules').click(
		function(e) {
			e.preventDefault();
			
			changetab('rules');
		}
	);
	
	 $('#prizes').click(
		function(e) {
			e.preventDefault();
			
			changetab('prizes');
		}
	);
	
	 $('#photo-gallery').click(
		function(e) {
			e.preventDefault();
			
			changetab('photo-gallery');
		}
	);

}); 

function entertowin() {
	$('#continue').mouseover(
		function () { 
			$('#continue-message').removeClass('hide');
			$('#continue-message').html("<p>Please verify you have entered all required fields (<span class='red'>*</span>) and click to submit your entry. Good luck!</p>"); }
	);
	
	
	$('#continue').click(
		function(e) {
			e.preventDefault();
	
			$('#continue').addClass('continue-clicked');
			$('#continue').unbind('mouseover');
			if ($("span[id^='bg_']").hasClass('bg-red') == true) { 
				$("span[id^='bg_']").removeClass('bg-red');
			} else { };
		    $('#continue-message').html("<span class='grey'>Working...Please wait.</span>");
		     
			var data = $('#form1').serialize();
			
			$.post('enter-to-win-do.php',data,
				function(json) {
				if (json.response == "1") {
					$('#enter-to-win-div').load("include-entered.php", function() { alert("Thank you for participating in the Sandals Emerald Bay Golf Getaway Sweepstakes!\n\nPlease check your email for an automated message confirming your entry.\n\nThe winner will be announced on or about September 7, 2010. Limit: one entry per person during the Promotion Period. Please refer to the Official Rules for more information on prizes and eligibility."); window.location = json.details; });
					
				
				} else if (json.response == "2") {
				
					for (i=0; i < json.labels.length; i++) {
						$('#bg_'+json.labels[i]).addClass("bg-red");
					}
					
					$('#continue-message').html("<span class='red'>"+json.details+"</span>");
					$('#continue').removeClass('continue-clicked');
					
				} else {
					
					$('#continue-message').html("<span class='red'>"+json.details+"</span>");
					$('#continue').removeClass('continue-clicked');
				}
				
				}, 
			'json');
		}
	);
	
	setupDemos();
}


function changetab(clicked) {
		var array = new Array();
			array[0] = "enter-to-win";
			array[1] = "rules";
			array[2] = "prizes";
			array[3] = "photo-gallery";
				
		for (i=0;i<array.length;i++) {
			
			if (array[i] == clicked) {
				
				if ($('#'+array[i]).hasClass('menu-bar-tab-on') == true) { } else {
				$('#'+array[i]).addClass('menu-bar-tab-on'); }
				
				$('#enter-to-win-div').load("include-loading.php");
				
				if (array[i] == "enter-to-win") {
					
					if ($.cookie('entered') == undefined) {
					
						$('#enter-to-win-div').load("include-"+array[i]+".php",entertowin);
					
					} else {
				
						$('#enter-to-win-div').load("include-entered.php");
					}
								
				
				} else if (array[i] == "prizes" || array[i] == "photo-gallery") {
					$('#enter-to-win-div').load("include-"+array[i]+".php",setupDemos);
				} else {
					$('#enter-to-win-div').load("include-"+array[i]+".php");
				}
				
			} else {
			
				if ($('#'+array[i]).hasClass('menu-bar-tab-on') == true) { 
				$('#'+array[i]).removeClass('menu-bar-tab-on'); } else { }
											
			}
			
		}
}

Shadowbox.init({
    overlayOpacity: 0.9
}, setupDemos);

function setupDemos() {

    Shadowbox.setup("a.course-gallery", {
        gallery:        "course",
        displayCounter:        false,
        slideshowDelay:        6,
        viewportPadding:        40,
        continuous:     true
    });

    Shadowbox.setup("a.prize-gallery", {
        gallery:        "prize",
        displayCounter:        false,
        viewportPadding:        40,
        continuous:     true
    });
    Shadowbox.setup("a.sebvideo-gallery", {
        gallery:        "sebvideo",
        displayCounter:        false,
        viewportPadding:        40,
        continuous:     true,
        height:     470,
        width:      744
    });
}

function datetimecheck() {
	var now = new Date();
	var over = new Date(2010, 7, 28, 0, 0, 0, 0); //javascript Aug is 7 not 8
	now = parseInt(now.getTime());
	over = parseInt(over.getTime());
	if (now > over) { return 0; } else { return 1; }
}
