jQuery(document).ready(function($){
	
	$('#usernameTop').bind('focus', function(){
		$('#head div.popup_msg').stop().css({display:'block'}).animate({opacity:1});
	});
	
	$('#head div.popup_msg')
		.mouseover(function(){$(this).attr('mousehover', 1)})
		.mouseout(function(){$(this).attr('mousehover', '')});
	
	$('div.middle-container').mouseover(function(){
		if($('#head div.popup_msg').css('display') == 'block'){
			
			if($('#head div.popup_msg').attr('mousehover')){
				return false;
			}
			
			$('#head div.popup_msg').stop().animate({opacity:0}, function(){
			
				$(this).css({display:'none'});
				
			});
		}
	})
	
	$('#usernameTop').bind('blur', function(){
		
		if($('#head div.popup_msg').attr('mousehover')){
			return false;
		}
		
		$('#head div.popup_msg').stop().animate({opacity:0}, function(){
			
			$(this).css({display:'none'});
			
		});
		
	});
	
	$('div.qtt a[href=#higer]').click(function(){
		return qtyUp(this);
	});
	
	$('div.qtt a[href=#lower]').click(function(){
		return qtyDwn(this);
	});
	$('div.qtt input.qt:not(.cartqty), div.qtt input.qty:not(.cartqty)').change(function(){
		
		if($(this).siblings('input.available').attr('value') != 'simple'){
			return false;
		}
		
		if(!(this.value >= 1)){
		
			this.value = 1;
		
		}else {
			
			var maxValue = parseInt($(this).siblings('input.available').attr('value'));
			
			if(parseInt(this.value) > maxValue){
				
				this.value = maxValue;
				
				alert(MSG_QTY_ALERT || 'The requested quantity not available.')
			
			}
		}
	});
	
	
	initCartQtyChange();
});

function qtyUp(e){
	jQuery(function($){
		var inputNode = $(e).siblings('input.qt, input.qty, input.cartqty');
		inputNode.attr('value', parseInt(inputNode.attr('value'))+1).change();
	});
	return false;
}
function qtyDwn(e){
	jQuery(function($){
		var inputNode = $(e).siblings('input.qt, input.qty, input.cartqty');
		var value = parseInt(inputNode.attr('value'));
		if(value > 1){
			inputNode.attr('value', value-1).change();
		}
	});
	return false;
}

var cartChangeTimeout = null;

function initSidebarCart(){
	jQuery(function($){
		$('div.side-col .mini-cart div.qtt a[href=#higer]').click(function(){
			return qtyUp(this);
		});
		
		$('div.side-col .mini-cart div.qtt a[href=#lower]').click(function(){
			return qtyDwn(this);
		});
	});
	initCartQtyChange();
}

function initCartQtyChange(){
	jQuery(function($){
		$('div.side-col div.qtt input.cartqty').change(function(){
			
			
			if($(this).siblings('input.available').attr('value') == 'simple'){
				
				
				
				var maxValue = parseInt($(this).siblings('input.available').attr('value'));
				
				if(parseInt(this.value) > maxValue){
					
					this.value = maxValue;
					
					alert(MSG_QTY_ALERT || 'The requested quantity not available.')
				
				}else if(parseInt(this.value) < 1){
					this.value = 1;
				}
				
			}
			
			if(cartChangeTimeout){
				clearTimeout(cartChangeTimeout);
			}
			
			var vars = {item_id:this.name, qty:this.value};
			
			cartChangeTimeout = setTimeout(function(){$.getJSON(
				'/custom_checkout',
				vars,
				function(response){
					if(response.success){
						
						var cart_content = $(response.minicart_html);
						
						$('div.side-col .mini-cart').replaceWith(response.minicart_html);
						
						
						initSidebarCart();
						
						
					}
				}
			);}, 1000);
			
		});
	});
}

function swf(url, width, height, vars, id){
	
	if(!id){
		if(typeof swfid == 'undefined'){
			swfid = 0;
		}
		id = (swfid+1);
	}
	
	return document.write(''+
		'<object type="application/x-shockwave-flash"  data="'+url+'" width="'+width+'" height="'+height+'" id="swf-'+id+'" style="zoom:1">'+
		'	<param name="allowScriptAccess" value="sameDomain" />'+
		'	<param name="allowFullScreen" value="false" />'+
		'	<param name="movie" value="'+url+'" />'+
		'	<param name="quality" value="high" />'+
		//'	<param name="bgcolor" value="transparent" />'+
		'	<param name="wmode" value="transparent" />' + 
		'	<param name="flashvars" value="'+vars+'"/>'+
		'	'+
		'</object>');
}