/* Author:
Declan boylan
*/

		
	$(document).ready(function(){ 

		$("li.australia-btn").hover( function(){
				$(".bg").hide();
        		$(".australia").show();
        		
		});

		$("li.ireland-btn").hover( function(){
				$(".bg").hide();
        		$(".ireland").show();
		});

		$("li.usa-btn").hover( function(){
        		$(".bg").hide();
        		$(".usa").show();
		});	
		
			
		
		$("#video-list ul li").not('.active').stop().animate({opacity: "0.5"}, 'fast');
		$("#video-list ul li").not('.active').hover(function() {
			 $(this).stop().animate({opacity: "1"}, 'fast');
		},
		 function() {
			$(this).stop().animate({opacity: "0.5"}, 'fast');
		});
		
			 var $allVideos = $("iframe[src^='http://player.vimeo.com'], iframe[src^='http://www.youtube.com'], object, embed"),
			 	$fluidEl = $("#video-player");
	    	
			 	$allVideos.each(function() {
			 		$(this)
			 		// jQuery .data does not work on object/embed elements
			 		.attr('data-aspectRatio', (this.height) / this.width)
			 		.removeAttr('height')
			 		.removeAttr('width');
	
			 	});
	
			 	$(window).resize(function() {
	
			 		var newWidth = $fluidEl.width();
			 		var newHeight = $fluidEl.height();
			 		$allVideos.each(function() {
	  
			 			var $el = $(this);

			 			$el
			 			.width(newWidth)
			 			.height( newHeight - 50) ;
		//	 			.height( ( (newWidth) * $el.attr('data-aspectRatio'))-20 ) ;
			 		});
			 	}).resize();
		});
	
	// preload images
	var cache = [];
  	// Arguments are image paths relative to the current page.
  	$.preLoadImages = function() {
    	var args_len = arguments.length;
    	for (var i = args_len; i--;) {
      		var cacheImage = document.createElement('img');
      		cacheImage.src = arguments[i];
      		cache.push(cacheImage);
    	}
  	};
  	$.preLoadImages("http://cooltv.ebowdev.com/assets/img/australia.jpg", "http://cooltv.ebowdev.com/assets/img/ireland.jpg" , "http://cooltv.ebowdev.com/assets/img/usa.jpg");
		
	//set default view mode
	$defaultViewMode="full"; //full (fullscreen background), fit (fit to window), original (no scale)
	//cache vars
	$bg=$("#video-player");
	$bgimg=$("#video-player #bgimg");
	$preloader=$("#preloader");
	$outer_container=$("#outer_container");
	$outer_container_a=$("#outer_container a.thumb_link");
	$toolbar=$("#toolbar");
	//$nextimage_tip=$("#nextimage_tip");
	
$(window).load(function() {
	$toolbar.data("imageViewMode",$defaultViewMode); //default view mode
	ImageViewMode($toolbar.data("imageViewMode"));
	//$('#video-list').tinyscrollbar(); 
	//$('.scrollbar').scrollbar();
	//cache vars
	outerMargin=100;
	innerMargin=120;
	//$customScrollBox.height($(window).height()-outerMargin);
	//$('#customScrollBox').tinyscrollbar(); 
	$('#video-list').height($(window).height()-outerMargin);
	//alert(height);
	//$('.videolist').css("height",height);
	//$('.scrollbar').scrollbar();
	$('#video-list').jScrollPane();
	/*
	$customScrollBox=$("#customScrollBox");
	CustomScroller();
	function CustomScroller() {
		outerMargin=100;
		innerMargin=120;
		$customScrollBox.height($(window).height()-outerMargin);
		$('#customScrollBox').tinyscrollbar(); 
	}
	*/
	//resize browser window functions
	/*
	$(window).resize(function() {
		FullScreenBackground("#bgimg"); //scale bg image
		$dragger.css("top",0); //reset content scroll
		CustomScroller();
	});
	*/
});
//Image scale function
function FullScreenBackground(theItem){
	var winWidth=$(window).width();
	var winHeight=$(window).height();
	var imageWidth=$(theItem).width();
	var imageHeight=$(theItem).height();
	if($toolbar.data("imageViewMode")!="original"){ //scale
		$(theItem).removeClass("with_border").removeClass("with_shadow"); //remove extra styles of original view mode
		var picHeight = imageHeight / imageWidth;
		var picWidth = imageWidth / imageHeight;
		if($toolbar.data("imageViewMode")!="fit"){ //image view mode: full
			if ((winHeight / winWidth) < picHeight) {
				$(theItem).css("width",winWidth).css("height",picHeight*winWidth);
			} else {
				$(theItem).css("height",winHeight).css("width",picWidth*winHeight);
			};
		} else { //image view mode: fit
			if ((winHeight / winWidth) > picHeight) {
				$(theItem).css("width",winWidth).css("height",picHeight*winWidth);
			} else {
				$(theItem).css("height",winHeight).css("width",picWidth*winHeight);
			};
		}
		//center it
		$(theItem).css("margin-left",((winWidth - $(theItem).width())/2)).css("margin-top",((winHeight - $(theItem).height())/2));
	} else { //no scale
		//add extra styles for original view mode
		$(theItem).addClass("with_border").addClass("with_shadow");
		//set original dimensions
		$(theItem).css("width",$bg.data("originalImageWidth")).css("height",$bg.data("originalImageHeight"));
		//center it
		$(theItem).css("margin-left",((winWidth-$(theItem).outerWidth())/2)).css("margin-top",((winHeight-$(theItem).outerHeight())/2));
	}
}

//image view mode function - full or fit
function ImageViewMode(theMode){
	$toolbar.data("imageViewMode", theMode); //store new mode
	FullScreenBackground($bgimg); //scale bg image
	//re-style button
	if(theMode=="full"){
		$toolbar.html("<span class='lightgrey'>IMAGE VIEW MODE &rsaquo;</span> FULL");
	} else if(theMode=="fit") {
		$toolbar.html("<span class='lightgrey'>IMAGE VIEW MODE &rsaquo;</span> FIT");
	} else {
		$toolbar.html("<span class='lightgrey'>IMAGE VIEW MODE &rsaquo;</span> ORIGINAL");
	}
}
//preload script images
var images=["ajax-loader_dark.gif","http://cooltv.ebowdev.com/assets/img/round_custom_scrollbar_bg_over.png"];
$.each(images, function(i) {
  images[i] = new Image();
  images[i].src = this;
});
