function getObj(id) {
	if (document.getElementById)
		var returnObj = document.getElementById(id);
	else if (document.all)
		var returnObj = document.all[id];
	else if (document.layers)
		var returnObj = document.layers[id];
	return returnObj;
}

function SelectWholesaleBelt(isChecked,id,sizeDiv,sizes) {
	if (isChecked) {
		getObj(sizeDiv).style.display = "block";
	} else {
		for (i=0; i<sizes.split(",").length; i++) {
			getObj("size_"+id+"_"+sizes.split(",")[i]).value = "";
		}
		getObj(sizeDiv).style.display = "none";
		getObj("subtotal_"+id).value = formatCurrency(0.00);
	}
}

function CountWholesaleBelts(id,sizes) {
	total = 0;
	sizeArray = sizes.split(",");
	for (i=0; i<sizeArray.length; i++) {
		sizeField = getObj("size_"+id+"_"+sizeArray[i]);
		if (!isNaN(parseInt(sizeField.value))) {
			total = total + parseInt(sizeField.value);
		}
	}
	return total;
}

function WholesaleTotals(id,price,sizes) {
	totalField = getObj("subtotal_"+id);
	total = CountWholesaleBelts(id,sizes);
	totalField.value = formatCurrency(total*price);
}

function selectThreadColor(id) {
	if (id > 0) {
		hexcolor = threadcolors[id];
		bordercolor = "#000000";
	} else {
		hexcolor = "#ffff99";
		bordercolor = "#ffff99";
	}
	getObj("ThreadColorPreview").style.backgroundColor = hexcolor;
	getObj("ThreadColorPreview").style.borderColor = bordercolor;
}

this.imagePreview = function(){	
	xOffset = 10;
	yOffset = 30;

	$("a.preview").hover(function(e){
		this.t = this.title;
		this.title = "";	
		var c = (this.t != "") ? "<br/>" + this.t : "";
		$("body").append("<p id='preview'><img src='"+ this.href +"' alt='Image preview' />"+ c +"</p>");								 
		$("#preview")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px")
			.fadeIn("fast");						
	},
	function(){
		this.title = this.t;	
		$("#preview").remove();
	});	
	$("a.preview").mousemove(function(e){
		$("#preview")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px");
	});			
};

function toggleShippingInfo(isChecked) {
	if (isChecked) {
		getObj("ShippingInfo").style.display = "none";
	} else {
		getObj("ShippingInfo").style.display = "block";
	}
}

function toggleHeardAboutUs(value) {
	if (value == 0) {
		getObj("HeardAboutOther").style.display = "block";
	} else {
		getObj("HeardAboutOther").style.display = "none";
	}
}

function SelectShippingMethod() {
	document.PaymentForm.validate.value = false;
	document.PaymentForm.submit();
}
function ApplyCoupon() {
	document.PaymentForm.validate.value = false;
	document.PaymentForm.submit();
}

// Gift Pack Belt Functions
function filterBelts() {
	getObj("Loading").style.display = "inline";
	document.FilterForm.submit();
}

function displayBelts(show) {
	$(".BeltContainer").hide();
	//alert(show.indexOf(54));
	getObj("Loading").style.display = "none";
	if (show.length) {
		for (i=0; i<show.length; i++) {
			if (getObj("belt"+show[i])) {
				getObj("belt"+show[i]).style.display = "block";
			}
		}
	}
}


function formatCurrency(num) {
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num))
	num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10)
	cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
	num = num.substring(0,num.length-(4*i+3))+','+
	num.substring(num.length-(4*i+3));
	return (((sign)?'':'-') + '$' + num + '.' + cents);
}


// starting the script on page load
$(document).ready(function() {
	imagePreview();
	
	$('a[rel=submenu_SHOP]').mouseenter(function() { var height = $(this).height(); var top = $(this).offset().top; var left = $(this).offset().left + ($(this).width() /2) - ($('#submenu_SHOP').width() / 2); $('#submenu_SHOP').show(); $('#submenu_SHOP').css({'top':top, 'left':left}); });
	$('a[rel=submenu_WEAR]').mouseenter(function() { var height = $(this).height(); var top = $(this).offset().top; var left = $(this).offset().left + ($(this).width() /2) - ($('#submenu_WEAR').width() / 2); $('#submenu_WEAR').show(); $('#submenu_WEAR').css({'top':top, 'left':left}); });
	$('a[rel=submenu_BUZZ]').mouseenter(function() { var height = $(this).height(); var top = $(this).offset().top; var left = $(this).offset().left + ($(this).width() /2) - ($('#submenu_BUZZ').width() / 2); $('#submenu_BUZZ').show(); $('#submenu_BUZZ').css({'top':top, 'left':left}); });
	$('a[rel=submenu_ABOUT]').mouseenter(function() { var height = $(this).height(); var top = $(this).offset().top; var left = $(this).offset().left + ($(this).width() /2) - ($('#submenu_ABOUT').width() / 2); $('#submenu_ABOUT').show(); $('#submenu_ABOUT').css({'top':top, 'left':left}); });

	$('.NavigationSubmenuBox').mouseleave(function () { $(this).hide(); });
	
	$("label").inFieldLabels({ fadeOpacity : 0.3 });
	
	
	$(".zoomImage").jqzoom({
		'showEffect'	:	"fadein",
		'zoomWidth'		:	400,
		'zoomHeight'	:	225,
		'xOffset'		:	25
	});
	
	$("a[rel^='prettyPhoto']").prettyPhoto({
		theme					: 'facebook',
		iframe_markup		: '<iframe src ="{path}" width="{width}" height="{height}" frameborder="no" scrolling="No"></iframe>'
	});
	
	
	$('.fade').innerfade({ speed:2000, timeout:5000, type:'sequence', containerheight:'300px' }); 
	
	$(".GiftPackBelt").click(function () { 
		$.prettyPhoto.open('http://www.google.com','Title','Description');
	});
	
	$('a.bookpreview').qtip({ style: { name: 'red', tip: true }, adjust: { screen: true }, position: { corner: { target: 'bottomMiddle', tooltip: 'topMiddle' } } })
});


function regformFocus() {
	
}
function regformBlur() {
	
}


function slideSwitch() {
    var $active = $('#slideshow IMG.active');
    if ( $active.length == 0 ) $active = $('#slideshow IMG:last');
    var $next =  $active.next().length ? $active.next()
        : $('#slideshow IMG:first');
    $active.addClass('last-active');
    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
        });
}

$(function() {
    setInterval( "slideSwitch()", 4000 );
});

