var wrap = false;

var slide_num = 1;
var pause=false;
var hover_next=false;
var hover_prev=false;
var scroll_amount = 0;

	function slide_transition(old_slide,new_slide) {
		$('#'+old_slide).hide();
		$('#'+new_slide).fadeIn();	
	}
	function play_slideshow() {
		if (!pause) {
			new_slide = slide_num+1;
			if ($('#'+new_slide).length>0){
				slide_transition(slide_num,new_slide);
				slide_num = slide_num+1;
				navTimeout = setTimeout(function() {
					if (!pause)
						play_slideshow();
				}, 1000);
			} else if (wrap) {
				slide_transition(slide_num,1);
				slide_num = 1;
			} else {
				pause = true;
				$('#play_pause').css('background-image',"none");
				$('#fastf').css('background-image',"none");
			}
		} else if (!wrap)
			$('#play_pause').css('background-image',"none");	
	}

	function fastf_slide() {
		new_slide = slide_num+1;
		if ($('#'+new_slide).length>0){
			//alert(slide_num);
			slide_transition(slide_num,new_slide);
			slide_num = slide_num+1;
			pause = true
		} else if (wrap) {
			slide_transition(slide_num,1);
			slide_num = 1;
			pause = true;		
		}
		if (!wrap && $('#'+(slide_num+1)).length<=0){
			$('#fastf').css('background-image',"none");	
		}
	}

	function rewind_slide() {
		new_slide = slide_num-1;
		if ($('#'+new_slide).length>0){
			slide_transition(slide_num,new_slide);
			slide_num = slide_num-1;
			pause = true
		} else if (wrap) {
			slide_transition(slide_num,last_slide);
			slide_num = last_slide;
			pause = true;		
		}
		if (!wrap && $('#'+(slide_num-1)).length<=0){
			$('#rewind').css('background-image',"none");	
		}
	}

	function timeout_next() {
		if (scroll_amount<5)
			scroll_amount = scroll_amount+0.1;
		navTimeout = setTimeout(function() {
			if (hover_next) {
				$('#slideshow_wrapper')[0].scrollBy(Math.ceil(scroll_amount));		
				timeout_next();
			}
		}, 10);	
	}
	
	function timeout_prev() {
		if (scroll_amount>-5)
			scroll_amount = scroll_amount-0.1;
		navTimeout = setTimeout(function() {
			if (hover_prev) {
				$('#slideshow_wrapper')[0].scrollBy(Math.ceil(scroll_amount));		
				timeout_prev();
			}
		}, 10);	
	}

$(document).ready(function() {

	$('#slideshow_wrapper').jScrollPane({showArrows:true});
	var i = 1;
	while ($('#'+(i+1)).length>0){
		i++;
	}

    $('#next').hover(function() {
		hover_next=true;
		timeout_next();
		}, function() {
		hover_next=false;
		scroll_amount = 0;
	});
 
    $('#prev').hover(function() {
		hover_prev=true;
		timeout_prev();
		}, function() {
		hover_prev=false;
		scroll_amount = 0;
	});
	
	$('#play_pause').hover(function() {
		if (wrap || slide_num<last_slide) {
			if (pause)
				$(this).css('background-image',"url('{img}play.png')");
			else 
				$(this).css('background-image',"url('{img}pause.png')");
			$('#fastf').css('background-image',"url('{img}fastf.png')");
		}
		if (wrap || slide_num>1)
			$('#rewind').css('background-image',"url('{img}rewind.png')");
		
	},function() {
		$(this).css('background-image',"none");	
		$("#rewind").css('background-image',"none");	
		$("#fastf").css('background-image',"none");	
	});
	$('#play_pause').click(function() {
		pause = !pause;
		if (pause)
			$(this).css('background-image',"url('{img}play.png')");
		else {
			if (wrap || slide_num<last_slide) {
			play_slideshow();
			$(this).css('background-image',"url('{img}pause.png')");
		}
	}
	});
	$('#rewind').click(function() {
		rewind_slide();
	});
	$('#rewind').hover(function() {
		if (wrap || slide_num>1)
			$(this).css('background-image',"url('{img}rewind.png')");
	},function() {
		$(this).css('background-image',"none");	
	});
	$('#fastf').hover(function() {
		if (wrap || slide_num<last_slide)
			$(this).css('background-image',"url('{img}fastf.png')");
	},function() {
		$(this).css('background-image',"none");	
	});
	$('#fastf').click(function() {
		fastf_slide();
	});

	var tabContainers = $('div.navigation,div.info');
	tabContainers.hide().filter('div.navigation:first,div.info:first').show();
	//.filter(':first').show();
	box_width = $('.main-img img',tabContainers.filter('#1')).width();
	box_left= 382 + box_width/2;
	$('a.buy',tabContainers.filter('#1')).css('left',box_left+'px');
	box_right= 425 - box_width/2;
	$('a.sign',tabContainers.filter('#1')).css('left',box_right+'px');
	div_title_width = $('div.title',tabContainers.filter('#1')).width();
	box_min_width = div_title_width+130;
	$('div.title',tabContainers.filter('#1')).css('margin-left',(425-div_title_width/2)+'px');	
	extra_height = 0;
	if (box_width<box_min_width) {
		$('div.title',tabContainers.filter('#1')).css('margin-top','40px');
		extra_height = 40;
	}
	box_height = $('.main-img img',tabContainers.filter('#1')).height();
	img_top_margin = 0;
	if (box_height<510) {
		img_top_margin = (480-box_height-12)/2;
		$('.main-img',tabContainers.filter('#1')).css('margin-top',img_top_margin+'px');
	}
	$('.main-img a.buy',tabContainers.filter('#1')).css('top',(img_top_margin+box_height+6)+'px');
	$('.main-img a.sign',tabContainers.filter('#1')).css('top',(img_top_margin+box_height+15)+'px');
	
	var wide_column_margin_top = 50;
	
	total_height = box_height+extra_height+40;
	if (total_height>510) {
		new_margin = total_height-510 + wide_column_margin_top;
	} else new_margin = wide_column_margin_top;
	if (new_margin<wide_column_margin_top) new_margin = wide_column_margin_top;
	$('#wide_column').css('margin-top',new_margin+'px');
	
	$('#myslideshow li a').click(function () {
		tabContainers.hide();
		tabContainers.filter(this.hash).show();
		box_width = $('.main-img img',tabContainers.filter(this.hash)).width();
//					alert(box_width);
		box_left= 382 + box_width/2;
		$('a.buy',tabContainers.filter(this.hash)).css('left',box_left+'px');

		box_right= 425 - box_width/2;
		$('a.sign',tabContainers.filter(this.hash)).css('left',box_right+'px');
		
		div_title_width = $('div.title',tabContainers.filter(this.hash)).width();
		box_min_width = div_title_width+130;
		$('div.title',tabContainers.filter(this.hash)).css('margin-left',(425-div_title_width/2)+'px');

		
		extra_height = 0;
		if (box_width<box_min_width) {
			$('div.title',tabContainers.filter(this.hash)).css('margin-top','40px');
			extra_height = 40;
		}
		box_height = $('.main-img img',tabContainers.filter(this.hash)).height();
		//a;ert(box_height);
		img_top_margin = 0;
		if (box_height<510) {
			img_top_margin = (480-box_height-12)/2;
			$('.main-img',tabContainers.filter(this.hash)).css('margin-top',img_top_margin+'px');
		}
		$('.main-img a.buy',tabContainers.filter(this.hash)).css('top',(img_top_margin+box_height+6)+'px');
		$('.main-img a.sign',tabContainers.filter(this.hash)).css('top',(img_top_margin+box_height+15)+'px');
		var wide_column_margin_top = 50;
		
		total_height = box_height+extra_height+40;
		if (total_height>510) {
			new_margin = total_height-510 + wide_column_margin_top;
		} else new_margin = wide_column_margin_top;
		if (new_margin<wide_column_margin_top) new_margin = wide_column_margin_top;
		$('#wide_column').css('margin-top',new_margin+'px');
/*		height = $(tabContainers.filter(this.hash)).height();
		//alert(height);
		if (height<528) height = 528;
		height = height+50;
		alert(height);
		$('#wrapper').height(height+'px');*/

		slide_num = parseInt(this.hash.substr(4));
		//alert(slide_num);
		pause = true;
		$('#myslideshow li a').removeClass('hover');
		$(this).addClass('hover');
		return false;
	});

$("*[id^=buyart]").fancybox({'hideOnOverlayClick': true, 'frameWidth': 'auto', 'frameHeight': 'auto', 'opacity': true, 'scrolling': 'no', 'overlayOpacity': .6, 'transitionIn': 'fade', 'transitionOut': 'fade' }); 
});
