function miniCart() {
	this.items = [];
	this.subtotal = 0.00;
	this.curIndex = -1;
	this.timeout = 0;
}

miniCart.prototype.next = function() {
	this.curIndex = ++this.curIndex % this.items.length;
	this.display();
}

miniCart.prototype.previous = function() {
	this.curIndex--;
	if(this.curIndex < 0)
		this.curIndex = this.items.length-1;
	this.display();
}

miniCart.prototype.add = function(newMiniCartItem) {
	this.items[this.items.length] = newMiniCartItem;
	if(this.curIndex == -1)
		this.curIndex = 0;
}

// Display the curIndex in the minicart
miniCart.prototype.display = function() {
	if(this.curIndex == -1) return;
	var p = this.items[this.curIndex];
	$('#mc-pName').text(p.name);
	$('#mc-pStyle').text(p.style);
	$('#mc-pSize').text(p.size);
	$('#mc-pColor').text(p.color);
	$('#mc-pPrice').html(p.amount + "@" + p.price.toFixed(2) + "&nbsp;&nbsp;<span style='color:red'>" + parseFloat(p.price * p.amount).toFixed(2) + "</span>");
	$('#mc-pImage > img').attr('src', decodeURIComponent(p.thumbnail));
}

function miniCartItem() {
	this.name = "";
	this.style = "";
	this.size = "";
	this.color = "";
	this.amount = 0;
	this.price = 0.00;
	this.thumbnail = "";
}

function blackout() {
	this.b = $('#blackout');
	this.m = $('#bo-messageContainer');
	this.l = $('#bo-loader');
}


blackout.prototype.resize = function() {
	this.b.height($(window).height());
	this.b.width($(window).width());
}

blackout.prototype.center = function(elm) {
	var w = elm.width(),
	    h = elm.height(),
	    sh = 0;

	if (navigator.userAgent.indexOf("MSIE") > -1) {
    sh = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;		
	}	else {
		sh = window.pageYOffset;
	}

	elm.css({
		'left': (elm.parent().width() - w)/2,
		'top' : sh + $(window).height() / 3 - h/2
	});
}

blackout.prototype.bindScroll = function(elm) {
	var h;

	if(navigator.userAgent.indexOf("MSIE 6") >= 0)
		return;
	
	$(window).bind('scroll', function(event) {
		if (navigator.userAgent.indexOf("Firefox") > -1) {
			h = window.pageYOffset;
		}
		else {
      h = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;		
    }
		elm.stop(true);
		elm.animate({
			'left': (elm.parent().width() - elm.width())/2,
			'top' : h+$(window).height()/3 - elm.height()/2
		}, 500, 'swing');
	});
}

blackout.prototype.error = function(s) {
	var is_ie = navigator.userAgent.indexOf("MSIE");
	if (is_ie < 0) { //not IE
		this.resize();
	}
	this.l.hide();

	this.bindScroll(this.m);
	$(this.m).find('blockquote').html(s);
	this.center(this.m);
	if (is_ie < 0) { //not IE
		this.b.show();
	}
	this.m.show();
}

blackout.prototype.showLoader = function() {
	var is_ie = navigator.userAgent.indexOf("MSIE");
	if (is_ie < 0) { //not IE
		this.resize();
	}
	this.m.hide();

	this.bindScroll(this.l);
	this.center(this.l);
	if (is_ie < 0) { //not IE
		this.b.show();
	}
	this.l.show();
}

blackout.prototype.show = function(id) {
	var is_ie = false, ieversion = false;
	if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)) { //test for MSIE x.x;
		ieversion=new Number(RegExp.$1) // capture x.x portion and store as a number
 		is_ie = true;
	}
	
	if(!is_ie || ieversion >= 8) {
 		this.resize();
	}
	this.m.hide();
	this.l.hide();
	if(!is_ie || ieversion >= 8) {
		this.b.show();
	}
	if(id != null) {
		this.bindScroll($(id));
		this.center($(id));
		$(id).show();
	}
}

blackout.prototype.hide = function(id) {
	$(window).unbind('scroll');
	this.b.hide();
	this.l.hide();
	this.m.hide();

	if(id != null)
		$(id).hide()
}

function submitForm() {
	document.loginform_logout.submit();
}

function togglePopUp(id) {
	if($('#'+id).css("display") == "none") {
		blackout.show('#'+id);
		if(id == "loginForm") $('#username').focus();
	}
	else {
		blackout.hide('#'+id);
	}
}

function swap_img(img,pid) {
	document.getElementById("mainimg_"+pid).src= img;
}

$(document).ready(function() {
	$('#input_user, #input_pass, #input_search').focus(function() {
		$(this).val('');
	});
	
	$('#input_user').blur(function() {
		if($(this).val() == '')
			$(this).val('user name');
	});
	
	$('#input_pass').blur(function() {
		if($(this).val() == '')
			$(this).val('password');
	});
	
	$('#input_pass').keypress(function(e) {
		if(e.keyCode == 13)
			document.login_form.submit();
	});
	
	$('#logo').click(function() {
		window.location='/customer/home.php';
	});
	
	$('.rotating_promo').click(function() {
		window.location='/Free-Returns';
	});
	
	$('#btn_login').click(function() {
		document.login_form.submit();
	});
	
	$('#btn_logout').click(function() {
		document.logout_form.submit();
	});
	
	$('#btn_search').click(function() {
		document.search_form.submit();
	});
	
	$('#input_search').blur(function() {
		if($(this).val() == '')
			$(this).val('Search site');
	});
	
	$('#input_search').keypress(function(e) {
		if(e.keyCode == 13)
			document.search_form.submit();
	});
	
	$('#btn_contact').click(function() {
		window.location='/Contact-Us/help/39/';
	});
	
	$('#secure_pass').keypress(function(e) {
		if(e.keyCode == 13)
			document.auth_form.submit();
	});
	
	$('#btn_secure_close, #btn_secure_cancel').click(function() {
		togglePopUp('loginForm');
	});
	
	$('#btn_secure_login').click(function() {
		document.auth_form.submit();
	});
	
	$('#btn_mc_checkout').click(function() {
		window.location='/customer/cart.php';
	});
});

