/* tshirttank.com. written by allwebpromotion.com. */

var tshirttank = {
	"emailHint"				:	'Type Your Email Address Here',
	"contentsImage"			:	null,
	"contentsImageWidth"	:	null,
	"contentsImageHeight"	:	null
};

function mysiteVerifyEmailAddress (emailAddress) {
	if (emailAddress.indexOf(".") == -1 || emailAddress.indexOf("@") == -1) {
		alert("Please enter a valid email address.");
		return false;
	}
	return true;
}

function mysiteSetStyleGroupPrice (jStyleGroup, price, salePrice) {
	if(salePrice) {
		$(jStyleGroup)
			.find('.price')
				.addClass('sale')
				.end()
				.find('.price span')
					.text(price)
				.end()
				.find('sale-price')
					.css('display', 'inline')
					.text(salePrice);
	} else {
		$(jStyleGroup)
			.find('.price')
				.removeClass('sale')
				.text(price)
			.end()
			.find('sale-price')
				.css('display', 'none');
	}
}

jQuery.fn.vjustify = function() {
    var maxHeight = 0;
    this.each(function(){
        if (this.offsetHeight > maxHeight) {
			maxHeight = this.offsetHeight;
		}
    });
    this.each(function(){
        $(this).height(maxHeight + "px");
        if (this.offsetHeight > maxHeight) {
            $(this).height((maxHeight - (this.offsetHeight-maxHeight)) + "px");
        }
    });

};

function countdownStart (init) {
	var expires = new Date();
	/*var timezoneDiff = 6;
	var timezoneOffsetHours = expires.getTimezoneOffset() / 60;
	timezoneOffset = timezoneDiff - Math.abs(timezoneOffsetHours);
	expires.setTime(expires.getTime() - (timezoneOffset * 60 * 60 * 1000));*/
	expires.setHours(23);
	expires.setMinutes(59);
	expires.setSeconds(55);
	if (init) {
		$('#countdown').countdown({
			'until'			: expires,
			'layout'		: '<span class="digits hours">%H%nn%H</span><span class="digits minutes">%M%nn%M</span><span class="digits seconds">%S%nn%S</span>',
			'description'	: '',
			'format'		: 'HMS',
			'onExpiry'		: countdownStart,
			'alwaysExpire'	: true
		});
	} else {
		$('#countdown').countdown('change', {
			'until' : expires
		});
	}
}

$(document).ready(function() {

	var isIE6 = $.browser.msie 
					&& /MSIE 6\.0/i.test(window.navigator.userAgent) 
					&& !/MSIE 7\.0/i.test(window.navigator.userAgent);

	//$("#footer-links .separator").html('&nbsp;&nbsp;&nbsp;');
	//$(".breadcrumbs .separator").html('&nbsp;&nbsp;&nbsp;');

	countdownStart(true);

	$(".contentsItem").mouseover(function(e){
		tshirttank.contentsImageWidth = $(this).find(".mainview").attr("width");
		$(this).find(".mainview").attr("width",$(this).find(".altview").attr("width"));
		tshirttank.contentsImageHeight = $(this).find(".mainview").attr("height");
		$(this).find(".mainview").attr("height",$(this).find(".altview").attr("height"));
		tshirttank.contentsImage = $(this).find(".mainview").attr("src");
		$(this).find(".mainview").attr("src",$(this).find(".altview").attr("src"));
	});

	$(".contentsItem").mouseout(function(e){
		$(this).find(".mainview").attr("width",tshirttank.contentsImageWidth);
		$(this).find(".mainview").attr("height",tshirttank.contentsImageHeight);
		$(this).find(".mainview").attr("src",tshirttank.contentsImage);
	});

	$(".gallery-thumb-link").mouseover(function(e){
		var thumbImage = $(this).find("img");
		var featureImageLink = $("#contentarea .gallery-feature-link");
		var featureImage = featureImageLink.find("img");
		featureImage.attr("src", $(this).attr("href"));
		featureImageLink.attr("href", $(this).attr("href"));
	});

	/* validate order form */

	$("#addtocart-form").submit(function(){
		var ok = false;
		$(this).find('.sizeOption').each(function (i) {
			if($(this).attr('checked')) {
				ok = true;
			}
		});
		if(!ok) {
			alert('Please select a size.');
		}
		return ok;
	});

	/* initialize style prices */

	$(".styleGroup").each(function(index, elem) {
		// elem == this
		if ($(elem).find(".styleGroupItem:first").attr("rel")) {
			var words = $(elem).find(".styleGroupItem:first").attr("rel").split('|');
			var len = words.length;
			var price, salePrice;
			if(len > 4) { price = words[3]; }
			if(len > 5) { salePrice = words[4]; }
			mysiteSetStyleGroupPrice(elem, price, salePrice);
		}
	});

	/* define click behavior for style thumbnails, set preview chosen and prices */

	$(".styleGroupItem").click(function(){
		//$("#preview-sample img").attr("src", "http://site.tshirttank.com/images/loading.gif");
		$("#preview-sample").css("backgroundImage", 'url(' + $(this).attr("href") + ')');
		// de-select all thumbs
		$(".styleGroupItem").removeClass("selected");
		// select this thumb
		$(this).addClass("selected");
		var words = $(this).attr("rel").split('|');	
		var price = '', salePrice = '';
		if (words) {
			var offsetleft, paddingLeft, paddingRight;
			for (var i=0;i<words.length;i++) {
				switch (i) {
					case 0:
						$("#build-style-input").attr("value", words[i]);
						break;
					case 1:
						$("#preview-chosen-kind").html(words[i]);
						break;
					case 2:
						$("#preview-chosen-color").text(words[i]);
						break;
					case 3:
						var price = words[i];
						break;
					case 4:
						var salePrice = words[i];
						break;
					case 5:
						src = words[i];
						/*if(isIE6) {
							src = 'http://site.tshirttank.com/scripts/pngtogif.php?i=' + src;
						}*/
						$("#preview-sample img").attr("src", src);
						break;
					case 6:
						$("#preview-sample img").css("marginTop", words[i] + 'px');
						break;
					case 7:
						offsetLeft = parseInt(words[i]);
						paddingLeft = 0;
						paddingRight = 0;
						if (offsetLeft > 0) {
							paddingLeft = offsetLeft;
						} else if (offsetLeft < 0) {
							paddingRight = Math.abs(offsetLeft);
						}
						$("#preview-sample img").css("paddingLeft", paddingLeft + 'px');
						$("#preview-sample img").css("paddingRight", paddingRight + 'px');
						break;
				}	
			}
			mysiteSetStyleGroupPrice($(this).parent(".styleGroup"), price, salePrice);
		}
		return false; // cancel default action
	});

	/* define click behavior for size buttons */

	$(".radioOptions input").click(function(){
		$("#preview-chosen-size").text($(this).attr("title"));
	});

	/* initialize preview image to selected thumbnail */

	$(".styleGroupItem.selected").trigger('click');

	/* define click behavior of preview addtocart link to call form submit */

	$("#preview-addtocart-link").click(function(){
		$("#addtocart-form").trigger('submit');
	});
	$("#preview-addtocart-link").css('display', 'block');

	/* equalize height of style group columns */

	$(".styleGroup").vjustify();

	/* email signup */

	$('#frmSS32-email').val(tshirttank.emailHint);
	$('#frmSS32-email').focus(function(){
		if($(this).val() == tshirttank.emailHint) {
			$(this).val('');
		}
	});
	$('#frmSS32-email').blur(function(){
		if($(this).val() == '') {
			$(this).val(tshirttank.emailHint);
		}
	});

	$('#frmSS32').submit(function(){
		if(!mysiteVerifyEmailAddress($('#frmSS32-email').val())) {
			return false; // cancel default action
		}
	});

});
