/**
 * 
 */

window.onload = function()
{
	if(document.compatMode == 'CSS1Compat' == false && navigator.appName == 'Microsoft Internet Explorer')
	{
		with (document.getElementsByTagName("head")[0].appendChild(document.createElement("script")))
		{
			setAttribute("type", "text/javascript", 0);
			setAttribute("src", "/"+JS_FOLDER+"/slimbox-2.04/js/slimbox2.js", 0);				
		}
		with (document.getElementsByTagName("head")[0].appendChild(document.createElement("link")))
		{
			setAttribute("rel", "stylesheet", 0);
			setAttribute("href", "/"+JS_FOLDER+"/slimbox-2.04/css/slimbox2.css", 0);				
		}
	}		
};

jQuery(document).ready(function() {
	advanced_stock_options();
	
	// any href with a rel="cpbox" attr will load within a lightbox (fixed width and height)
	jQuery("a[rel='cpbox']").click(function(e) {
		e.preventDefault();
		var href = jQuery(this).attr('href');
		jQuery.colorbox({
			'width'		:	'80%',
			'height'	:	'80%',
			'href'		:	href,
			'iframe'	:	true
		});
	});
	
	dont_add_wmode = 0;
	
	jQuery("embed").each(function(y) {
		if (jQuery(this).attr('wmode'))
		{
			dont_add_wmode = 1;
		}
	});

	if (!dont_add_wmode)
	{
		jQuery("embed").attr("wmode", "opaque");
		var embedTag;
		jQuery("embed").each(function(i) {
		       embedTag = jQuery(this).attr("outerHTML");
		       if ((embedTag != null) && (embedTag.length > 0)) {
		            embedTag = embedTag.replace(/embed /gi, "embed wmode=\"opaque\" ");
		            embedTag = "<param name=\"wmode\" value=\"opaque\"/>"+embedTag;  
		            jQuery(this).attr("outerHTML", embedTag);
		            
		       } else {
		    	   	jQuery(this).wrap("<div></div>");
		       }
		});
	}
	
	//Website Zoom Lightboxes
	if(document.compatMode == 'CSS1Compat' || navigator.appName != 'Microsoft Internet Explorer'){
		quirks = 0;
	}else{
		quirks = 1;
	}
	
	if (quirks)
	{
		jQuery("#product_details_image").css("width", "220px");					
		jQuery("#product_details_image #product-image").css("width", "200px");		
	}
	else
	{
		jQuery("a[rel='lbox']").colorbox();
		jQuery("a.zoom_lbox").colorbox();
	}
});

function advanced_stock_options()
{
	jQuery('.advopt').change(function() {
		var parent = jQuery(this).parent().parent().parent().attr('id');
		var counter = 0;
		var opts = new Array;
		var proceed = true;
		
		// on dropdown change fire the ajax event below if sel value of dropdown is not 'null'
		jQuery('#'+parent+' select option:selected').each(function() {
			if(jQuery(this).val() == 'null') {
				proceed = false;
			} else {
				opts[counter] = jQuery(this).val();
				counter++;
			}
		});
		
		if(proceed == true) {
			jQuery.ajax({
				url			:	'../ajax_c.php/refresh_adv_opts',
				data		:	'parent='+parent+'&options='+opts,
				success		:	function(data) {
					// parse results into JSON array
					ret = jQuery.parseJSON(data);
					
					// option combo was found in database, should always be the case but just to be safe
					if(ret.status == 'OK') {
						jQuery('#stockcount-'+parent).val(ret.stock_level);
						// if stock level is 1 or more then the item can be purchased
						if(ret.stock_level >= 1) {
							var price = ret.price_impact; // get option combo impact
							jQuery('#subopt-'+parent).val(ret.opt); // hidden combo id to pass to add basket
							jQuery('#'+parent+' .stock-level').html(ret.stock_level+' in stock'); // visual indicator of stock levels
							jQuery('#'+parent+' .adjusted-price').html(price); // visual indicator of adjusted price
							jQuery('#cart_buttons_'+parent+' .add_to_cart').css('display', 'inline !important '); // show add to cart
							jQuery('#cart_buttons_'+parent+' .add_to_cart').show();
							jQuery('#atb-'+parent).show(); // show add to basket button
						} else {
						// stock is zero, disable order buttons
							jQuery('#'+parent+' .stock-level').html('Out of stock'); // display 'out of stock'
							jQuery('#'+parent+' .adjusted-price').html(''); // empty the price div
							jQuery('#cart_buttons_'+parent+' .add_to_cart').css('display', 'none'); // hide add to cart
							jQuery('#atb-'+parent).hide(); // hide add to basket button
						}
					} else if(ret.status == 'NONE'){
					// should never occur, only fires if no combo option is returned
						jQuery('#options-summary-'+parent).html('');
						jQuery('#atb-'+parent).hide();
						jQuery('#cart_buttons_'+parent+' .add_to_cart').css('display', 'none');
					}
				},
				error		:	function(a,b,c) {
					alert('Error: '+c);
				}
			});
		}
	});	
}
