// JavaScript Document

function wl_closenTarget(url) {
	window.parent.location.href= url;
	close_wl_dialog();
}

function addToWishlist(wlButtonMode) {

	var thisObj = $("#addToWishlist");
	
	if(wlButtonMode == 'skateboard') {
			if (doCoreMetrix)
			{
				cmCreateConversionEventTag('Skate Board', 1, 'Add to Wish List - SKB', 0);
				createCookie('AddToWishList', 'Skate Board|Add to Wish List - SKB' );
			}
			openWishListDialogForm(document.getElementById("skb_form"));
	} else {
		showBubble(thisObj);
		var errMsg = validateProduct();
		if (errMsg == "") {
			form_name = document.product_form;
			// alert(doCoreMetrix);
			if (doCoreMetrix)
			{
				cmCreateConversionEventTag(eval(form_name).sku.value, 1, eval(form_name).coreMetricsCategory.value, 0);
				createCookie('AddToWishList', eval(form_name).sku.value + '|'  + eval(form_name).coreMetricsCategory.value );
			}
			openWishListDialogForm(document.product_form);
		}
	}

}

function QVaddToWishlist() {
	var errMsg = "";
	if (errMsg == "") {
		openQVWishListDialogForm();
	}
}
// used for PDP and skate board builder
function openWishListDialogForm(sourceForm) {
	var arguments = getFormValues(sourceForm);
	openWishListDialogForm_common(arguments);
}
// for quick view add to wish list
function openQVWishListDialogForm() {
	var arguments = $("#quickview_product_form").serialize();
	if (doCoreMetrix)
	{
		cmCreateConversionEventTag($("#quickview_product_form input[name='sku']").val(), 1, $("#quickview_product_form input[name='coreMetricsCategory']").val(), 0);
		createCookie('AddToWishList', $("#quickview_product_form input[name='sku']").val() + '|'  + $("#quickview_product_form input[name='coreMetricsCategory']").val() );
	}
	openWishListDialogForm_common(arguments);
}
// for move to wishlist from shopping cart.
function openCartWishListDialogForm(cartline_id) {
	var form_name = "document.shopping_cart_" + cartline_id;
	var arguments = getFormValues(eval(form_name));
	if (doCoreMetrix)
	{
		cmCreateConversionEventTag(eval(form_name).sku.value, 1, eval(form_name).coreMetricsCategory.value, 0);
		createCookie('AddToWishList', eval(form_name).sku.value + '|'  + eval(form_name).coreMetricsCategory.value );
	}
	// need different cart id than common open dialog
	var html = '<div id="ws_login_container"></div>';
	var html_shadow = '<div id="ws_login_container_shadow"><div><div></div></div></div>';
	
	$("body").prepend(html);
	$("body").prepend(html_shadow);
	
	target = $("#cart_addToWishlist_" + cartline_id + " a");
	var targetOffset = target.offset();
	var button_width = target.width();
	var button_height = target.height();
	
	var width = $("#ws_login_container").width();
	var height = $("#ws_login_container").height();
	
	var left = targetOffset.left;
	left -= (width - button_width) / 2;
	var top = targetOffset.top; 
	top -= height + 10;

	$("#ws_login_container").css({left: left, top: top});
	$("#ws_login_container_shadow").css({left: left + 6, top: top + 6, opacity: 0.5});
	$("#ws_login_container").load(loginTemplatePath + "?" + arguments, {secured: secured_dialog});
	
	$('#ws_login_container').click(function(e) {
		e.stopPropagation();
	});
	$(document).click(close_wl_dialog);	
	
}
// for PDP and skateboard and quickview -- add to wishlist
function openWishListDialogForm_common(arguments) {
	var html = '<div id="ws_login_container"></div>';
	var html_shadow = '<div id="ws_login_container_shadow"><div><div></div></div></div>';
	
	$("body").prepend(html);
	$("body").prepend(html_shadow);
	
	var targetOffset = $("#addToWishlist img").offset();
	var button_width = $("#addToWishlist img").width();
	var button_height = $("#addToWishlist img").height();
	
	var width = $("#ws_login_container").width();
	var height = $("#ws_login_container").height();
	
	var left = targetOffset.left;
	left -= (width - button_width) / 2;
	var top = targetOffset.top;
	top -= height + 10;
	
	$("#ws_login_container").css({left: left, top: top});
	$("#ws_login_container_shadow").css({left: left + 6, top: top + 6, opacity: 0.5});
	if (typeof(bv_RR_enabled) != "undefined")
		arguments = arguments + "&bv_RR_enabled=" + bv_RR_enabled;
	if (typeof(bv_AA_enabled) != "undefined")
		arguments = arguments + "&bv_AA_enabled=" + bv_AA_enabled;
		
	$("#ws_login_container").load(loginTemplatePath + "?" + arguments, {secured: secured_dialog});
	
	$('#ws_login_container').click(function(e) {
		e.stopPropagation();
	});
	$(document).click(close_wl_dialog);	
}

// submit sign up for registration form from dialog
function toRegistration() {
	
	document.gotoRegistration.submit();
}


function close_wl_dialog() {
	if (window.parent == null) {
		$("#ws_login_container_shadow").remove();		
		$("#ws_login_container").remove();
	}
	else {
		window.parent.$("#ws_login_container_shadow").remove();		
		window.parent.$("#ws_login_container").remove();
	}
}
	
function resizeDialogBox(width, height) {
	$(document).ready(function() {
		var x = ($("#wl_iframe_id").width() - width) / 2;
		var y = $("#wl_iframe_id").height() - height;
		
		x = "+=" + x + "px";
		y = "+=" + y + "px";
		
		$("#wl_iframe_id").css("visibility", "hidden");
		
		$("#ws_login_container_shadow").animate({left: x, width: width, top: y, height: height});
		$("#ws_login_container_shadow div").animate({width: width - 2, height: height - 2});
		$("#ws_login_container_shadow div div").animate({width: width - 4, height: height - 4});		
		$("#ws_login_container").animate({left: x, width: width, top: y, height: height});
		$("#wl_iframe_id").animate({width: width, height: height}, function() {
			$("#wl_iframe_id").css("visibility", "visible");
			// focus needs to wait until the animation is finished
			setTimeout("focusInput()", 300);
		});		
	});
}

function focusInput() {
	$("form input[type='text']:first", frames['wl_iframe_id'].document).focus();
}

function conversionEventTagForIFrame (theEvent, stage, category) {
	cmCreateConversionEventTag(theEvent, stage, category, 0);	
}

function createCookieforIFrame (cookieName, cookieValue){
	createCookie(cookieName, cookieValue);
}

function eraseCookieForIFrame(cookieName) {
	eraseCookie(cookieName); 
}