//**** VALLK_BOX -------------------------------------------------------------------------------------------------
var vallk_box_load=false;
var target="";
var c;
$(document).ready(function(){
	
	$("body").prepend("<div id='block_screen'></div><div id='vContainer' style='position:absolute;'></div>");
	
	$(".vallk_box").live("click",function(event){
		event.preventDefault();
		t=$(this);
		target=t.attr("href");
		show_vallk_box(t);	
	});
	
	$("#block_screen").live("click",function(event){
		event.preventDefault();
		t=$(this);
		if(target.indexOf('episodeBoxIE')==-1 && target.indexOf('registro')==-1){
			close_vallk_box(t);	
		}
	});
	
	$(window).scroll(function () {
		var isiPad = null;
		if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPad/i))) isiPad='yes';
		if(vallk_box_load==true && isiPad==null){
			position_vallk_box();
		}
	});
	
	$(window).resize(function () {
		var isiPad = null;
		if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPad/i))) isiPad='yes';
		if(vallk_box_load==true && isiPad==null){
			position_vallk_box();
		}
	});
	
	$("#vContainer .vallk_close").live("click",function(event){
		event.preventDefault();
		close_vallk_box();
	});

	$("#vContainer").change(function(){
		h=vContainer.find("#lightbox_one").outerHeight();
		w=vContainer.find("#lightbox_one").outerWidth();
		alert(w+" - "+h);
		vContainer.css("width",w+"px");
		vContainer.css("height",h+"px");
		position_vallk_box();	
	});
	
});

$(document).keyup(function(event){
	if(c==undefined){
		if(event.which==27) close_vallk_box(null);
	}
});

function close_vallk_box(t){
	vallk_box_load=false;
	$("#vContainer").fadeOut(250,function(){
		$(this).html("");
		$("#block_screen").fadeOut(300);
	});
}

function show_vallk_box(t){
	var vContainer=$("#vContainer");
	var href;
	var h,w,l;

	if(typeof t=="string") href=t; else	href=t.attr("href");
	values=href.split("?");
	if(values[1]!=undefined){
		values=values[1].split("&");
		for(i=0;i<values.length;i++){
			value=values[i].split("=");
			if(value[0]=="w") w=value[1];
			if(value[0]=="h") h=value[1];
			if(value[0]=="l") l=value[1];
			if(value[0]=="c") c=value[1];
		}
	}
	/*if(vContainer.css("width")!=w+"px" && vContainer.css("height")!=h+"px"){
		vContainer.hide();
	}*/
	if(l==undefined){
		vContainer.css("width","72px");
		vContainer.css("height","72px");
		vContainer.css("background","url(http://contenido.s3.amazonaws.com/img/loading_back.png) center center no-repeat");
		vContainer.html("<div style='display:block;width:72px;height:72px;background:url(http://contenido.s3.amazonaws.com/img/loading-circles-catalog.gif) center center no-repeat'></div>");
		
		vContainer.show();
		
		$("#block_screen").fadeIn(250);
		
		position_vallk_box();
	}

	$.ajax({
		type: 	'GET',
		url:	href,
		success:  	function(r){
			if(r!=""){
				if(l==undefined){
				vContainer.fadeOut(250,function(){
					vContainer.css("-moz-box-shadow","0 0 10px rgb(0,0,0)");
					vContainer.html(r);
					if(h==undefined) h=vContainer.find("#lightbox_one").outerHeight();
					if(w==undefined) w=vContainer.find("#lightbox_one").outerWidth();
					
					vContainer.css("width",w+"px");
					vContainer.css("height",h+"px");
					position_vallk_box();
					
					vContainer.fadeIn(250,function(){
						vallk_box_load=true;
						
					});
				});
				}else{
					vContainer.css("-moz-box-shadow","0 0 10px rgb(0,0,0)");
					vContainer.html(r);
					if(h==undefined) h=vContainer.find("#lightbox_one").outerHeight();
					if(w==undefined) w=vContainer.find("#lightbox_one").outerWidth();
					
					vContainer.css("width",w+"px");
					vContainer.css("height",h+"px");
					position_vallk_box();

					vallk_box_load=true;
				}
			}
		},
		error: function(r){
		}
	});			
}

function position_vallk_box(){

	var windowHeight;
	var windowWidth;
	
	if (typeof window.innerWidth != 'undefined'){
		windowWidth = window.innerWidth,
		windowHeight = window.innerHeight
	}else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0){
		windowWidth = document.documentElement.clientWidth,
		windowHeight = document.documentElement.clientHeight
	}else{
		windowWidth = document.getElementsByTagName('body')[0].clientWidth,
		windowHeight = document.getElementsByTagName('body')[0].clientHeight
	}
	
	var scrOfY = 0;
	if( typeof( window.pageYOffset ) == 'number' ) {
		scrOfY = window.pageYOffset;
	}else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
		scrOfY = document.body.scrollTop;
	}else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
		scrOfY = document.documentElement.scrollTop;
	}

	$("#block_screen").css("width",windowWidth);
	$("#block_screen").css("height",$(document).height());
	var boxWidth=$("#vContainer").css("width").replace("px","");
	var boxHeight=$("#vContainer").css("height").replace("px","");
	$("#vContainer").css("left",windowWidth/2-boxWidth/2);
	$("#vContainer").css("top",windowHeight/2-boxHeight/2+scrOfY);
	/*$("#vContainer").clearQueue().animate({
			top: windowHeight/2-boxHeight/2+scrOfY
			}, {
				duration: 250,
				specialEasing: {
				  height: 'easeInBounce'
				}
			});*/
}

//****-------------------------------------------------------------------------------------------------
