// Sliders options:
var SLIDERS_DELAY = -1000;
var SLIDERS_TIMEOUT = 8000;
var SLIDERS_SPEED = 2000;

var is_video_init = false; // Video load or not
var active_slider = null; // Video slider


$(document).ready(function(){
	initSlider();
	showHighlight();
	ajustSlidedeck();
	captionsPanel();
	rolloverImg();
	initPhoto();
	initCustomLog();
});

function initPhoto() {
	$("a[rel^='prettyPhoto']").prettyPhoto({
		theme: 'dark_square',
		show_title: false 

	});
	$('.columnize').columnize();	
}

function showHighlight() {
	if ($('#highlight').size()){
		$('#highlight').cycle({ 
			delay:		SLIDERS_DELAY,
			timeout:       	SLIDERS_TIMEOUT,  
			speed:         	SLIDERS_SPEED,
			after: function(currSlideElement, nextSlideElement, options, forwardFlag){
				sliderAfter(currSlideElement, nextSlideElement, options, forwardFlag, $('#highlight'), true);
			}
		});
	}
	
	if ($('#studio-slider').size()){
		$('#studio-slider').cycle({ 
			delay:		SLIDERS_DELAY,
			timeout:       	SLIDERS_TIMEOUT,  
			speed:         	SLIDERS_SPEED,
			after: function(currSlideElement, nextSlideElement, options, forwardFlag){
				sliderAfter(currSlideElement, nextSlideElement, options, forwardFlag, $('#studio-slider'), false);
			},
			next: '.studio-slider-wrapper .slider-control-right',
			prev: '.studio-slider-wrapper .slider-control-left'
		});
	}
	$('.studio-slider-wrapper .slider-control-right,.studio-slider-wrapper .slider-control-left').click(function(){
		if ($('#studio-slider').size()){
			$('#studio-slider').cycle('resume');
		}
	})
}

function sliderAfter(currSlideElement, nextSlideElement, options, forwardFlag, Slider, is_resize_parent){
	if (!Slider.size()){
		return;
	}
	
	$(currSlideElement).removeClass('activeSlide');
	$(nextSlideElement).addClass('activeSlide');
	
	var new_height = $(nextSlideElement).height();
	var old_height = $(currSlideElement).height();

	if ($(nextSlideElement).find('.studioplayer').size()) {
		Slider.cycle('pause');

		// check if embed video loaded now or not
		if (!is_video_init && (null === active_slider)){
			//console.log('not init!!!!');
			var next_ind = 0;
			Slider.find('.panel').each(function(ind, obj){
				if ($(obj).hasClass('activeSlide')){
					next_ind = ind;
				}
			});
			active_slider = {Slider: Slider, isResizeParent: is_resize_parent, next_ind: next_ind};
		}else{
			//console.log('playing!!!!');
			playStudioSliderVideo(Slider, currSlideElement, nextSlideElement, is_resize_parent);
		}
	}
	else {
		options.timeout = 8000;

		new_height = $(nextSlideElement).find('img').height();
		if (!new_height){
			new_height = $(nextSlideElement).height();
		}
		$(nextSlideElement).height(new_height + 'px');
		if(new_height - old_height) {
			resizeSliderHolders(new_height, Slider, is_resize_parent);
		}
	}
}



function initSlider(){
	if (!$('a.vimeo').length){
		return false;
	}
	
	$('a.vimeo').append('<img style="display:none;" src="http://www.agencetuxedo.com/wp/wp-content/themes/tuxedo/images/play.png" class="PLAY" alt="play" />');
	$('a.vimeo,img.PLAY').hover(function(){
			$(this).find('img.PLAY').show();
		},
		function(){
			$(this).find('img.PLAY').hide();
		}
	)
};

function ajustSlidedeck() {
	if (!$('dl.slidedeck').length){
		return false;
	}

	$('dl.slidedeck dd').each(function(){
		if($(this).children('.slidesVertical').size()==0){
			alignVertical($(this));
		}
		else {
			alignVertical($(this).find('.slidesVertical li'));
		}
	}) 
	
}
function alignVertical(container){
	var innerContent='<div class="vertilign">'+container.html()+'</div>';
	container.html(innerContent);
	$(container).find('.vertilign').css({'overflow':'hidden'});
	var offset=($(container).height()-$(container).find('.vertilign').outerHeight())/2;
	$(container).find('.vertilign').css('marginTop',offset+'px')
}
function captionsPanel(){
	$('.imgCaption').slideUp(120);
	$('#highlight').hover(
		function() { $('.imgCaption').slideDown(150); },
		function() { $('.imgCaption').slideUp(150);
	});
}
function rolloverImg(){
	
	//---------------------------- rollover images
	$('img.rl').mouseover(function(){ this.src = this.src.replace("_up","_over"); })
				.mouseout(function(){ this.src = this.src.replace("_over","_up");});
	
	$('img.loc_active').mouseover(function(){ this.src = this.src.replace("_active","_over"); })
				.mouseout(function(){ this.src = this.src.replace("_over","_active");});
	
	$('img.loc_passive').mouseover(function(){ this.src = this.src.replace("_passive","_over"); })
				.mouseout(function(){ this.src = this.src.replace("_over","_passive");});
	
	$('.post-ratings img').click(function(){
		$('div.ratingTitre div#scored').fadeOut('slow');
		$('div.ratingTitre div#scored').text((parseInt($('div.ratingTitre div#scored').text())+1));
		$('div.ratingTitre div#scored').fadeIn(3000);
	});
}


function alertStatus(e) {
        alert("e.success = " + e.success +"\ne.id = "+ e.id +"\ne.ref = "+ e.ref);
}





// Custom log window for IE8 dubuging
var MyLog;
var MyLogHTML = '<div id="custom_log" style="position: fixed; top: 0; left: 0; height: 150px; width: 1000px; opacity: 0.9; background: #FFFFFF; overflow: auto;"></div>';
function initCustomLog(){
	$(document).ready(function(){
		// init custom log panel
		$('body').append(MyLogHTML);
		MyLog = $('#custom_log');
		MyLog.hide();
	});
}
function mylog(str){
	MyLog.append('<p>' + str + '</p>');
}
function mylogClear(){
	MyLog.empty();
}
