/*
 * Main Functions
 * - getNutritionInfo(url)					- sets options and opens window give a url
 * - openNutritionInfo(productId)			- creates the url and calls getNutritionInfo(url)
 * - openVideoFronUrl(vname)				- given a video name finds the obj with related data and calls openVideo()
 * - setVideoLinks()						- binds openVideo to links in the page  
 * - openVideo(obj)							- collects and sets data and opens the video player modal
 * - setVideoData(id, name, title, prank)	- sets given data to the page
 * - videoGoogleEvent(id, title)			- sends event data to google analytics
 * - toggleZipBg()							- sets bg-images and events for zip code input 
 * - testPLPCookies()						- test product locator prank cookie "_plp" returns true or false
 * - testVPPCookies()						- test video player prank cookie "_vpp" returns true or false
 * - getPLPCookies()						- gets "_plp" cookie value - returns 0 or 1
 * - setPLPCookies()						- sets the "_plp" cookie value to 1
 * - getVPPCookies()						- gets "_vpp" cookie value - returns 0 or 1	
 * - setVPPCookies()						- sets the "_vpp" cookie value to 1
 * - getCookies(cookieName)					- gets a cookie value - returns cookie value or 0 if not set
 * - setCookies(cookieName, cookieValue)	- sets a cookie value
 * - showError(msg)							- shows error message
 * - LoggerServices()						- logs t the console
 * - ie6Notice()							- test for IE6 and displays a message
 */


jQuery.ajaxSetup({
	type: "POST"
});


// Test Flash Version
var flash = false;
if (swfobject.hasFlashPlayerVersion("10")) {
	var flash = true;
}	

$(document).ready(function() {
	
	$("#dialog").dialog({
		autoOpen: false,
		draggable: false,
		modal: true,
		resizable: false,
		position: 'center',
		zIndex: 9999,
		close: function(event, ui) { $("#dialog").html(""); }
	})
	
	ie6Notice();
	
});


/* Products */
function getNutritionInfo(url) {
	var features = "width=583,height=550,status=0,toolbar=0,scrollbars=auto, noresize";
	window.open(url, "NutritionalInfo", features);
}

function openNutritionInfo(productId) {
	var url = "nutrition_info.aspx?Site=1&Product=" + productId;
	getNutritionInfo(url);
}

function openVideoFronUrl( vname ) {
	var str =  "[vname=" + vname + "]"
	var $link = $( ".video_title" + str );
	openVideo( $link );
}

function setVideoLinks() {
	$("a.video_link").click(function(event) {
		event.preventDefault();
		openVideo(this);
	});
}


/* Video */
$('body').data('vidID', '');
$('body').data('vidName', '');
$('body').data('vidTitle', '');
$('body').data('vidPrank', '0');

function openVideo( obj ) {
	
	var $link = jQuery(obj);
	var id = $link.attr('vid');
	var name = $link.attr('vname');
	var title = $link.attr('vtitle');
	var prank = 0;
	// dynamic site 
	// var playerUrl = "/video-player.do?name=" + name + "&title=" + title;
	// flast site 
	var playerUrl = "video-player.html?name=" + name + "&title=" + title;
	
	if (!testVPPCookies()) { 
		prank = 1; 
		setVPPCookies();
	}	
	
	setVideoData( id, name, title, prank );
	videoGoogleEvent( id, title );
	
	$.ajax({
		url: playerUrl,
		type: "GET",
		success: function(html) {
			$("#dialog").dialog( "option", "height", 420 );
			$("#dialog").dialog( "option", "width", 530 );		
			$("#dialog").dialog( "option", "position", 'center' );
			$("#dialog").html(html);
			$("#dialog #videoTitle").html( title );
			$("#dialog").dialog("open");
		},
		error: function() {
			showError("An error occurred.");
		}
	});
	
}

function setVideoData( id, name, title, prank ) {
	$('body').data('vidID', id);
	$('body').data('vidName', name);
	$('body').data('vidTitle', title);
	$('body').data('vidPrank', prank);
}

function videoGoogleEvent( id, title ) {
	_gaq.push(['_trackEvent', 'Video', title]);
}


/* locator */
function toggleZipBg() {
	$('#zip').focusin(function() {
		$(this).removeClass("bg_zip");
	});
	$('#zip').focusout(function() {
		if ($(this).val().length == 0) {
			$(this).addClass("bg_zip");
		}
	});
	if ($("#zip").val().length == 0) {
		$("#zip").addClass("bg_zip");
	}
}



/* cookies */
function testPLPCookies() {
	if ( getPLPCookies() == '0') {
		return false;
	} else {
		return true;
	}
}

function testVPPCookies() {
	if ( getVPPCookies() == '0') {
		return false;
	} else {
		return true;
	}
}

function getPLPCookies() { return getCookies('_plp'); }
function setPLPCookies() { setCookies('_plp', '1'); }

function getVPPCookies() { return getCookies('_vpp'); }
function setVPPCookies() { setCookies('_vpp', '1'); }


function getCookies(cookieName) {
	if ( !$.cookies.get(cookieName) || $.cookies.get(cookieName) === null || $.cookies.get(cookieName) === undefined) {
		return '0';
	} else {
		return $.cookies.get(cookieName);
	}
}

function setCookies( cookieName, cookieValue ) {
	$.cookies.set(cookieName, cookieValue, {path : '/'});
}


/* utils */
function showError(err) {
	alert(err);
}

var LoggerServices = {
	log: function(msg) {
		try {
			console.log(msg);
		} catch (err) { }
	}
}; // LoggerServices.log( string ); 

function ie6Notice() {
	if ( $.browser.msie && $.browser.version=="6.0" ) {
		$("#dialog").dialog({ dialogClass: 'ui-state-highlight' })
		$("#dialog").dialog({ resizable: false });
		$("#dialog").dialog({ draggable: false });
		$("#dialog").dialog({ closeOnEscape: false });
		$("#dialog").dialog( "option", "height", 170 );
		$("#dialog").dialog( "option", "width", 400 );			
		$("#dialog").dialog( "option", "modal", true );
		$("#dialog").html("<strong>Sorry, this site is not compatible with Internet Explorer 6. Please upgrade your browser.</strong>");
		$("#dialog").parents(".ui-dialog:first").find(".ui-dialog-titlebar-close").remove();
		$("#dialog").parents(".ui-dialog:first").find(".ui-button").remove();
		$("#dialog").addClass("ie-notice");
		$("#dialog").dialog("open");
	}
}

function getURLParameter(paramName) {
    return decodeURI(
        (RegExp(paramName + '=' + '(.+?)(&|$)').exec(location.search)||[,null])[1]
    );
}
