var t;
var time = 10000; //10 seconds between each slide
/**
* Called when actually sliding to a slide
*/
function slideToSlide(newSlide) {
	clearInterval(t);
	$('#slideText').fadeOut();
	$('#playButton').fadeOut();
	var newLeft = newSlide * $('#sliderViewport').width();
	$('#slider').stop().animate({
		left: '-'+newLeft+'px'
	}, 'slow', function() {
		t = setInterval(function(){nextSlide();}, time);
		var theSlide = $('.slide').eq(newSlide);
		$('.visibleSlide').removeClass('visibleSlide');
		theSlide.addClass('visibleSlide');
		$('#slideText').html(theSlide.children('p').html());
		$('#slideText').fadeIn();
		$('#playButton').fadeIn();
	});
	$('#slider').data('curSlide', newSlide);
}

/**
* Called when automatically rotating the images
*/
function nextSlide() {
	var nextSlide = $('#slider').data('curSlide') + 1;
	var maxSlides = $('#pager a').length;
	if (nextSlide >= maxSlides) {
		nextSlide = 0;
	}
	
	compressViewport('slideToSlide('+nextSlide+');');
	
	$('.activeSlide', $('#slideControls')).removeClass('activeSlide');
	$('#pager a').eq(nextSlide).addClass('activeSlide');
}

function prevSlide() {
	var prevSlide = $('#slider').data('curSlide') - 1;
	var lastSlide = $('#pager a').length-1;
	if (prevSlide < 0) {
		prevSlide = lastSlide;
	}
	compressViewport('slideToSlide('+prevSlide+');');

	$('.activeSlide', $('#slideControls')).removeClass('activeSlide');
	$('#pager a').eq(prevSlide).addClass('activeSlide');
}

function expandViewport() {
	if ($('#sliderViewport').height() != 360) {
		$('#slideControls').fadeOut();
		clearInterval(t); //stop the timeout when expanded
		
		//fade in to black at the same time
		$('#sliderViewport .visibleSlide').stop().animate({
			opacity: 0
		},550);
		$('#sliderViewport').stop().animate({
			height: '360px'
		}, 'slow', function() {
			//$('#slideControls').fadeIn(); //temporary - will proabbly need to fade in the video controls here, seemlessly, somehow...
			
			// opacity back to 1 after fadeOut
			$('#sliderViewport').data('posterFrame', $('#sliderViewport .visibleSlide').css('background-image'));
			$('#sliderViewport .visibleSlide').css({'opacity':1,'background':'transparent'});
			
			$('.visibleSlide').append('<div id="videoReplace"></div>');
			var config = {
				"settings": {
					"forceflash" : true,
					"flash": {
						"swfUrl": "/flash/VideoPlayer.swf",
						"id": "videoReplace",
						"width": "640",
						"height": "360",
						"version": "9.0.0",
						"expressInstallSwfurl": "/flash/expressInstall.swf",
						"flashvars": {
							"type": "file", 
							"playlist":  "/playlist/"+$('.visibleSlide').attr('rel')+""
						},
						"params": {
							"wmode" : "transparent",
							"allowfullscreen": "false", 
							"allowscriptaccess": "always"
						},
						"attributes": {}
					}
				}
			}			
			Landlord.loadVideoPlayer(config);
		});
		$('#video-banner').stop().animate({
			height: '+='+140+'px'
		},'slow');
	}
}
function compressViewport(callback) {
	if ($('#sliderViewport').height() != 220) {
		$('#sliderViewport').stop().animate({
			height: '220px'
		}, 650, function() {
			eval(callback);
			t = setInterval('nextSlide()', time); //start the timeout again
		});
	}
	else {
		eval(callback);
	}
}

// flash event
function stopButtonClick() {
	//check pop up
	if ($('#popup-holder:visible').length > 0){removePopup();}
	else{
		$('#sliderViewport .visibleSlide').css({'opacity':0});
		$('#videoReplace').remove(); //need to remove the video
		/* console.log($('#sliderViewport').data('posterFrame')); */
		$('#sliderViewport .visibleSlide').stop().animate({opacity:1},1000).css({'background-image':$('#sliderViewport').data('posterFrame')});
		setTimeout(function(){compressViewport('$("#slideControls").fadeIn();')},100);	
	}	
}
// flash event
function captionButtonClick(captionID){
	//check pop up
	if($('#popup-holder:visible').length > 0) $('#popup-holder .popup_caption').fadeIn();
	else {
		$('#sliderViewport .visibleSlide').children('.caption').fadeIn();
		// workign together videos
		$('.still-video-holder:eq('+(parseInt(captionID) - 1)+') .caption').fadeIn();
	}
}

function videoFinished() {
	//check pop up
	if($('#videoReplace').length > 0)removePopup();
	stopButtonClick();
}
//for pop up video
function removePopup(){
	$('#popup-holder').fadeOut();
	$('#videoReplace').remove();
	$('#fade').fadeOut(600).remove();
}

$(function(){

	/* initialize */
	var numOfVideos = $('#sliderViewport .slide').length;
	/* console.log(numOfVideos); */
	//pager  no dot or navi if only one video
	$('#slideText').html($('#sliderViewport .slide:first').children('p').html());
	if (numOfVideos > 1) {
		for (i=1;i<=numOfVideos;i++)
		{$('#pager').append('<li class="left"><a class="jsButton">'+i+'</a></li>');}
		$('#pager li:first a').addClass('activeSlide');	
		$('#slideControls .nav').prepend('<a id="nextButton" class="jsButton right">NEXT</a><a id="prevButton" class="jsButton right">PREV</a>');	
		t = window.setInterval(function(){nextSlide();}, time);
	}
	
	$('#slider').data('curSlide', 0); //set the startng value
	//manual changing
	$('#pager a').bind('click', function() {
		if (!$(this).is('.activeSlide')) {
			
			compressViewport('slideToSlide('+$(this).parent().index()+');');
			
			$('.activeSlide', $('#slideControls')).removeClass('activeSlide');
			$(this).addClass('activeSlide');
		}
	});
	
	$('#nextButton').bind('click', function() {
		nextSlide();
	});
	$('#prevButton').bind('click', function() {
		prevSlide();
	});
	$('#playButton').bind('click', function() {
		expandViewport();
	});
	$('.caption .closeBtn').bind('click', function() {
		
		if($(this).parent().parent().parent().hasClass('still-video-holder')) { //work together videos
			$(this).parent().parent().fadeOut();
			var id = $(this).parent().parent().siblings('object').attr('id');
			if(typeof(id)!= 'undefined') sendMessage(id, 'playVideo', "{}");
		} 
		else {
			$('.caption:visible').fadeOut();
			sendMessage('videoReplace', 'playVideo', "{}");
		}
		return false;
	});
	
});
