﻿$(function(){

var slideshow = $('#_slideshow'), ssBtn = slideshow.find('a[h]'), ssContent = slideshow.find('div.inner');

ssBtn.click(function(){return false;}).bind('mouseenter aaa', function(){
	var url = $(this).attr('u'), href = $(this).attr('h');
	$(this).active();
	ssContent.slideshow({
		html:'<a href="'+href+'" target="_blank"><img src="'+url+'" width="540" height="160" /></a>'
	});
}).round({otherObj:ssContent, stop:'mouseenter', start:'mouseleave', run:'aaa', time:4000});

//$('.textmarquee').marquee({loop:false, wrap:true});
$('#marqueeb').marquee({loop:true, wrap:true, direction:'top'});

$('.promarquee').each(function(){
	var $this = $(this), left = $this.find('.left'), right = $this.find('.right'), marquee = $this.find('.marquee'),
		speed = marquee.attr('s') || 1, direction = $this.find('[d]');

	left.click(function(){
		marquee.marquee({direction:'left', loop:true, animate:true, speed:speed, run:false, virtual:true});
		marquee.imageDelayLoad();
		return false;
	});
	right.click(function(){
		marquee.marquee({direction:'right', loop:true, animate:true, speed:speed, run:false, virtual:true});
		marquee.imageDelayLoad();
		return false;
	});
});

var $imgBig = $('#imagebig').mouseout(function(){
	//	$imgBig.hide();
	}), $img = $('#imagebig').find('img'), hideTime, isHide = true;
	
$('[big]').live('mouseenter', function(){
	var $this = $(this), url = $this.find('img').attr('src');
	if (url == emptyimage) return;
	hideTime && clearTimeout(hideTime);
	hideTime = '';
	$img.unbind('load').bind('load', function(){
		var isShow = false;
		if ($imgBig.is(':visible')) isShow = true;
		$imgBig.show().find('img').css({width:'', height:''});
		var ibWidth = $img[0].width, ibHeight = $img[0].height, offset = $imgBig.offset(), width = $imgBig.width(), height = $imgBig.height();
		$imgBig.width(ibWidth).height(ibHeight);
		$imgBig.nearLayer($this).css({opacity:1});
		var theOffset = $imgBig.offset();
		isShow 
			&& $imgBig.stop().css({width:width, height:height, left:offset.left, top:offset.top})
				.animate({width:ibWidth, height:ibHeight, left:theOffset.left, top:theOffset.top})
			&& $imgBig.find('img').stop().css({width:width, height:height})
				.animate({width:ibWidth, height:ibHeight});
			
	}).attr('src', url.replace('/small/', '/big/'));
}).live('mouseleave', function(){
	var $this = $(this);
	hideTime = setTimeout(function(){
		$imgBig.animate({opacity:0}, function(){
			$(this).hide();
		});
	}, 100);
});

var pfPro = $('div.mspro'), pfPage = 1, pfTime;
pfPro.mouseenter(function(){
	pfTime && clearTimeout(pfTime);
	pfTime = '';
}).mouseleave(function(){
	loadIndexPfPro();
});

function loadIndexPfPro(){
	pfTime && clearTimeout(pfTime);
	pfTime = setTimeout(function(){
		var top = strToInt(pfPro.css('top'))-330, $child = pfPro.children();
		pfPro.stop().animate({top:top}, 1000, function(){
			pfPro.imageDelayLoad(false, function(){
				var width = $(this).attr('width'), height = $(this).attr('height');
				$(this).removeAttr('auto').autoSize(width, height, function(){
					$(this).pos('abs').center(true);
				});
			});
			if (++pfPage % 2) {
				pfPro.css('top', 0);
				$child.children(':lt(8)').remove();
				return loadIndexPfPro();
			}
			$.ajax.server(U('index/pf'), {page:pfPage}, function(response){
				pfPro.css('top', 0);
				$child.children(':lt(8)').remove();
				$child.append($(response.view).css({zoom:1}));
				loadIndexPfPro();
			});
		});
	}, 5000);
}

loadIndexPfPro();

var search = $('#_search');

search.find('.search_list [b]').click(function(){
	var $this = $(this).active(), type = $this.attr('b');
	search.find('.searchchild [b]').hide().find('input').attr('disabled', 'true').end().filter('[b='+type+']').show().find('input').each(function(){
		$(this).removeAttr('disabled');
	});
	search.find('[type=hidden]').val(type).length;
}).filter('.active').click();

search.find('form').form({type:'blank'});

if ($('#_indexptype').is('.active')) {
	var ptypeHeight = 0, ptypeTimer;
	$('#_indexptype').hover(function(){
		if (ptypeTimer) clearTimeout(ptypeTimer), ptypeTimer = '';
		var $next = $(this).next();
		$next.addClass('active');
		if (!ptypeHeight) {
			ptypeHeight = $next.height();
			$next.height(0);
		}
		$next.stop().animate({height:ptypeHeight});
	}, function(){
		var $next = $(this).next();
		ptypeTimer = setTimeout(function(){
			$next.stop().animate({height:0}, function(){
				$(this).removeClass('active').css({display:''});
			});
		}, 500);
	}).next().hover(function(){
		if (ptypeTimer) clearTimeout(ptypeTimer), ptypeTimer = '';
	}, function(){
		$(this).prev().mouseout();
	});
}

$('#proimgs').each(function(){
	var $this = $(this), $img = $this.find('img.show'), width = $img.attr('width'), height = $img.attr('height');
	$this.find('li').click(function(){
		var url = $(this).active().find('img').attr('src').replace('/small/', '/big/');
		$img.attr('src', url).autoSize(width, height, function(){
			$(this).center(true);
		}).parent().attr('href', url);
		return false;
	});
});

$('input:button.addcart').click(function(){
	var $this = $(this), $wrap = $this.parents('.cart:eq(0)'), isGo = $this.is('.go'),
		$count = $wrap.find('input.count'),
		barcode = $this.attr('d'), url = $this.attr('u');
	$.ajax.server(url, {count:strToInt($count.val()), barcode:barcode}, function(response){
		if (isGo) return redirect(response.url);
		alert('添加购物车成功！');
	});
});

$('.cartcount').each(function(){
	var $this = $(this), $price = $this.find('.price'), $allPrice = $('#allprice');
	$(this).find('input').change(function(){
		var v = strToInt($(this).val()), onePrice = $(this).attr('p'), 
			thePrice = 0, price = strToInt($price.text().replace('￥', '')),
			allPrice = strToInt($allPrice.text());
		if (v<1) $(this).val(v=1);
		$price.html('￥'+(thePrice=v*onePrice));
		$allPrice.html(allPrice+(thePrice-price))
	});
});

$('.updowntext').each(function(){
	var $this = $(this), $text = $this.find('input:text'),
		$up = $this.find('.up'), $down = $this.find('.down');
	$up.click(function(){
		var val = strToInt($text.val());
		if (val > 1) return $text.val(val-1).change(), false;
		$text.val(1).change();
		return false;
	});
	$down.click(function(){
		var val = strToInt($text.val());
		$text.val(val+1).change();
		return false;
	});
});

$('.cartdelete').click(function(){
	var $this = $(this), id = $this.attr('i');
	$.ajax.server(U('cart/delete'), {id:id}, function(response){
		if (response.status == 'ok') $this.parents('.cartcount:eq(0)').fadeOut(function(){
			$(this).remove();
		});
	})
	return false;
});

function imgAutoLoad(){
	$('img[auto]').each(function(){
		var width = $(this).attr('width'), height = $(this).attr('height');
		$(this).removeAttr('auto').autoSize(width, height, function(){
			$(this).pos('abs').center(true);
		});
	});
}

imgAutoLoad();

$('[tabs]').each(function(){
	var $this = $(this), tabs = $this.attr('tabs').split('|'), $content = $(tabs[1]), content = [];
	$this.find(tabs[0]).each(function(i){
		var $this = $(this), url = $this.attr('u');
		if (!url) content[i] = $content.eq(i).html();
		$this.click(function(){
			if ($this.is('.active')) return;
			$this.active();
			if (content[i]) {
				$content.eq(i).length ? $content.eq(i).show().html(content[i])&&$content.filter('not(:eq('+i+'))').hide() : $content.html(content[i]);
			} else if (url) {
				$.ajax.server(url, function(response){
					content[i] = response.view;
					$content.eq(0).show().html(response.view)&&$content.filter('not(:eq('+i+'))').hide()
				});
			}
		});
	});
});

});

