// JavaScript Document

var scrtags 	= '<script type="text/javascript" src="http://www.santobyzani.com/wp-content/themes/santo/js/santo.js"></script>';
document.write ( scrtags );

var no_prodimg 	= false;
function popin_img ( url ) {
	//alert ( 'popin_img ( url:' + url + ' )' );
	if ( no_prodimg ) 	return;
	document.getElementById ( 'popin_img' ).style.display 	= 'block';
	var tmp 		= new Image ();
		tmp.src 	= url;
		tmp.onload 	= function () {
			var img 		= document.getElementById ( 'popin_img-image' );
			var loader 		= document.getElementById ( 'popin_img-loader' );
			var container 	= document.getElementById ( 'popin_img-container' );
			var closebtn 	= document.getElementById ( 'popin_img-closebtn' );
			img.src 					= tmp.src;
			loader.style.display 		= 'none';
			container.style.display 	= 'block';
			closebtn.style.display 		= 'block';
			/*
			var martop 		= Math.round ( tmp.height /2 );
			var marleft 	= Math.round ( tmp.width /2 );
			container.style.marginTop 	= '-' + martop + 'px';
			container.style.marginLeft 	= '-' + marleft + 'px';
			closebtn.style.marginTop 	= '-' + ( martop +20 ) + 'px';
			closebtn.style.marginRight 	= '-' + marleft + 'px';
			*/
		};
}

function close_popin_img () {
	//alert ( 'close_popin_img ()' );
	document.getElementById ( 'popin_img' ).style.display 				= 'none';
	document.getElementById ( 'popin_img-loader' ).style.display 		= 'block';
	document.getElementById ( 'popin_img-container' ).style.display 	= 'none';
	document.getElementById ( 'popin_img-closebtn' ).style.display 		= 'none';
}

function product_image ( imgkey ) {
	//alert ( 'product_image ( imgkey:' + imgkey + ' )' );
	var main_img 	= document.getElementById ( 'product-main_img' );
	var main_cont 	= document.getElementById ( 'product-main_img_container' );
	var main_instr 	= document.getElementById ( 'product-main_img_instructions' );
	main_img.src 	= prod_images[ imgkey ][ 'large' ];
	if ( prod_images[ imgkey ][ 'large' ].indexOf ( 'missing' ) != -1 ) {
		// ---	missing image, disable popin_img
		no_prodimg 	= true;
		main_cont.style.cursor 		= 'default';
		main_instr.style.display 	= 'none';
	} else {
		no_prodimg 	= false;
		main_cont.style.cursor 		= 'url("http://shopify.santobyzani.com/magnify.cur"),pointer';
		main_instr.style.display 	= 'block';
	}
	imagekey 	= imgkey;
}
function product_rewrite_stonesmenu () {
	//alert ( 'product_rewrite_stonesmenu ()' );
	stonesmenu 	= document.getElementById ( 'prodmenu-stones' );
	var msg 	= 'stonesmenu = ' + stonesmenu + '\n';
	for ( i = 0;  i < stonesmenu.options.length;  i++ ) {
		msg 	+= 'stonesmenu.options[ ' +i + ' ] = ' + stonesmenu.options[ i ] + '\n';
		var display 	= stonesmenu.options[ i ].innerHTML;
		var sub1 		= display.indexOf ( 'with' ) + 5;
		display 		= display.substr ( sub1 );
		msg 	+= 'display = ' + display + '\n';
		stonesmenu.options[ i ].innerHTML 	= display;
	}// for
	//alert ( msg );
}
function product_showmoney ( 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 );
}
var product_variantcallback = function ( variant, selector ) {
	//alert ( 'product_variantcallback ( variant:' + variant + ', selector:' + selector + ' )' );
	// ---	set the image
	for ( i = 0;  i < selector.product.variants.length;  i++ ) {
		var curr_id 	= selector.product.variants[ i ].id;
		if ( curr_id == variant.id ) {
			product_image ( i +1 );
		}
	}
	
	//var prodbtn 	= document.getElementById ( 'product-submit' );
	var prodprice 	= document.getElementById ( 'product-showprice' );
	if ( variant && variant.available == true ) {
		//prodbtn.disabled 		= false;
		prodprice.innerHTML 	= product_showmoney ( variant.price /100 );
		//prodprice.innerHTML 	= Shopify.formatMoney ( variant.price, "{{shop.money_with_currency_format}}");
	} else {
		//prodbtn.disabled 		= true;
		prodprice.innerHTML 	= ( variant ) ? "Sold Out" : "Unavailable"; // update price-field message
	}// if else
}

function hide_prodform () {
	var opm 	= document.getElementById ( 'product-form' );
	opm.style.display 	= 'none';
}


function cart_update_qty ( id, qty, incr ) {
	//alert ( 'cart_update_qty ( id:' + id + ', qyt:' + qty + ', incr:' + incr + ' );' );
	var newqty 	= ( incr *1 ) + ( qty *1 );
	document.getElementById ( 'updates_' +id ).value 	= newqty;
	document.getElementById ( 'cartpg-form' ).submit ();
}
function cart_del_item ( id ) {
	//alert ( 'cart_del_item ( id:' + id + ' );' );
	document.getElementById ( 'updates_' +id ).value 	= 0;
	document.getElementById ( 'cartpg-form' ).submit ();
}

function parse_links () {
	// ---	cycle all document links
	for ( i = 0;  i < document.links.length;  i++ ) {
		currHref 	= document.links[ i ].href;
		currText 	= document.links[ i ].innerHTML;
		if ( currHref && currHref.indexOf ( "{" ) != -1 ) 	document.links[ i ].href 		= parse_eml ( currHref );
		if ( currText && currText.indexOf ( "{" ) != -1 ) 	document.links[ i ].innerHTML 	= parse_eml ( currText );
	}
}
function parse_eml ( str ) {
	posBegin 	= str.indexOf ( "{" );
	posEnd 		= str.indexOf ( "}" );
	strBegin 	= str.substr ( 0, posBegin );
	strEnd 		= str.substr ( posEnd +2 );
	strParse 	= str.substr ( ( posBegin +1 ), ( posEnd - ( posBegin +1 ) ) );
	arrParse 	= strParse.split ( "," );
	strEml 		= strBegin + arrParse[ 2 ] + "@" + arrParse[ 1 ] + "." + arrParse[ 0 ] + strEnd;
	return strEml;
}
window.onload = function() { parse_links (); };

function chain_value ( sel ) {
	// ---	check if chain has a value selected
	//		sending value="" will cause variant id error in shopify
	var chainvar 	= document.getElementById ( 'chain-variant' );
	if ( sel.value ) {
		chainvar.name 	= 'id[]';
		chainvar.value 	= sel.value;
	} else {
		chainvar.name 	= '';
		chainvar.value 	= '';
	}
}

function toggle_layer ( div_id ) {
	oDiv 	= document.getElementById ( div_id );
	if ( oDiv.style.display == "none" || !oDiv.style.display ) {
		oDiv.style.display = "block";
	} else {
		oDiv.style.display = "none";
	}
}


