// contenuto inserito all'interno dell'overlay dei video
var socialContent = '<iframe src="//www.facebook.com/plugins/like.php?href={_FBURL_}&amp;send=false&amp;layout=button_count&amp;width=110&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;font&amp;height=25" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:110px; height:25px;float:left" allowTransparency="true"></iframe><div class="tooltip" title="Questo link ti porterà ad un sito per il quale non è applicabile la privacy policy di momentimagici.com.<br/>Ti incoraggiamo a verificare la privacy policy di ciascun sito che visiti."><a href="https://twitter.com/share" class="twitter-share-button" data-text="Johnson\'s® Baby - {_TWITTERTEXT_}" data-count="horizontal">Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script></div>';

var $selectedHomeThumb;
var $selectedHomeSlide;

// tooltip
function tooltip() {	
	/* CONFIG */		
		xOffset = 10;
		yOffset = 20;		
		// these 2 variable determine popup's distance from the cursor
		// you might want to adjust to get the right result		
	/* END CONFIG */		
	$(".tooltip").hover(function(e){											  
		this.t = this.title;
		this.title = "";									  
		$("body").append("<p id='tooltip'>"+ this.t +"</p>");
		$("#tooltip")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px")
			.fadeIn("fast");		
    },
	function(){
		this.title = this.t;		
		$("#tooltip").remove();
    });	
	$(".tooltip").mousemove(function(e){
		$("#tooltip")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px");
	});			
}
// end tooltip



// starting the script on page load

$('document').ready(function() {
	
	// abilita il codice solo su pagina video
	//if ($('.video-container').length > 0) {
		$('.vid-img a, .vid-img-home a').click(function(event) {
			event.preventDefault();
			openVideo($(this));
			setupOverlaySocialButton($(this).attr('rel'));
			return false;
		});
	//}
	
	// se è in homepage abilita la navigazione della homepage
	if ($('#home-slideshow').length > 0) {
		
		// registra i valori di default
		$selectedHomeThumb = $($('#slider ul.thumbs li')[0]);
		$selectedHomeSlide = $($('.home-slideshow-item')[0]);
		
		// abilita il click sulle thumb
		$('#slider ul.thumbs li a').click(function(event) {
			event.preventDefault();
			updateSliderImg($(this).parent());		// chiama la funzione di cambio immagine passando come parametro il <li> selezionato
			return false;
		});
		
		
		// avvia il timer per il cambio automatico di immagini
		setInterval( "slideSwitch()", 10000 );
	}
	
});


//////////////////////////////////
//			HOMEPAGE			//
//////////////////////////////////

// seleziona la thumb e immagine relativa
function updateSliderImg($item) {
	$selectedHomeThumb = $item;
	
	// aggiorna le thumb
	$('#slider ul.thumbs li.active').removeClass('active');
	$selectedHomeThumb.addClass('active');
	
	// aggiorna l'img grande
	$selectedHomeSlide.hide();
	$selectedHomeSlide = $($('.home-slideshow-item')[$item.index()]);
	$selectedHomeSlide.show();
}

// cambia automaticamente la selezione dell'immagine in home
function slideSwitch() {
	if ($selectedHomeThumb.index() == $('#slider ul.thumbs li').length - 1)
		updateSliderImg($($('#slider ul.thumbs li')[0]));
	else
		updateSliderImg($($('#slider ul.thumbs li')[$selectedHomeThumb.index() + 1]));
}

//////////////////////////////////
//			   VIDEO			//
//////////////////////////////////

// aggiunge i pulsanti social sotto il videoplayer
function setupOverlaySocialButton(_text) {
	// grab delle informazioni
	var itemRel = _text.split(';');
	var _hash = itemRel[2].substring(itemRel[2].indexOf('=') + 1, itemRel[2].length);
	var _title = itemRel[3].substring(itemRel[3].indexOf('=') + 1, itemRel[3].length);
	
	// window.location.hash = _hash;
	window.location.hash = "!" + _hash;
	
	// aggiorna l'hash e la url da passare a facebook
	//var _url_ = window.location.href.split('/vid#');
	var _url_ = window.location.href.split('/!');
	var fbUrl = encodeURIComponent(_url_[0]) + '/' + _hash;
	var shareHtml = socialContent;
	shareHtml = shareHtml.replace('{_FBURL_}', fbUrl);
	shareHtml = shareHtml.replace('{_TWITTERTEXT_}', _title);
	
	// renderizza i pulsanti
	$('#sb-social').html(shareHtml);
	tooltip();
}

function openVideo(objLink){
	var $elem = objLink.attr('rel');
	if(!$elem) return false;
	var elemInfo = $elem.split(';');
	var elemWidth 	= elemInfo[0].substring(elemInfo[0].indexOf('=') + 1, elemInfo[0].length);
	var elemHeight 	= elemInfo[1].substring(elemInfo[1].indexOf('=') + 1, elemInfo[1].length);
	var title		= elemInfo[3].substring(elemInfo[3].indexOf('=') + 1, elemInfo[3].length);
	var poster 		= elemInfo[4].substring(elemInfo[4].indexOf('=') + 1, elemInfo[4].length);
	var video 		= elemInfo[5].substring(elemInfo[5].indexOf('=') + 1, elemInfo[5].length);

	Shadowbox.open({
		content:    '/html/singlevideo.php?source=' + video + '&width=' + elemWidth + '&height=' + elemHeight + '&poster=' + poster,
		player:     "iframe",
		title:      title,
		height:     elemHeight,
		width:      elemWidth
	});
}

