// V1.06 - Full Stock Control code

var showboxcarticons = false;		// whether to show icons in the Cart for selection boxes
var showsamplecarticons = false;	// whether to show icons in the Cart for sample packs
var showsinglecarticons = false;	// whether to show icons in the Cart for single boxes

function addtobox(anc, uiwidget){	// called when a paper type is clicked - add to box or warn if box full
	var pid = uiwidget.replace(/v_(.*)_\d+/, '$1');				// extract ProductID
	var thisimg = document.getElementById('picon_' + uiwidget);		// the associated product icon
	var full = true;
	for ( var i=1; i<=5; i++ )						// each box position
		{
		var boxitem = document.getElementById('sel_' + i + '_' + pid);	// the icon for each box position 
		if ( boxitem.src.indexOf('x40.png') > -1 )			// if empty
			{
			boxitem.src = thisimg.getAttribute('tip'); 		// replace box item image with product image
			boxitem.setAttribute('tip',thisimg.getAttribute('tip'));// also save as tooltip image
			boxitem.rel = uiwidget;					// remember the items widget name
			boxitem.alt = boxitem.title = anc.rel;			// and alt and title
			var inbox = document.getElementById('full_' + pid);	// our fullness indicator
			if (inbox) inbox.innerHTML = (inbox.innerHTML - 0) + 1;	// bump by one
			full = false;
			break;
			}
		}
	if ( full ) alert('Box is full! Remove something if you want to add a different paper.');
	return false;
}

function removeitem(pid, idx){	// called when "Remove" clicked - empty box position
	var boxitem = document.getElementById('sel_' + idx + '_' + pid);
	if ( boxitem.title != '' ) 
		{
		boxitem.src = document.getElementById('x40_' + pid).src;	// the empty icon
		var inbox = document.getElementById('full_' + pid);		// our fullness indicator
		if (inbox) inbox.innerHTML = (inbox.innerHTML - 0) - 1;		// decrease by one
		boxitem.rel = boxitem.alt = boxitem.title = '';			// clear rel title and alt
		boxitem.setAttribute('tip', '');				// disable tooltip
		boxitem.setAttribute('qty', 0);					// clear quantity
		}
	return false;
}

function addboxtocart(pid){	// called when add to cart pressed - check box is full and if so set hidden checkboxes for each item
	$(".pl_checkbox[name^='v_" + pid + "_']").each(function() {$(this).attr('checked', false);});	// clear all checkboxes for this item
	var count = 0;
	for ( var i=1; i<=5; i++ )						// each part of box
		{
		var item = document.getElementById('sel_' + i + '_' + pid );	// the part of box icon
		var thiswidget = item.rel;
		if ( thiswidget && (thiswidget != '') ) 			// if uiwidget set, something is in box
			{
			count++;
			for (var j=0; j<5; j++)					// each checkbox for this paper type
				{
				var widgetbaseid = thiswidget.replace(/(.*_)\d+$/, '$1');	// the main part of the name
				var widgetidx = thiswidget.replace(/.*_(\d+)$/, '$1') - 0;	// the base index
				var awidget = widgetbaseid + (widgetidx + j);			// check each possible checkbox in turn
				var checkbox = document.getElementById(awidget);		
				if ( ! checkbox.checked )			// until empty one found
					{
					checkbox.checked = true;		// set it
					break;					// and done
					}
				}
			}
		}
	if ( count != 5 )							// must have exactly five for full box
		{
		alert('Box is not complete. Add more paper.');			// warn user what to do
		return false;							// no add to cart
		}
	return true;								// all is good so add to cart	
}

// V2 (V1.02) - B P Override Code

// Selection Boxes
function addtobox2(pid, index, name, qty){	// called when a paper type is clicked - add to box or warn if box full
	var thisimg = document.getElementById('picon_' + index + '_' + pid);	// the associated product icon
	var full = true;
	for ( var i=1; i<=5; i++ )						// each box position
		{
		var boxitem = document.getElementById('sel_' + i + '_' + pid);	// the icon for each box position 
		if ( boxitem.src.indexOf('x40.png') > -1 )			// if empty
			{
			boxitem.src = thisimg.getAttribute('tip'); 		// replace box item image with product image
			boxitem.setAttribute('tip',thisimg.getAttribute('tip'));// also save as tooltip image
			boxitem.setAttribute('qty', qty);			// save component quantity
			boxitem.alt = boxitem.title = name;			// and alt and title
			var inbox = document.getElementById('full_' + pid);	// our fullness indicator
			if (inbox) inbox.innerHTML = (inbox.innerHTML - 0) + 1;	// bump by one
			full = false;
			break;
			}
		}
	if ( full ) alert(document.getElementById('pfull_' + pid).innerHTML);	// warning message if box full
	return false;
}

function removeitem2(pid, idx){	// called when "Remove" clicked - empty box position
	var boxitem = document.getElementById('sel_' + idx + '_' + pid);
	if ( boxitem.title != '' ) 
		{
		boxitem.src = document.getElementById('x40_' + pid).src;	// the empty icon
		var inbox = document.getElementById('full_' + pid);		// our fullness indicator
		if (inbox) inbox.innerHTML = (inbox.innerHTML - 0) - 1;		// decrease by one
		boxitem.alt = boxitem.title = '';				// clear title and alt
		boxitem.setAttribute('tip', '');				// disable tooltip
		boxitem.setAttribute('qty', 0);					// clear quantity
		}
	return false;
}

function addboxtocart2(pid, caption, carterror){	// called when add to cart pressed - check box is full and if so pass parameters to BPO Cart
	var count = 0;
	var ovr = '+0¦' + caption + '¬¬';
	for ( var i=1; i<=5; i++ )						// each part of box
		{
		var item = document.getElementById('sel_' + i + '_' + pid );	// the part of box icon
		var thisname = item.title;
		if ( thisname != '' ) 						// if title set, something is in box
			{
			count++;
			ovr += '¦' + thisname + '¬¬';				// add it to POST string
			if ( showboxcarticons )
				{
				ovr += '¦image¬' + item.getAttribute('tip') + '¬';	// add icon to POST string
				ovr += 'title="' + item.getAttribute('title') + '"';	// tooltip title
				}
			}
		}
	if ( count != 5 )							// must have exactly five for full box
		{
		alert(carterror);						// warn user what to do
		return false;							// no add to cart
		}
	document.getElementById('ovr_' + pid).value = ovr;			// save cart info	
	return true;								// all is good so add to cart	
}

// Sample Packs - add / remove as Selection Boxes but allow add to cart if 1 or more item selected
function addsampletocart2(pid, caption, carterror){	// called when add to cart pressed - check something in sample pack and if so pass parameters to BPO Cart
	var count = 0;
	var ovr = '+0¦' + caption + '¬¬';
	for ( var i=1; i<=5; i++ )						// each part of pack
		{
		var item = document.getElementById('sel_' + i + '_' + pid );	// the part of box icon
		var thisname = item.title;
		if ( thisname != '' ) 						// if title set, something is in box
			{
			count++;
			ovr += '¦' + thisname + '¬¬';				// add it to POST string
			if ( showsamplecarticons )
				{
				ovr += '¦image¬' + item.getAttribute('tip') + '¬';	// add icon to POST string
				ovr += 'title="' + item.getAttribute('title') + '"';	// tooltip title
				}
			}
		}
	if ( count == 0 )							// must have exactly five for full box
		{
		alert(carterror);						// warn user what to do
		return false;							// no add to cart
		}
	document.getElementById('ovr_' + pid).value = ovr;			// save cart info	
	return true;								// all is good so add to cart	
}

// Single Boxes
function addsinglepack2(pid, index, name, qty){	// called when a paper type is clicked - add to box or warn if box full
	var thisimg = document.getElementById('picon_' + index + '_' + pid);	// the associated product icon
	var boxitem = document.getElementById('sel_1_' + pid);			// the icon for the box position 
	boxitem.src = thisimg.getAttribute('src'); 				// replace box item image with product image
	boxitem.setAttribute('tip',thisimg.getAttribute('tip'));		// also save as tooltip image
	boxitem.setAttribute('qty', qty);					// save component quantity
	boxitem.alt = boxitem.title = name;					// and alt and title
	return false;
}

function removesinglepack2(pid){	// called when "Remove" clicked - empty box position
	var boxitem = document.getElementById('sel_1_' + pid);
	if ( boxitem.title != '' ) 
		{
		boxitem.src = document.getElementById('x70_' + pid).src;	// the empty icon
		boxitem.alt = boxitem.title = '';				// clear title and alt
		boxitem.setAttribute('tip', '');				// disable tooltip
		boxitem.setAttribute('qty', 0);					// clear quantity
		}
	return false;
}

function addsinglepacktocart2(pid, caption, carterror){	// called when add to cart pressed - check something selected if so pass parameters to BPO Cart
	var count = 0;
	var ovr = '+0¦' + caption + '¬¬';
	var item = document.getElementById('sel_1_' + pid );			// the selection icon
	var thisname = item.title;
	if ( thisname != '' ) 							// if title set, something is in box
		{
		ovr += '¦' + thisname + '¬¬';					// add it to POST string
		if ( showsinglecarticons )
			{
			ovr += '¦image¬' + item.getAttribute('tip') + '¬';	// add icon to POST string
			ovr += 'title="' + item.getAttribute('title') + '"';	// tooltip title
			}
		document.getElementById('ovr_' + pid).value = ovr;		// save cart info	
		return true;							// all is good so add to cart	
		}
	alert(carterror);							// warn user what to do
	return false;								// no add to cart
}

// COMMON CODE

/*
 * 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.pbImagePreview = function(){	
	/* CONFIG */
		xOffset = 220;
		yOffset = -320;
		// these 2 variable determine popup's distance from the cursor
		// you might want to adjust to get the right result
	/* END CONFIG */
	$("img.pl_popup,img.cwimage").hover(function(e){
		this.t = this.title;
		this.title = "";	
		this.a = this.alt;
		this.alt = "";
		this.p = this.getAttribute('tip') ? this.getAttribute('tip') : this.src;	// use src if no tip attribute (for Cart)
		if ( (this.p == '') && (this.t == '') ) return;		// only popup if there's an image or a title
		var c = (this.t != "") ? "<br/>" + this.t : "";
		var i = (this.p != '') ? "<img src='" + this.p + "' alt='Image preview' />" : "";
		$("body").append("<p id='pbpreview'>" + i + c + "</p>");						 
		$("#pbpreview")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px")
			.fadeIn("fast");						
    },
	function(){
		this.title = this.t;	
		this.alt = this.a;	
		$("#pbpreview").remove();
    });	
	$("img.pl_popup,img.cwimage").mousemove(function(e){
		$("#pbpreview")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px");
	});			
};

// starting the script on page load
$(document).ready(function(){
	pbImagePreview();
	$('.pl_image').dropShadow({left:1, top:1, opacity:0.9, blur:1});
});


