var asseturl = (("https:" == document.location.protocol) ? "https://s3.amazonaws.com/assets.chopshopstore.com/assets" : "http://assets.chopshopstore.com.s3.amazonaws.com/assets");

$(document).ready(function() {
	/* Prepare Flash */
	var uname = '';
	$('body').css('display', 'none');
	if ($('p#welcomeback span').size() > 0) var uname = $('p#welcomeback span').text();
	$('#home').flash(asseturl+'/index.swf', 'index_swf', '748', '374', '8', "#ffffff", {}, {allowScriptAccess:'always'});
	$('#home_woot').flash(asseturl+'/index.swf', 'index_swf', '748', '374', '8', "#ffffff", {homeData: "_xml/homeWoot.xml"}, {allowScriptAccess:'always'});
	$('#shopheader').flash(asseturl+'/swf/header.swf', 'header_swf', '822', '90', '8', "#ffffff", {username: uname}, {wmode:'transparent', allowScriptAccess:'always'});
	$('#shopheader').css('visibility', 'visible');
	$('div#featured li.productflip').productflip(160, 160);
	$('ul#productlist li.productflip p.itemdescription').productflip(180, 180);
	for(var i=0; i < $('ul#productlist li').size();i++) if (((i+1)%3)==0) $('ul#productlist li:nth('+i+')').css('margin-right', '0px');
	$('div#news div.item').makenews();
	$('div#featuredartist h2').artist();
	$('div#news div.item:last').css('background', 'none');
	$('div#news div.item:last').css('padding-bottom', '0');	
	$('ul#productlist').append('<div style="clear:both"></div>');
	$('div#page div.product').product();
	$('select#product_select').change(update_product_listing);
	$('select#amount').change(update_product_price);	
	$('input[@type=hidden]').css('display', 'none');
	/* Add classes for browser fixes */
	if (jQuery.browser.safari) $('body').addClass('safari');
	if (jQuery.browser.msie) $('body').addClass('ie');
	if (jQuery.browser.mozilla) $('body').addClass('mozilla');
	if (jQuery.browser.msie) enableAlphaImages();
	/* Check on the visitor's background preferences */
	if ($.cookie('backgroundpref')) $('html').css('background-image', 'url('+asseturl+$.cookie('backgroundpref')+')');

	/* Setup behaviors for background selector */
	$('#back1').click(function() {
		$('html').css('background-image', 'url('+asseturl+'/images/wood_01.jpg)');
		$.cookie('backgroundpref', '/images/wood_01.jpg', {expires: 365});
	});
	$('#back2').click(function() {
		$('html').css('background-image', 'url('+asseturl+'/images/wood_02.jpg)');	
		$.cookie('backgroundpref', '/images/wood_02.jpg', {expires: 365});		
	});
	$('#back3').click(function() {
		$('html').css('background-image', 'url('+asseturl+'/images/wood_03.jpg)');	
		$.cookie('backgroundpref', '/images/wood_03.jpg', {expires: 365});	
	});
	$('#back4').click(function() {
		$('html').css('background-image', 'url('+asseturl+'/images/wood_04.jpg)');
		$.cookie('backgroundpref', '/images/wood_04.jpg', {expires: 365});
	});

	/* Make sure links don't get that annoying stroke line around them in IE */
	$('a').bind('click', function() {this.blur();});

	/* Menu fixes */
	$('#menu ul li p').hover(function(){
		if (this.className.indexOf('current') < 0) $(this).addClass('hover');
	}, function(){
		$(this).removeClass('hover');	
	});
	$('#menu ul li ul li').hover(function(){
		if (this.className.indexOf('current') < 0) $(this).addClass('hover');		
	}, function(){
		$(this).removeClass('hover');	
	});
	
	$('.countryselect').countryselect();
	$('.chopform').disableonclick();
	$('a.sizinghelp').click(function(e){
		e.preventDefault();
		open_sizing_window($(this).attr('href'));
	});	
	$('body').css('display', 'block');	
});

$.fn.disableonclick = function() {
	var img = new Image();
	img.src = '/assets/images/thanks.gif';
	return this.each(function() {
		$(this).submit(function(e) {
			$('input[@name=submit]', this).attr('src', asseturl+'/images/thanks.gif');
			$('input[@name=submit]', this).attr('disabled', 'disabled');
			return true;
		});
	});
};

$.fn.makenews = function() {
	return this.each(function() {		
		this.newsimage = $(this).find('img').clone().get(0).src;
		$(this).find('img').remove();
		$(this).find('div').css('background', 'url("'+this.newsimage+'") left top no-repeat');
		$(this).find('div').css('padding-left', '45px');
	});
};

$.fn.countryselect = function() {
	return this.each(function() {
		var country = this;
		this.stateid 	= $(this).attr('lang');
		this.select 	= $('#'+this.stateid+'_select').get(0);
		this.input	 	= $('#'+this.stateid+'_input').get(0);
		if ($(this).val() == 'US') {
			$(this.input).hide();
			$(this.input).css('display', 'none');		
		} else {
			$(this.select).hide();
			$(this.select).css('display', 'none');				
		}
		$(this).change(function() {
			if ($(this).val() != 'US') {
				$(this.input).val($(this.select).val());
				$(this.select).hide();
				$(this.input).show();
			} else {
				$(this.select).val($(this.input).val());
				$(this.input).hide();	
				$(this.select).show();
			}
		});
		$(this.input).change(function() {
			$('#'+country.stateid).val($(this).val());
		});
		$(this.select).change(function() {
			$('#'+country.stateid).val($(this).val());
		});		
	});
};

$.fn.flash = function(flash_file, id, width, height, version, background_color, variables, params) {
	return this.each(function() {
		this.swfo = new SWFObject(flash_file, id, width, height, version, background_color);
		for(variable in variables) this.swfo.addVariable(variable, escape(variables[variable]));
		for(param in params) this.swfo.addParam(param, params[param]);
		this.swfo.write(this);
	});
};

$.fn.productflip = function(height, width) {
	return this.each(function() {
		this.variables = {};
		this.parent = ($(this).parent().get(0));
		this.pid = this.id || this.parent.id;
		this.variables.stageHeight = height;
		this.variables.stageWidth = width;		
		if( $(this.parent).find('p.itemtype').size() < 2) {
			this.variables.itemUrl = $(this.parent).find('h3 a').attr('href');
			this.variables.itemImage = $(this.parent).find('p.itemimage a').attr('href');
			this.variables.itemImageHover = $(this.parent).find('p.itemimagehover a').attr('href');
			this.variables.itemType = $(this.parent).find('p.itemtype').text();
			this.variables.itemPrice = $(this.parent).find('p.itemprice').text();
			this.variables.itemStock = $(this.parent).find('p.itemstock span').text();			
		} else {
			this.variables.itemUrl = $(this).find('h3 a').attr('href');
			this.variables.itemImage = $(this).find('p.itemimage a').attr('href');
			this.variables.itemImageHover = $(this).find('p.itemimagehover a').attr('href');
			this.variables.itemType = $(this).find('p.itemtype').text();
			this.variables.itemPrice = $(this).find('p.itemprice').text();
			this.variables.itemStock = $(this).find('p.itemstock span').text();			
		}
		$(this).flash(asseturl+'/swf/productFlip.swf', 'productflip_'+this.pid, height, width, '8', '#eeeeee', this.variables, {allowScriptAccess:'always'});
		$(this).css('visibility', 'visible');
	});
};

$.fn.artist = function() {
	return this.each(function() {
		this.variables = {};
		$(this).flash(asseturl+'/artists/index.swf', 'featuredartist', '564', '386', '8', '#ffffff', this.variables, {allowScriptAccess:'always'});
	});
};

$.fn.product = function() {
	return this.each(function() {
		this.variables = {};
		var flash_vars = this.variables;
		flash_vars.itemImageGallery	= '';
		flash_vars.itemImageRight	= '';		
		this.parent = ($(this).parent().get(0));		
		this.pid = this.id || this.parent.id;
		this.variables.itemTitle = $(this).find('.itemTitle').text();		
		$(this).find('p.itemImageGallery img').each(function() {
			if (flash_vars.itemImageGallery != "") flash_vars.itemImageGallery += ",";
			flash_vars.itemImageGallery += $(this).attr('src');
		});
		$(this).find('p.itemImageRight img').each(function() {
			flash_vars.itemImageRight += $(this).attr('src')+",";
		});		
		this.variables.productXML = "/flash_product.php?id="+this.pid;
		$(this).flash(asseturl+'/swf/product.swf', 'product_'+this.pid, '564', '294', '8', '#ffffff', this.variables, {allowScriptAccess:'always'});
	});
};

/* Product Page Related Functions */

function update_product_listing(event)
{
	pid = $('select#product_select').val();
	cqty = $('select#amount').val();	
	pid = pid.split('__');
	vid = pid[0];
	qty = $('input#available_'+vid).val();
	price = $('input#price_'+vid).val();	
	options = '';
	if (qty > 25) qty = 25;
	for(i=1;i<= qty;i++)
	{
		options += '<option value="'+i+'">'+i+'</option>';
	}
	$('#product_price').html('$'+Number(price).toFixed(2));
	$('select#product_avail').html(options);
	if (cqty < qty) $('select#product_avail').val(cqty);
	else $('select#product_avail').val(qty);
}

function update_product_price(event)
{
	pid = $('select#product_select').val();	
	pid = pid.split('__');
	vid = pid[0];
	qty = $('select#amount').val();
	price = $('input#price_'+vid).val();	
	$('#product_price').html('$'+Number(Number(price).toFixed(0)*qty).toFixed(0));
}

function enableAlphaImages(){
	var rslt = navigator.appVersion.match(/MSIE (\d+\.\d+)/, '');
	var itsAllGood = (rslt != null && Number(rslt[1]) >= 5.5);
	if (itsAllGood) {
		for (var i=0; i<document.all.length; i++){
			var obj = document.all[i];
			var bg = obj.currentStyle.backgroundImage;
			var img = document.images[i];
			if (bg && bg.match(/\.png/i) != null) {
				var img = bg.substring(5,bg.length-2);
				var offset = obj.style["background-position"];
				obj.style.filter =
				"progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+img+"', sizingMethod='scale')";
				obj.style.backgroundImage = "url('"+asseturl+"/images/spacer.gif')";
				obj.style["background-position"] = offset; // reapply
			} else if (img && img.src.match(/\.png$/i) != null) {
				var src = img.src;
				img.style.width = img.width + "px";
				img.style.height = img.height + "px";
				img.style.filter =
				"progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+src+"', sizingMethod='scale')"
				img.src = asseturl+"/images/spacer.gif";
			}
		}
	}
}

function open_sizing_window(url)
{
	window.open(url, 'Sizing', 'width=847,height=384, copyhistory=no,directories=no,location=no, resizeable=yes, status=no, menubar=no');
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}