// ----------------------------- //
//   JUDGEGILL JAVASCRIPT        //
//   BY KEVIN WILDE              //
//   START CREATIVE LTD          //
//   kevin@startcreative.co.uk   //
//   DATE: 05-10-2009            //
// ----------------------------- //

var ul = $('#vid-carousel ul');
var li = ul.children('li');
var reset = 0;
var time = 6578;
var margin;

function scrollAni(x){
	var width = x.width();
	var thisMargin = parseInt(x.css('margin-right'));
	var pos = parseInt(x.css('left'));
	x.animate({'left':(pos-(width+thisMargin))+reset},time,'linear',function(){
		var currentPos = parseInt(x.css('left'));
		if(currentPos<=100){
			x.css({'left':'1638px'});
		};
		reset = 0;
		time = 6578;
		scrollAni(x);
	});
}

function carouselPos(){
	li.each(function(){
		var pos = parseInt($(this).css('left'));
		if(pos<273){
			time = (6578/273)*pos;
			reset = (pos-273)* -1;
		}
	});
}

function removeBlock(){
	$('#vid-blocker').css({'display':'none'});
}

function vidScroller(){
	var clicked = false;	
	var imgWidth = 0;
	var rMargin = 0;
	var actImgWidth = 0;
	var finalPos = 0;	
	
	//setup elements for animation
	for(var i = 0; i < li.length; i++){
		//hard coded number (sorry), to meny bugs
		finalPos += 273;
		li.eq(i).css({'position':'absolute','left':finalPos});
	}
	
	ul.css({'margin-left':'-273px'});
	
	//animate elements
	li.each(function() {
		scrollAni($(this));
	});
	
	setTimeout('removeBlock()',3000);
	
	$('#vid-carousel').hover(
		function(){
			li.stop();
			carouselPos();
		},
		function(){
			if(!clicked){
				li.each(function(){
					scrollAni($(this));
					});
			}else{
				li.stop();
			}
		}
	);
	
	var image = $('img#vid-cell');
	var offset;	
	
	$('#vid-carousel a').click(function(){
		var pos = $(this);
		var source = pos.attr('href');
		//eval("var video_object = {flv:'assets/files/SanDisk01.flv',swf:''};")
		eval(pos.attr('rel'));
		var video = '';
		var swfpath = '';
		if(video_object.flv != undefined && video_object.flv != '') {
			vr = video_object.flv.split("/");
			video = vr[2];
		}
		if(video_object.swf != undefined && video_object.swf != '') {
			sr = video_object.swf.split("/");
			swfpath = sr[2];
		}
		//alert('swf ' +swf+ ' flv:'+video);
		li.stop();
		$('#overlay').css({'display':'block'});
		clicked = true;
		offset = pos.offset();
		image.attr('src',source).load(function(){
			image.css({'left':offset.left,'top':offset.top,'z-index':'3000'}).stop().animate({ 
				width:"676px",
				height:"435px",
				marginLeft:((offset.left - 290) * -1)+"px",
				marginTop:((offset.top - 190) * -1)+"px"
				},1000,function(){
					$('#image-frame').find('a.close').removeClass('close').addClass('vid-close');
					
					$('#image-frame a.vid-close').click(function(){
						$('#flashContent').css({'display':'none'});
						$('#overlay').animate({'opacity':'0'},500);
						$(this).parent().fadeOut(500,function(){
							image.stop().animate({ 
								width:"265px",
								height:"170px",
								marginLeft:"0",
								marginTop:"0"
								},1000,function(){
									$('#image-frame').find('a.vid-close').removeClass('vid-close').addClass('close');
									$('#image-frame a').unbind();
									image.css({'display':'none','z-index':'1'}).attr('src','assets/images/common/empty.gif');
									clicked = false;
									$('#overlay').css({'display':'none'});
									li.each(function(){scrollAni($(this))});
								}
							);
						})
						return false;
					});
					
					$('#image-frame').fadeIn(500);
					$('#overlay').animate({'opacity':'0.5'},500,function(){
						$('#flashContent').css({'display':'block'});
						playVideo({flv:video, swf:swfpath});
						image.unbind();
					});
				}
			);
		});
		return false;
	});
}

function imageScrollWidth(){
	var car = $('#image-carousel .holder');
	var n = car.find('.store-info').length;
	var i = car.find('li').length;
	var w = car.find('img').width();
	car.width(((n+i)*w)+((n+i)*4));
}

function imageEnlarge(){	
	var image = $('img#image-cell');
	var offset;	
	
	$('#image-carousel a').click(function(){
		var pos = $(this);
		var picLoc = pos.parent().parent();
		var picCount = picLoc.children().length;
		var source = pos.attr('href');
		var viewer = $('#image-veiwer');
		var imagePos = pos.attr('title');
		var imageNum = imagePos.split('0');
		
		li.stop();
		carouselPos();
		
		//create amount of anchor tags needed for image set
		for(i=1;i<=picCount;i++){
			var picPath = picLoc.find('li').eq(i-1).find('a').attr('href');
			$('#image-frame').find('p').append('<a href="'+picPath+'">'+i+' </a>');
		}
		
		//highlight number for relevent clicked picture
		$('p.pagination a').eq(imageNum[1]-1).addClass('selected');
		
		//add click functions too anchor tags created above
		$('p.pagination a').click(function(){
			var picPath = $(this).attr('href');
			$('#image-frame .block').css({'display':'block'});
			$(this).parent().find('a.selected').removeClass('selected');
			$(this).addClass('selected');
			$('#image-veiwer').append('<img width="676" height="435" src="'+picPath+'" />');
			$('#image-veiwer img').load(function(){
				$('#image-veiwer').find('img').eq(0).fadeOut(500,function(){
					$('#image-frame .block').css({'display':'none'});
					$('#image-veiwer').find('img').eq(1).css({'z-index':'11000'});
					$(this).remove();
				});
			});
				
			return false;
		});
		
		$('#overlay').css({'display':'block'});
		offset = pos.offset();
		image.attr('src',source).load(function(){
			image.css({'left':offset.left,'top':offset.top,'z-index':'3000'}).stop().animate({ 
				width:"676px",
				height:"435px",
				marginLeft:((offset.left - 290) * -1)+"px",
				marginTop:((offset.top - 190) * -1)+"px"
				},1000,function(){
					$('#image-veiwer').append('<img width="676" height="435" src="'+source+'" />');
					$('#image-veiwer').find('img').css({'z-index':'11000'});
					$('#image-frame').fadeIn(500);
					$('#image-frame').find('a.close').removeClass('close').addClass('image-close');
					
					$('#image-frame a.image-close').click(function(){
						$('#image-veiwer img').fadeOut(500);
						$('#overlay').animate({'opacity':'0'},500);
						$(this).parent().fadeOut(500,function(){
							image.stop().animate({ 
								width:"120px",
								height:"77px",
								marginLeft:"0",
								marginTop:"0"
								},1000,function(){
									$('#image-veiwer').empty();
									$('#image-frame').find('p').html('');
									$('#image-frame').find('a.image-close').removeClass('image-close').addClass('close');
									$('#image-frame a').unbind();
									image.css({'display':'none','z-index':'1'}).attr('src','assets/images/common/empty.gif');
									$('#overlay').css({'display':'none'});									
									li.each(function(){scrollAni($(this))});
								}
							);
						})
						return false;
					});
					
					$('#overlay').animate({'opacity':'0.5'},500);
					image.unbind();
				}
			);
		});
		return false;
	});
	
}

function handleSliderSlide(e, ui){
	var maxScroll = $(".window").attr("scrollWidth") - $(".window").width();
	$(".window").attr({scrollLeft: ui.value * (maxScroll / 100) });
}

function playVideo(videoPathObject){
	var videoPath = videoPathObject.flv != undefined ? videoPathObject.flv : null;
	var flashvars = videoPath !=null ? {FLV_PATH: "../files/"+videoPath} : {};
	var params = {wmode: "transparent"};
	var swfpath = videoPathObject.swf != undefined && videoPath == '' ? videoPathObject.swf : "assets/swf/jg_flv_player.swf";
	swfobject.embedSWF(swfpath, "flvPlayer", "676", "435", "9.0.0", "assets/swf/expressInstall.swf",flashvars, params);
}

$(document).ready(function(){
	vidScroller();
	imageScrollWidth();
	imageEnlarge();
	
	$('#scroll-bar .bar').slider({
		animate:true,
		step:0.001,
		slide:handleSliderSlide
	});
});