var $  = jQuery;

var box_height = null;
var c_height   = null;

$(function(){
	wrapper();
	ajaxMenus();
	loadFotos(ajax_href)
});

function ajaxMenus () {
	$("ul.menu > li > a").unbind()
	
	.bind('mouseover', function(){
		var $this = $(this);
		var submenu = $this.attr("id").replace("m","yn-menu");
		var level = $this.attr("id").match(/m([\d]+)-[\d]+/)[1];
		var menu = '#yn-menu'+level;
		var $submenu = $("#"+submenu);
		
		if($submenu.length){
			if(level){
				$(menu).find("ul").css("display", "none");
				$submenu.css("display", "block");
				$(menu).fadeIn("slow");
			}
		} else {
			$(menu).fadeOut("fast", function(){
				$(menu).find("ul").css("display", "none");
			});
		}
		
	})
	
	.bind('click', function(){
		var $this = $(this);
		var href = $this.attr('rel');
		console.log(href);
		var $content = $("#yn-content");
		var submenu = $this.attr("id").replace("m","yn-menu");
		var level = $this.attr("id").match(/m([\d]+)-[\d]+/)[1];
		var menu = '#yn-menu'+level;
		var $submenu = $("#"+submenu);
		
		$.ajax({
			'beforeSend': function(){
				$("#yn-fotos").fadeTo("fast", 0.5, function(){
					$(this).empty();
					if($this.hasClass("root")){
						$("#yn-menu1, #yn-menu2").fadeOut("fast", function(){
							$(this).find("ul").css("display", "none");
						});
					} else {
						$(menu).fadeOut("fast", function(){
							$(this).find("ul").css("display", "none");
						});
					}

					$content.html("").height(100).addClass('loading');
				});
			},
			'url' : href,
			'success' : function(data){
				$content.html(data);
			},
			'complete' : function(){
				$content.removeClass('loading');
				loadFotos(href);
				// fb();
			}
		});
		
		return false;
	});
}

function loadFotos (href) {
	
	var $fotos = $("#yn-fotos");
	var href_f = href.replace('ajax','ajax-fotos');
		
	$.ajax({
		'beforeSend': function(){
			$fotos.html("");
		},
		'url' : href_f,
		'success' : function(data){
			$fotos.html(data);
			if(data){
				$fotos.fadeTo('slow',1);
				fancy();
			}
		},
			
		'complete' : function(){
			
		}
	});
}

function wrapper () {
	var w = $(window).width();
	var h = $(window).height();
	
	$("#yn-wrapper").height(h);
	$("#yn-fotos").css({ 'opacity' : 0.5 });
	$('input#s').example('digite e tecle enter');
	
	var $box   = $("#yn-box");
	var $fotos = $("#yn-fotos");
	var box_h  = $box.height();
	var limit  = parseInt($box.height() + $box.offset().top);
	var foot_h = parseInt($("#yn-footer").height());
	
	box_height = box_h + (h - (limit+foot_h))
	c_height   = (box_height - 60);
	
	$box.height(box_height);
	$fotos.height(box_height);
	$("#yn-texto").height(c_height);
	
	fb();
}

function showMenu (ul_id) {

	for (var i = 1; i <= 2; i++) {
		var $ul = $("#yn-menu"+i+"-"+ul_id);
		
		if($ul.length){
			$("#yn-menu"+i).find("ul").css("display", "none");
			$ul.css("display", "block");
			$("#yn-menu"+i).fadeIn("slow")
		}
	};
}

function showParentMenu (parent_id) {

	for (var i = 1; i <= 2; i++) {
		var $ul = $("#yn-menu"+i+"-"+parent_id);
		
		if($ul.length){
			$("#yn-menu"+i).find("ul").css("display", "none");
			$ul.css("display", "block");
			$("#yn-menu"+i).fadeIn("slow")
		}
	};
}

function fancy () {
	// Supported file extensions
	    var thumbnails = 'a:has(img)[href$=".bmp"],a:has(img)[href$=".gif"],a:has(img)[href$=".jpg"],a:has(img)[href$=".jpeg"],a:has(img)[href$=".png"],a:has(img)[href$=".BMP"],a:has(img)[href$=".GIF"],a:has(img)[href$=".JPG"],a:has(img)[href$=".JPEG"],a:has(img)[href$=".PNG"]';

	    jQuery(thumbnails).addClass("fancybox").attr("rel","fancybox").getTitle();

	    jQuery("a.fancybox").fancybox({
		    'imageScale': true,
		    'padding': 5,
		    'zoomOpacity': true,
		    'zoomSpeedIn': 300,
		    'zoomSpeedOut': 300,
		    'overlayShow': true,
		    'overlayOpacity': 0.3,
		    'hideOnContentClick': false,
		    'centerOnScroll': true
		});
}

function fb () {
	jQuery("a.iframe").fancybox({
		'frameWidth': 500,
		'frameHeight': 450,
		'padding': 0
	});
}
