////////////////////////////////////////
//COOLSLIDER
////////////////////////////////////////
(function($){
	$.fn.coolSlider = function() {
		
		var base = this;
		var $parent = jQuery(this);
		var $liste = $parent.find('ul'); //items container
		var $nextbtn = $parent.find('#next');
		var $prevbtn = $parent.find('#prev');
		var listId = $liste.attr('id'); //id of the items container
		var nbItems = jQuery('#'+listId+' li').length; //nombre d'items dans le slider
		var itemWidth = jQuery('.item').outerWidth(true); //item width
		
		//calculate container width depending of the number of items
		var containerWidth= nbItems*itemWidth;
		//set container width
		$liste.width(containerWidth);
		
		//INIT
		if(getIndex()==0) {
			$prevbtn.hide();
		}
		
		//return the container left position
		function getPos() {
			if($liste.html() != null)
			{
				var position=$liste.position();
				return position.left;
			}
		}
		
		//return the current position of the slider
		function getIndex() {
			var pos=getPos();
			var index = pos/itemWidth;
			return index;
		}
		
		//click on "next" button
		$nextbtn.click( function() {
			var curIndex = getIndex();
			
			if(nbItems+curIndex==3) {
					$nextbtn.hide();
				}
				
				
			$liste.animate({ left : '-='+itemWidth },
			500,
			function () {
				var curIndex = getIndex();
				/*if(nbItems+curIndex==2) {
					$nextbtn.fadeOut('fast');
				}*/
				if(curIndex<0) {
					if(!$prevbtn.is(":visible")) $prevbtn.fadeIn();
				}
			}
		);
			
			return false;
		});
		
		//click on "previous" button
		$prevbtn.click( function() {
			
			var curIndex = getIndex();
			//if the slider is at the first position, hide the previous button
			if(curIndex==-1) {
				$prevbtn.hide();
			}
				
			$liste.animate({ left : '+='+itemWidth },
			500,
			function () {
				var curIndex = getIndex();
				/*
				//if the slider is at the first position, hide the previous button
				if(curIndex==0) {
					$prevbtn.fadeOut('fast');
				}
				*/
				if(nbItems+curIndex>2) {
					if(!$nextbtn.is(":visible")) $nextbtn.fadeIn();
				}
			}
		);
			
			return false;
		});
		
		$liste.find('li').each(function(i){
			var link=jQuery(this,i).find('a').attr('href')||'';
			var target=jQuery(this,i).find('a').attr('target')||'';
			
			if(link!=='') {
				jQuery(this,i).hover(function() {
					jQuery(this,i).css('cursor', 'pointer');
				}, function() {
					jQuery(this,i).css('cursor', 'default');
				});
				jQuery(this,i).click(function() {

					if(target=="_blank")
					{
						window.open (link,"");
					}
					else
					{	
						jQuery.scrollTo("#header",800);
						document.location.href= link;
					}
				});
			}
		});		
	}
})(jQuery);
////////////////////////////////////////
//END OF COOLSLIDER
////////////////////////////////////////

////////////////////////////////////////
//BUBBLE SLIDER
////////////////////////////////////////
(function($){
	$.fn.bubbleSlider = function() {
	
	var $parent = jQuery(this);
	var $liste = $parent.find('ul'); //items container
	var $navContainer = $parent.find('#nav');
	var listId = $liste.attr('id'); //id of the items container
	var IndexItem = jQuery('li').index(jQuery('#'+listId));
	var nbItems = jQuery('#'+listId+' li').length; //nombre d'items dans le slider
	var itemWidth = jQuery('#'+listId+' li').width(); //item width

	//calculate container width depending of the number of items
	var containerWidth= nbItems*itemWidth;
	//set container width
	$liste.width(containerWidth);
	
	//return the container left position
		function getPos() {
			if($liste.html() != null)
			{
				var position=$liste.position();
				return position.left;
			}
		}
		
		//return the current position of the slider
		function getIndex() {
			var pos=getPos();
			var index = pos/itemWidth;
			return index;
		}
	
	//current index visible
	var curindex=getIndex();
	//Generate the right number of bubbles depending of the number of items
	if(nbItems>1) {
		for(i=0; i<nbItems; i++) {
			if(curindex == i)  {
				jQuery('#'+listId+' li:eq('+i+')').addClass('active');
				bubble = 'on';
			} else bubble = '';
			$navContainer.append('<a href="#" id="bubble-'+i+'" class="'+bubble+'"></a>');
		}
		var posNav= (itemWidth-(nbItems*12))/2;
		$navContainer.css('left', posNav);
	}
	
	//resize the #nav div to center it
	var navBubbleWidth = $navContainer.find('a').outerWidth(); //get total width of a bubble
	var navContainerWidth= navBubbleWidth*nbItems; //calculate the width depending of the number of bubbles
	$navContainer.width(navContainerWidth); //applying the calculated width
		
		$navContainer.find('a').each( function(i) {
		if(jQuery(this,i).hasClass('on'))  $selectedbubble=i;
			jQuery(this,i).click(function() {
				jQuery('#'+listId+' li').removeClass('active');
				$navContainer.find('a.on').removeClass('on');
				jQuery(this).addClass('on');
				
				var moveFrom=itemWidth*(i - $selectedbubble);
				
				$liste.animate({ left : '-='+moveFrom },500);

				$selectedbubble=i;
				jQuery('#'+listId+' li:eq('+i+')').addClass('active');
				return false;
			});
		});
	
	}
})(jQuery);
////////////////////////////////////////
//END OF BUBBLE SLIDER
////////////////////////////////////////

////////////////////////////////////////
//TAB SLIDER
////////////////////////////////////////
(function($){
	$.fn.tabSlider = function() {
	settings = {
		BoxPerPane : 6
	}
	
	var $parent = jQuery(this);
	var $container = $parent.find('#playlistContainer'); //items container
	var $pane = $container.find('.pane');
	var nbPane = $pane.length; //nombre d'items dans le slider
	var paneWidth = $pane.width(); //item width

	if(nbPane>1) {
	//return the container left position
		function getPos() {
			var position=$container.position();
			return position.left;
		}
		
		//return the current position of the slider
		function getIndex() {
			var pos=getPos();
			var index = pos/paneWidth;
			return index;
		}
	
		function createNavPlaylist() {
			//current index visible
			var curindex=getIndex();

			//create the nav container
			if(jQuery('#navPlaylist').length==0) jQuery('#tabs').append('<div id="navPlaylist"></div>');
			else ('#navPlaylist').html('');
			$navContainer=jQuery('#navPlaylist');
			//Generate the right number of bubbles depending of the number of items
			
			for(i=0; i<nbPane; i++) {
				if(curindex == i)  bubble = 'on';
				else bubble = '';
				$navContainer.append('<a href="#" id="bubbleTab-'+i+'" class="'+bubble+'"></a>');
			}
			
			//resize the #nav div to center it
			var navBubbleWidth = $navContainer.find('a').outerWidth(); //get total width of a bubble
			var navContainerWidth= navBubbleWidth*nbPane; //calculate the width depending of the number of bubbles
			var posNav = (paneWidth/2) - (navContainerWidth/2);
			$navContainer.css('left', posNav+'px'); //applying the calculated width
			
		}

		if(jQuery('#navPlaylist').length==0 && nbPane>1) createNavPlaylist();
		else $navContainer=jQuery('#navPlaylist');
		
		//problème ici ??
		$navContainer.find('a').each( function(i) {

		if(jQuery(this,i).hasClass('on')) $selectedbubbleTab=i;
		jQuery(this,i).click(function() {
		$navContainer.find('a.on').removeClass('on');
			jQuery(this).addClass('on');
			
			var moveFrom=paneWidth*(i - $selectedbubbleTab);

			$container.animate({ left : '-='+moveFrom },500);

			$selectedbubbleTab=i;
			return false;
			});
		});


		
		}


	}
})(jQuery);
////////////////////////////////////////
//END OF BUBBLE SLIDER
////////////////////////////////////////

function facebookLoggIn()
{
	FB.login(function(response) {
			if (response.session)
			{
				jQuery('.facebook_image').show();
				jQuery('.connect_button').hide();
				jQuery('.disconnect_button').show();
				//UniversalMusicLoggin(FB,response);
			}
			else
			{
				// user cancelled login
			}
  		}, {perms:'email'});
}

function facebookLoggOut()
{
	FB.logout(function(response) {
  			jQuery('.facebook_image').hide();
  			jQuery('.connect_button').show();
  			jQuery('.disconnect_button').hide();
  		})
}

function facebookOnLoggin(FacebookObject,responseObject)
{
	FacebookObject.api('/me', function(response) {

		jQuery('.facebook_image').show();
		jQuery('.disconnect.facebook').show();
		jQuery('.connect').hide();
		
		jQuery('#first_name').attr('value',response.first_name);
		jQuery('#last_name').attr('value',response.last_name);
				
		//submit to universalmusic login form (in V3)
		//jQuery("#UserLoginForm").submit();
		//alert(jQuery('#first_name').attr('value')+"-"+jQuery('#last_name').attr('value')+"-"+jQuery('#uid').attr('value')+"-"+jQuery('#callback_url').attr('value'))

		jQuery('.salutations.facebook').html('Bonjour, '+jQuery('#first_name').attr('value')+' '+jQuery('#last_name').attr('value'));

		jQuery('.addToPlaylist').show();
		jQuery('#tabs-3').show();
		jQuery('li.playlist').show();
		
		var flashObj = swfobject.getObjectById("flashcontent");
		try
		{
			flashObj.onLoggedInExternal();
		}
		catch(err)
		{}
		
	});
	
	jQuery('#uid').attr('value',responseObject.session.uid);
	jQuery('#callback_url').attr('value',window.location);
}

function facebookOnLoggout(FacebookObject,responseObject)
{
	jQuery('.facebook_image').hide();
	jQuery('.connect').show();
	jQuery('.disconnect.facebook').hide();
	
	jQuery('.salutations.facebook').html('');
	
	jQuery('#first_name').attr('value','');
	jQuery('#last_name').attr('value','');
	jQuery('#uid').attr('value','');
	jQuery('#callback_url').attr('');
	
	//test if universalloggedin
	if(jQuery('.disconnect.universal').is(":visible") == false)
	{
		/*jQuery('.addToPlaylist').hide();
		jQuery('#tabs-3').hide();
		jQuery('li.playlist').hide();*/ /*maybe in V2*/
	}
	
	var flashObj = swfobject.getObjectById("flashcontent");
	try
	{
		flashObj.onLoggedOutExternal();
	}
	catch(err)
	{}
}

function redirectUserToUniversalLoggin()
{
	window.location.href = settings.loginUrl + "?from_url=" +window.location.href;
}

function loggout()
{
	//test if universalloggedin
	if(jQuery('.disconnect.universal').is(":visible") == false)
	{
		facebookLoggOut();
	}
	else
	{
		window.location.href = settings.logoutUrl + "&amp;from_url=" +window.location.href;
	}
}

function addToPlaylist(p,post_type) {
	jQuery.get(url+"/wp-content/themes/offtv/add_to_playlist.php?p="+p+"&post_type="+post_type, function(response) {
		var elem = response.split('%');
		if( elem[0] == "OK") {
			jQuery('li.playlist span').html(parseInt(jQuery('li.playlist span').html())+1);
			jQuery("#tabs").tabs("load",2);
			jQuery('.deletePlaylist').show();
		}
		
		jQuery.growl("Ma sélection", elem[1]);
	});
	return false;
}
function removeFromPlaylist(p,post_type) {
	jQuery.get(url+"/wp-content/themes/offtv/remove_from_playlist.php?p="+p+"&post_type="+post_type, function(response) {
	var elem = response.split('%');
	if( elem[0] == "OK") {
		jQuery('li.playlist span').html(parseInt(jQuery('li.playlist span').html())-1);
		if(jQuery('li.playlist span').html() == "0") jQuery('.deletePlaylist').hide();
		if(jQuery('li.playlist span').html() == "6")
		{
			jQuery("#tabs").tabs("load",2);
			jQuery('#navPlaylist').remove();
		}
	}
				
	jQuery.growl("Ma sélection", elem[1]);
	});
	return false;

}
function deletePlaylist() {
	jQuery.get(url+"/wp-content/themes/offtv/delete_playlist.php", function(response) {
	var elem = response.split('%');
	if( elem[0] == "OK") {
		jQuery('li.playlist span').html("0");
		jQuery('#navPlaylist').html('');
		jQuery("#tabs").tabs("load",2);
		jQuery.growl("Ma sélection", elem[1]);
		}
	});
	return false;

}
function manageHelpBubble(el) {
if(el==undefined) $el=jQuery('.bubble');
else $el=jQuery(el).find('.bubble');
		//HELP BUBBLE
		$el.each(function() {

			var titleppp = jQuery(this).attr('title');
			jQuery(this).append('<div class="help-bubble"><div class="top"></div><div class="content">'+titleppp+'</div><div class="bottom"></div></div>');
			jQuery(this).removeAttr('title');
			
			//HIDE/DISPLAY THE BUBBLE
			jQuery(this).hover(function() {
				jQuery(this).find('.help-bubble').fadeIn();
				} , function() {
					jQuery(this).find('.help-bubble').fadeOut();
				}
			);
		
		});
}
	
function manageBoxActions() {
	jQuery('.box').find('a.share-fb').hide();
	jQuery('.box').find('a.delete-from-playlist').hide();

	//CLICK THE BUTTON ADD TO PLAYLIST
	jQuery('.addToPlaylist').unbind("click");
	jQuery('.addToPlaylist').click(function() {
		var p = jQuery(this).parent().find("input[name='p']").val();
		var post_type = jQuery(this).parent().find("input[name='post_type']").val();
		addToPlaylist(p,post_type);
		return false;
	});
	//CLICK THE BUTTON REMOVE FROM PLAYLIST
	jQuery('.delete-from-playlist').unbind("click");
	jQuery('.delete-from-playlist').click(function() {
		var p = jQuery(this).parent().find("input[name='p']").val();
		var post_type = jQuery(this).parent().find("input[name='post_type']").val();
		removeFromPlaylist(p,post_type);
		jQuery(this).parent().parent().parent().fadeOut('',function() {jQuery(this).remove()});
		return false;
	});
	//CLICK THE BUTTON REMOVE ALL FROM PLAYLIST
	jQuery('.deletePlaylist').unbind('click');
	jQuery('.deletePlaylist').bind('click',function() {
		deletePlaylist();
		return false;
	});
	
	//popup for fb link
	jQuery('.box .share-fb').each(function() {
		jQuery(this).click(function(){
			openPopup(jQuery(this).attr('href'));
			return false;
		});
	});
	
	//VIDEO-LIST ITEM CLICK
	jQuery('.box').click(function(){
		document.location.href = jQuery(this).find('a').attr('href');
		jQuery.scrollTo("#header",800);
	});
	
	//VIDEO-LIST ITEM HOVER
	jQuery('.box').hover(function(){
		jQuery(this).addClass('hover');
		jQuery(this).css("cursor","pointer");
		jQuery(this).find('a.share-fb').fadeIn();
		jQuery(this).find('a.delete-from-playlist').fadeIn();
	}, function() {
		jQuery(this).removeClass('hover');
		jQuery(this).css("cursor","default");
		jQuery(this).find('a.share-fb').fadeOut();
		jQuery(this).find('a.delete-from-playlist').fadeOut();
	});
}
	
function manageExternalLinks() {
	jQuery('.external').each(function() {
		jQuery(this).attr('target','_blank');		
	});
}

function openPopup(url,fullscreen) {
	
	if(fullscreen !=true )
	window.open(url,'popup','height=500,width=650');
	else
	window.open(url);
	return false;
}

function openUrl(url) {
	
	window.location.href = url;
	return false;
}

function changeLikeBtnURL(url,id,title)
{

	var shortUrl = "?post_type=video&p="+id;
	var fullUrl = "http%3A%2F%2Fwww.off.tv%2F%3Fpost_type%3Dvideo%26p%3D"+id;
	
	//jQuery('#facebook iframe').attr("src", "http://www.facebook.com/plugins/like.php?channel_url=http%3A%2F%2Fstatic.ak.fbcdn.net%2Fconnect%2Fxd_proxy.php%23cb%3Df62127594dbc98%26origin%3Dfile%253A%252F%252F%252Ff264a15f4f581b2%26relation%3Dparent.parent%26transport%3Dpostmessage&colorscheme=light&font=tahoma&href=" + url + "&layout=button_count&locale=fr_FR&node_type=link&sdk=joey&show_faces=false&width=110&send=false&api_key=130259133654836");
	jQuery('#facebook').html('<iframe src="http://www.facebook.com/plugins/like.php?href='+fullUrl+'&amp;show_faces=false&amp;layout=button_count" scrolling="no" frameborder="0" style="height: 30px; width: 100px" allowTransparency="true"></iframe>');
	jQuery('#twitter').html('<iframe allowtransparency="true" frameborder="0" scrolling="no" src="http://platform.twitter.com/widgets/tweet_button.html?count=horizontal&amp;lang=fr&amp;text='+title+' via @off&url=' + encodeURIComponent(url)+'&counturl='+fullUrl+'" style="width:110px; height:50px;"></iframe>');

	jQuery('#ggplus').html('<g:plusone size="medium" href="'+shortUrl+'"></g:plusone>');
	gapi.plusone.go("ggplus");
	//gapi.plusone.render("ggplus");
}


function showSocial()
{
	jQuery('.iframe-container').show();
}

function hideSocial()
{
	jQuery('.iframe-container').hide();
}

function setPosition(x, y)
{
	jQuery('.iframe-container').css("left", x + "px");
	jQuery('.iframe-container').css("top", x + "px");
}

function trackStats(eventName, itemType,itemId, affiliateId)
{
	//alert(itemId +"-"+ eventName +"-"+ affiliateId +"-"+ itemType);
	
	//pageTracker._trackEvent(itemType, eventName, itemId+'-'+window.location.hash);
	
	//track affiliate id if there is one
	if(affiliateId != undefined && affiliateId != '')
	pageTracker._trackEvent(itemType + '-' + itemId + '-' + window.location.hash,itemType + "-" + eventName, affiliateId);
	else
	pageTracker._trackEvent(itemType + '-' + itemId + '-' + window.location.hash,itemType + "-" + eventName, "offtv");
}

//----------------------------------
//  Wrapper functions for like / tweet layer
//  called by flash
//----------------------------------
function socialLayer_init(id)
{
	 var jqueryObj = jQuery('.iframe-container');
	window['AFlashproxyId_' + id] = {};
	window['AFlashproxyId_' + id].jqueryObj = jqueryObj;
}

function socialLayer_setCSSProperty(id, cssProp, val)
{
	window['AFlashproxyId_' + id].jqueryObj.css(cssProp, val + 'px');
}

function socialLayer_setVisible(id, val)
{
	if(val)
		window['AFlashproxyId_' + id].jqueryObj.show();
	else
		window['AFlashproxyId_' + id].jqueryObj.hide();
}

