// Proyecto: Diablitos Fest
// Programador: Hector Cabrera | hector.cabrera@wikot.com
// Developed by Wikot Technologies | www.wikot.com

/*
 * Droppy 0.1.2
 * (c) 2008 Jason Frame (jason@onehackoranother.com)
 */
$.fn.droppy = function(options) {
    
  options = $.extend({speed: 250}, options || {});
  
  this.each(function() {
    
    var root = this, zIndex = 1000;
    
    function getSubnav(ele) {
      if (ele.nodeName.toLowerCase() == 'li') {
        var subnav = $('> ul', ele);
        return subnav.length ? subnav[0] : null;
      } else {
        return ele;
      }
    }
    
    function getActuator(ele) {
      if (ele.nodeName.toLowerCase() == 'ul') {
        return $(ele).parents('li')[0];
      } else {
        return ele;
      }
    }
    
    function hide() {
      var subnav = getSubnav(this);
      if (!subnav) return;
      $.data(subnav, 'cancelHide', false);
      setTimeout(function() {
        if (!$.data(subnav, 'cancelHide')) {
          $(subnav).slideUp(options.speed);
        }
      }, 500);
    }
  
    function show() {
      var subnav = getSubnav(this);
      if (!subnav) return;
      $.data(subnav, 'cancelHide', true);
      $(subnav).css({zIndex: zIndex++}).slideDown(options.speed);
      if (this.nodeName.toLowerCase() == 'ul') {
        var li = getActuator(this);
        $(li).addClass('hover');
        $('> a', li).addClass('hover');
      }
    }
    
    $('ul, li', this).hover(show, hide);
    $('li', this).hover(
      function() { $(this).addClass('hover'); $('> a', this).addClass('hover'); },
      function() { $(this).removeClass('hover'); $('> a', this).removeClass('hover'); }
    );
    
  });
  
};

var timerScreen;
var lastScreen;
var nextTab = "s2";
var rotateTime = 5000; // 5 segs entre diapositivas

$(document).ready(function(){
    $('#bandas_select').droppy();

	// tabs - backstage
	$("#tabs li a").click(function(){
		$(this).parent().addClass("activeTab").siblings().removeClass("activeTab");
		$(".boxes:visible").hide();
		//$("#"+$(this).attr("rel")).slideDown();
		//$("#"+$(this).attr("rel")).show();
		$("#"+$(this).attr("rel")).fadeIn();
		return false;
	});
	
	$("#selector").click(function(){
		toggleSelector();
		return false;
	});
	
	// tabs - home
	lastScreen = $(".screened:last").attr("id");
	
	timerScreen = setTimeout('rotateScreen()', rotateTime);
	
	$("#screen_tabs li a").click(function(){
		var activetab = $(this).attr("rel");
		if ($("#"+activetab).is(":hidden")) {
			clearTimeout(timerScreen);
			$(".screened:visible").hide();
			$(this).parent().addClass("activeScreenTab").siblings().removeClass("activeScreenTab");
			$("#"+activetab).fadeIn();
		}
		return false;
	});
	
	// tabs - perfil
	$("#perfil_tabs li a").click(function(){
		$("#perfil_tabs li").removeClass("p_activeTab");
		$(this).parent().addClass("p_activeTab");
		$("#biotxt, #inftxt").hide();
		$("#"+$(this).attr("rel")).show();
		return false;
	});
	
	// lista videos - perfil
	$(".vidItem img, .vidItemDesc h4 a").click(function(){
		$(".vidItem").removeClass("beingPlayed");
		if ($(this).parent().is("h4")) {
			$(this).parent().parent().parent().addClass("beingPlayed");
		} else {
			$(this).parent().parent().addClass("beingPlayed");
		}
		
		return false;
	});
	
	// voto video - perfil
	$("#btn_megusta_perfil, #btn_nomegusta_perfil").click(function(){
		$("#cintillo_sml_perfil a").removeClass("perfil_mivoto");
		$(this).addClass("perfil_mivoto");
		return false;
	});
	
	/*$("#btn_megusta_wallpaper, #btn_nomegusta_wallpaper, #btn_megusta_wallpaper_fav, #btn_nomegusta_wallpaper_fav").click(function(){
		$("#cintillo_wallpaper a").removeClass("perfil_mivoto");
		$(this).addClass("perfil_mivoto");
		return false;
	});*/
	
	// seleccion wallpaper
	$("#btn_descargar_wallpaper").click(function(){
        var idf = $("#topwallpaper a").attr("idf");
        var clave = $("#topwallpaper a").attr("clave");
	    $("#wallpaper_home #wallpaper_select #imgSelect1").attr("href","javascript:setDownload(\"image1280x960\"," + idf + ",\"" + clave + "\");");
	    $("#wallpaper_home #wallpaper_select #imgSelect2").attr("href","javascript:setDownload(\"image1024x768\"," + idf + ",\"" + clave + "\");");
	    $("#wallpaper_home #wallpaper_select #imgSelect3").attr("href","javascript:setDownload(\"image800x600\"," + idf + ",\"" + clave + "\");");
	    
		if ($("#wallpaper_select").is(":hidden")) {
			$("#wallpaper_velo").slideDown("normal", function(){
				$("#wallpaper_select").fadeIn();
			});			
		} else {
			$("#wallpaper_select").fadeOut("normal", function(){
				$("#wallpaper_velo").slideUp();
			});			
		}		
		return false;
	});
		
	
	$("#btn_descargar_wallpaper_fav").click(function(){
		if ($("#wallpaper_select_fav").is(":hidden")) {
			$("#wallpaper_velo_fav").slideDown("normal", function(){
				$("#wallpaper_select_fav").fadeIn();
			});			
		} else {
			$("#wallpaper_select_fav").fadeOut("normal", function(){
				$("#wallpaper_velo_fav").slideUp();
			});			
		}
		return false;
	});
	
	$("#titulo_popular_colapsable a").click(function(){
		if ($(this).hasClass("popular_colapsado")) {
			$(this).removeClass("popular_colapsado");
			$("#rankingperfil").slideUp();
		} else {
			$(this).addClass("popular_colapsado");
			$("#rankingperfil").slideDown();
		}
		return false;
	});
	
	// ranking perfil		
	$("#video_perfil_fav_close").click(function(){	    
		$("#video_perfil_fav").hide("fast", function(){		      
			$("#velo_display_fav").hide();			
		});		
		$("#video_perfil_fav #object").html("");  
		return false;
	});
	
	$("#wallpaper_perfil_close_fav").click(function(){
		$("#wallpaper_perfil_fav").hide("fast", function(){
			$("#velo_display_fav").hide();
		});
		return false;
	});
	
	$("#rankingperfil #topvideos a").click(function(){
		var target = $(this).attr("href");
		/*$("#velo_display_fav").show();
		$("#video_perfil_fav").fadeIn();*/
		$("#velo_display_fav").slideDown("normal", function(){
			$("#video_perfil_fav").fadeIn();
		});
		
		$('#rankingperfil #object').html(insertarVideo(target));
		return false;
	});
	
	// popups footer
	$("#footer p a").popIt({center:1, width:617, height:695});
	// personajes comentarios
	$("#personaje_left, #personaje_right").fadeIn();
	
});

function toggleSelector() {
	if ($("#velo_lista_bandas").is(":visible")) {
		$("#lista_bandas").fadeOut("normal", function(){
			$("#velo_lista_bandas").slideUp();
		});			
	} else {
		$("#velo_lista_bandas").slideDown("normal", function(){
			$("#lista_bandas").fadeIn();
		});
	}
}

function rotateScreen() {
	$("#screen_tabs li").removeClass("activeScreenTab");
	
	$("#screen_tabs li a[rel="+nextTab+"]").parent().addClass("activeScreenTab");
	$(".screened").hide();
	$("#"+nextTab).fadeIn();
	
	if (nextTab != lastScreen) {
		nextTab = $("#screen_tabs li a[rel="+nextTab+"]").parent().next().children("a").attr("rel");
	} else {
		nextTab = "s1";
	}
	
	timerScreen = setTimeout("rotateScreen()", rotateTime);
}
