var imgDir = '/img/';
var slidesThumbs = new Array();
var params = '';
var slideThumbPath = '';
var slideFullSizePath = '';
var pathReal = '';
var thumbsPaneWidth = slidePanePadding = 0;

$(document).ready(function() {
	pathReal = (location.href.indexOf("www.")>-1) ? '/frontcache/history/history.php' : '/frontcache/history/history.php';
	initSlideGal();
	
	$('.spanlink').click(function(e) {
		lft = e.pageX+10>560 ? e.pageX-200 : e.pageX+200;
		tp = e.pageY-250;
		$('#peoplewin').css('top', tp).css('left', lft).show();
	});
	
	$('.himg img').mouseover(function(e) {
		if(!$('#tooltip:visible').length){
			lft = e.pageX+10>560 ? e.pageX-200 : e.pageX+10;
			tp = e.pageY-400;
			$('#tooltip img').attr('src', $(this).attr('bigimg'));

			params = $(this).attr('params').split(';');
			tipHTML  = "<img src='" + params[0] + "' style='float:left;margin:0;position:relative;left:" + params[4] + "px' alt='' title=''>";
			tipHTML += "<div style='color:#999;font-size:5em !important;font-weight:bold;position:relative;top:-10px;left:" + (params[4]*2) + "px;margin:0;'>";
			if (params[1]!=''){
				tipHTML += params[1] + "</div><div style='font-size:12px !important;color:#000;font-weight:bold;position:relative;top:-60px;left:" + params[5] + "px'>";
			} else{
				tipHTML += params[1] + "</div><div style='font-size:12px !important;color:#000;font-weight:bold;position:relative;top:10px;left:" + params[5] + "px'>";
			}
			tipHTML += params[2] + "<br /><span style='color:#000;font-size:12px !important;font-weight:normal !important;'>" + params[3] + "</span></div>";
		
			$('#tooltip #text').html(tipHTML);
			$('#tooltip').css('top', tp).css('left', lft).show();
		}
	});
	
	$('.himg img').mouseout(function() {
		$('#tooltip').hide();
		$('#tooltip img').attr('src', '/img/1px.gif');
	});
	
	$('#viewer_x').click(function(){
		$('#peoplewin').hide();
	});
		
});

function initSlideGal(){
	slideThumbPath = '/uploads/history/'+$('#winnerhistory').attr('season')+'/gallery/thumbs/';
	slideFullSizePath = '/uploads/history/'+$('#winnerhistory').attr('season')+'/gallery/';

	$.get(pathReal, { year: $('#winnerhistory').attr('season') }, function(data) {
		slidesThumbs = data.split(';');

		// for(i=0;i<5;i++){
			// $('.imgbox #img'+i).attr('src', slideThumbPath+slidesThumbs[i]);
		// }
		for(i=0;i<slidesThumbs.length;i++){
			$("#slideview .belt").append("<div class='imgbox' pos=0><img class='img0' src='"+slideThumbPath+slidesThumbs[i]+"' alt='' title='' relid='"+i+"'></div>");
			//$('.imgbox #img'+i).attr('src', slideThumbPath+slidesThumbs[i]);
		}

		$("#img0").parent().css('border', '1px solid #c03');
		viewSlide = slideFullSizePath + slidesThumbs[0];
		$('div.focusedimage img').attr('src', viewSlide);
		
		$('#slideview .belt').width(slidesThumbs.length*92);
		/*if(!$.browser.msie) {
			$('#slideview .belt').width( slidesThumbs.length*4 + 2 );

			total = 0; nn = 0;
			for(i=0;i<slidesThumbs.length;i++){
				$('.imgbox .img0').eq(i).load(function() {
					nn++;
					$('#slideview .belt').width( $('#slideview .belt').width() + $(this).width() );
					//if(nn == slidesThumbs.length) alert($('#slideview .belt').width());
				});	// alert($('#slideview .belt').width());
			}
		}*/
		$('#event_gal').show();
	
		// slidePanePadding = thumbsPaneWidth<401 ? ($('#slideview').width() - thumbsPaneWidth)/2 : 0;
		// $('.imgbox[pos=0]').css('margin-left', slidePanePadding);
		
	});

	$('.imgbox img').live('click', function(){
		viewSlide = slideFullSizePath + slidesThumbs[$(this).attr('relid')];
		$('.focusedimage img').attr('src', viewSlide);
		$('.imgbox').css('border', 'none');
		$(this).parent().css('border', '1px solid #c03');
	});

		var carouselStep = 90;
		var currPos = 0;
		
		$('#prev').live('click', function(){
			endPos = 0;
			
			newPos = currPos+carouselStep;
			if(newPos > 0) newPos = endPos;
			$('#slideview .belt').animate({marginLeft: newPos}, 300);
			currPos = newPos;

			if(currPos == 0)
				$('#prev img').attr('src', imgDir+'leftarrow_idle.gif');
			
			if(currPos != 0)
				$('#next img').attr('src', imgDir+'rightarrow.gif');
			
		});
		
		$('#next').live('click', function(){
			endPos = $('#slideview .belt').width()*-1 + 490
			
			newPos = currPos-carouselStep;
			if(newPos-490 < $('#slideview .belt').width()*-1) newPos = endPos;
			$('#slideview .belt').animate({marginLeft: newPos}, 300);
			currPos = newPos;
			
			// thumbsPaneWidth = 0;
			// mover = parseInt($('#loc_step').val())+1<slidesThumbs.length ? parseInt($('#loc_step').val())+1 : slidesThumbs.length-1;
			// if(mover==slidesThumbs.length-1){
				// $('#next').css('cursor', 'default');
				// $('#next').attr('src', imgDir+'leftarrow_idle.gif');
			// }
			if(currPos == endPos)
				$('#next img').attr('src', imgDir+'rightarrow_idle.gif');
				
			if(currPos != 0)
				$('#prev img').attr('src', imgDir+'leftarrow.gif');
			
			// $('#loc_step').val(mover);
			// $('.imgbox #img0').attr('relid', mover-4);
			// $('.imgbox #img0').attr('src', slideThumbPath+slidesThumbs[mover-4]);
			// $('.imgbox #img1').attr('relid', mover-3);
			// $('.imgbox #img1').attr('src', slideThumbPath+slidesThumbs[mover-3]);
			// $('.imgbox #img2').attr('relid', mover-2);
			// $('.imgbox #img2').attr('src', slideThumbPath+slidesThumbs[mover-2]);
			// $('.imgbox #img3').attr('relid', mover-1);
			// $('.imgbox #img3').attr('src', slideThumbPath+slidesThumbs[mover-1]);
			// $('.imgbox #img4').attr('relid', mover);
			// $('.imgbox #img4').attr('src', slideThumbPath+slidesThumbs[mover]);
			
			// for(i=0;i<5;i++){
				// thumbsPaneWidth += $('.imgbox #img'+i).width();
			// }
			
			// slidePanePadding = thumbsPaneWidth<401 ? ($('#slideview').width() - thumbsPaneWidth)/2 : 0;
			
			// $('.imgbox[pos=0]').css('margin-left', slidePanePadding);
			
		});

}