//-------------------------------------------------------
// CHECK TEXT SIZE
//-------------------------------------------------------
function checkTextSize(pObject){

	if(pObject[0].hasAttribute("maxlength") ){
	
            max		= parseInt(pObject.attr("maxlength"));
            text 	= pObject.val();

            if(pObject.parent().find(".maxlength").length == 0){

                    node = "<div class=\"maxlength\"><span class=\"textSize\"></span>/<span class=\"textSizeMax\"></span></div>";
                    pObject.parent().append(node);

            }

            pObject.parent().find(".textSize").html(text.length);
            pObject.parent().find(".textSizeMax").html(max);
		
	}else if(pObject[0].hasAttribute("minlength") ){
            
            min		= parseInt(pObject.attr("minlength"));
            text 	= pObject.val();

            if(pObject.parent().find(".maxlength").length == 0){

                    node = "<div class=\"maxlength\"><span class=\"textSize\"></span> carac.</div>";
                    pObject.parent().append(node);

            }

            pObject.parent().find(".textSize").html(text.length);
            pObject.parent().find(".textSizeMax").html(min);            
            
        }
		
}

$(".field input[type=text],.field textarea").keyup(function(){
	
	checkTextSize($(this));
	
});

//-------------------------------------------------------
// PAGE SCROLL
//-------------------------------------------------------
$("#pageScrollTop").click(function(){

	$('html, body').animate({
	    scrollTop: 0
	}, 1000);	
	
});

//-------------------------------------------------------
// REDIRECT LINK
//-------------------------------------------------------
$( document ).on( "click", "a[data-redirect]", function() {	

	dataRedirect 	= $(this).attr("data-redirect");
	
	if(dataRedirect != ""){
	
		tabRedirect		= dataRedirect.split(";");
	
		separator = "?";
		if($(this).attr("href").indexOf("?") != -1) separator = "&";
		
		redirectURL		= $(this).attr("href") + separator + tabRedirect[0] + "=" + tabRedirect[1];
		
		document.location = redirectURL;
	
		return false;
	
	}
	
});

//-------------------------------------------------------
//ON CLICK OUT
//-------------------------------------------------------
$.fn.onclickout = function(callback){
 var elm = this;
 $(document).on("click", function(evt){
     if($(evt.target).closest(elm).length === 0){
         callback.call(elm, evt);
     }
 });
 return elm;
};	

//-------------------------------------------------------
// TABS
//-------------------------------------------------------
$( document ).on( "click", ".tabs-menu li", function() {	
	
	$(".tab").hide();
	$(".tab[rel=" + $(this).attr("rel") + "]").show();
	$(".tab[rel=" + $(this).attr("rel") + "] .accordion-content").show();
        
	$(this).parent().find("li").removeClass("selected");
	$(this).addClass("selected");		
	
	$(".tab h2.accordion").removeClass("selected");
	$(".tab[rel=" + $(this).attr("rel") + "] h2.accordion").addClass("selected");	
	
});

$( document ).on( "click", ".tab h2.accordion", function() {	
	
	accordionVisible = $(".tab h2.accordion").css("display");
	
	if(accordionVisible == "block"){
		
		accordionContent 		= $(this).parent().find(".accordion-content");
		accordionContentDisplay = accordionContent.css("display");
		
		$(".tab").show();
		$(".tab .accordion-content").hide();
		
		$(".tab h2.accordion").removeClass("selected");
		
		var offset = $(this).offset();
		$('html,body').animate({scrollTop: (offset.top)},100); 
		
		if(accordionContentDisplay == "none"){
			
			accordionContent.show();	
			$(this).addClass("selected");				
			
		}else{
			accordionContent.hide();
		}					
		
	}
	
});

//-------------------------------------------------------
//GENERAL
//-------------------------------------------------------
$(document).ready(function(){	
 
    $(".field input[type=text],.field textarea,.field input[type=password]").each(function(){
            checkTextSize($(this));	
    });	

    $(".colorbox").colorbox({maxWidth:"100%",maxHeight:"100%",current:"{current}/{total}"});

    $(".shared").click(function(){

            var posTop,posLeft,optionModifie;

            hauteur = 600;
            largeur = 800

            // calcul de la position
            posTop = (screen.height - hauteur) / 2;
            posLeft = (screen.width - largeur) / 2;

            // gestion des options
            option = "directories=no,menubar=no,location=no,resizable=no,fullscreen=no";
            option += ",width=" + largeur;
            option += ",height=" + hauteur;
            option += ",top=" + posTop + ",left=" + posLeft;

            // ouverture du popup
            open($(this).attr("href"),$(this).attr("title"),optionModifie);

            return false;

    });		

    if (typeof tabFirst == "undefined") tabFirst = 1; 

    setTimeout(function(){
        
        accordionVisible = $(".tab h2.accordion").css("display");

        if(accordionVisible == "block"){	
                $(".tab").show();
                $(".tab .accordion-content").hide();
                $(".tab h2.accordion").removeClass("selected");
        }else{
                $(".tabs-menu li:nth-of-type(" + tabFirst + ")").click();
        }
        
    },1000);
    
    /* *************************************************************************************************** */

    $("textarea").each(function(){

            dataContent = $(this).attr("data-content");

            if(dataContent != undefined){

                    dataContentDecoded = $.base64Decode(dataContent);
                    $(this).html(dataContentDecoded);

            }

    });
    
    /* *************************************************************************************************** */

    $("[data-video]").click(function(){
    	
    	encode = $(this).attr("data-video");

        if(encode != ""){

            codeHTML = $.base64Decode(encode);

            if(!$(this).hasClass("load")){
                         
                $(this).addClass("load");
                $(this).html(codeHTML);
            	
            }

        }
        
    });
	
}
);