﻿///<reference path="/layout/js/lib/jquery.min.js" />
var savedSKU = '';
var ie = (navigator.userAgent.indexOf("MSIE") > 0) ? true : false;
var s;
var _modelname = "";

function CookieJWebQL(container) {
	this._container = container;
	this._trigger = '.quick-view';
	this._h =  document.body.clientHeight;
	this._x = document.body.clientWidth;
}

CookieJWebQL.prototype.InitQuickLook = function () {
	var coord = $(this).offset();
	var scroll = $(window).scrollLeft();
	var corZ = $(window).width();
	var corC = $('.custom-lightbox').width();
	var corX = (corZ - corC) / 2;
	$(this._trigger).click(function () {
		$('.overlay').css('height', this.h + 'px').css("display", "block");
		$('.overlay-bg').css('height', this.h + 'px').css("display", "block");
		$('.custom-lightbox').css({ 'marginTop': ($(window).height() - $('.custom-lightbox').height()) / 3 + $(window).scrollTop() + "px", 'left': ($(window).width() - $('.custom-lightbox').width()) / 2 + $(window).scrollLeft() + "px" });
		$('.custom-lightbox').fadeIn(400);
		return false;
	});

	old_width = $(document).width();

	window.onresize = function () {

		var window_w = $(document).width();
		if (old_width > window_w) {
			var xy = (old_width - window_w) / 2;
			new_cord = corX - xy;
		} else if (old_width < window_w) {
			var xy = (window_w - old_width) / 2;
			new_cord = corX + xy;
		}
		old_width = window_w;
		corX = new_cord;
		$('.custom-lightbox').css('left', new_cord);
	}

}

CookieJWebQL.prototype.LoadQuickLook = function (productid, color) {
	var box = document.getElementById('quickviewcont');
	var where;
	var url = '/store/QuickLook.aspx';
	var params = 'productid=' + productid + '&action=1&where=' + where;
	var xmlHttp = this.InitAjax();
	xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState == 4) {
				if (document.all) {
						var result=xmlHttp.responseText;
							result=result.replace(/<[//]{0,1}(form|Form)[^><]*>/g, ""); 
						 box.innerHTML = result;  
				}
				else
				{
						box.innerHTML = xmlHttp.responseText;
				} 
				
				ql.PostRenderQuickLook(productid);

				if (typeof document.body.style.maxHeight == "undefined") {             
					hideSelects('hidden');
				}
				down = false;
    	}
	}
	xmlHttp.open("POST", url, true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(params);

	return;
}
CookieJWebQL.prototype.PostRenderQuickLook = function (productid) {
       $('.overlay').fadeTo("fas", 0.6);
		this.LoadJS();
		change_unitcost(productid);
		initializeAllProducts('true');

		$('.custom-lightbox .close-lightbox, .overlay').click(function () {
			$('.overlay').fadeOut(500);
			$('.custom-lightbox').fadeOut(500);
		})

		_modelname = document.getElementById('ModelName').innerHTML;

		Cufon.replace('#QVTitle h2.cattitle');

}
CookieJWebQL.prototype.LoadJS = function () {
	if(typeof document.getElementById("JLoader") != "undefined") {
		var content = document.getElementById("jLoader").innerHTML;
		var head = document.getElementsByTagName("head");
		var script = document.createElement("script");
		head[0].appendChild(script);
		script.type = "text/javascript";
		script.text = content;
	}
}
CookieJWebQL.prototype.InitAjax = function () {
	var xmlHttp;

	try {
		xmlHttp = new XMLHttpRequest();
	}
	catch (e) {
		try {
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
			try {
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e) {
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}
	return xmlHttp;
}

var ql = new CookieJWebQL('.custom-lightbox');


HideCharacters = function() {
	var dsclen = $('.qvdsc').length;
	$('.qvdsc').hide();
}

function setValue(sku, color) {
	current_sku = sku;
	current_color = color;

	return;
}

function BuildQuicklookJs() {
}

function showPopUp(productid, color) {
	ql.LoadQuickLook(productid);
	HideCharacters();
}

function clearsizes() {
	window.location.reload(true);
}

function s_config(obj) {
	try {
		s = s_gi('onestophuds');
		s.linkTrackVars = 'prop7,eVar12';
		s.prop7 = 'ajaxproductdetail';
		s.eVar12 = 'ajaxproductdetail';
		s.tl(obj, 'o', 'ajaxproductdetail');
		s.linkTrackVars = 'None';
		s.prop7 = '';
		s.eVar12 = '';
	}
	catch (e) { }
}


function trackme(url, query) {
	if (window.XMLHttpRequest) {
		request = new XMLHttpRequest();

	} else if (window.ActiveXObject) {
		request = new ActiveXObject("Microsoft.XMLHTTP");
	}
	if (request) {
		request.onreadystatechange = function () {
			return false;
		};
		request.open("get", url, true);
		request.send(query);
	}
}

function addToCart(productid) {
	var qty = document.getElementById('qty_sel_ddl').value;
	var productid = document.getElementById('ProductIDHidden').value;
	var color = document.getElementById('ProductColors').value;
	var size = document.getElementById('ProductSizes').value;
	var modelname = document.getElementById('ModelName').innerHTML;
	var price = document.getElementById('UnitCost').innerHTML;

	if (color == '-1') {
		$('#message').show();
		$('#message').html('Please select a color!');	
		$('#message').css('alert');	

		return false;
	}

	if (size == '-1') {
		$('#message').show();
		$('#message').html('Please select a size!');
		$('#message').css('alert');	
		return false;
	}

	if (!qty.match(/^[0-9]+$/) || qty == 0) {
		alert('quantity is Invalid');
		return false;
	}

	var xmlHttp = ql.InitAjax();
	var params = '?productid=' + productid + '&colorid=' + color + '&sizeid=' + size + "&quantity=" + qty + "&ajax-add=1";
    var prodPlacementMember = -1;
	if (prodPlacementJSON && prodPlacementJSON.ProductPlacement) {
	    
	    for (var index = 0; index < prodPlacementJSON.ProductPlacement.length; index++) {
	        if (prodPlacementJSON.ProductPlacement[index].ProductID == productid) {
	            if (prodPlacementJSON.ProductPlacement[index].ColorID > 0) {
	                if (prodPlacementJSON.ProductPlacement[index].ProductID == productid && prodPlacementJSON.ProductPlacement[index].ColorID == color) {
	                    prodPlacementMember = prodPlacementJSON.ProductPlacement[index].PlacementMemberID;
	                    break;
	                }
	                continue;
	            } else {
	                prodPlacementMember = prodPlacementJSON.ProductPlacement[index].PlacementMemberID
                }
            }
        }
    }
	if (prodPlacementMember > 0) {
	    params += "&placementMemberID=" + prodPlacementMember;
	}

	document.getElementById('productid').value = productid;
	document.getElementById('colorid').value = color;
	document.getElementById('sizeid').value = size;
	document.getElementById('params').value = params;

	var url = '/store/os/addtocart.aspx';
	url += params;

	$('#message').show();
	$('#message').html('processing...');
		
	$.ajax({
		url: url,
		success: function () {
			var notice = $('#message');
			var checkout = $('#checkout');
			var wishlist = $('#wishlist');
			notice.html('<span id="cart-msg">' + qty + ' ' + _modelname + ' <br/> has been added to your bag!</span>');
			wishlist.hide();
			checkout.show();
			cj.GetCartCount();
			cj.ShowMiniCart();
//			UpdateShoppingBag();
//			load_minicart_contents();
		},
		error: function () {
			notice.html('item has not been added!');
		}
	});

	return false;
}

function addToWishList() {
	var qty = document.getElementById('qty_sel_ddl').value;
	var productid = document.getElementById('ProductIDHidden').value;
	var color = document.getElementById('ProductColors').value;
	var size = document.getElementById('ProductSizes').value;
	var modelname = document.getElementById('ModelName').innerHTML;
	var price = document.getElementById('UnitCost').innerHTML;

	if (color == '-1') {
		$('#message').show();
		$('#message').html('Please select a color!');
		return false;
	}

	if (size == '-1') {
		$('#message').show();
		$('#message').html('Please select a size!');
		return false;
	}

	if (!qty.match(/^[0-9]+$/) || qty == 0) {
		alert('quantity is Invalid');
		return false;
	}

	var xmlHttp = ql.InitAjax();
	var params = '?productid=' + productid + '&colorid=' + color + '&sizeid=' + size + "&quantity=" + qty + "&ajax-additem=1";

	document.getElementById('productid').value = productid;
	document.getElementById('colorid').value = color;
	document.getElementById('sizeid').value = size;
	document.getElementById('params').value = params;

	var url = '/store/MyWishList.aspx';
	url += params;

	$('#message').show();
	$('#message').html('processing...');

	$.ajax({
		url: url,
		success: function (transport) {
			wishListSuccess(transport, qty, modelname);
		},
		error: function () {
			notice.html('item has not been added!');
		}
	});

	return false;
}

function wishListSuccess(transport, qty, modelname) {
	if (transport) {
		var start = transport.indexOf("##JSONSTART##");
		if (start != -1) {
			var jsonString = transport.substr(start, transport.indexOf("##JSONEND##"));
			jsonString = jsonString.replace("##JSONSTART##", "");
			var json = eval('(' + jsonString + ')');
			if (json.Response.Redirect) {
				redirect(json.Response.RedirectURL);
				return;
			}
		}
	}
	var notice = $('#message');
	var checkout = $('#checkout');
	notice.html('<span id="cart-msg">' + qty + ' ' + _modelname + ' has been added <br/> to your <a class=qlwishListLink href=/store/MyWishList.aspx >wish list</a>');
	checkout.hide('display', 'block');
	checkout.hide();
	UpdateShoppingBag();
	load_minicart_contents();
}
function redirect(url) {
	window.location = url;
}

function hideSelects(action) {
	if (action != 'visible') { action = 'hidden'; }
	if (navigator.appName.indexOf("MSIE")) {
		for (var S = 0; S < document.forms.length; S++) {
			for (var R = 0; R < document.forms[S].length; R++) {
				if (document.forms[S].elements[R].options) {
					document.forms[S].elements[R].style.visibility = action;
				}
			}
		}
	}
}
function getItemColor(productid) {
	if (!document.getElementById('quickViewBtn_' + productid))
		skuColor = '';
	else
		skuColor = document.getElementById('quickViewBtn_' + productid).title;

	return skuColor;
}

function getFirstChildWithTagName(element, tagName) {
	for (var i = 0; i < element.childNodes.length; i++) {
		if (element.childNodes[i].nodeName == tagName) return element.childNodes[i];
	}
}

function getHash(url) {
	var hashPos = url.lastIndexOf('#');
	return url.substring(hashPos + 1);
}

