function viewport() {
	var win = new Object()
	if (self.innerHeight) { // all except Explorer
		win.x = self.innerWidth;
		win.y = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight)	{
		// Explorer 6 Strict Mode
		win.x = document.documentElement.clientWidth;
		win.y = document.documentElement.clientHeight;
	}
	else if (document.body) { // other Explorers
		win.x = document.body.clientWidth;
		win.y = document.body.clientHeight;
	}
	
	return win
}

function zoomXpress(evt) {
	var itemURL = $(this).parent().attr('href');
	divID = $(this).parent().attr('id') + popSuffix;
	
	//Check to see if it is already open
	if( $.inArray(divID, popUps) >= 0 ) {
		//Reorder popUps and stack em - fix this
		popUps.splice($.inArray(divID, popUps), 1)
		popUps.push(divID);
		stackPopUps();
	} else {

		//Create main-div 
		var newPopUp = $('<div>');
		newPopUp.attr('id', divID);
		newPopUp.addClass('vwd-popUp');
		newPopUp.css({	'position'	: 'absolute',
						'display'	: 'none'
					 });
		//Add Top Border
		var topBorder = $("<div class='jqHandle'>");
		topBorder.css({	'height'			: '26px',
						'background-color'	: '#0874cc',
						'text-align'		: 'right',
						'cursor'			: 'move'
					  });
		topBorder.append("<img class='jqmClose' style='cursor:pointer;' src='/lib/yhst-30049403779351/close.gif'/>");
		newPopUp.append(topBorder);
		newPopUp.bind('mousedown', clickPopUp);
		
		//Add it to <body>
		$('body').prepend(newPopUp);

//		$.get(itemURL, popShop);
		$.ajax({
					 		type			: "GET",
							url				: itemURL,
							success		: popShop
//							error			: ajaxError
					 });
	}

	evt.preventDefault();
}

function ajaxError(XMLHttpRequest, textStatus, errorThrown) {
	console.log(XMLHttpRequest);
	console.log(textStatus);
	console.log(errorThrown);
	console.log(this);
	console.log(XMLHttpRequest.getResponseHeader('Location'));
}

function popShop(data, textStatus) {
//	console.log(textStatus);
	var jqDiv = $('#' + divID);  //This could be done better
	jqDiv.width(400).css({
							'border'			: '1px solid black',
							'position'			: 'absolute',
							'background-color'	: '#ffffff'
						});
	//Set top & left location of main div
	var pageY, pageX;
	if(typeof(window.pageYOffset)=='number') {
		pageY=window.pageYOffset;
		pageX=window.pageXOffset;
	} else {
		pageY=document.documentElement.scrollTop;
		pageX=document.documentElement.scrollLeft;
	}

	if(popUps.length > 0) {
		var lastPopUp = '#' + popUps[popUps.length - 1];
		var topOff = parseInt($(lastPopUp).css('top')) + 26;
		jqDiv.css('top', (topOff > pageY) ? topOff : (30 + pageY));
		jqDiv.css('left', parseInt($(lastPopUp).css('left')) + 26);
	} else {
		jqDiv.css({'top':30 + pageY,'left':30 + pageX});
	}
	
	//Build Body Div
	//Container Div
	var wrapTag = $("<div class='popUp-wrap'>");
	//Image
	var dataBit = $(data).find('#thumb-img').text();
	var nextTag = $('<img>');
	nextTag.attr({'id':'product-image', 'src':dataBit});
	wrapTag.append(nextTag);
	
	//Item Table
	var itemTableForm = $(data).find('#itemtable').parent();
	
	//Set form submit
	itemTableForm.bind('submit', addToCart);
	
	itemTableForm.find('#itemtable').attr('width', 200);
	
	//Remove sku
	itemTableForm.find('#itemtable-sku').remove();
	//Remove availability
	itemTableForm.find('#itemtable-availablity').remove();
	//Remove Options
	//itemTableForm.find('.itemtable-options').remove();

	//set form taget to hidden iframe
	itemTableForm.attr('target', 'vwd-frame');
	
	//change filename for add-to-cart button
	itemTableForm.find("#addtocartimg").attr('src', 'http://lib.store.yahoo.net/lib/yhst-30049403779351/add-to-cart-pop.gif');
	
	wrapTag.append($("<div id='itemInfo'>").append(itemTableForm));
	
	nextTag = $("<a style='margin:5px;display:block;clear:both;'>View Item</a>");
	nextTag.attr('href', $(data).find('[name=.autodone]').val());
	wrapTag.append(nextTag);
	
	dataBit = $(data).find("#abstract").text();
	if(dataBit != "")
		wrapTag.append($("<div id='itemDesc'>").append(dataBit));
	
	//Add wrapTag to jqDiv
	jqDiv.append(wrapTag);
	
	//Fire it up
	jqDiv.jqm({modal:false,overlay:0,onHide:closePopUp,toTop:true}).draggable({handle:'.jqHandle'}).jqmShow();
	
	//Push it on the stack
	popUps.push(divID);
	//Set z-indexes
	stackPopUps();
}

function xpressItemIn() {
	$(this).append("<div id='iconWrap'><img style='cursor:pointer;' src='http://lib.store.yahoo.net/lib/yhst-30049403779351/xpress-icon.png'/></div>");
	var iconWrap = $(this).find('#iconWrap');
	iconWrap.css({
					'position'	: 'absolute',
					'z-index'	: 1,
					'top'		: ($(this).find('img').height() / 2) - 13,
					'left'		: ($(this).find('img').width() - 150) / 2
				 });
	iconWrap.bind('click', zoomXpress);
}

function xpressItemOut() {
	$(this).parent().find('#iconWrap').remove();
}	

function clickPopUp(evt) {
	popUps.splice($.inArray(this.id, popUps), 1)
	popUps.push(this.id);
	stackPopUps();
}

function addToCart(evt) {
	if($('#waitBox').length < 1) {
		var nextTag = $("<div id='waitBox' style='text-align:center;display:none;'><p>Placing your Order</p><img width='43' height='11' src='/lib/yhst-30049403779351/ajax-loader.gif'/></div>")
							.jqm({modal:true});
		$('body').prepend(nextTag);
	}
	var vp = new viewport();
	var pageY, pageX;
	if(typeof(window.pageYOffset)=='number') {
		pageY=window.pageYOffset;
		pageX=window.pageXOffset;
	} else {
		pageY=document.documentElement.scrollTop;
		pageX=document.documentElement.scrollLeft;
	}

	$('#waitBox').css({
					  top	: pageY + ((vp.y - $('#waitBox').height())/2),
					  left	: pageX + ((vp.x - $('#waitBox').width())/2)
					  });
	$('#waitBox').jqmShow();
		
	closeID = $(this).find("#itemtable-addtocart input").val() + popSuffix;
//	console.log(closeID);
}

function frameLoad() {
	if(closeID != null) {
		$('#' + closeID).remove();
		popUps.splice($.inArray(popUps, closeID), 1);
		stackPopUps();
		$('#waitBox').jqmHide();
		refreshFloatCart();
	}
}

function stackPopUps() {
	var z = 3000;
	
	$.each(popUps, function(i, val){
		$('#' + this).css('z-index', z);
		z++;
	});		
}

function closePopUp(hash) {
	var id = hash.w.attr('id');
	$('#' + id).remove();
	popUps.splice($.inArray(popUps, id), 1);
}

function initXpress() {
	$('.itemImage > a').hover(xpressItemIn, xpressItemOut);
	$('.itemImage > a').each(function(i) {
		$(this).css({
					'position'	: 'relative',
					'display' 	: 'block',
					'margin'	: '0 auto',
					'padding'	: 0,
					'height'	: $(this).find('img').height(),
					'width'		: $(this).find('img').width(),
					'z-index'	: 0								
					});
	});
}

var popSuffix = '-popUp';  //Pop Up wrapper suffix
var divID; //formed name of currently building popup does not include #
var closeID;  //name of the currently closing popUp
var popUps = new Array(); //an array of strings with the divIDs open with the focused one on the bottom

var preLoad = new Array(); //preloading Array
preLoad[0] = new Image(120,26);
preLoad[0].src = 'http://lib.store.yahoo.net/lib/yhst-30049403779351/xpress-icon.png';
preLoad[1] = new Image(113,20);
preLoad[1].src = 'http://lib.store.yahoo.net/lib/yhst-30049403779351/add-to-cart-pop.gif';
preLoad[2] = new Image(43,11);
preLoad[2].src = 'http://lib.store.yahoo.net/lib/yhst-30049403779351/ajax-loader.gif';
