/*
 *  error trapping code
 *  
 */
	/*
		onerror=handleErr
		
		function handleErr(msg,url,l)
		{
		//Handle the error here
		return true or false
		}
	*/
	
		
	/*
	 * Console logger
	 * 
	 * Message variables
	 *  %s	String
	 *  %d, %i	Integer (numeric formatting is not yet supported)
	 *  %f	Floating point number (numeric formatting is not yet supported)
	 *  %o	Object hyperlink
	 * 
	 *  only logs if globalVars.log = true
	 */
	
	$.log = function() {
	  	if(window.console) {
			if($.browser.safari){
				// Safari console
				var args = arguments[0];
				window.console.log(args); // fix to show args, Safari doesn't like .apply
			}
			if ($.browser.mozilla) {
				// Firefox with firebug
				window.console.log.apply(this, arguments);
			}
	  	} else {
	  		// no Firebug
	    	//alert(message);
		}
	};
	
		
/* 
 *  fix IE6 link background image flicker bug
 */
try {  document.execCommand("BackgroundImageCache", false, true); } catch(err) {} 


// load jQuery when DOM is ready
$(document).ready(function(){
	
	/*
	 *  De-obfuscate email addresses
	 * 
	 *  replaces email address @ in "mailto" links and link text
	 * 
	 */
		function deobfuscate(x){
			return x.replace("--@--","@");
		}
		$("a").each(function (i){
			var h = this.href;
			if (h.indexOf("mailto")==0) {
				var t = deobfuscate($(this).html());
				this.href = deobfuscate(h);
				$(this).html(t);
			}
		});
		
	/*
	 *  external links
	 *  tags with class="external" target="_blank"
	 *  optional Google Analytics tracking code trigger
	 */
		// (add https and don't include urls that start with site domain
		var pre = "/LINK/";				// prefix to add to URL for GA tracking code
		$('a[@href^="http://"]')
			.addClass('external')			// add CSS class for styling
			.attr('target', '_blank')		// open in popup window
			.click(function(){				// GA tracking code
				var href = $(this).attr("href");
				href = href.substr(7,9999);	// trim off http://
				var i = href.indexOf("www.");	// trim off www.
				if (i===0){
					href = href.substr(4,9999);
				}
				href = encodeURI(href);		// encode URL
				$.log(pre + href);			// log href to console
				//pageTracker._trackPageview(pre + href)	// GA tracker code
				//return false;				// cancel link action
			})
		;

	/* search box */
		$("#search-text").focus(function(){
			$(this).attr("value","");
		});
	
	/* product browser */
	
	/* designer select box */
		$("#product-browser-designer").change(function(){
			d = $(this).attr("value");
			window.location = "/product-list.php?designer=" + d;
		});
	
	/* prevent right click on images */
		$("img").bind("contextmenu",function(e){
			$.log("right-clicked on image");
			return false;
		});
		
	/* add to cart button hover */
	$("#button-add-cart").hover(
			function(){
				$(this).addClass("active");
			},
			function(){
				$(this).removeClass("active");
			}
	);

	/* audio player control */
	
		var playerHTML = "";
		var audioControl = $("#audio_control a");
		// check if player frame is present
		if(typeof top.audio_player != "undefined"){
			// get flash player element from player frame
			var playerElement = $(top.audio_player.document).find("#flashPlayer");
			// restore player control
			$.log(top.audio_player.audioControlStatus);
			if (top.audio_player.audioControlStatus){
				$.log("audio is playing");
				audioControlSetPlaying();
			} else {
				$.log("audio is muted");
				audioControlSetMuted();
			}
		} else {
			$.log("Player not available");
			// hide controls
			audioControl.hide();
		}
		// update controls to show muted
		function audioControlSetMuted(){
				$.log("Mute");
				// change controls
				audioControl.find("img").attr("src","/images/eq_mute.gif");
				audioControl.attr("title","Play audio");
				audioControl.find("span").html("Play")
		}
		// update controls to show playing
		function audioControlSetPlaying(){
				$.log("Play");
				// change controls
				audioControl.find("img").attr("src","/images/eq_play.gif");
				audioControl.attr("title","Mute audio");
				audioControl.find("span").html("Mute")		
		}
		// mute audio
		function audioMute(){
				// set global var
				top.audio_player.audioControlStatus = false;
				// clear html of player to mute
				top.audio_player.audioControlHTML = playerElement.html();
				$.log(top.audio_player.audioControlHTML);
				playerElement.html("");
		}
		// play audio
		function audioPlay(){
				// set global var
				top.audio_player.audioControlStatus = true;
				// restore html of player
				$.log(top.audio_player.audioControlHTML);
				playerElement.html(top.audio_player.audioControlHTML);
		}
		// audio control click event
		$(audioControl).click(function(){
				if(top.audio_player.audioControlStatus == true){
					// mute
					audioControlSetMuted();
					audioMute();
					return false;
				}else{
					// play
					audioControlSetPlaying();
					audioPlay();
					return false;
				}
		});
	
	/* press gallery */
	
		$("#press-gallery li").each(function(){
			$(this).hover(
				function(){
					$(this).addClass("hover");
				},
				function(){
					$(this).removeClass("hover");
				}
			);
		});
		
	/* admin link clears frameset and opens page */
	
		$("#site_info .admin").click(function(){
			top.location = $(this).attr("href");
			return false;
		});
	
	
	/* product detail buy now form
	 *   sets hidden form values for PayPal and IC button forms
	 */
		// size
		$("#product-detail .size select").change(function(){
			var val = $(this).val();
			// PayPal
			$("#product-detail #paypal-form input[name=os0]").attr("value",val);
			// IC
			$("#product-detail #ic-form input[name=ItemSize1]").attr("value",val);
		});
		// colors
		$("#product-detail .color select").change(function(){
			var val = $(this).val();
			// PayPal
			$("#product-detail #paypal-form input[name=os1]").attr("value",val);
			// IC
			$("#product-detail #ic-form input[name=ItemColor1]").attr("value",val);
		});
		// quantity
		$("#product-detail .quantity input").change(function(){
			var val = $(this).val();
			// PayPal
			$("#product-detail #paypal-form input[name=quantity]").attr("value",val);
			// IC
			$("#product-detail #ic-form input[name=ItemQuantity1]").attr("value",val);
			
		});

	
	/* product catalog grunge image borders */
	/*
	var maxWidth = 160;
	var maxHeight = 160;
	$("#product-list img").each(function(){
		var width = $(this).width();
		var height = $(this).height();
		var left_offset = ((maxWidth - width) / 2)+1;
		var right_offset = ((maxWidth - width) / 2)+2;
		var y_offset = ((maxHeight - height) / 2);
		$(this).after("<img class='image-border-left' style='left:" + left_offset + "px;' alt='' src='/images/product-border-left.png'/>")
			.after("<img class='image-border-right' style='right:" + right_offset + "px;' alt='' src='/images/product-border-right.png'/>")
			.after("<img class='image-border-top' alt='' src='/images/product-border-top.png'/>")
			.after("<img class='image-border-bottom' alt='' src='/images/product-border-bottom.png'/>")
		;
	});
	*/
	
	// fancybox
	// see http://fancybox.net/howto
	$(document).ready(function(){
		$("a.fancybox").fancybox({
			'overlayOpacity'		:	0.5,
			'hideOnContentClick': true,
			'zoomOpacity'			: true,
			'overlayShow'			: true,
			'zoomSpeedIn'			: 500,
			'zoomSpeedOut'			: 500
		});
	});
});

function fix_pngs() {
 if (navigator.appVersion.match(/MSIE [0-6]\./)) {
	 $('.png').each(function () {
 		if (this.currentStyle.backgroundImage != 'none') {
 			var image = this.currentStyle.backgroundImage;
 			image = this.currentStyle.backgroundImage.substring(5, image.length - 2);
	 		if(image.indexOf(".png") !=-1) {
		 		$(this).css({
	 			'backgroundImage': 'none',
 				'filter': "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod=crop, src='" + image + "')"
 				});
 			}
	 	}
 	});
 }
}
