// Proyecto: Diablitos Fest
// Programador: Hector Cabrera | hector.cabrera@wikot.com
// Developed by Wikot Technologies | www.wikot.com

$(document).ready(function () {
    /* velo registro */
    var dh = $(document).height();
    var dw = $(document).width();

    /*if ( dh < ( $("#registro").height() + parseInt($("#registro").css("top")) ) ) {
		dh = $("#registro").height() + ( parseInt($("#registro").css("top")) * 2 );
	}*/
	if (dh < 1050) dh = 1050;

    $("#velo_registro").width(dw).height(dh);
    var regpos = parseInt((dw - $("#registro").width()) / 2);

    $("#registro, #modificar, #login, #lyrics").css({ "left": regpos + "px" });

    $("#btn_registro, #btn_log_reg a").click(function () {
		//CODIGO PARA ABRIR EL VELO DE REGISTRO AGREGADO
		var topOffset = $(document).scrollTop() + 50;
		$('#registro').css('top',topOffset);
		dh = $(document).height();
        $("#velo_registro").height(dh).show();
		//CODIGO PARA ABRIR EL VELO DE REGISTRO AGREGADO
        $("#registro").fadeIn();
        $("#hazlogin").show();
        if($.cookie("existBD")!="1"){
            $("#hazlogin").hide();
            $(".iframed iframe").attr("src", "reminder.aspx");
        }else{
            $(".iframed iframe").attr("src", "registro.aspx");
        }
        return false;
    });

    $("#foto_perfil img, #btn_modificar, #avatarPhoto").click(function () {
		//CODIGO PARA ABRIR EL VELO DE REGISTRO AGREGADO
		var topOffset = $(document).scrollTop() + 50;
		$('#registro').css('top',topOffset);
		dh = $(document).height();
        $("#velo_registro").height(dh).show();
		//CODIGO PARA ABRIR EL VELO DE REGISTRO AGREGADO
        $("#registro").fadeIn();
        $("#hazlogin").hide();
        $(".iframed iframe").attr("src", "modificar.aspx");
        return false;
    });

    $("#display_login").click(function () {        
        resizeParent("reg_top", 280, false, false);
        $(".iframed iframe").attr("src", "login.aspx");        
        return false;
    });

    $("#btn_forgot_pass").click(function () {
        resizeParent("reg_top", 280, false, false);
        $(".iframed iframe").attr("src", "recuperar_pass.aspx");
        $("#hazlogin").hide();
		//CODIGO PARA ABRIR EL VELO DE REGISTRO AGREGADO
		var topOffset = $(document).scrollTop() + 50;
		$('#registro').css('top',topOffset);
		dh = $(document).height();
        $("#velo_registro").height(dh).show();
		//CODIGO PARA ABRIR EL VELO DE REGISTRO AGREGADO
        $("#registro").fadeIn();
        return false;
    });

    $("#registro #close a").click(function () {
        $.cookie("recordatorio", "no");
        $("#avatarPhoto").attr("src","photo.aspx?id="+$.cookie("UserId"));            
        $("#nombre_usuario").html($.cookie("firstnUser"));
        $("#genero_musical").html("g&eacute;nero musical: "+$.cookie("generoMusical")); 
        cerrar_velo_registro();
        return false;
    });

    /* velo modificar */
    /*$("#foto_perfil a").click(function(){
    $("#velo_registro").show();
    $("#modificar").fadeIn();
    return false;
    });*/

    $("#close_modificar a").click(function () {                   
        $("#modificar").fadeOut("normal", function () {            
            $("#velo_registro").hide();
        });
        return false;
    });


    /* velo lista de bandas */
    $("#velo_lista_bandas").height($("#lista_bandas").height() + 8);

    /* velo - batalla */
    $("#batalla_close a").click(function () {
        $("#video_batalla, #velo_batalla").hide();
        $("#selector").bind("click", toggleSelector);
        return false;
    });

    /* velo videos home */
    $("#topvideos a").click(function () {
        
        var target = $(this).attr("href");
        var title = $(this).attr("title");
        var idb = $(this).attr("idb");
        var idf = $(this).attr("idf");
        var key = $(this).attr("key");
        var fb = $(this).attr("fb");
        var clave = $(this).attr("clave");
        
        setView(idf,"video");
        
        $("#velo_video_home").slideDown("normal", function () {
            $("#video_home").fadeIn();
        });

        $("#video_home #object").html(insertarVideo(target));
        $("#titulo_video_home").html(title);
        $("#btn_player_home").html("<a href='javascript:setDownload(\"video\"," + idf + ",\"" + clave + "\");' id='btn_descargar'><span>Descargar</span></a><a href='perfil.aspx?id=" + idb + "&key=" + key + "&fb=" + fb + "' id='btn_ver_perfil'><span>Ver perfil</span></a>");
        //$("#video_home #btn_votar").attr("href", "votacion.aspx?id=" + idb);
        //$("#video_home #btn_votar").attr("href", "javascript:setVote(" + idb + ");");
        
        //$("#video_perfil_fav #btn_votar").attr("href", "javascript:setVote(" + idb + ");");
        
        return false;
    });
    
    

    $("#video_home_close a").click(function () {
        $("#video_home").fadeOut("normal", function () {         
            $("#velo_video_home").slideUp();
        });
        $("#video_home #object").html("");  
        return false;
    });

    /* velo wallpaper home */
    $("#topwallpaper a").click(function () {      
        var target = $(this).attr("href");
        var imgName = $(this).attr("rel");
        var idf = $(this).attr("idf");
        
        //--------------
        $("#cintillo_wallpaper a").removeClass("perfil_mivoto"); 
           
        if($.cookie("setIlikeimage"+idf)==null){        
	        $("#wallpaper_home #cintillo_wallpaper #btn_nomegusta_wallpaper").attr("href","javascript:setIlike(\"image\"," + idf + ", \"nomegusta\");");
	        $("#wallpaper_home #cintillo_wallpaper #btn_megusta_wallpaper").attr("href","javascript:setIlike(\"image\"," + idf + ", \"megusta\");"); 
	    }else{
	        $("#wallpaper_home #cintillo_wallpaper #btn_nomegusta_wallpaper").attr("href","javascript:updateIlike("+ idf + "," + $.cookie("setIlikeimage"+idf) + ",\"nomegusta\",\"image\");");
	        $("#wallpaper_home #cintillo_wallpaper #btn_megusta_wallpaper").attr("href","javascript:updateIlike("+ idf + "," + $.cookie("setIlikeimage"+idf) + ",\"megusta\",\"image\");");
	        if($.cookie("setIlikeAccimage"+idf)=="megusta")
	            $("#cintillo_wallpaper #btn_megusta_wallpaper").addClass("perfil_mivoto");
	        else
	            $("#cintillo_wallpaper #btn_nomegusta_wallpaper").addClass("perfil_mivoto");
	    }
        //--------------

        $("#velo_video_home").slideDown('normal', function () {
            $("#wallpaper_home").fadeIn();
        });
        changeWallpaper(target, imgName, $("#canvas_wallpaper"), $("#wallpaper_select"));
        return false;
    });

    $("#wallpaper_home_close").click(function () {
        $("#wallpaper_home").fadeOut('normal', function () {
            $("#velo_video_home").slideUp();
        })
        return false;
    });

    /* velo wallpaper perfil */
    $("#carrusel_canvas ul li a").click(function () {
        $("#velo_video_perfil").slideDown("normal", function () {
            $("#wallpaper_perfil").fadeIn();
        });
        return false;
    });

    $("#wallpaper_perfil_close").click(function () {
        //$("#velo_video_perfil, #wallpaper_perfil").hide();
        $("#wallpaper_perfil").fadeOut("normal", function () {
            $("#velo_video_perfil").slideUp();
        });
        return false;
    });

    $("#rankingperfil #topwallpaper a").click(function () {
        //$("#velo_display_fav, #wallpaper_perfil_fav").show();
        $("#velo_display_fav").slideDown("normal", function () {
            $("#wallpaper_perfil_fav").fadeIn();
        });
        return false;
    });

    $(".btn_wallpaper").click(function () {
        var target = $(this).parent().attr("href");
        var imgName = $(this).parent().attr("rel");
        $("#velo_backstage").show();
        $("#wallpaper_backstage").fadeIn();

        changeWallpaper(target, imgName, $("#canvas_wallpaper_fav"), $("#wallpaper_select_fav"));

        return false;
    });

    $(".btn_video").click(function () {
        var target = $(this).parent().attr("href");
        $("#velo_backstage").show();
        $("#video_backstage").fadeIn();

        $('#video_backstage #object').html(insertarVideo(target));

        return false;
    });

    $("#wallpaper_backstage #wallpaper_perfil_close_fav, #video_backstage #video_perfil_fav_close").click(function () {
        //$("#wallpaper_backstage, #video_backstage").fadeOut();
        //$("#velo_backstage").hide();
        $("#wallpaper_backstage, #video_backstage").fadeOut("normal", function () {
            $("#velo_backstage").slideUp();
        });
        $("#video_backstage #object").html("");  
        return false;
    });

    // ver letra
    $("#ver_letra").click(function () {
        $("#velo_registro").show();
        $("#lyrics").fadeIn();
        return false;
    });

    $("#lyrics #close a").click(function () {
        $("#lyrics").fadeOut();
        $("#velo_registro").hide();
        return false;
    });

    // velo ringtone
    /*$("#topringtones a, #topsingle a").click(function(){
    $("#velo_video_home").show();
    return false;
    });*/

    // link de prueba
    $("#btn_votacion_2").click(function () {
        var url = $(this).attr("href");
        mostrarVeloVotacion(url, "votacion_2");
        return false;
    });
});

function mostrar_velo_batalla() {
	$("#selector").unbind("click");
	$("#velo_batalla").show();
	$("#video_batalla").fadeIn();
}

function resizeParent(o, ht, changeBottom, showhazLogin) {
	if (showhazLogin) {
		$("#hazlogin").fadeIn();
		$("#reg_bot").css("background", "url(images/bg_velo_bottom.gif)");
	} else {
		$("#hazlogin").fadeOut();
	}
	
	if (changeBottom) {
		$("#"+o).animate({height: ht+"px"}, 700, function(){			
			$("#reg_bot").css("background", "url(images/bg_velo_bottom_cintillo.gif)");
		});		
	} else {
		$("#"+o).animate({height: ht+"px"}, 700);
	}
}

function cerrar_velo_registro() {
	$("#registro").fadeOut("normal", function(){
		$("#velo_registro").hide();
		$("#reg_top").css("height", "650px");
		$("#reg_bot").css("background", "url(images/bg_velo_bottom.gif)");
		$("#error_formulario").hide();
	});
}

function insertarVideo(url, w, h) {
	if (w == null){
		w = 480;
	}
	if (h == null){
		h = 295;
	}	
	return '<object width="'+w+'" height="'+h+'"><param name="movie" value="'+url+'"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><param name="wmode" value="transparent"></param><embed src="'+url+'color1=0x3a3a3a&color2=0x999999" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="'+w+'" height="'+h+'" wmode="transparent"></embed></object>';	
}

function mostrarVeloVotacion(url, target, title, id_video, id_band, twitter, fb, key) {  
    //alert(url+"-"+target+"-"+title+"-"+id_video+"-"+id_band+"-"+twitter+"-"+fb+"-"+key);  
	$(".velo").show();
	var size = url.length-2;
	var url_ = url.substr(1,size);
	size = title.length-2;
	var title_ = title.substr(1,size);
	size = twitter.length-2;
	var twitter_ = twitter.substr(1,size);
	size = fb.length-2;
	var fb_ = fb.substr(1,size);
	size = key.length-2;
	var key_ = key.substr(1,size);
	
	//alert("llamando a velo votacion "+ url_+" - "+target+" - "+title_+" - "+id_video+" - "+id_band+" - "+twitter_+" - "+fb_);
	
	if (target == "votacion_2") {
		insertarVideoVotacion(url_, $("#video_votacion_2 #object"));
		insertarInfoVideoVotacion(title_, $("#video_votacion_2 #titulo_video_home"), id_video, id_band, $("#video_votacion_2 #btn_player_home"), twitter_, fb_, key_);
		$("#video_votacion_2").fadeIn();
	} else if (target == "votacion_9") {
		insertarVideoVotacion(url_, $("#video_votacion_9 #object"));
		insertarInfoVideoVotacion(title_, $("#video_votacion_9 #titulo_video_home"), id_video, id_band, $("#video_votacion_9 #btn_player_home"), twitter_, fb_, key_);
		$("#video_votacion_9").fadeIn();
	} else if (target == "votacion_12") {
		insertarVideoVotacion(url_, $("#video_votacion_12 #object"));
		insertarInfoVideoVotacion(title_, $("#video_votacion_12 #titulo_video_home"), id_video, id_band, $("#video_votacion_12 #btn_player_home"), twitter_, fb_, key_);
		$("#video_votacion_12").fadeIn();
	}
}

function insertarVideoVotacion (url, target) {
    //alert("insert video "+url+" - "+target);
	target.html('<object width="480" height="295"><param name="movie" value="'+url+'"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><param name="wmode" value="transparent"></param><embed src="'+url+'" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="480" height="295" wmode="transparent"></embed></object>');
}

function insertarInfoVideoVotacion (title, target, id_video, id_band, target2, twitter, fb, key) {
    //alert('insert info video '+title+" - "+target+" - "+id_video+" - "+id_band+" - "+target2+" - "+twitter+" - "+fb+" - "+key);
	target.html('<div id="titulo_video_home">'+title+'</div>');
	target2.html("<div id='btn_player_home'><a href='javascript:setDownload(\"video\"," + id_video + ",\"" + key + "\");' id='btn_descargar'><span>Descargar</span></a><a href='perfil.aspx?id="+id_band+"&key="+twitter+"&fb="+fb+"' id='btn_ver_perfil'><span>Ver perfil</span></a></div>");
}


function changeWallpaper(url, imageName, targetCanvas, targetSelector) {
	targetCanvas.children("img").attr("src", url);
	targetSelector.children("li#select1").children("a").attr("href", "images/"+imageName+"_1280x960.jpg");
	targetSelector.children("li#select2").children("a").attr("href", "images/"+imageName+"_1024x760.jpg");
	targetSelector.children("li#select3").children("a").attr("href", "images/"+imageName+"_800x600.jpg");
}
