function setIE6SelectBox(strName) {
    var isIE5 = (navigator.appVersion.indexOf("MSIE 5") != -1) ? true : false;
    var isIE6 = (navigator.appVersion.indexOf("MSIE 6") != -1) ? true : false;
    var isOpera = (navigator.appVersion.indexOf("Opera") != -1) ? true : false;
    if (isIE5 || isIE6 || isOpera)
        document.getElementById(strName).innerHTML = 
		"<iframe style=\"display: block; z-index: -1; filter:mask(); left: expression(((parseInt(this.parentNode.currentStyle.borderLeftWidth)||0)*-1)+'px'); ; width: expression(this.parentNode.offsetWidth+'px'); position: absolute; ; TOP: expression(((parseInt(this.parentNode.currentStyle.borderTopWidth)||0)*-1)+'px'); height: expression(this.parentNode.offsetHeight+'px')\" tabIndex=\"-1\"></IFRAME>" + document.getElementById(strName).innerHTML;
} 
/**
 * jQuery.timers - Timer abstractions for jQuery
 * Written by Blair Mitchelmore (blair DOT mitchelmore AT gmail DOT com)
 * Licensed under the WTFPL (http://sam.zoy.org/wtfpl/).
 * Date: 2009/02/08
 *
 * @author Blair Mitchelmore
 * @version 1.1.2
 *
 **/
jQuery.fn.extend({
	everyTime: function(interval, label, fn, times, belay) {
		return this.each(function() {
			jQuery.timer.add(this, interval, label, fn, times, belay);
		});
	},
	oneTime: function(interval, label, fn) {
		return this.each(function() {
			jQuery.timer.add(this, interval, label, fn, 1);
		});
	},
	stopTime: function(label, fn) {
		return this.each(function() {
			jQuery.timer.remove(this, label, fn);
		});
	}
});
jQuery.event.special
jQuery.extend({
	timer: {
		global: [],
		guid: 1,
		dataKey: "jQuery.timer",
		regex: /^([0-9]+(?:\.[0-9]*)?)\s*(.*s)?$/,
		powers: {
			// Yeah this is major overkill...
			'ms': 1,
			'cs': 10,
			'ds': 100,
			's': 1000,
			'das': 10000,
			'hs': 100000,
			'ks': 1000000
		},
		timeParse: function(value) {
			if (value == undefined || value == null)
				return null;
			var result = this.regex.exec(jQuery.trim(value.toString()));
			if (result[2]) {
				var num = parseFloat(result[1]);
				var mult = this.powers[result[2]] || 1;
				return num * mult;
			} else {
				return value;
			}
		},
		add: function(element, interval, label, fn, times, belay) {
			var counter = 0;
			
			if (jQuery.isFunction(label)) {
				if (!times) 
					times = fn;
				fn = label;
				label = interval;
			}
			
			interval = jQuery.timer.timeParse(interval);

			if (typeof interval != 'number' || isNaN(interval) || interval <= 0)
				return;

			if (times && times.constructor != Number) {
				belay = !!times;
				times = 0;
			}
			
			times = times || 0;
			belay = belay || false;
			
			var timers = jQuery.data(element, this.dataKey) || jQuery.data(element, this.dataKey, {});
			
			if (!timers[label])
				timers[label] = {};
			
			fn.timerID = fn.timerID || this.guid++;
			
			var handler = function() {
				if (belay && this.inProgress) 
					return;
				this.inProgress = true;
				if ((++counter > times && times !== 0) || fn.call(element, counter) === false)
					jQuery.timer.remove(element, label, fn);
				this.inProgress = false;
			};
			
			handler.timerID = fn.timerID;
			
			if (!timers[label][fn.timerID])
				timers[label][fn.timerID] = window.setInterval(handler,interval);
			
			this.global.push( element );
			
		},
		remove: function(element, label, fn) {
			var timers = jQuery.data(element, this.dataKey), ret;
			
			if ( timers ) {
				
				if (!label) {
					for ( label in timers )
						this.remove(element, label, fn);
				} else if ( timers[label] ) {
					if ( fn ) {
						if ( fn.timerID ) {
							window.clearInterval(timers[label][fn.timerID]);
							delete timers[label][fn.timerID];
						}
					} else {
						for ( var fn in timers[label] ) {
							window.clearInterval(timers[label][fn]);
							delete timers[label][fn];
						}
					}
					
					for ( ret in timers[label] ) break;
					if ( !ret ) {
						ret = null;
						delete timers[label];
					}
				}
				
				for ( ret in timers ) break;
				if ( !ret ) 
					jQuery.removeData(element, this.dataKey);
			}
		}
	}
});
jQuery(window).bind("unload", function() {
	jQuery.each(jQuery.timer.global, function(index, item) {
		jQuery.timer.remove(item);
	});
});
//初始化
//設定網頁語言
var strUrl = location.pathname;
if (strUrl.indexOf("/") != -1) {
	var getSearch = strUrl.split("/");
	var $lang=getSearch[1];
}

$(function(){
		   
setIE6SelectBox('cart_list');
	/*初始化及設定清單位址*/
	var $cart_list = $('#cart_list')
		,$cart_time
		,$show_cart=$('.show_cart')
		,$cart_show=1
		,$cart_height=$cart_list.height();
		//css({position:'absolute',top:$(window).scrollTop()+$(window).height()-$cart_list.height(),left:$('#footer').offset().left}).height();
	$(window).resize( function(){
		$cart_list.css({left:$('#footer').offset().left})
	}).resize();
	//設定移除商品
	$('#cart_item_area').delegate('.delect_item', 'click',function(e){
			var $item=$(this);
			$.post($lang+'/del_cart.html',{'pro_id':$item.attr('rel')},function(data){
				if(data=='success'){
					$('#item_num').text($('#item_num').text()-1);
					$item.parent().fadeOut(500);
					$('.add_car[rel="'+$item.attr('rel')+'"]').show().find('~ .in_cart').hide();
					if($('#item_num').text()<1){
						$('#Checkout').hide();
						$show_cart.eq(1).click();
					}
				}
			})
	});
	//取得購物車內產品
	function get_cart(){
		$('#car_item_area').empty();
		$.getJSON($lang+"/get_cart",function(data){
			if(data.length>0){
				//變更購物車數量
				$('.show_cart').eq(($cart_show+1)%2).show(300);
				$('#item_num').text(data.length);
				$('#Checkout').show();
				if($cart_show==0){
					$('#cart_item_area').empty();
					$.each(data, function(i,item){
						var li=$('<li><img src="/image/delect.png" class="delect_item" rel="'+item.id+'-'+item.norm_str+'"/>&nbsp;'+item.name.substr(0,5)+'&nbsp;X'+item.num+'<br></li>'),
							a_href=$('<a/>',{'href':'/'+$lang+'/product_item/'+item.id+'.html'}),
							img=$('<img/>',{'src':'/picture/'+$lang+'/product/narrow/'+item.picture,'title':item.name}).addClass('cart_item_pic');
						$('#cart_item_area')
						.append(li.append(a_href.append(img)))
					});
				}
			}else{
				$('#item_num').text('0');
				$('#Checkout,.show_cart').hide();
			}
		});
	}
	//設定清單點擊事件
	$show_cart.click(function(){
		$cart_show=$(this).index();
		$show_cart.toggle();
		if($cart_show==0){
			$cart_height=160;
		}else if($cart_show==1){
			$cart_height=30;
		}
		get_cart();
		$cart_list.animate({height:'0px'},300).animate({height:$cart_height+'px'},300)
		return false;
	}).eq(1).hide();
	//設定整體畫面高度
	if(($('#footer').offset().top+parseInt($('#footer').css('height')))<$(window).height()){
		$('#navigation').css({'height':($(window).height()-parseInt($('#footer').css('height'))-parseInt($('#header').css('height')))});
	};
	//加入購物車
	$('.add_car').live('click', function(){
		var $item=$(this)
			,item_id=$item.attr('rel')
			,product_img=$('#img_'+item_id)
			,queryString = $('#cart_form_'+item_id).formSerialize();
		$.post('/'+$lang+'/add_cart.html',queryString,function(data){
			if(data>0){
				var options={to:"#car",className:'addcar'};
				//$item.hide().next('.in_cart').show();
				product_img.effect('transfer',options,1000,get_cart());
			}
		})
	});
	$('.cart_form').submit(function(){return false;})
	//runing
	get_cart();
	//回頂端
	$('.top').click(function(){
		$('html, body').animate({
			scrollTop: 0
		}, 500, 'linear');
		return false;
	});
	//回首頁
	$("#header_footer").click(function() {
		location.href=" ";
	});
	//將有開啟script而不需要出現的元素隱藏
	$('.s_hide').hide();
	//變更語言
	$('#change_lang').change(function(){
		window.location='/welcome/index/'+$(this).val();
	})
	//回上頁
	$('.page_prev').click(function(){
		history.back();
	});
	//廣告點擊次數
	$('img[id^="link_"]').click(function(){
		$.post("adv_count.php", { link_id: $(this).attr('id')} );
	});
	//新聞跑馬燈
	//初始化
	if($('.news_run').length>0){
		var news_num=$('.news_run').hide().eq(0).show().end().length
			,now_num=0;
		function run_news(){
			$('body').everyTime('3s','A',function(){
				now_num=(now_num+1)%news_num;
				$('.news_run').eq((now_num-1)).animate({opacity:'hide'},'slow',function(){
					$('.news_run').eq(now_num).show(500)});
			},0,true);
		};
		run_news();
		$('#news_box').hover(
			function(){
				$('body').stopTime ('A');
			},function(){
				run_news();
			}
		);
	}
	//熱門商品輪播
	$.fn.hot_show=function(){
		//初始化
		var timer
			,$hot_num=0
			,$runing=true
			,$div=[]
			,$change_hot_img
			,$hot_product_body=$('#hot_product_body').css({'overflow':'hidden'})
			,$hot_count=$('.hot_view')
			.each(function(i){
				$div[i]=[$(this).find('h1').text(),$(this).find('img').addClass('mod_img').attr('src'),$(this).find('a').attr('href')];
				if(i==0){
					$change_hot_img=$(this).hover(function(){clearTimeout(timer);$('#img_control').show();$runing=false;},function(){}).find('h3').remove().end();
				}else{
					$(this).hide();
				}
				$('<a class="change_hot_img">'+(i+1)+'</a>').click(function(){
					$runing=false;
					$('#img_control').show();
					clearTimeout(timer);
					$hot_num=i%$hot_count;
					img_next();
				}).appendTo($('#hot_product_body'));
				$('.change_hot_img').eq(0).addClass('change_hot_img_focus');
			})
			.length;
		$('.change_hot_img:last').after($('<a id="img_control">play</a>').click(function(){$runing=true;auto_run();}));
		function img_next(){
				$('.change_hot_img').removeClass('change_hot_img_focus')
				.eq(($hot_num%$hot_count))
				.addClass('change_hot_img_focus');
				$change_hot_img
				.animate({opacity:0},500,function(){
					$(this)
					.find('h1').text($div[($hot_num%$hot_count)][0])
					.end().find('img').attr({'src':$div[($hot_num%$hot_count)][1]})
					.end().find('a').attr({'href':$div[($hot_num%$hot_count)][2]})
					.end().animate({opacity:1},500);					  
				})
				//alert($change_hot_img.css('position'))
		};
		function auto_run(){
			if($runing){
				$('#img_control').hide();
				$hot_num++;
				timer = setTimeout(auto_run, 3000);
				img_next();
			}
		}
		$('#img_control').hide();
		if($hot_count>0){
			setTimeout(auto_run, 3000);
		}
	};
	$('#hot_product').hot_show();
	/*輸入提示串*/
	$('body').delegate('.key_word','focus',function(){
		if($(this).val()==$(this).attr('title')){
			$(this).val('');
		}
	})
	$('body').delegate('.key_word','blur',function(){
		if($(this).val()==''){
			$(this).val($(this).attr('title'));
		}
	})
	if($('.key_word').val()==''){
		$('.key_word').val($('.key_word').attr('title'));
	}
	/*重新設定驗證碼*/
	$('#captcha').click(function(){
		$captcha=$(this);
		$.get('/'+$lang+'/captcha/ajax.html',{},function(data){
			$captcha.attr('src',data).find('~ input').keyup();
		})
	});
	//購物圖片顯示
	$(".show_img").hover(
		function () {
			var img_name=$(this).find('td .img_name').val(),
			 	startPosition=$(this).offset(),
			 	transfer=$('<div class="img_area"></div>')
				.appendTo(document.body)
				.css({
					top: startPosition.top+$(this).height()+10,
					left: startPosition.left,
					position: 'absolute',
					display:'none'
				})
				.append("<img src='picture/"+$lang+"/product/narrow/"+img_name+"' width='150' />").fadeIn(500)
		  },
		  function () {
			$(".img_area").remove();
		  }
	);
	//購物流程1-2
	$('#modify_item').click(function(){
		$('#checkout_act').val('modify');
		$('#modify_cart').submit();
	})
	//隱藏及顯示購物車資訊
	$('#item_area').toggle(function(){
		$(this).find('img').attr({'src':'/image/hide_item.png'});
		$('#cart_info').slideToggle(300);
	},function(){
		$(this).find('img').attr({'src':'/image/show_item.png'});
		$('#cart_info').slideToggle(300);
	});
	$('#cart_info').hide();
	//設定消費者資訊輸入欄位
	$('#shop_info').delegate('[class^=validate]:not([name^=receive])','blur copy_info',function(){
		if($('#check_receive').attr('checked')){
			$('#receive_'+$(this).attr('name')).val($(this).val())
		}							 
	})
	$('#check_receive').click(function(){
		if($(this).attr('checked')){
			$('[class^=validate]:not([name^=receive])').trigger('copy_info');
		}
			$('#receive_info').slideToggle(300);
	});
});
