// build to order V1.11 jQuery

var btopprices = new Object();

function toggleinfo(link, id){		// when More... info link clicked
	if ( link.innerHTML == btomoretext )
		{
		$('#pdetail_' + id).show(btoinfoanimate);
		link.innerHTML = btolesstext;
		}
	else	
		{
		$('#pdetail_' + id).hide(btoinfoanimate);
		link.innerHTML = btomoretext;
		}
	return false;	
}

function expandcomponent(pid, idx){		// display hidden permutations within component
	$('#btocomp_' + pid + '_' + idx + ' input:radio').each(function()
		{if(! this.checked)$('#compitem_' + this.name + '_' + this.value).show(btochoiceanimate);});
}

function btoreview(btn, pid, idx){		// reopen component and show all choices
	if ( btn.innerHTML == btoexpandtext)
		{
		$("#btotbl_" + idx + ' .btoitem').show(btochoiceanimate);
		btn.innerHTML = btocontracttext;
		if (btoinactiveopacity) $('#btocomp_' + pid + '_' + idx).animate({opacity: 1});

		}
	else
		{
		$("#btotbl_" + idx + ' .btoitem').each(function()
			{if( $('input:radio:checked', this).length == 0 ) $(this).hide(btochoiceanimate);});
		btn.innerHTML = btoexpandtext;
		if (btoinactiveopacity) $('#btocomp_' + pid + '_' + idx).animate({opacity: btoinactiveopacity});
		}
	return false;
}

function btomoveon(btn, pid, idx){		// this components Next... button clicked.  
	// Hide all but current selection and display next component (or Cart button).

	// fade down the item
	var thiscomp = $('#btocomp_' + pid + '_' + idx);
	if (btoinactiveopacity) thiscomp.animate({opacity: btoinactiveopacity});

	// hide all but selected item
	$('input:radio', thiscomp).each(function()
		{if(! this.checked)$('#compitem_' + this.name + '_' + this.value).hide(btochoiceanimate);});
		
	// enable the Expand link
	$('#btoexpand_' + idx).show();
	$('#btoexpand_' + idx)[0].innerHTML = btoexpandtext;
	
	// display the progress tick
	$('#btotick_' + idx)[0].className = 'btotick';
		
	// now display next option	
	var nextcomp = $('#btocomp_' + pid + '_' + (idx + 1));
	if ( nextcomp.length )
		{
		// hide the previous set of buttons as we don't need them anymore
		$('#btns_' + pid + '_' + idx).hide();
		// display next component
		nextcomp.show(btocompanimate);
		// indicate this component is now visible (for the price calc routine)
		nextcomp.attr('btovis', 1);			
		}
		
	// all must be done so enable Qty and Cart	
	else
		{
		// hide the previous sets Next button as we don't need it anymore
		if ( $('#nxtbtn_' + pid + '_' + idx).length ) $('#nxtbtn_' + pid + '_' + idx).hide();
		// enable the back button just in case it's a single Component product
		if ( $('#backbtn_' + pid + '_' + idx) ) $('#backbtn_' + pid + '_' + idx).show();
		// enable Qty and Cart button
		var qtycart =  document.getElementById('qtycart_' + pid);
		if ( qtycart ) qtycart.className = 'btoshow';
		}
	
	// update calculated total
	calctot(pid);
	return false;
}

function btomoveback(btn, pid, idx){		// Back... button clicked.  
	// Hide current selection and reopen previous
	
	var qtycart =  document.getElementById('qtycart_' + pid);
	if ( qtycart.className == 'btohide' )
		{
		// normal progress (we're not showing the cart) - hide current
		var thiscomp = $('#btocomp_' + pid + '_' + idx);
		thiscomp.hide(btocompanimate)
		thiscomp.attr('btovis', 0);	
		
		// reopen previous
		if (btoinactiveopacity) $('#btocomp_' + pid + '_' + (idx - 1)).animate({opacity: 1});
		expandcomponent(pid, idx - 1);
		
		// hide the Expand link
		$('#btoexpand_' + (idx - 1)).hide();
		
		// show the buttons for previous
		if ( $('#btns_' + pid + '_' + (idx - 1)).length ) $('#btns_' + pid + '_' + (idx - 1)).show();
		}
		
	else	
		// special case if we're on last selection
		{
		qtycart.className = 'btohide';		// hide the qty and cart
		
		// reopen current selection at full opacity
		if (btoinactiveopacity) $('#btocomp_' + pid + '_' + idx).animate({opacity: 1});
		expandcomponent(pid, idx);

		// hide the Expand link
		$('#btoexpand_' + (idx)).hide();

		// and reenable buttons
		var thisbtns = $('#btns_' + pid + '_' + idx);
		if ( thisbtns.length )thisbtns.show();
		var nxtbtns = $('#nxtbtn_' + pid + '_' + idx);
		if ( nxtbtns.length ) nxtbtns.show();
		// hide the back button if we're on the first item
		if ( (idx == 1) && $('#backbtn_' + pid + '_' + idx).length ) $('#backbtn_' + pid + '_' + idx).hide();
		}

	calctot(pid);
	return false;
}	

function calctot(pid){		// calculate total for all visible Components
	var rtot = btopprices[pid] - 0;		// possible product price
	// all divs with id starting btocomp_<pid>_
	$("div[id^='btocomp_" + pid + "_']").each(function(){
		if ( this.getAttribute('btovis') == 1) rtot += $("input:radio:checked", this)[0].getAttribute('ppraw') - 0;});
		// display the price
		if ( $('#stot_' + pid).length ) $('#stot_' + pid).html('<h2 class="btototal">Prix: &euro;' + rtot.toFixed(2) + '</h2>');

	// now inhibit any select tags on inactive radios
	$("input:radio[name^='v_" + pid + "_']").each(function(){	// all relevant radios
		var radio = this;
		$("#btoopt_" + radio.name + '_' + radio.value).each(function(){	// matching SELECT
			this.disabled = ! radio.checked;		//    disable SELECT if radio unchecked
			}); 
		});
}

function btosetup(pid,ppraw){	// called when product being laid out - save product price
	btopprices[pid] = ppraw;
}
	
function initallbtos(){			// called when DOM loaded - display all running totals
	for (var pid in btopprices) calctot(pid);
}

function btosubmit(pid){		// called when form submitted - pass optional size info to Perl
	var sizeinfo = '';	// 0¦Prefix1¬Value1¬Suffix1¦Prefix2¬Value2¬Suffix2
	$("input:radio[name^='v_" + pid + "_']:checked").each(function(){		// all relevant checked radios
		var radio = this;
		$("#btoopt_" + radio.name + '_' + radio.value).each(function(){	// matching SELECT
			sizeinfo += '¦' + this.getAttribute('bto') + '¬' + this.options[this.selectedIndex].text;
			});
		});	

	if( sizeinfo ) $('#bto_' + pid)[0].value = '0' + sizeinfo;				// save for cart scripts
	return true;
}

$(document).ready(function(){initallbtos();});

/*
 * Image preview script - tweaked drillpine for unique namespace and repositioning
 * written by Alen Grakalic (http://cssglobe.com)
 * for more info visit http://cssglobe.com/post/1695/easiest-tooltip-and-image-preview-using-jquery
 */
 
this.btoImagePreview = function(){	
	xOffset = btotooltipy;	// NB back to front
	yOffset = btotooltipx;

	$("a.btopreview").click(function(e){return false;});	// inhibit click
	$("a.btopreview").hover(function(e){
		this.t = this.title;
		this.title = "";	
		var c = (this.t != "") ? "<br/>" + this.t : "";
		$("body").append("<p id='btopreview'><img id='btopopimg' src='"+ this.href +"' alt='Image preview' />"+ c + "</p>");								 
		$("#btopreview")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px")
			.fadeIn(btotooltipfade);						
    },
	function(){
		this.title = this.t;	
		$("#btopreview").remove();
    });	
	$("a.btopreview").mousemove(function(e){
		pw = $("#btopopimg").width();
		$("#btopreview").width(pw);
		var border_top = $(window).scrollTop();
		var ttheight = $("#btopreview").height();
		top_pos = (border_top+(xOffset*2)>=(e.pageY - ttheight)) ? border_top + xOffset : e.pageY - ttheight - xOffset;
		$("#btopreview")
			.css("top", top_pos + "px")
			.css("left",(e.pageX + yOffset) + "px");
	});			
};

// starting the script on page load
$(document).ready(function(){
	btoImagePreview();
});
