/*
 * JS by Sofresh Inc.
 */

$(document).ready(function(){
	jQuery('.navsub').hover(function() {
		jQuery('ul:first', this).stop(true, true).animate( { width:"65px" }, 500);
		jQuery('a:first', this).addClass('active_sub');
	},
	function() {
		jQuery('ul:first', this).animate( { width:"0px" }, 300);
		jQuery('a:first', this).removeClass('active_sub');
	});
	
	if ($('#section_products').html() != null){
		show_alias();
		view_product(0);
	}
/*	$('#i_like_checkout').attr('class','btn_137').click(function(){
		alert('dd');
		checkout_page();
	});

*/

	if ($('.promo_carousel').html() != ''){
		home_promo_carousel();
	}
	
	
	// Print 
	$('.pressList li').hover(
		function(){
			$(this).parent().find('li').css({opacity:'0.5'});
			$(this).css({opacity:'1'});
		},
		function(){
			$('.pressList li').css({opacity:'1'});
		}
	)
});

function postal_code(obj){
	var pattern = /[^a-z0-9]/ig;
	var str = obj.value.replace(pattern,'');
	obj.value = str;
	
}
var check_out_onlick = 1;
function showCheckForm(check_form){
	if(!redirect_url()) return false;
	$('.checkout_form,#popup_login').hide();
	if(check_form==1){
		$('.checkout_form').show();
		$('.signin_as_guest').hide();
	}else $('#popup_login').show();
	/*$('input#credit_amex,input#credit_master,input#credit_visa').click(function(){
		alert($('input[name="credit"]').val());
		if($('#id_amex_alert input:checked').val()== 'amex'){
			alert(amex_alert);
		}
		check_shipping();
	});*/
	
}
function changeCredit(val){
	if(val== 'amex'){
		alert(amex_alert);
	}
	check_shipping();
	
}
function tabControl(obj, num){
	var curentTab=obj.id;
	var curentContent=curentTab.replace("tab","cont");

	curentContent_1=curentTab.replace("tab","cont");
	var tabs=$('#' + curentTab).parent().parent().children().children();
	var contents=$('#' + curentContent).parent().children();

	$(tabs).each( function(){								
			$(this).removeClass('active');
	});		
	
	$(contents).each( function(){								
			$(this).hide();
	});	
	
	$('#' + curentTab).addClass('active');
	$('#' + curentContent).fadeIn('slow');	
}

function checkout_removeItem(){
	$('.remove').click(function(){
		/* Linh Added */							
		$.post(domain+'cart_data.php?act=remove&id='+this.name,{},function(data){
			$('#cartNum').html(data);
			
			if(data=='0'){
				$('.checkout_form').css({'display':'none'});
				$('#i_like_checkout').attr('class','btn_137 btn_disable');
				check_out_onlick = 0;
				$('.checkout_empty_text').html('You currently have no items in your shopping bag');
				$('#subtotal').html('');
			}
		
		});
		
		/* End*/
		$('.checkout_product_'+this.name).remove();
		//css({backgroundColor:"red"}).fadeOut(1000, function(){
			//$(this).remove();
			//var removeItemID = $(this).parent().parent().attr('class');
			//$('.'+removeItemID+'').remove();
			checkout_page();
			
		//});
		
				
		return false;
	});
	
}

function checkout_same(){
		$('.checkout_form #ship_firstname').val($('.checkout_form  #bill_firstname').val());
		$('.checkout_form  #ship_lastname').val($('.checkout_form  #bill_lastname').val());
		$('.checkout_form  #ship_str_number').val($('.checkout_form  #bill_str_number').val());
		$('.checkout_form  #ship_str_name').val($('.checkout_form  #bill_str_name').val());
		$('.checkout_form  #ship_unit').val($('.checkout_form  #bill_unit').val());
		$('.checkout_form  #ship_postal').val($('.checkout_form  #bill_postal').val());
		$('.checkout_form  #ship_phone_code').val($('.checkout_form  #bill_phone_code').val());
		$('.checkout_form  #ship_phone_number').val($('.checkout_form  #bill_phone_number').val());
		$('.checkout_form  #ship_phone_number2').val($('.checkout_form  #bill_phone_number2').val());
		$('.checkout_form  #ship_city').val($('.checkout_form  #bill_city').val());
		var bill_city = $('#bill_country').val();
		//checkout_switch_ship($('#bill_country').val());
		$('#ship_country option').each(function(){
			if ($(this).val() == $('#bill_country').val()){
				load_state('ship_state',$(this).val(),$('#bill_state').val(),null,function(){
																						   				
				});
				check_shipping(0,$('#bill_country').val(),$('#bill_state').val());	
				//load_city('ship_city',$('#bill_state').val(),$('#bill_city').val());
				$(this).attr('selected','selected');
				
			}
		});
		
		
/*		$('#ship_state option').each(function(){
			if ($(this).val() == $('#bill_state').val()){
				$(this).attr('selected','selected');
				alert('dd');
				check_shipping(0,$('#bill_country').val(),$('#bill_state').val());
			}
		});
*/		
}


var delayRotate = 5000;
var animationSpeed = 750;
var tempRotateI = 0;
var promo_len;
var promo_current = 0;
var promo_auto;
function home_promo_carousel(){
	$('.carousel img:first').css({ top:'0px'}).show();	
	$('.promo_carousel').append('<div class="disable"></div>');

	$('.carousel img').each(function(i){
		$(this).attr('rel',i);
	});
	 
	promo_len = $('.carousel img').length;
	$('.carousel img').addClass('current');
	promo_current = $('.carousel img.current').attr('rel');
	
	for (i=0; i<promo_len; i++)
		$('.promo_nav').append('<img src="images/b.gif" alt="'+i+'">');		
	$('.promo_nav img:first').addClass('active');
	if (promo_len>1) setTimeout("home_promo_auto()",delayRotate)
	
	
	$('.promo_nav img').live('click', function(){
		if ($(this).attr('class') != "active"){
			$('.disable').show();
			clearTimeout(promo_auto);
			tempRotateI = Number($(this).attr('alt'))-1;
			home_promo_auto();
		}
	});
}

function home_promo_auto(){
	tempRotateI = tempRotateI+1;
	
	//if (tempRotateI>promo_current){
		$('.promo_nav img').removeClass('active');
		$('.promo_nav img:eq('+tempRotateI+')').addClass('active');
		$('.carousel img:eq('+promo_current+')').stop(true, true).animate({ left:'-343px'}, animationSpeed,
			function(){
				$(this).attr('class', '');
			}
		);
		$('.carousel img:eq('+tempRotateI+')').addClass('right').css({ left:'343px'}).show().stop(true, true).animate({ left:'0px'}, animationSpeed,
			function(){ 
				$(this).attr('class','current');
				promo_current = $('.carousel img.current').attr('rel');
				$('.disable').hide();
			}
		);
	//}


	if ((tempRotateI+1) == promo_len){
		tempRotateI=-1;	
	}
	promo_auto=setTimeout("home_promo_auto()", delayRotate);
}
