// JavaScript Document
$(document).ready( function() {
	if ($.fn.xnCarousel) $("#carousel_ul").xnCarousel({step:7});
	if ($.fn.xnTabs) $(".tabs").xnTabs({speed:500, classSelected:"bkg-red", classNotSelected:"notSelected"});
	if ($.fn.xnSlider) $("#headlines").xnSlider({interval:5000});
	
	if ($.fn.nivoSlider)
		$('#slider').nivoSlider({'effect':'fold', 'pauseTime':5000});
	
	
	// carousel
	$("#carousel_ul li a[class='selected']").each(function() {
		var cls = $(this).parent("li").attr("class") ;
		if ($.support.opacity) 
			$("div#events > div[class!='" + cls + "']").animate({opacity:0}, 500, "linear").hide();
		else
			$("div#events > div[class!='" + cls + "']").hide();
	});
		
	$("#carousel_ul li a").click(function() {
		$("#carousel_ul li a").removeClass("selected");
		$(this).addClass("selected");
		
		var rel = $(this).attr("rel").split('*');
		$("#day-events h4 em").html( rel[0] );
		$("#day-events h4 span").html( rel[1] );
		
		var cls = $(this).parent("li").attr("class") ;
		
		if ($.support.opacity) {
			$("div#events > div[class!='" + cls + "']").hide().animate({opacity:0}, 500, "linear");
			$("div#events > div[class='" + cls + "']").show().animate({opacity:1}, 500, "linear");
		}
		else {
			$("div#events > div[class!='" + cls + "']").hide();
			$("div#events > div[class='" + cls + "']").show();
		}
	});
	
	if ($("img.resize").length) 
	   	$("img.resize").resizeImage({maxWidth:150,maxHeight:150});
	
	if ($("img.guide").length)
		$("img.guide").resizeImage({maxWidth:70,maxHeight:70});
	
	if ($("img.top_video").length)
		$("img.top_video").resizeImage({maxWidth:290,maxHeight:170});
	
	
	if ( $('div.comments a') && jQuery.tipsy)
		$('div.comments a').tipsy({gravity: 's'});
	
	
	
	
	
	// player   
	if ( ("div#player" ).length ) {
	   var filename = $("input:hidden#root").val() + '/' + $("input:hidden#folder_video").val() +  $("input:hidden#folder" ).val() + '/' + $("input:hidden#filename").val() ;
	   var filename_hd = $("input:hidden#root").val() + '/' + $("input:hidden#folder_video").val()  + $("input:hidden#folder_hd").val() + '/' + $("input:hidden#filename_hd").val() ;
	   var preview = $("input:hidden#folder_video_thumbnails").val() + $("input:hidden#folder").val() + '/' + $("input:hidden#preview").val() ;
	   var short_url = $("input:hidden#short_url").val() ;
	   
	   if ($("input:hidden#folder_hd").val() != '' )
    	    var flashvars = {
                'file': filename,
                'image': preview,
                'skin':'swf/metarby10.swf',
        		'plugins':'hd-1', 
                'dock':'true',
                'hd.file':filename_hd,
                'hd.state':'false'
                
            };
       else
            var flashvars = {
                'file': filename,
                'image':preview,
                'skin':'swf/metarby10.swf',
                'dock':'true'             };
       
       
       var params = {
           'allowfullscreen':'true',
           'allowscriptaccess':'always',
           'wmode':'transparent'
       };
       
       var attributes = {};
       
       swfobject.embedSWF("swf/player-viral.swf", "player" , "480", "340", "9.0.0","swf/expressInstall.swf", flashvars, params, attributes);
    }
   
	//article player   
	var total_videos = parseInt($("#nbvideos").val());
	for( x = 0; x < total_videos; x++) {
		var index = "_" + x ;
		
		if ( ("div#player" + index).length ) {
		   var filename = $("input:hidden#root").val() + '/' + $("input:hidden#folder_video").val() +  $("input:hidden#folder" + index).val() + '/' + $("input:hidden#filename" + index).val() ;
		   var filename_hd = $("input:hidden#root").val() + '/' + $("input:hidden#folder_video").val()  + $("input:hidden#folder_hd" + index).val() + '/' + $("input:hidden#filename_hd" + index).val() ;
		   var preview = $("input:hidden#folder_video_thumbnails").val() + $("input:hidden#folder" + index).val() + '/' + $("input:hidden#preview" + index).val() ;
		   var short_url = $("input:hidden#short_url" + index).val() ;
		   
		   if ($("input:hidden#folder_hd" + index).val() != '' )
	    	    var flashvars = {
	                'file': filename,
	                'image': preview,
	                'skin':'swf/metarby10.swf',
	        		'plugins':'hd-1', //,fbit-1,tweetit-1',
	                'dock':'true',
	                //'tweetit.link':short_url,
	                //'fbit.link':short_url,
	                'hd.file':filename_hd,
	                'hd.state':'false'
	                
	            };
	       else
	            var flashvars = {
	                'file': filename,
	                'image':preview,
	                'skin':'swf/metarby10.swf',
	        		//'plugins':'fbit-1,tweetit-1',
	                'dock':'true'
	                //'tweetit.link':short_url,
	                //'fbit.link':short_url
	             };
	       
	       
	       var params = {
	           'allowfullscreen':'true',
	           'allowscriptaccess':'always',
	           'wmode':'transparent'
	       };
	       
	       var attributes = {};
	       
	       swfobject.embedSWF("swf/player-viral.swf", "player" + index, "480", "340", "9.0.0","swf/expressInstall.swf", flashvars, params, attributes);
	    }
    }
    
   	
   	
   	// home top video player   
	if ( ("div#top_video_player").length ) {
	   var filename = $("input:hidden#root").val() + '/' + $("input:hidden#folder_video").val() +  $("input:hidden#top_video_folder").val() + '/' + $("input:hidden#top_video_filename").val() ;
	   var filename_hd = $("input:hidden#root").val() + '/' + $("input:hidden#folder_video").val()  + $("input:hidden#top_video_folder_hd").val() + '/' + $("input:hidden#top_video_filename_hd").val() ;
	   var preview = $("input:hidden#folder_video_thumbnails").val() + $("input:hidden#top_video_folder").val() + '/' + $("input:hidden#top_video_preview").val() ;
	   var short_url = $("input:hidden#top_video_short_url").val() ;
	   
	   if ($("input:hidden#top_video_folder_hd").val() != '' )
    	    var flashvars = {
                'file': filename,
                'image': preview,
                'skin':'swf/metarby10.swf',
        		'plugins':'hd-1',
                'dock':'true',
                'hd.file':filename_hd,
                'hd.state':'false'
                
            };
       else
            var flashvars = {
                'file': filename,
                'image':preview,
                'skin':'swf/metarby10.swf',
                'dock':'true'
             };
       
       
       var params = {
           'allowfullscreen':'true',
           'allowscriptaccess':'always',
           'wmode':'transparent'
       };
       
       var attributes = {};
       
       swfobject.embedSWF("swf/player-viral.swf", "top_video_player", "300", "220", "9.0.0","swf/expressInstall.swf", flashvars, params, attributes);
    }
    
    
   	// articles or webtv ?
    var webtv = false;
	if ( $("input:hidden[id='webtv']").length && ( $("input:hidden[id='webtv']").val() == '1') )	
		webtv = true;
	
	// habillage	
    $("input:hidden[id*='graphics:']").each( function() {
		var ID = $(this).attr('id');
		var val = $(this).val();
		
		try {
			if ( ID == 'graphics:background-color' ) {
				
				if ( webtv == true ) {
					$('#webtv-graphics').css({'background-color':'#' + val}).css({'background-color':'#' + val});
					//$('#single-video').css({'background-color':'#' + val});
				}
				else {
					//$('#container').css({'background-color':'#' + val});
					$('body').css({'background-color':'#' + val});
				}
			}
			
			if ( ID == 'graphics:background-image' ) {
				if (webtv == true) {
					$('#webtv-graphics').css({'background-image': 'url(' + val + ')'});
				}
				else {
					$('body').css({'background-image': 'url(' + val + ')'});	
				}
			}
			
			
			if ( ID == "graphics:margin-top" ) {
				$('#container').css( {'margin-top': val + 'px'} );				
			}
		}
		catch(e) {
			//
		}
	});
	
	
	// search
	$("div.block h3 a").click(function() {
		if ($(this).parent().parent().hasClass('can-expand')) {
			if ($(this).parent().parent().hasClass('open'))
				$(this).parent().parent().animate({'height': '30px'}).removeClass('open');
			else {
				var h = parseInt($(".result-list").height()) + 150;
				
				$(this).parent().parent().animate({'height': h}).addClass('open');
			}
		}		
	});
	
	if ( $("#x").val() == 1 )
		$("#results .block:eq(0) h3 a").trigger("click");
	
	
	if ($("img.result-resize").length)
	   $("img.result-resize").resizeImage({maxWidth:80,maxHeight:80});
	   
	
	
	// contact
	$("#fContact").submit(function() {
		var doSubmit = true;
		$("#contact form .rq").each( function(e) {
			if( $(this).val() == "" ) { 
				$(this).addClass("required");
				doSubmit = false;
			}
		});
		
		return doSubmit;			
	});
	
	$("#send-to-friend").click(function(){
		if ( !$("#fSendToFriend").is(":visible") )
			$("#fSendToFriend").slideDown(); //.css({"position":"absolute", "top":$(this).css("top"), "left":$(this).css("left"), "z-index":150});
		else
			$("#fSendToFriend").slideUp();
			
		return false;
	});
	
	$("#frmNewsletter").submit(function() {
		return false;
	});
	 
	$("#btnNL").click(function() {
		var e = $("input:text[id='f:nl-email']").val();
		var valid = false;
		
		$.post("ajx/valid_email.php", {"e":e}, function(data){
			if (data == "1") {
				$.getScript("js/facebox.js", function() {
					alert('Votre email a \u00E9t\u00E9 enregistr\u00E9');
					$("input:text[id='f:nl-email']").val("");
				});
			}
			else {
				alert("Email invalide");
			}
		});
	});
	
	
	if ( $("#stars").length && $.fn.raty ) {
		$('div#stars').raty({
			scoreName: 'entity.score',
			number: 10,
			path: 'images',
			readOnly:  ( ($("#r").val() == '1') ? true : false) ,
			starOff:     'star-off.png',                                  // The image of the off star.
			starOn:      'star-on.png',
			start: $("#rate").val(),
			onClick: function(score) {
			    $.post("ajx/rate.php", {'referenceid':$("#refid").val(), 'score':score, 'content_type':$("#content_type").val()}, function(data) {
			    	// publish to fb
			    	$.fn.raty.readOnly(true);
			    });
			}	
		});
	}	
	
	
	// fb share icon click
	$("a[class='fb-share-article']").click(function() {
		var url = $(this).attr("href");
		var title = $(this).attr("rel");
		
		if (url == undefined) url = window.location.href;
		if (url == "") url = window.location.href;


		window.open('http://www.facebook.com/share.php?u=' + url + '&t='+encodeURIComponent(title), 'share', 'toolbar=0,status=0,width=626,height=436');
	
		return false;
	});
	
	// twitter share icon click
	$("a[class='tw-share-article']").click(function() {
		var url = $(this).attr("href");
		var title = $(this).attr("rel");
		
		if (url == undefined) url = window.location.href;
		if (url == "") url = window.location.href;
		
		var o = this;
		
		if ( url.substring(0, 14) == "http://bit.ly/") {
			window.open('http://twitter.com/home?status=' + url + ' : ' + encodeURIComponent(title) , 'share');				
		}
		else {
			$(this).bitly({url:url, onFinish:function() {
				window.open('http://twitter.com/home?status=' + $(o).attr("href") + ' : ' + encodeURIComponent(title) , 'share');
			}});
		}
		
		
		
		
		return false;
	}); 
	
	
	$("#postit").click(function() {	 
		/*
		FB.api('/me/friends', 'get', function(response) {
			var friends = response.data;
			
			for(i=0; i < ; i++) {
				var friend = friends[i];
				alert( friend.name );
			}
		});
		*/
		
		var wall_post = {
			"message":'Reading Connect JS documentation',
			"picture": 'http://www.tuniscope.com/images/fb.jpg',
			"link": 'http://www.tuniscope.com',
			"name": 'Harrer El Wakt',
			"caption": 'Wenti?',
			"description":"Kifech t3addi fi waqtek fi romdhane?"
		} ;
		
		FB.api('/me/feed', 'post', wall_post, function(response) {
		  if (!response || response.error) {
		    alert('Error occured');
		  } else {
		    alert('Post ID: ' + response);
		  }
		});
		
		
		
	});
});


