function initNR(){
    sidebarSliders();
	externalLinks();
	narracaoLinks();
    contactFormMagic();
	searchFormTip();
    $("#sidebar div#subMenu ul li:last-child a").css("border","none");
    redesSociais();
    dropdownAlbuns();
    dropdownSeasons();
    if(typeof(trataLinksVideos)!="undefined") trataLinksVideos();
	$(".gender input[type=checkbox]").attr("checked", "");
}

function sidebarSliders(){
    nomes = ['fotos', 'videos']
    for (var i = 0;i<nomes.length ;i++ ){
        var nome=nomes[i]
        var pos = getCookie('ck'+nome)
        if (pos!='fechado') pos = 'aberto'
        if (pos=='fechado'){
          $("#slide-"+nome+" h4").next().hide()
          $("#slide-"+nome+" .maisoutros").hide()
        }
        $("#slide-"+nome+" h4 a.controlSidebarPanel").click(function () {
          var nome = $(this).parent().parent().attr('id').split('-')[1];
		  var classe = $(this).hasClass("fechar")?"abrir":"fechar";
          $(this).attr("class", "controlSidebarPanel");
		  $(this).addClass(classe);
		  if(classe=='abrir'){
              $("#slide-"+nome+" .maisoutros").hide()    
		  }else{
              $("#slide-"+nome+" .maisoutros").show()
		 }
          $(this).parent().next().slideToggle("fast")
          setCookie('ck'+nome, classe=='abrir'?'fechado':'aberto')
        });
    }
}

function contactFormMagic(){
    var fcon=$("form.form-contato");
    var sass=$("form.form-contato fieldset.fs-assunto select#assunto");
    if(fcon) if(sass){
        if($("form.form-contato fieldset.fs-assunto select#assunto").val()==-1)
            $("form.form-contato fieldset.fs-contato").hide();

        sass.change(function (){
            $("form.form-contato fieldset.fs-contato").hide();
            var furl=$(this.options[this.selectedIndex]).attr("title");
            if(furl!=""){
                location=furl;
                //$("form.form-contato .mensagem-aguarde").show('slow');
            }else if($(this).val()!=-1){
                $("form.form-contato fieldset.fs-contato").show();
            }
        });
    }
}

function externalLinks(){
	$(".external").attr("target","_blank");
	$(".externals a").attr("target","_blank");
}

function narracaoLinks(){
    var destino='';
	$(".narracao").click(function (){
        var destino=$(this).attr('href');
        var idjogo=destino.split('=');
        idjogo=idjogo.pop();
		if(window.open(destino, "narracaoTorneio"+idjogo, "height=670,width=553,resizable=yes,scrollbars=no,status=no,toolbar=no")){
			return false;
		}else{
			return true;
		}
    });
}

function searchFormTip(){
	if(document.forms.length>0){
		var s_frm=document.forms[0];
		var s_el=s_frm.elements["s"];
		if(s_el!="undefined"){
			addEvent(s_el,"focus",s_elFocus);
			addEvent(s_el,"blur",s_elBlur);
			addEvent(s_frm,"submit",s_fSubmit);
		}
	}
}

function s_fSearchTerm(){
	var sq="";
	if(document.forms.length>0){
		var s_frm=document.forms[0];
		var s_elq=s_frm.elements["sq"];
		if(s_elq!="undefined") sq=s_elq.value;
	}
	return sq;
}
function s_fSubmit(e){
	var s_frm=getSource(e);
	var s_el=s_frm.elements["s"];
	if(s_el!="undefined") if(s_el.value==s_fSearchTerm()) s_el.value="";
	s_frm.elements["sq"].parentNode.removeChild(s_frm.elements["sq"]);
}
function s_elFocus(e){
	var s_el=getSource(e);
	if(s_el.value==s_fSearchTerm()) s_el.value="";
}
function s_elBlur(e){
	var s_el=getSource(e);
	if(s_el.value.replace(/\s/gi,'')=='') s_el.value=s_fSearchTerm();
}

function redesSociais(){
  $('#maisRedeSocial').hide();
  $(".outros").show();
  $(".outros").click(function () { 
    $('#maisRedeSocial').show(); 
  });
  $(".outros").hover(function () {
      $('#maisRedeSocial').show();
    }, function () {
      $('#maisRedeSocial').hide();
  });
}

function dropdownAlbuns(){
    $('div.maisoutros ul').hide();
    $('div.maisoutros span').toggle(function () {
      $('div.maisoutros ul').slideDown();
    }, function () {
      $('div.maisoutros ul').slideUp("fast");
	});
	$('#slide-fotos h4 span').toggle(function () {
      $('div.maisoutros ul').slideDown();
    }, function () {
      $('div.maisoutros ul').slideUp("fast");
	});
}
function dropdownSeasons(){
    $('ul.temporadasNR ul').hide();
    $('ul.temporadasNR span').toggle(function () {
      $('ul.temporadasNR ul').slideDown();
    }, function () {
      $('ul.temporadasNR ul').slideUp("fast");
  });
}
//////////////


function fals(e){
	if(typeof(e)!='undefined') if(e.preventDefault) e.preventDefault();
	return false;
}

function addEvent(obj, evType, fn){
    if(obj.addEventListener) obj.addEventListener(evType, fn, true);
    if(obj.attachEvent) obj.attachEvent("on"+evType, fn);
}

function getSource(e){
	if(typeof(e)=='undefined') var e=window.event;
	obj=e.target?e.target:e.srcElement;
	if(obj.nodeType == 3) obj=obj.parentNode;
	return obj;
}

function getForm(e){
	frm=getSource(e);
	while(frm.tagName!="FORM") frm=frm.parentNode;
	return frm;
}

function getCookie( name ) {
    var start = document.cookie.indexOf( name + "=" );
    var len = start + name.length + 1;
    if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
      return null;
    }
    if ( start == -1 ) return null;
    var end = document.cookie.indexOf( ';', len );
    if ( end == -1 ) end = document.cookie.length;
    return unescape( document.cookie.substring( len, end ) );
}

function setCookie( name, value, expires, path, domain, secure ) {
    var today = new Date();
    today.setTime( today.getTime() );
    if ( expires ) {
      expires = expires * 1000 * 60 * 60 * 24;
    }
    var expires_date = new Date( today.getTime() + (expires) );
    document.cookie = name+'='+escape( value ) +
      ( ( expires ) ? ';expires='+expires_date.toGMTString() : '' ) + //expires.toGMTString()
      ( ( path ) ? ';path=' + path : ';path=/' ) +
      ( ( domain ) ? ';domain=' + domain : '' ) +
      ( ( secure ) ? ';secure' : '' );
}

function deleteCookie( name, path, domain ) {
    if ( getCookie( name ) ) document.cookie = name + '=' +
        ( ( path ) ? ';path=' + path : ';path=/') +
        ( ( domain ) ? ';domain=' + domain : '' ) +
        ';expires=Thu, 01-Jan-1970 00:00:01 GMT';
}


//////////////

$(document).ready(initNR);

