var map = {606:1,630:2,625:3,612:4,597:5,585:6,594:7,51:8,399:9,404:10,437:11,466:12,445:13,504:14,575:15};
var pictures = {
	1:{imgs:['IMG_0836','IMG_0837','IMG_0840'],brand:'Inway',name:'Challenge 30 DLX 草绿色',price:'282',link:'http://www.yangsam.com/goods_606.html#brand'},
	2:{imgs:['DSCF2201','DSCF2208','IMG_0812','IMG_0818','IMG_0821'],brand:'Inway',name:'California 65 冰川蓝',price:'423',link:'http://www.yangsam.com/goods_630.html#brand'},
	3:{imgs:['DSCF1980','DSCF1990','IMG_0636','IMG_0644'],brand:'Inway',name:'Grand tour 65 蓝色',price:'780',link:'http://www.yangsam.com/goods_625.html#brand'},
	4:{imgs:['DSCF2242','DSCF2246','DSCF2255','DSCF2264'],brand:'Inway',name:'Fly fox 30 冰川蓝',price:'185',link:'http://www.yangsam.com/goods_612.html#brand'},
	5:{imgs:['DSCF2232','DSCF2238','IMG_0849','IMG_0870'],brand:'Kingcamp',name:'Orioles 35 橙色',price:'263',link:'http://www.yangsam.com/goods_597.html#brand'},
	6:{imgs:['DSCF2105','DSCF2157','IMG_0759','IMG_0765','IMG_0875'],brand:'Kingcamp',name:'Compact 35 黑色',price:'316',link:'http://www.yangsam.com/goods_585.html#brand'},
	7:{imgs:['DSCF2031','DSCF2034','DSCF2058','DSCF2059'],brand:'Kingcamp',name:'Business luggage 黑色',price:'498',link:'http://www.yangsam.com/goods_594.html#brand'},
	8:{imgs:['DSCF2158','DSCF2171','DSCF2173','DSCF2187','IMG_0798'],brand:'Osprey',name:'Aura 65 Garnet',price:'1936',link:'http://www.yangsam.com/goods_51.html#cate'},
	9:{imgs:['IMG_0770','IMG_0771','IMG_0774','IMG_0781'],brand:'Mangrove',name:'MA10240040091 黑/绿色',price:'251',link:'http://www.yangsam.com/goods_399.html#brand'},
	10:{imgs:['DSCF2004','DSCF2018','DSCF2019','IMG_0654'],brand:'Mangrove',name:'Alaska65 25L 黑色',price:'263',link:'http://www.yangsam.com/goods_404.html#brand'},
	11:{imgs:['DSCF2026','IMG_0658','IMG_0659'],brand:'Mountain wolf',name:'Ocean plus 橙色',price:'352',link:'http://www.yangsam.com/goods_437.html#brand'},
	12:{imgs:['DSCF2076','IMG_0701','IMG_0710','IMG_0711'],brand:'Mountain wolf',name:'Solar 38L 红色',price:'352',link:'http://www.yangsam.com/goods_466.html#brand'},
	13:{imgs:['DSCF2038','DSCF2041','IMG_0674','IMG_0677','IMG_0678'],brand:'Doite',name:'Back draft 灰色',price:'208',link:'http://www.yangsam.com/goods_445.html#brand'},
	14:{imgs:['DSCF2216','IMG_0823','IMG_0829'],brand:'Xlneeko',name:'Neeko 30 橙色',price:'234',link:'http://www.yangsam.com/goods_504.html#brand'},
	15:{imgs:['IMG_0851','IMG_0856','IMG_0862'],brand:'Xlneeko',name:'XL 1092 军绿色',price:'251',link:'http://www.yangsam.com/goods_575.html#brand'}
};

var imgsObject = {};

var selectedIndex = 0;
var slideIndex = 1;
var offset = null;
var stopSlide = true;

$(function(){
	
	$('#info .mask,#prevPic,#nextPic').css('opacity', 0.5);
	$('#info .bg').css('opacity',0.3);
	
	$('#close').click(function(){
		$('#minimain').removeShadow();
		$('#mask').remove();
		$('#minimain').animate({top:offset.top,left:offset.left,width:140,height:270},400,function(){
			$(this).hide();
		});
	});
	
	$('#minimain').click(function(evt){
		if (!stopSlide)return;
		stopSlide = false;
		
		if (($(this).offset().left+$(this).width()/2) >= evt.pageX) {
			slideIndex--;
		} else {
			slideIndex++;
		}
		
		$('#photo').animate({scrollLeft:slideIndex*500}, 180, function(){
			if (slideIndex < 1)slideIndex = imgsObject[selectedIndex].length;
			if (slideIndex > imgsObject[selectedIndex].length)slideIndex = 1;
			$('#photo').scrollLeft(slideIndex*500);
			stopSlide = true;
		});
	}).mousemove(function(evt){
		if (($(this).offset().left+$(this).width()/2) >= evt.pageX) {
			$('#prevPic').show();
			$('#nextPic').hide();
		} else {
			$('#prevPic').hide();
			$('#nextPic').show();
		}
	}).mouseout(function(evt){
		$('#nextPic,#prevPic').hide();
	});
	
	$('#content a').click(function(){
		$('<div />').attr('id', 'mask').css('position','absolute').css('top',0).css('left',0).css('background-color','#000000').css('z-index',0).css('width',$(document).width()).css('height',$(document).height()).css('opacity', 0.5).appendTo(document.body);
		selectedIndex = $('#content a').index(this)+1;
		slideIndex = 1;
		offset = $(this).offset();
		$('#minimain').css('width',140).css('height',270).css('top', offset.top).css('left',offset.left).show();
		var offsetTop = ($(window).height() > 720 ? ($(window).height() - 720) / 2 : 0);
		$('#minimain').animate({top:$(window).scrollTop() + offsetTop,left:($(window).scrollLeft()+$(window).width() / 2 - 260),width:520,height:720},400,function(){
			$('#photo').scrollLeft(500);
			$(this).dropShadow();
		});
		$('#photo img').remove();
		if (!imgsObject[selectedIndex]) {
			loadPic(selectedIndex);
		}
		$('#photo').append(imgsObject[selectedIndex][imgsObject[selectedIndex].length-1].clone());
		for (var i = 0; i < imgsObject[selectedIndex].length; i++) {
			$('#photo').append(imgsObject[selectedIndex][i]); 
		}
		$('#photo').append(imgsObject[selectedIndex][0].clone());
		$('#photo').scrollLeft(500);
		$('#info .brand').html(pictures[selectedIndex].brand);
		$('#info .title').html(pictures[selectedIndex].name);
		$('#info .price').html('价格：'+pictures[selectedIndex].price+'元');
		$('#info .buy a').attr('href', pictures[selectedIndex].link);
	});

	setInterval("slideShow()", 4000);
	
	preloadPictures();
	
	var hash = window.location.hash;
	if (map[hash.replace('#','')]) {
		$('#content a[href="'+hash+'"]').click();
		// fixed safari on page refresh
		if ($.browser.safari) {
			$(window).scroll(function(){
				if ($('#minimain').is(':visible')) {
					setTimeout(function() {
						var minimain = $('#minimain');
						if (!minimain.shadowId()) {
							minimain.stop();
							var offsetTop = ($(window).height() > 720 ? ($(window).height() - 720) / 2 : 0);
							$('#minimain').animate({top:$(window).scrollTop() + offsetTop,left:($(window).scrollLeft()+$(window).width() / 2 - 260),width:520,height:720},400,function(){
								minimain.dropShadow();
							});
						} else {
							minimain.removeShadow();
							minimain.css('top',($(window).height() > 720 ? ($(window).height() - 720) / 2 : 0)+$(window).scrollTop());
							minimain.dropShadow();
						}
					}, 100);
				}
				$.event.remove(window,'scroll',arguments.callee);
			});
		}
	}
});

function slideShow()
{
	if ($('#minimain').is(':visible')) {
		return;
	}
	var gallerys = $('.gallery');
	var gIndex = {};
	for (var i=0; i<5; i++) {
		var index = 0;
		do {
			index = Math.floor(Math.random() * 100) % gallerys.length;
		} while(gIndex[index]);
		gIndex[index] = true;
		$('#status').html(index);
		var gallery = gallerys[index];
		var imgs = $('img', gallery);
		var visibleImg = $('img:visible', gallery);
	
		var nextImg = imgs[imgs.length - 1] == visibleImg[0] ? $(imgs[0]) : visibleImg.next();
		visibleImg.css('opacity',1).fadeTo(2000,0,function(){$(this).hide();});
		nextImg.show().css('opacity',0).fadeTo(2000,1);
	}
}
var loadPicTimer = null;
function preloadPictures(start)
{
	if (!start) {
		start = 1;
	}
	if (loadPicTimer)clearTimeout(loadPicTimer);
	if (start > 15)return;
	loadPic(start);
	loadPicTimer = setTimeout('preloadPictures('+(start+1)+')', 1000);
}

function loadPic(index)
{
	if (imgsObject[index])return;
	imgsObject[index] = [];
	for (var j = 0; j < pictures[index].imgs.length; j++) {
		imgsObject[index].push($('<img />').attr('src', 'images/big/'+index+'/'+pictures[index].imgs[j]+'.JPG').css('width','500px'));
	}
}