﻿$(document).ready(function() {
	$('#ContestHelpLightBox').colorbox({
	    onLoad: function() {
    	},
    	onComplete : function() { 
		},
		inline: true, href: '#ContestHelp', width: 700
	});
		$('#ContestThankYouLightBox').colorbox({
		inline: true, href: '#ContestThankYou', width: 650
	});
	$('#LoserLightBox').colorbox({
		inline: true, href: '#loser'
	})
	$('.RequestGiftCardLightBox').colorbox({
		inline: true, href: '#RequestGiftCar'
	});
	$('#RequestInformationLightBox').colorbox({
		inline: true, href: '#RequestInformation', width: 550
	});
	$('#colorbox').detach().appendTo('#aspnetForm');
	$('#ThankYouLightBox').colorbox({
		inline: true, href: '#thank_you'
	});
	$('#WinnerLightBox').colorbox({
		inline: true, href: '#winner', width: 650
	});
	$('.RulesAndRegsLightBox').colorbox({
		inline: true, href: '#RulesAndRegulations', width: 850
	});
 });

 
var addthis_config = {
     ui_cobrand: "Gishy Goo! &nbsp;",
	 ui_header_color: "#ffffff",
     ui_header_background: "#1899be"
}

jQuery(document).ready( function($) {
	/* make sharepoint site-ribbon persistent */
	$('#s4-ribbonrow').stickyHeader('#s4-bodyContainer');

	blankImage = '/Style Library/iepngfix/blank.gif';            
    if($.browser.msie && $.browser.version=="6.0") {
        $('.png').each(function(){
            if (this.tagName == "IMG"){
	            this.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+this.src+"', sizingMethod='scale')"; 
	            this.height = this.height;
	            this.width = this.width;
	            this.src = blankImage;
            }else{
	            bgImage = $(this).css('backgroundImage').replace('url("','').replace('")','');
	            this.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+bgImage+"', sizingMethod='scale')"; 
	            this.style.backgroundImage = 'none';
            }
            this.style.visibility = 'visible';
        });   
    } else {
    	$('.page_shadow_4,.page_shadow_6').css('height', $('#wrapper').height() );
    }            
});

$(document).ready(function () {
    $("#submitCode").click(function () {	           
        $.ajax({
            url: '/_layouts/GishyGooPrizeWinnerPicker/FindWinner.aspx',
            type: 'POST',
            data: ({ action: 'check', code: $('#code').val() }),
            dataType: 'xml',
            success: function (xml) {
               // $("#proc").hide();
                var confirmation = "";
                var value = "";
                var msg = "";
                $(xml).find("Coupon").each(function () {
                    confirmation = $('Confirmation', this).text();
                    $('#ConfirmationCode').val(confirmation);
                    value = $('Value', this).text();
                    if (confirmation == null || confirmation == "")
                    {
                    	$.colorbox({inline: true, href: '#loser'});
                    }
                    else if(value != null && value != "")
                    {
                    	$('#cardValue').text(value);
					    $.colorbox({inline: true, href: '#winner', width: 650});
                    }
                    msg = $('msg', this).text();
                });
            },
            error: function (xhr, status, error) {
                var err = eval("(" + xhr.responseText + ")");
                alert(err.Message + " status=" + status + "  error=" + error);
            }
        });
        
        //omniture code
        var omniture = new Object();
   		omniture.events="event2";
    	s.t(omniture);
    	//end omniture code
    });
}); 

//Send confirmation data
$(document).ready(function () {
    $("#save").click(function () {
        var valid = true;
        var errorFields = "Please enter a value for ";
        if ($('#firstname').val() == '') {valid = false; errorFields += "First Name"; $('#firstname').addClass('error');}
        if ($('#lastname').val() == '') {valid = false; if (errorFields!="") {errorFields += ", "} errorFields += "Last Name"; $('#lastname').addClass('error');}
	    if ($('#address').val() == '') {valid = false; if (errorFields!="") {errorFields += ", "} errorFields += "address"; $('#address').addClass('error');}
        if ($('#city').val() == '') {valid = false; if (errorFields!="") {errorFields += ", "} errorFields += "city"; $('#city').addClass('error');}
        if ($('#state').val() == '') {valid = false; if (errorFields!="") {errorFields += ", "} errorFields += "state"; $('#state').addClass('error');}
        if ($('#zip').val() == '') {valid = false; if (errorFields!="") {errorFields += ", "} errorFields += "zip"; $('#zip').addClass('error');}
		if ($('#email').val() == '') {valid = false; if (errorFields!="") {errorFields += ", "} errorFields += "Email"; $('#email').addClass('error');}  
		if ($('#confirmemail').val() == '') {valid = false; if (errorFields!="") {errorFields += ", "} errorFields += "Confirm Email"; $('#confirmemail').addClass('error');} 
		if ($('#phonenum').val() == '') {valid = false; if (errorFields!="") {errorFields += ", "} errorFields += "Phone Number"; $('#phonenum').addClass('error');}
        
        var ofAge = 'N';
        if ($('#age').is(':checked')) ofAge='Y';

        if (ofAge=='N' && valid==true) {valid = false; errorFields = "You must be at least 13 years old to enter.";}
		if (($('#email').val() != $('#confirmemail').val()) && valid==true) {valid = false; errorFields = "Email and Confirmation Email must match."; $('#email').addClass('error'); $('#confirmemail').addClass('error');}

        if (valid==true)
        {
	        $.ajax({
	            url: '/_layouts/GishyGooPrizeWinnerPicker/FindWinner.aspx',
	            type: 'POST',
	            data: ({ action: 'save', confirmation: $('#ConfirmationCode').val(),
	                firstname: $('#firstname').val(), lastname: $('#lastname').val(),
	                phonenum: $('#phonenum').val(), email: $('#email').val(),
	                age: ofAge, address: $('#address').val(), city: $('#city').val(),
	                state: $('#state').val(),zip: $('#zip').val()
	            }),
	            dataType: 'xml',
                success: function (xml) {
                    $("#proc").show();
                    var status = "";		                   
                    $(xml).find("Coupon").each(function () {
                        status = $('Status', this).text();
                        if (status == null || status == "")
                        { alert("Something went wrong!....."); }
                        else if (status == "S")
                        { 
                        	$('#confirmationNumber').text($('#ConfirmationCode').val());
                        	$.colorbox({inline: true, href: '#ContestThankYou', width: 650});
						}
                        else if (status == "F")
                        { alert("Failed to save"); }
                    });
                },
	            error: function (xhr, status, error) {
	                $("#proc").hide();
	                var err = eval("(" + xhr.responseText + ")");
	                alert(err.Message + " status=" + status + "  error=" + error);
	            }
	        });
        }
        else
        {
        	$('.gishy_modal_bodycopy p.gishy_form_error span').text(errorFields);
        	$('.gishy_modal_bodycopy p.gishy_form_error').show();
        	$(this).colorbox.resize();
        }
    });
    
    $('.ms-rtestate-field').css("display", "block");
    $('.modalClose').click(function () {
    	$.colorbox.close();
    	return false;
    });
}); 

//Omniture code for 'Buy Now' clicks
$(document).ready(function () {
    $('a[href*="store.gishygoo.com"]').each(function(){
    	$(this).click(function () {
    		var parentID = $(this).parent().attr('id');
    		var omniture = new Object();
    		if(parentID == 'home_heading') //if clicked home page link
    			omniture.events="event4";
    		else //clicked the top nav link
    			omniture.events="event3";
    		s.t(omniture);
       	});
    });
}); 
//end Omniture code

/* This function is executed on postback of a Ultradent custom form so we can do different processing. */
function SubmitProcess()
{
	if ($('#successMessage').text()!="")
	{
		$('.gishy_modal_header').text('Thank You!');
		$('.gishy_modal_bodycopy').text('Thank you for requesting more information about Gishy Goo. Your questions are important to us and we will get back to you within the next 2 - 3 business days.');
	}
	$('#RequestInformation').colorbox.resize();
	/* This function required in SubmitProcess to attach the surveySubmit.click code each time, since the buttons are recreated. */
	$('#surveySubmit').click(function() {
	    $('#surveySubmit').hide();
		$('#dummySubmit').show();
	});
}

/*****************************************/
/* Scripts for the What Is GishyGoo Page */
/*****************************************/
onslide = 0;
tempOnSlide = 1;
slideWidth = 388;
fromtimer = 0;
currentDiv1 = 0;
currentDiv2 = 0;
playbtn_pos_open = "260px";
playbtn_pos_close = "197px";
timervalue = 3500;

function gotoSlide(xpos, fromTimer){
	/*console.log("xpos = " + xpos);*/
	tempOnSlide = xpos+1;
	currentDiv2 = "#slide_nav_controlls .btn"+[xpos+1];

	for (i = 1; i<6; i++){
	currentDiv1 = "#slide_nav_controlls .btn"+[i];
	$(currentDiv1).css('background-image', 'none');
	}

	$(currentDiv2).css('background-image', 'url("/images/btn_bg.png")');
	// $(currentDiv2).css('behavior', 'url(iepngfix/iepngfix.htc)');	
 
	if(!fromTimer){
		/*console.log(tempOnSlide);*/
		clearInterval(slideTimer);
		$('#gishy_playbtn').animate({top:playbtn_pos_open}, 'slow');
	}
	if(xpos!=0){
	xpos = "-"+(xpos*slideWidth)+"px";
	}
	$('#gishyslide_imagewrapper').stop().animate({left:xpos}, 'slow');

	} 
function playIt(){
	onslide = tempOnSlide;
	slideTimer = setInterval('nextSlide(tempOnSlide)', timervalue);
	$('#gishy_playbtn').animate({top:playbtn_pos_close}, 'slow');
}


function nextSlide(slide){
	if(slide == 5){
		onslide = 0;	
	}
	gotoSlide(onslide++, 1);
}

function positionReasonPopup() {
	var reason_top = $('#reasons_box').offset().top - 250;
	var reason_left = $('#reasons_box').offset().left - 100;
	$("#reasons").offset( { top: reason_top, left: reason_left } );
	$(".reason_1, .reason_2, .reason_3").each( function() { $(this).hide() });
}	

$(document).ready(function () {
  	$('#main_mod').height( $('#box_overlay').outerHeight() + 650 );
	slideTimer = setInterval('nextSlide(onslide)', timervalue);
	
	$('#reason_1').hover(
		function(e) { positionReasonPopup(); $(".reason_1").each( function() { $(this).show() } ); },
		function(e) { $(".reason_1").each( function() { $(this).hide() }); }
	);
	$('#reason_2').hover(
		function(e) { positionReasonPopup(); $(".reason_2").each( function() { $(this).show() }); },
		function(e) { $(".reason_2").each( function() { $(this).hide() }); }
	);
	$('#reason_3').hover(
		function(e) { positionReasonPopup(); $(".reason_3").each( function() { $(this).show() }); },
		function(e) { $(".reason_3").each( function() { $(this).hide() }); }
	);

	$('#gishy_playbtn').click(function() {
  		playIt();
  	});
	$('div.btn_bg.btn1').find('img').click(function() {
  		gotoSlide(0);
  	});
	$('div.btn_bg.btn2').find('img').click(function() {
  		gotoSlide(1);
  	});
	$('div.btn_bg.btn3').find('img').click(function() {
  		gotoSlide(2);
  	});
	$('div.btn_bg.btn4').find('img').click(function() {
  		gotoSlide(3);
  	});
	$('div.btn_bg.btn5').find('img').click(function() {
  		gotoSlide(4);
  	});
});



/* VIDEO PAGE */

	var tag = document.createElement('script');
	tag.src = "http://www.youtube.com/player_api";
	var firstScriptTag = document.getElementsByTagName('script')[0];
	firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
	
	var player;

	function playVideo(video_id) {
		player.stopVideo();
		player.clearVideo();
		player.loadVideoById(video_id);
		player.playVideo();
	};
	
	function newPlayer(video_id, autoPlay) {

		player = new YT.Player('player',
		{
			height: '350',
			width: '574',
			videoId: video_id,
			playerVars:
			{
				//playlist: 'DaZIsQzPAPA,J6PFbDXMDRA,CfbfWXSLkck',
				'autoplay': 1,
				'autohide': 1,
				'enablejsapi': 1,
				'wmode': 'opaque'
			},
			events:
			{
				'onReady': function(event) { event.target.playVideo(); },
				'onPlayerStateChange': function(){}
			}
		});
	};
	
	function onYouTubePlayerAPIReady() {
		
			newPlayer('2QoQEgqlfgw');
		 
	};
	
	
$(document).ready(function(){
	
		var $play_list = $('#play-list');
		$play_list.delegate('a', 'click', function(e){
		
			var target = e.currentTarget,
				vid = target.getAttribute('id');

			if(vid)
			{
				playVideo(vid.match(/video-(.*)/)[1]);
				$play_list.find('li').removeClass('selected');
				$(target).parent().addClass('selected');
				e.preventDefault();
			}

			
		});
});


