$(document).ready(function(){
	Font.init();
	
	add_search_action();
	/*
	if(typeof sIFR == "function"){
	    sIFR();
	};
	if(typeof sIFR == "function"){
    	sIFR.replaceElement("div#sec-content h3", named({sFlashSrc: "/public/sifr/futuraheavy.swf", sColor: "#ffffff", sCase: "upper", sBgColor: "#000000", sWmode:"transparent", sLinkColor: "#ffffff"}));
	}
	*/
	
	//les liens de scroll
	if ($('a.scroll').length) {
	  $('a.scroll').click(function() {
		  var $target = $(this.hash);
		  $target = $target.length && $target || $('[name=' + this.hash.slice(1) +']');
		  $('html,body').animate({ scrollTop: $target.offset().top }, 1000);
		  return false;
	  })
	}
	
	reorderTabs($('ul.tabs'));
	
	$('div#header input.button').hover(
	function(){
		if($.browser.msie==true){
			$(this).css('cursor','hand');
		}else{
			$(this).css('cursor','pointer');
		}
	}
	,
	function(){
		$(this).css('cursor','default');
	}
	)
});

function reorderTabs($tabs){
		$tabs = $tabs || $('ul.tabs');
		$tabs.each(function(){
	        var zindex = 10,
	            zindexMod = 1,
	            $this = $(this);
	            
	        $this.find('li').each(function(){
	            var $this = $(this);
	            $this.css('z-index', zindex);
	            if ( $this.hasClass('selected') ) {
	                zindexMod = -1;
	            }
	            zindex = zindex + zindexMod;
	        }).filter('.selected').css('z-index', '12');
	    });
	}


function stream(test){
	alert(test)
}

function add_vote_positif_action(){
	$('a.vote-positif').click(function(){
		
		$('#tooltipHelper').hide();
		if($(this).attr('rel')=='done'){
			votepts = 2;
		}else{
			votepts = 1;
		}
		value = {
			id:$(this).attr('id').split('-')[1],
			vote:votepts	
		};
		$.post("/public/ajax/update-vote.php",value,callback_vote_action);
		
		return false;
	});
	
	$('a.vote-positif').tooltip({ 
								    track: true, 
								    delay: 200, 
								    showURL: false, 
								    showBody: " - ",
									extraClass:'inputsTooltipPositif', 
								    opacity: 0.85 
								});
}

function add_vote_negatif_action(){
	$('a.vote-negatif').click(function(){
		$('#tooltipHelper').hide();
		if($(this).attr('rel')=='done'){
			votepts = -2;
		}else{
			votepts = -1;
		}
		value = {
			id:$(this).attr('id').split('-')[1],
			vote:votepts	
		};
		$.post("/public/ajax/update-vote.php",value,callback_vote_action);
		return false;
	});
	$('a.vote-negatif').tooltip({ 
								    track: true, 
								    delay: 200, 
								    showURL: false, 
								    showBody: " - ",
									extraClass:'inputsTooltipNegatif', 
								    opacity: 0.85 
								});
}

function callback_vote_action(data){
	toadd  = data.split('-')[2];
	id   = data.split('-')[1];
	data = data.split('-')[0];
	if(data == 'positif' || data=='negatif'){
		if(data == 'positif'){
			pts = parseInt($('.points .big').text())+parseInt(toadd);
			$('.points .big').text(pts);
			
			//on switch les boutons
			$('a.vote-positif').after('<span class="vote-positif" id="positif-'+id+'">Vote positif</span>');
			$('a.vote-positif').remove();
			$('a.vote-negatif').attr('rel','done');
			if($('span.vote-negatif').length>0){
				$('span.vote-negatif').after('<a href="/" class="vote-negatif" id="negatif-'+id+'" title="Ce vid&eacute;o est mauvais. Je veux lui enlever un point!" rel="done">Vote n&eacute;gatif</a>');
				$('span.vote-negatif').remove();
				add_vote_negatif_action();
			}
			
		}else if(data=='negatif'){
			pts = parseInt($('.points .big').text())-parseInt(toadd);
			$('.points .big').text(pts);
			
			//on switch les boutons
			$('a.vote-negatif').after('<span class="vote-negatif" id="negatif-'+id+'">Vote n&eacute;gatif</span>');
			$('a.vote-negatif').remove();
			$('a.vote-positif').attr('rel','done');
			if($('span.vote-positif').length>0){
				$('span.vote-positif').after('<a href="/" class="vote-positif" id="positif-'+id+'" title="Ce vid&eacute;o est excellent. Je veux lui donner un point!" rel="done">Vote positif</a>');
				$('span.vote-positif').remove();
				add_vote_positif_action();
			}
		}
	}
}

function add_search_action(){
	$('#field_recherche').focus(function(){
		$(this).val("");
	});	
}

function echeck(str) {

	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1){
	  // alert("Invalid E-mail ID")
	   return false
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	  // alert("Invalid E-mail ID")
	   return false
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
	   // alert("Invalid E-mail ID")
	    return false
	}

	 if (str.indexOf(at,(lat+1))!=-1){
	  //  alert("Invalid E-mail ID")
	    return false
	 }

	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
	   // alert("Invalid E-mail ID")
	    return false
	 }

	 if (str.indexOf(dot,(lat+2))==-1){
	   // alert("Invalid E-mail ID")
	    return false
	 }
	
	 if (str.indexOf(" ")!=-1){
	   // alert("Invalid E-mail ID")
	    return false
	 }

		 return true					
}


Font = {
	menu:{
		 link:'.sIFR-root {color: #00a0dc;cursor:pointer;}',
		 hover:'.sIFR-root {color: #000000;cursor:pointer;}'
	},
	type:{},
	init:function(){
		Font.addFont('gothamBold', 'gotham_bold');
		Font.addFont('gothamBook', 'gotham_book');

		sIFR.repaintOnResize = false;
		
		//Font.menuInit();
		Font.titleInit();
	},
	addFont:function(fontname, filename){
		Font.type[fontname] = { src: '/public/sifr/'+filename+'.swf' }; 
        sIFR.activate([Font.type[fontname]] ); 
	},
	getFont:function(fontname){
		return Font.type[fontname];
	},
	titleInit:function(){      
	    Font.titleColor = $('div.box:first').find('h3').find('span').css('color');

        sIFR.replace(Font.getFont('gothamBook'), {
            selector: '.box h3 span.text',
            css: [ '.sIFR-root {color: '+Font.titleColor+';cursor:arrow;}' ],
            transparent:true,
            forceSingleLine : false,
            tuneHeight:-5,
            selectable:true,
            wmode : 'transparent',
            onReplacement:$.noop,
            onRollOver:$.noop
        });
			
	},
	replace:function($el, value){
		var myid = $el.find('object').attr('id'),
		    sObj = sIFR.replacements[myid];
		
		//if this call was made before sifr was init
		if(typeof(sObj) == 'undefined'){
		  //replace the text
		  $el.text(value); 
		  return;	
		}	
		$el.find('.sIFR-alternate').text(value);
		sObj.replaceText(value);
	},
	/*rgb2hex: function (rgb) {
		var hexDigits = ["0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"];
		rgb = rgb.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);
		function hex(x) {
		  return isNaN(x) ? "00" : hexDigits[(x - x % 16) / 16] + hexDigits[x % 16];
		}
	    alert(rgb);
		return '#000000';
		return "#" + hex(rgb[1]) + hex(rgb[2]) + hex(rgb[3]);
	},*/
	rgb2hex:function (color) {
	    if (color.substr(0, 1) === '#') {
	        return color;
	    }
	    var digits = /(.*?)rgb\((\d+), (\d+), (\d+)\)/.exec(color);
	    
	    var red = parseInt(digits[2]);
	    var green = parseInt(digits[3]);
	    var blue = parseInt(digits[4]);
	    
	    var rgb = blue | (green << 8) | (red << 16);
	    return digits[1] + '#' + rgb.toString(16);
	}
}

