var validate = new Validate();

$(document).ready(function(){
	$("#link-visualizar-lado-lado").click ( function() {
			$(".lista-padrao").removeClass("lista-padrao-vertical");
			$(".lista-padrao").addClass("lista-padrao-horizontal");
			$("#link-visualizar-detalhes").removeClass("selecionado");
			$(this).addClass("selecionado");
			detalhes(0);
			return false;
    });
	$("#link-visualizar-detalhes").click ( function(){
			$(".lista-padrao").removeClass("lista-padrao-horizontal");
			$(".lista-padrao").addClass("lista-padrao-vertical");
			$("#link-visualizar-lado-lado").removeClass("selecionado");
			$(this).addClass("selecionado");			
			detalhes(1);
			return false;
    });
	
	$("#lista-ajuda span").hide();
	$("#lista-ajuda a").click ( function(){
            $(this).find("span").slideToggle("slow");
			return false;
	});
});
// Funcao complementar que Liga/Desliga os links da paginacao em relacao a visualizacao de detalhes
// ligado = boleano (0 - mostra lado a lado; 1 - mostra com detalhes)
function detalhes(ligado)
{
    var strMatch;
    var strNew;

	if (ligado) {
		strMatch = "&s=0";
		strNew = "&s=1";
	} else {
	 	strMatch = "&s=1";
		strNew = "&s=0";
	}	
    for (var i=0; i<document.links.length; i++) {
        if (document.links[i].href.match(strMatch)) {
            texto = document.links[i].href;
            novo = texto.replace(strMatch,strNew);
            document.links[i].href = novo;
        }else{
            if((document.links[i].id == "link-paginacao") || (document.links[i].id == "link-menu-videos-ordenacao") || (document.links[i].id == "link-menu-videos-periodo")){
                texto = document.links[i].href;
                novo = texto.concat(strNew);
                document.links[i].href = novo;
            }
        }
    }
}

function isEmpty(str)
{
  if(str==null) return true;
  for(var intLoop=0; intLoop<str.length; intLoop++)
    if(" "!=str.charAt(intLoop))
      return false;
  return true; 
}

$.fn.clearForm = function()
{
	return this.each(function()
    {
		var type = this.type, tag = this.tagName.toLowerCase();		
		if (tag == 'form')
			return $(':input',this).clearForm();			
		if (type == 'text' || type == 'password' || tag == 'textarea')
			this.value = '';
		else if (type == 'checkbox' || type == 'radio')
			this.checked = false;
		else if (tag == 'select')
			this.selectedIndex = -1;
	});
};

function validaData(data)
{
    var valorData = data;
    if(isEmpty(valorData))
       return false;
    if(valorData.length > 0 && valorData.length!=10) {
        return false;
    }
    if(valorData.charAt(2)!='/' || valorData.charAt(5)!='/') {
        return false;
    }
  
    var dia = valorData.substr(0,2);
    var mes = valorData.substr(3,2);
    var ano = valorData.substr(6,4);
    if(isNaN(dia) || isNaN(mes) || isNaN(ano) || ano<0001) {
        return false;
    }
  
    if( mes == 01 || mes == 03 || mes == 05 || mes == 07 || mes == 08 || mes == 10 || mes == 12 ) {
        if(dia <= 0 || dia > 31) {
            return false;
        }
    } else if( mes == 04 || mes == 06 || mes == 09 || mes == 11 ) {
        if(dia <= 0 || dia > 30) {
            return false;
        }
    } else if( mes ==02 ) {
        if((ano%4 == 0 && !ano%100 == 0) || (ano%400 == 0) ) {
            if( dia <= 0 || dia > 29 ) {
                return false;
            }
        } else if( dia <= 0 || dia > 28 ) {
            return false;
        }
    } else {
    	return false;
    }
    return true;
}

function toggleDiv(link) {
	var div = link.substr((link.indexOf("-")+1));
	$("#"+div).slideToggle("normal",function(){
		var style_div = $("#"+div).css('display');
		if(style_div == "none"){
			$("#"+link).html("mostrar");
			$.cookie(div,'none',{path:'/'});
		}else{
			$("#"+link).html("ocultar");
			$.cookie(div,'block',{path:'/'});
		}
	});
}

function cookieToggleDiv(div){
	var cookie = $.cookie(div);
	$('#'+div).css('display',cookie);

	var link = ("#link-"+div);
	if(cookie == "none"){
		$(link).html("mostrar");
	}else{
		$(link).html("ocultar");
	}
}

function hex(x) {
    hexDigits = new Array("0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f");
    return isNaN(x) ? "00" : hexDigits[(x - x % 16) / 16] + hexDigits[x % 16];
}

//Function to convert hex format to a rgb color
function rgb2hex(rgb) {
    rgb = rgb.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);
    if( rgb ) {
        if(validate.isNum(rgb[1]) && validate.isNum(rgb[2]) && validate.isNum(rgb[3]) ){
            return "#" + hex(rgb[1]) + hex(rgb[2]) + hex(rgb[3]);
        }else{
            return "#000000";
        }
    }else{
        return "#000000";
    }
}

/**
 * Comment
 */
function changeLabel(element,acao) {
    var id = element.id;
    var tipo = element.type;
    switch( acao ) {
        case 1:
            if ( tipo == 'text' ) {
                if ( $.trim($("#"+id).val().toLowerCase())  == $("#"+id).attr("title").toLowerCase() ){ $("#"+id).val(''); }
            }else if ( tipo == 'textarea' ){
                if ( $.trim($("#"+id).text().toLowerCase()) == $.trim($("#"+id).attr("title").toLowerCase()) ){ $("#"+id).text(""); }
            }
            break;
        case 2:
            if ( tipo == 'text' ) {
                if ($.trim($("#"+id).val()) == ''){ $("#"+id).val($("#"+id).attr("title")); }
            }else if ( tipo == 'textarea' ){
                if ($.trim($("#"+id).text()) == ''){ $("#"+id).text($("#"+id).attr("title")); }
            }
            break;
    }
}
