/* product.js */

function passKey() {
  document.my_form.key.value=document.my_form.keyID.value;
}

function thumbImg(x) {
	var temp = Images[x].split('_1');
	var thumb = temp[0] + "_0.jpg";
	return thumb;
}
function altImg(x) {
	var temp = Images[x].split('_1');
	var alt = temp[0] + "_2.jpg";
	return alt;
}

/*
*/
$(function() {
	var oldBG = $('#cart a').css('backgroundColor');
	var oldC  = $('#cart a').css('color');
	var newBG = '#FFDD99';
	var newBG = '#FFD777';
	var newC  = '#992200';
	
	// HOVER "add to cart" button //
	var addSrc = $("#add_to_cart input").attr('src');
	$("#add_to_cart input").hover(function(){
		var temp = addSrc.split('.png');
		temp = temp[0];
		var hoverSrc = temp+'-hover.png';
		$(this).attr('src',hoverSrc);
	},function(){
		$(this).attr('src',addSrc);
	});
	
	$('#options-price input').attr('readonly','readonly');
	
	$("form#my_form").submit(function() { // SUBMIT FORM //
	
		var opt_idx = $("#image_change").attr('selectedIndex');
		var opt_val = $("#image_change").val(); // undefined if there is none
		if ((opt_val != undefined) && (opt_idx == undefined)) opt_idx = 0;
	//	alert(opt_val);
		var actn = $(this).attr('action');
		var keyy = $("input[name='key']").val();
		var qtyy = $("#qty").val();
		
		// ERROR HANDLING //////
		if (( opt_idx == 0 ) || ( qtyy == 0 ) || isNaN(qtyy)) {
			var err = '';
			if (opt_idx == 0) {
				err = '<span class="red">Please choose a Color/Size option.</span>';
			} else if ( (qtyy == 0) || isNaN(qtyy)) {
				err = '<span class="red">Incorrect quantity amount.</span>';
			}
			$('#product-text .added').hide().html('ERROR! ' + err).fadeIn(600);
			return false;

		} else { // IF option & quantity are correct //
		
			var num = parseInt( $('#num').text() ) + parseInt(qtyy);
			$('#num').text(num);
			
			$('#cart a').animate({ backgroundColor: newBG, color: newC }, 250)
						.animate({ backgroundColor: newBG, color: newC }, 1000)
						.animate({ backgroundColor: oldBG, color: oldC }, 2200);
			$('.added').hide().text('Item added to cart').css({ color:'#003300'}).fadeIn(600);
			$('#add_to_cart input').attr('src','../images/added_to_cart.png').value('readonly');
			$('#add_to_cart').attr('id','added_to_cart');
			/*
			<span id="add_to_cart" class="half">
				<input type="image" src="http://www.gofreshforce.com/images/add_link.png" name="add" alt="Add To Cart" value="action" />
			*/

			$.scrollTo( '#page', 700 );
			$.post(actn, { key: keyy, qty: qtyy, add: "action" });
			return false;
		}
	}); // END product form submit


	// product description => fix bad formatting
	var codetext = $('p.description').text();
	var textt = codetext.split('<br><br>');
	var newtext = "";
	for (i=1 ; i<textt.length ; i++) {
		newtext += textt[i] + "</p><p>";
		$('p.description').html(newtext);
	}

	$("#alt_href").attr('href',altImg(0) );
	$("#alt_img").attr( 'src', altImg(0) );
	// Product Page => Change Options (color/size)
	$("#image_change").change(function(){
		$('#product-text .added').hide('fast');
		var idx = this.selectedIndex;
		if( idx > 0 ) {
			$('#my_href').attr( 'href',Images[idx] );
			$('#my_img').attr(  'src', Images[idx] );
			$('#alt_href').attr('href',altImg(idx) );
			$('#alt_img').attr( 'src', altImg(idx) );
		} else { idx = 0; }
	});
	

}); // END jquery function
$(document).ready(function() {

	$("#alt_href").css('opacity',0);
//	$('#product-sidebar').css({ opacity: .5 }).find('.dyn').hide();
	

}); // END document.ready
$(window).load(function() {
	
	thisAnchor = $('#sidebar-details').find('small');
	$('#sidebar-details').find('div').load('/artisans/'+catcode+'.html .text p:first-child', function(){
		$(this).find('p:last-child').append(thisAnchor);
	});

	var w = $("#alt_img").height();
	var t = $("#alt_img").attr('src').split('.')[2].split('/')[2];
	if ((w != "undefined") && (w != "null") && (w > "20")) { // 20 is default // 2nd image exists
		//	alert(t);
		if (t != 'transparent') $("#alt_href").animate({opacity:1},'fast');
	}
	
	if ( $('#sidebar-details div').text() != '') {
		$('#product-sidebar').animate({ opacity: 1 }, 'slow').find('*').fadeIn('fast');
		link = $('#product-sidebar .breadcrumb a');
		$(this).append(link);
	} else {
	//	$('#product-sidebar').animate({ opacity: 0 }, 'fast');
	}
	
	// You Might Also Like... //
	otherProducts = '/cgi-bin/commerce.cgi?search=action&category='+catcode+' table.results';
	var promotext = trim( $('#other-products div').text() );
	//alert(promotext);
	if (promotext == "" ) $('#other-products .maincol div').load(otherProducts, function(){
		$(this).find('td').filterFor(DESC, {
			toThis: 'a span',
			remove: '#other-products'
		});
	});
	/*
<div id="other-products">

	<h4 class="center">You might also like...</h4>
	<div class="maincol center bold" style="width:100%">

		<span class="left" style="width: 50%"><!---CROSS_SALE---></span>
		<span class="right" style="width: 50%"><!---UP_SALE---></span>
		<div></div>

	</div><!-- .maincol -->
</div><!-- #other-products -->
	*/
	
	catParent = getVar('parent');
	$("#cat_"+catParent).addClass('selected');
	if (catParent != 'undefined') {
		var temp = $('table.results a').attr("href");
		$('table.results a').attr("href", temp+= "&parent="+catParent);
	}
	
});// END window load // LAST
