// JavaScript Document

var basestack = 0;
var showtopmenu = 0;
var menuobj = 0;
var showtopmenu_1 = 0;
var showtopmenu_2 = 0;
var showtopmenu_3 = 0;
var showtopmenu_4 = 0;
var showtopmenu_5 = 0;
var showtopmenu_6 = 0;
var showtopmenu_7 = 0;

$(function(){
		   
	//货币选择
	$(".currencies").hover(
		function(){
			$(this).children("div").addClass("active");
   	},function(){
			$(this).children("div").removeClass("active")
   	}
  )
	
	//主菜单延时监测
	setInterval(function(){
		showTopMenu(menuobj);
	},600);
	//主菜单
	function showTopMenu(){
		if(showtopmenu_1 == 1){
			setTimeout(function(){
				$(".navbar a[rel='1']").addClass("active");
				$(".navbar .s_1").show();
			},300);
		}else{
			setTimeout(function(){
				$(".navbar a[rel='1']").removeClass("active");
				$(".navbar .s_1").hide();
			},0);	
		}
		if(showtopmenu_2 == 1){
			setTimeout(function(){
				$(".navbar a[rel='2']").addClass("active");
				$(".navbar .s_2").show();
			},300);
		}else{
			setTimeout(function(){
				$(".navbar a[rel='2']").removeClass("active");
				$(".navbar .s_2").hide();
			},0);	
		}
		if(showtopmenu_3 == 1){
			setTimeout(function(){
				$(".navbar a[rel='3']").addClass("active");
				$(".navbar .s_3").show();
			},300);
		}else{
			setTimeout(function(){
				$(".navbar a[rel='3']").removeClass("active");
				$(".navbar .s_3").hide();
			},0);	
		}
		if(showtopmenu_4 == 1){
			setTimeout(function(){
				$(".navbar a[rel='4']").addClass("active");
				$(".navbar .s_4").show();
			},300);
		}else{
			setTimeout(function(){
				$(".navbar a[rel='4']").removeClass("active");
				$(".navbar .s_4").hide();
			},0);	
		}
		if(showtopmenu_5 == 1){
			setTimeout(function(){
				$(".navbar a[rel='5']").addClass("active");
				$(".navbar .s_5").show();
			},300);
		}else{
			setTimeout(function(){
				$(".navbar a[rel='5']").removeClass("active");
				$(".navbar .s_5").hide();
			},0);	
		}
		if(showtopmenu_6 == 1){
			setTimeout(function(){
				$(".navbar a[rel='6']").addClass("active");
				$(".navbar .s_6").show();
			},300);
		}else{
			setTimeout(function(){
				$(".navbar a[rel='6']").removeClass("active");
				$(".navbar .s_6").hide();
			},0);	
		}
		if(showtopmenu_7 == 1){
			setTimeout(function(){
				//$(".navbar a[rel='7']").addClass("active");
				$(".navbar .s_7").show();
			},300);
		}else{
			setTimeout(function(){
				//$(".navbar a[rel='7']").removeClass("active");
				$(".navbar .s_7").hide();
			},0);	
		}
	}
	
	$(".navbar ul li:has(ul)").hover(
		function(){
			menuobj = $(this);
			switch($(this).attr('rel')){
				case '1':
					showtopmenu_1 = 1;
					break;
				case '2':
					showtopmenu_2 = 1;
					break;
				case '3':
					showtopmenu_3 = 1;
					break;
				case '4':
					showtopmenu_4 = 1;
					break;
				case '5':
					showtopmenu_5 = 1;
					break;
				case '6':
					showtopmenu_6 = 1;
					break;
				case '7':
					showtopmenu_7 = 1;
					break;
			}
   	},function(){
			basestack = 1;
			switch($(this).attr('rel')){
				case '1':
					showtopmenu_1 = 0;
					break;
				case '2':
					showtopmenu_2 = 0;
					break;
				case '3':
					showtopmenu_3 = 0;
					break;
				case '4':
					showtopmenu_4 = 0;
					break;
				case '5':
					showtopmenu_5 = 0;
					break;
				case '6':
					showtopmenu_6 = 0;
					break;
				case '7':
					showtopmenu_7 = 0;
					break;
			}
   	})
	
	//帮助
	$('.help').click(
		function(){
			$('.pophelp').fadeIn('normal');
			$('.poppolicy').fadeOut('normal');
  		}
  	)
	$('.pophelp .close').click(
		function(){
			$('.pophelp').fadeOut('normal');
  		}
  	)
	$('.pophelp ul').hover(
		function(){
			$(this).css('background','#F6F5F6');
		},
		function(){
			$(this).css('background','white');
		}
	)
	
	//帮助中心跳转
	$('.email').click(
		function(){
			document.location.href = $(this).attr('rel');
		}
	)
	$('.customer').click(
		function(){
			document.location.href = $(this).attr('rel');
		}
	)
	
	//购物车
	$('.bag').click(
		function(){
			$('.popbag').animate({top: -37},500);
   	}
	)
	$('.nobag').click(
		function(){
			document.location.href = 'cart.html';
   	}
	)
	$('.popbag .close').click(
		function(){
			$('.popbag').animate({top: -600},300);
   	}
	)
  
  //保险说明
	$('.return').click(
		function(){
			$('.poppolicy').fadeIn('normal');
			$('.pophelp').fadeOut('normal');
   	}
  )
	$('.poppolicy .close').click(
		function(){
			$('.poppolicy').fadeOut('normal');
   	}
  )
  
  //搜索提交
  $('#header_search').submit(
  	function(){
  		if($('#header_search_keyword').val() != $('#header_search_keyword').attr('rel')){
  			$(this).attr('action', $(this).attr('action').replace('.html', '/' + $('#header_search_keyword').val() + '.html'));
  		}else{
  			return false;
  		}
  	}
  )
  
  //切换货币
  $('.currencies .changecurr').click(
  	function(){
	  	$.ajax({
				type: 'post',
				url: 'currencies/changecurrency',
				dataType: 'json',
				data: {
					currency_id: $(this).attr('rel')
				},
				success: function(data, textStatus){
					if(data['sucess'] == 1){
						//刷新页面
						document.location.reload();
					}
				}
			});
		}
  )
	
	//--------------input--------------
	$('input:text').focus(
		function(){
			if(this.value == this.defaultValue && $(this).attr('readonly') != 'readonly'){
				this.value='';
				$('input:text').css('color','#333333');
			}
		}
	)
	$('input:text').blur(
		function(){
			if(!this.value){
				this.value=this.defaultValue;
				$('input:text').css('color','#cdcdcd');
			}
		}
	)
	$('input:password').focus(
		function(){
			if(this.value==this.defaultValue){
				this.value='';
				$('input:password').css('color','#333333');
			}
		}
	)
	$('input:password').blur(
		function(){
			if(!this.value){
				this.value=this.defaultValue;
				$('input:password').css('color','#cdcdcd');
			}
		}
	)
	$('textarea').focus(
		function(){
			if(this.value==this.defaultValue){
				this.value='';
				$('textarea').css('color','#333333');
			}
		}
	)
	$('textarea').blur(
		function(){
			if(!this.value){
				this.value=this.defaultValue;
				$('textarea').css('color','#cdcdcd');
			}
		}
	)
	$('#authcode').click(
		function(){
			$('#authcode img').attr('src', $(this).attr('rel') + '/' + Math.random());
			return false;
		}
	)
	
	//倒计时
	$.fn.countDown = function (options) {

		config = {};

		$.extend(config, options);

		diffSecs = this.setCountDown(config);
	
		if (config.onComplete)
		{
			$.data($(this)[0], 'callback', config.onComplete);
		}
		if (config.omitWeeks)
		{
			$.data($(this)[0], 'omitWeeks', config.omitWeeks);
		}

		$('#' + $(this).attr('id') + ' .digit').html('<div class="top"></div><div class="bottom"></div>');
		$(this).doCountDown($(this).attr('id'), diffSecs, 500);

		return this;

	};

	$.fn.stopCountDown = function () {
		clearTimeout($.data(this[0], 'timer'));
	};

	$.fn.startCountDown = function () {
		this.doCountDown($(this).attr('id'),$.data(this[0], 'diffSecs'), 500);
	};

	$.fn.setCountDown = function (options) {
		var targetTime = new Date();

		if (options.targetDate)
		{
			targetTime = new Date(options.targetDate.month + '/' + options.targetDate.day + '/' + options.targetDate.year + ' ' + options.targetDate.hour + ':' + options.targetDate.min + ':' + options.targetDate.sec + (options.targetDate.utc ? ' UTC' : ''));
		}
		else if (options.targetOffset)
		{
			targetTime.setFullYear(options.targetOffset.year + targetTime.getFullYear());
			targetTime.setMonth(options.targetOffset.month + targetTime.getMonth());
			targetTime.setDate(options.targetOffset.day + targetTime.getDate());
			targetTime.setHours(options.targetOffset.hour + targetTime.getHours());
			targetTime.setMinutes(options.targetOffset.min + targetTime.getMinutes());
			targetTime.setSeconds(options.targetOffset.sec + targetTime.getSeconds());
		}

		var nowTime = new Date();

		diffSecs = Math.floor((targetTime.valueOf()-nowTime.valueOf())/1000);

		$.data(this[0], 'diffSecs', diffSecs);

		return diffSecs;
	};

	$.fn.doCountDown = function (id, diffSecs, duration) {
		$this = $('#' + id);
		if (diffSecs <= 0)
		{
			diffSecs = 0;
			if ($.data($this[0], 'timer'))
			{
				clearTimeout($.data($this[0], 'timer'));
			}
		}

		secs = diffSecs % 60;
		mins = Math.floor(diffSecs/60)%60;
		hours = Math.floor(diffSecs/60/60)%24;
		days = Math.floor(diffSecs/60/60/24);
		

		$this.dashChangeTo(id, 'seconds_dash', secs, duration ? duration : 800);
		$this.dashChangeTo(id, 'minutes_dash', mins, duration ? duration : 1200);
		$this.dashChangeTo(id, 'hours_dash', hours, duration ? duration : 1200);
		$this.dashChangeTo(id, 'days_dash', days, duration ? duration : 1200);

		$.data($this[0], 'diffSecs', diffSecs);
		if (diffSecs > 0)
		{
			e = $this;
			t = setTimeout(function() { e.doCountDown(id, diffSecs-1) } , 1000);
			$.data(e[0], 'timer', t);
		} 
		else if (cb = $.data($this[0], 'callback')) 
		{
			$.data($this[0], 'callback')();
		}

	};

	$.fn.dashChangeTo = function(id, dash, n, duration) {
		  $this = $('#' + id);
		 
		  for (var i=($this.find('.' + dash + ' .digit').length-1); i>=0; i--)
		  {
				var d = n%10;
				n = (n - d) / 10;
				$this.digitChangeTo('#' + $this.attr('id') + ' .' + dash + ' .digit:eq('+i+')', d, duration);
		  }
	};

	$.fn.digitChangeTo = function (digit, n, duration) {
		if (!duration)
		{
			duration = 800;
		}
		if ($(digit + ' div.top').html() != n + '')
		{

			$(digit + ' div.top').css({'display': 'none'});
			$(digit + ' div.top').html((n ? n : '0')).slideDown(duration);

			$(digit + ' div.bottom').animate({'height': ''}, duration, function() {
				$(digit + ' div.bottom').html($(digit + ' div.top').html());
				$(digit + ' div.bottom').css({'display': 'block', 'height': ''});
				$(digit + ' div.top').hide().slideUp(10);

			
			});
		}
	};
	
});

jQuery.extend({
	dataSubmiting:function(obj, offset){
		//提交按钮锁定
		$(obj).css('margin-top', '-' + offset + 'px');
		$('.submiting').html('SUBMITING ');
		$i = 0;
		submiting = setInterval(function(){
			if($i < 6){
				$('.submiting').append('.');
				$i ++;
			}else{
				$('.submiting').html('SUBMITING ');
				$i = 0;
			}
		},300);
	},
	dataSubmitingReset:function(obj){
		//回到提交按钮
		clearInterval(submiting);
		$(obj).css('margin-top', '0px');
		$('.submiting').html('');
	},
	dataSubmitEnd:function(obj, type){
		//提交完成
		switch(type){
			case 1:
				clearInterval(submiting);
				$(obj).css('margin-top', '0px');
				break;
			case 2:
				clearInterval(submiting);
				$('.submiting').html('SUBMIT SUCESSFUL');
				break;
		}
	}
})


