/* 	Flash Video Player + Controller
--------------------------------------*/
/* konsole is a safe wrapper for the Firebug console. */
var konsole = {
  log: function(args){},
  dir: function(args){}
};
// Remove below here when in production
if (typeof window.console != 'undefined' && typeof window.console.log == 'function') {
  konsole = window.console;
  konsole.log("konsole initialized");
}



function VideoController() {
	var self = this;
	this.currentURL = "";
	this.isPlayingAll = true;
	this.videoList = [];
	this.swfID =  (navigator.appName.indexOf("Microsoft") !== -1) ? 'FeelPlayerIE' : 'FeelPlayer';
	this.playAll_InActiveHTML = "PLAY ALL";
	this.playAll_ActiveHTML = "<a onclick=\"javascript:videoController.playAll(); return false;\" href=\"#\" class=\"playall\" title=\"PLAY ALL\"> &nbsp; </a>"; // <span>(<span>x</span>)</span> PLAY ALL
	this.flashSelector = ".FeelPlayer";
	
	this.init = function () {
		// select each carousel item
		$('#media_carousel li a').each( function () {
			self.videoList.push( $(this).attr('href'));
		} );
		
	};
	
	///// 
	///  Play All Button
	//

	this.playAll = function () {
		self.isPlayingAll = !self.isPlayingAll;
		if (self.isPlayingAll) { 
			$('#controls_left a').html(self.playAll_ActiveHTML);
			$(self.flashSelector).externalInterface({method:'loadVideoFile', args:self.videoList[0]});
		}	else {
			$('#controls_left a').html(self.playAll_InActiveHTML);
		}
	};
	
	///// 
	///  Play All Events (from flash buttons and flash finished playing)
	//
	this.playNext = function (curr) {
	
		var position = $.inArray(curr, self.videoList);
		
		if (position < 0) return;
		position++;
		
		if (position === (self.videoList.length)) { 
			if($.browser.webkit === true) { return; }
			position = 0; 
		}
		
		if(self.videoList[position]) 
			$(self.flashSelector).externalInterface({method:'loadVideoFile', args:self.videoList[position]});
	};
	
	this.playPrev = function (curr) {
		var position = $.inArray(curr, self.videoList);
		if (position < 0) return;
		position--;
		if (position < 0) position = self.videoList.length - 1;
		if (self.videoList[position])
			$(self.flashSelector).externalInterface({method:'loadVideoFile', args:self.videoList[position]});
	}
	
	///// 
	///  Flash Comms
	//
	
	this.openVideoSRC = function ( url ) {
	
		if (self.isPlayingAll == true) {
			self.isPlayingAll = false;
			$('#controls_left a').html(self.playAll_InActiveHTML);
		}
	 
		//$('#media_player').children.remove();
		$('#media_player').empty();
		
		this.currentURL = url;
	 
		var videoTemplate = "/video-player-template/";
		var replacementTag = '<param name="FlashVars" value="videoURL={url}" />';
		var link = replacementTag.replace(/{url}/ ,url);
		
		var html5 = '<video controls="" src="{url}" width="528" height="360"></video>'
		html5 = html5.replace(/{url}/ ,url);
		
	 
		$.ajax({ url: videoTemplate, success: function(data) {
	 		html = data.replace(/<cms place="video-url"><\/cms>/g, link);
	 		html = html.replace(/<cms place="video-html5" width="[0-9]*" height="[0-9]*"><\/cms>/g, html5);
	 		$('#media_player').html(html);
		}});
	}
}



var videoController = new VideoController();

// AS -> JS

function videoFinished(filename, afterTimeout) {
	if (!afterTimeout) {
  		setTimeout(function() { videoFinished(filename, true) }, 20);
 	} else {
 		if (videoController.isPlayingAll === true) videoController.playNext(filename);
 	}
}

function previousVideo(filename, afterTimeout) {
	if (!afterTimeout) {
  		setTimeout(function() { previousVideo(filename, true) }, 20);
 	} else {
		if (videoController.isPlayingAll === true) videoController.playPrev(filename);
	}
}

function nextVideo(filename, afterTimeout) {
	if (!afterTimeout) {
  		setTimeout(function() { nextVideo(filename, true) }, 20);
 	} else {
		if (videoController.isPlayingAll === true) videoController.playNext(filename);
	}

}

$(document).ready(function(){
/*
	var thisVersion = "1.11";
	
	if ($.browser.webkit) {
		$( 'title' ).html ( 'Feel Films (Beta ' + thisVersion + ' - ' + $.browser.version + ')'); 
	} else {
		$( 'title' ).html ( 'Feel Films (Beta ' + thisVersion + ')' ); 
	} */

	videoController.init();
	
	// Link Scrubber Script
	$("a").bind("focus",function(){
		if(this.blur) this.blur();
	});
	
	$('p').defuscate();
	
	// Gallery Cycle Code
	$("#main_image").cycle({ 
		fx:    'fade',
		speed: 800, // speed of the transition (any valid fx speed value)
		timeout: 3000, // milliseconds between slide transitions (0 to disable auto advance)
		pause:  0 
	});
	
	// Commercials Carousel
	if($("#media_carousel li").length > 1)
	{
    	$("#media_carousel div").carousel("#Previous", "#Next");
	}
	
	// Hover Image Fade In and Out
	$("#media_carousel ul li a img").hover(function(){
		$(this).fadeTo("fast", 0.7);
	},function(){
		$(this).fadeTo("slow", 1.0);
	});
	
	// Commercials Page Director Boxes
	var marginTop = "-110px"; // "-82px";
						   
	$(".dl_image_hover, .dl_image_hover span").fadeTo("fast", 0.0);
	
	$('.dl_image_hover').css({'margin-top' : marginTop });
	
	//$("#media_holder ul li").hover(function(){
	
	$("ul.directors_list li").hover(function(){
		$(".dl_image span", this).fadeTo("fast", 0.0);
		$(".dl_image_hover", this).fadeTo("slow", 0.7);
		$(".dl_image_hover span", this).fadeTo("fast", 1.0);
		},function(){
		$(".dl_image span", this).fadeTo("slow", 0.7);
		$(".dl_image_hover", this).fadeTo("fast", 0.0);
		$(".dl_image_hover span", this).fadeTo("fast", 0.0);
	});
	
	// Contents Page Work Boxes
	$(".wl_image span").css({backgroundColor: 'black', opacity: 0.7 });
	$(".wl_image_hover").css({backgroundColor: '#c61a1c', opacity: 0.7 });
	
	// Contents Page Work Boxes
	//var marginTop = "-110px";
						   
	$(".wl_image_hover, .wl_image_hover span").fadeTo("fast", 0.0);
	
	$('.wl_image_hover').css({'margin-top' : marginTop });
	
	$("#media_holder ul li").hover(function(){
		$(".wl_image span", this).fadeTo("fast", 0.0);
		$(".wl_image_hover", this).fadeTo("slow", 0.7);
		$(".wl_image_hover span", this).fadeTo("fast", 1.0);
		},function(){
		$(".wl_image span", this).fadeTo("slow", 0.7);
		$(".wl_image_hover", this).fadeTo("fast", 0.0);
		$(".wl_image_hover span", this).fadeTo("fast", 0.0);
	});
	
	// Features Carousel
	if($("#features_holder li").length > 3)
	{
    	$("#features_holder").carousel("#featuresPrevious", "#featuresNext");
	}

	// Feature Page Boxes
	$(".fl_image span").css({backgroundColor: 'black', opacity: 0.7 });
	$(".fl_image_hover").css({backgroundColor: '#c61a1c', opacity: 0.7 });
	
	// Features Page Boxes
	$(".fl_image_hover, .fl_image_hover span").fadeTo("fast", 0.0);
	
	$("#features_holder ul li").hover(function(){
		$(".fl_image span", this).fadeTo("fast", 0.0);
		$(".fl_image_hover", this).fadeTo("slow", 0.7);
		$(".fl_image_hover span", this).fadeTo("fast", 1.0);
		},function(){
		$(".fl_image span", this).fadeTo("slow", 0.7);
		$(".fl_image_hover", this).fadeTo("fast", 0.0);
		$(".fl_image_hover span", this).fadeTo("fast", 0.0);
	});
	
	// Features Gallery Page
	$("#media_carousel ul li a").click(function(){
		var largePath = $(this).attr("href");
		$("#featureLargeImg").attr({ src: largePath }).hide().fadeIn(500);
		return false;
	});
	
	
	// New Carousel
	$(document.body).ready(function(){
		var totalPages = $('ul.directors_list').size(),
			current = 0,
			speed = 400;
		
		$('#newsNext').click( function () {
			if (current < totalPages -1) {
				current ++;
			} 
			$('.news_carousel').scrollTo($('ul.directors_list:eq(' + current + ')'), speed);
			
			console.log(current);
		});
		
		$('#newsPrevious').click( function () {
			if (current > 0) {
				current--;
			} 
			$('.news_carousel').scrollTo($('ul.directors_list:eq(' + current + ')'), speed);	
			
			console.log(current);
		});
	});
	
});


/* Controllers */

function imageGalleryLoad( url ) {
	width = $('#feature-image').width();
	height = $('#feature-image').height();
	jitUrl = '/image/2/' + width + '/' + height + '/5/' + url; 
	$('#media_player').empty();
	$('#media_player').html('<img src=' + jitUrl + ' width="' + width + ' height="' + height + '" id="feature-image" alt="" class="" />');
}

