﻿
var tb_pathToImage = "/images/screenshots/style/loadingAnimation.gif";
var tb_pathToCloseLabelImage = "/images/screenshots/style/closelabel.gif";
var txt_hint_close = "Pulse aquí para cerrar la ventana";
var txt_hint_resize = "Pulse aquí para redimensionar la imagen";
var txt_resize_caption = "Pulse en la imagen para redimensionarla";
var txt_close_caption = "Pulse en la imagen para cerrarla";
var text_close_link = "Cerrar";

//on page load call tb_init
$(document).ready(function(){   
	if( !is_IE6())
    {   
	    tb_init('a.thickbox, area.thickbox, input.thickbox');//pass where to apply thickbox
	    tb_init_movie('a.embedbox, area.embedbox, input.embedbox');//pass where to apply embedbox
	    imgLoader = new Image();// preload image
	    imgLoader.src = tb_pathToImage;
	}
});

function tb_init(domChunk){
	$(domChunk).click(function(){
	var t = this.title || this.name || null;
	var a = this.href || this.alt;
	var g = this.rel || false;
	tb_show(t,a,g);
	this.blur();
	return false;
	});
}

function tb_show(caption, url, imageGroup) {//function called when the user clicks on a thickbox link

	try {
		if (typeof document.body.style.maxHeight === "undefined") {//if IE 6
			$("body","html").css({height: "100%", width: "100%"});
			$("html").css("overflow","hidden");
			if (document.getElementById("TB_HideSelect") === null) {//iframe to hide select elements in ie6
				$("body").append("<iframe id='TB_HideSelect'></iframe><div id='TB_overlay'></div><div id='TB_window'></div>");
				$("#TB_overlay").click(tb_remove);
			}
		}else{//all others
			if(document.getElementById("TB_overlay") === null){
				$("body").append("<div id='TB_overlay'></div><div id='TB_window'></div>");
				$("#TB_overlay").click(tb_remove);
			}
		}
		
		if(tb_detectMacXFF()){
			$("#TB_overlay").addClass("TB_overlayMacFFBGHack");//use png overlay so hide flash
		}else{
			$("#TB_overlay").addClass("TB_overlayBG");//use background and opacity
		}
		
		if(caption===null){caption="";}
		$("body").append("<div id='TB_load'><img src='"+imgLoader.src+"' /></div>");//add loader to the page
		$('#TB_load').show();//show loader
		
		var baseURL;
	   if(url.indexOf("?")!==-1){ //ff there is a query string involved
			baseURL = url.substr(0, url.indexOf("?"));
	   }else{ 
	   		baseURL = url;
	   }
	   
	   var urlString = /\.jpg$|\.jpeg$|\.png$|\.gif$|\.bmp$/;
	   var urlType = baseURL.toLowerCase().match(urlString);

		if(urlType == '.jpg' || urlType == '.jpeg' || urlType == '.png' || urlType == '.gif' || urlType == '.bmp'){//code to show images
				
			imgPreloader = new Image();
			imgPreloader.onload = function(){		
			imgPreloader.onload = null;
				
			// Resizing large images - orginal by Christian Montoya edited by me.
			var pagesize = tb_getPageSize();
			var x = pagesize[0] - 150;
			var y = pagesize[1] - 150;
			var imageWidth = imgPreloader.width;
			var imageHeight = imgPreloader.height;
			var originalSize = true;
			if (imageWidth > x) {
			    originalSize = false;
				imageHeight = imageHeight * (x / imageWidth); 
				imageWidth = x; 
				if (imageHeight > y) { 
					imageWidth = imageWidth * (y / imageHeight); 
					imageHeight = y; 
				}
			} else if (imageHeight > y) { 
			    originalSize = false;
				imageWidth = imageWidth * (y / imageHeight); 
				imageHeight = y; 
				if (imageWidth > x) { 
					imageHeight = imageHeight * (x / imageWidth); 
					imageWidth = x;
				}
			}
			// End Resizing
			
			TB_WIDTH = imageWidth + 30;
			TB_HEIGHT = imageHeight + 60;
			
			tb_render(caption, imageWidth, imageHeight, TB_WIDTH, TB_HEIGHT, url, originalSize, false);
			
			
			$("#TB_closeWindowButton").click(tb_remove);
			$("#TB_closeWindowLink").click(tb_remove);
		
			document.onkeydown = onkeydown_event;	
			tb_position();
			$("#TB_load").remove();
			
			if( originalSize)
			{
			    $("#TB_ImageOff").click(tb_remove);
			}
			else
			{
			    $("#TB_ImageOff").addClass("zoomIn");
			    $("#TB_ImageOff").click(function(){
			
			            $("#TB_window").remove();
					    $("body").append("<div id='TB_window'></div>");
					    tb_show_full_size(caption, url);
					    return false;	
			    });
			}
			
			$("#TB_window").css({display:"block"}); //for safari using css instead of show
			};
			
			imgPreloader.src = url;
		}
		
	} catch(e) {
		//nothing here
	}
}

function onkeydown_event(e)
{
    if (e == null) { // ie
	    keycode = event.keyCode;
	} else { // mozilla
	    keycode = e.which;
	}
	if(keycode == 27){ // close
		tb_remove();
		tb_remove_movie();
	} 
}

//helper functions below
function tb_showIframe(){
	$("#TB_load").remove();
	$("#TB_window").css({display:"block"});
}

function tb_remove() {
 	$("#TB_imageOff").unbind("click");
	$("#TB_closeWindowButton").unbind("click");
	$("#TB_window").fadeOut("fast",function(){$('#TB_window,#TB_overlay,#TB_HideSelect').trigger("unload").unbind().remove();});
	$("#TB_load").remove();
	if (typeof document.body.style.maxHeight == "undefined") {//if IE 6
		$("body","html").css({height: "auto", width: "auto"});
		$("html").css("overflow","");
	}
	document.onkeydown = "";
	document.onkeyup = "";
	return false;
}

function tb_position() {
    var pagesize = tb_getPageSize();
	var x = pagesize[0];
	var y = pagesize[1];
	
	var deltX = (TB_WIDTH - x) /2;
	var deltY = (TB_HEIGHT - y) /2;
	
	if( deltX < 0)
        $("#TB_window").css({marginLeft: '-' + parseInt((TB_WIDTH/2),10) + 'px', width: TB_WIDTH + 'px'});
    else
        $("#TB_window").css({marginLeft: '-' + parseInt(x/2,10) + 'px'});
        
	if ( !(jQuery.browser.msie && jQuery.browser.version < 7)) { // take away IE6
	    if( deltY < 0)
		    $("#TB_window").css({marginTop: '-' + parseInt((TB_HEIGHT / 2),10) + 'px'});
		else
		   $("#TB_window").css({marginTop: '-' + parseInt((y / 2),10) + 'px'});
	}
}

function tb_getPageSize(){
	var de = document.documentElement;
	var w = window.innerWidth || self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
	var h = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight;
	arrayPageSize = [w,h];
	return arrayPageSize;
}

function tb_detectMacXFF() {
  var userAgent = navigator.userAgent.toLowerCase();
  if (userAgent.indexOf('mac') != -1 && userAgent.indexOf('firefox')!=-1) {
    return true;
  }
}

function is_IE6() {
    return jQuery.browser.msie && jQuery.browser.version < 7;
}
 		

function tb_render(caption, width, height, winWidth, winHeight, url, showOriginal, enlarged ) {
    if( jQuery.browser.msie || jQuery.browser.opera || jQuery.browser.mozilla || jQuery.browser.safari)
    {
    	if( !showOriginal )
    	{
    	        
    	        $("#TB_window").append("<table>" + 
                                 "<tr>"+ 
                                 "<td><div id='TB_caption'>"+caption+"</div><div class='captioncenter'>" + txt_resize_caption + "</div><div id='TB_closeWindow'><a href='#' id='TB_closeWindowButton' title='"+txt_hint_close+"'><img id='TB_closeWindowImageButton' alt='"+txt_hint_close+"' src='"+tb_pathToCloseLabelImage+"' /></a></div><div class='closelink'><a href='#' id='TB_closeWindowLink' title='"+ txt_hint_close+"'>" + text_close_link + "</a></div></td>" +
                                 "</tr>" + 
                                 "<tr>" +
                                 "<td><a href='' id='TB_ImageOff' title='"+txt_hint_resize+"'><img id='TB_Image' src='"+url+"' width='"+width+"' height='"+height+"' alt='"+txt_hint_resize+"'/></a></td>"+
                                 "</tr>" +
                                 "<tr><td></td></tr>"+
                                 "</table>");
                
                $("#TB_Magnifier").click(function(){
			    
			        $("#TB_window").remove();
					$("body").append("<div id='TB_window'></div>");
					tb_show_full_size(caption, url);
					return false;	
			    
			    });
    	}
    	else
    	{
             if( enlarged )
             {
                $("#TB_window").append("<table>" + 
                                        "<tr><td>"+ 
                                        "<div id='TB_caption'>"+caption+"</div><div class='captioncenter'>" + txt_resize_caption + "</div><div id='TB_closeWindow'><a href='#' id='TB_closeWindowButton' title='"+txt_hint_close+"'><img id='TB_closeWindowImageButton' alt='"+txt_hint_close+"' src='"+tb_pathToCloseLabelImage+"' /></a></div><div class='closelink'><a href='#' id='TB_closeWindowLink' title='"+ txt_hint_close+"'>" + text_close_link + "</a></div>" + 
                                        "</td></tr>" + 
                                        "<tr><td>" + 
                                        "<a href='' id='TB_ImageOff' title='"+txt_hint_resize+"'><img id='TB_Image' src='"+url+"' width='"+width+"' height='"+height+"' alt='"+txt_hint_resize+"'/></a>" + 
                                        "</td></tr>" + 
                                        "</table>");
             }
             else
             {
                $("#TB_window").append("<table>" + 
                                        "<tr><td>"+ 
                                        "<div id='TB_caption'>"+caption+"</div><div class='captioncenter'>" + txt_close_caption + "</div><div id='TB_closeWindow'><a href='#' id='TB_closeWindowButton' title='"+txt_hint_close+"'><img id='TB_closeWindowImageButton' alt='"+txt_hint_close+"' src='"+tb_pathToCloseLabelImage+"' /></a></div><div class='closelink'><a href='#' id='TB_closeWindowLink' title='"+ txt_hint_close+"'>" + text_close_link + "</a></div>" + 
                                        "</td></tr>" + 
                                        "<tr><td>" + 
                                        "<a href='' id='TB_ImageOff' title='"+txt_hint_close+"'><img id='TB_Image' src='"+url+"' width='"+width+"' height='"+height+"' alt='"+txt_hint_close+"'/></a>" + 
                                        "</td></tr>" + 
                                        "</table>");
             
             }
         }
    }
    else 
    {
       
    }
}

function tb_presettings() {
     $("#TB_window").css("position", "absolute");  
}


function tb_show_full_size(caption, url) {

	try {
		if (typeof document.body.style.maxHeight === "undefined") {//if IE 6
			$("body","html").css({height: "100%", width: "100%"});
			$("html").css("overflow","hidden");
			if (document.getElementById("TB_HideSelect") === null) {//iframe to hide select elements in ie6
				$("body").append("<iframe id='TB_HideSelect'></iframe><div id='TB_overlay'></div><div id='TB_window'></div>");
				$("#TB_overlay").click(tb_remove);
			}
		}else{//all others
			if(document.getElementById("TB_overlay") === null){
				$("body").append("<div id='TB_overlay'></div><div id='TB_window'></div>");
				$("#TB_overlay").click(tb_remove);
			}
		}
		
		if(tb_detectMacXFF()){
			$("#TB_overlay").addClass("TB_overlayMacFFBGHack");//use png overlay so hide flash
		}else{
			$("#TB_overlay").addClass("TB_overlayBG");//use background and opacity
		}
		
		if(caption===null){caption="";}
		$("body").append("<div id='TB_load'><img src='"+imgLoader.src+"' /></div>");//add loader to the page
		$('#TB_load').show();//show loader
		
	    var baseURL;
	    if(url.indexOf("?")!==-1){ //ff there is a query string involved
			baseURL = url.substr(0, url.indexOf("?"));
	    }else{ 
	   		baseURL = url;
	    }
	   
	    var urlString = /\.jpg$|\.jpeg$|\.png$|\.gif$|\.bmp$/;
	    var urlType = baseURL.toLowerCase().match(urlString);

		if(urlType == '.jpg' || urlType == '.jpeg' || urlType == '.png' || urlType == '.gif' || urlType == '.bmp'){
				
			imgPreloader = new Image();
			imgPreloader.onload = function(){		
			imgPreloader.onload = null;
				
			var imageWidth = imgPreloader.width;
			var imageHeight = imgPreloader.height;
			
			TB_WIDTH = imageWidth + 30;
			TB_HEIGHT = imageHeight + 60;
			
			tb_presettings();
			tb_render(caption, imageWidth, imageHeight, TB_WIDTH, TB_HEIGHT, url, true, true);
			
			$("#TB_closeWindowButton").click(tb_remove);//bind event
			$("#TB_closeWindowLink").click(tb_remove);
		
			document.onkeydown = onkeydown_event;
			
			tb_position();
			$("#TB_load").remove();
			$("#TB_ImageOff").addClass("zoomOut");
			$("#TB_ImageOff").click(function(){
			
			        $("#TB_window").remove();
					$("body").append("<div id='TB_window'></div>");
					tb_show(caption, url, false);
					return false;	
			});
			$("#TB_window").css({display:"block"}); //for safari using css instead of show
			};
			
			imgPreloader.src = url;
			scroll( 0, 0);
		
		}
		
	} catch(e) {
		//nothing here
	}
}


//*************************************************

function tb_init_movie(domChunk){
	$(domChunk).click(function(){
	var t = this.title || this.name || null;
	var a = this.href || this.alt;
	tb_show_movie(t,a);
	this.blur();
	return false;
	});
}

function tb_show_movie(caption, url) {

	try {
	        if(document.getElementById("TB_movie_overlay") === null){
                    $("body").append("<div id='TB_movie_overlay'></div><div id='TB_movie_window'></div>");
                    $("#TB_movie_overlay").click(tb_remove_movie);
            }
           
		    if(tb_detectMacXFF()){
			    $("#TB_movie_overlay").addClass("TB_overlayMacFFBGHack");//use png overlay so hide flash
		    }else{
			    $("#TB_movie_overlay").addClass("TB_overlayBG");//use background and opacity
		    }
		
		    if(caption===null){caption="";}
		    $("body").append("<div id='TB_movie_load'><img src='"+imgLoader.src+"' /></div>");//add loader to the page
		    $('#TB_movie_load').show();//show loader
		
		    var baseURL;
	        if(url.indexOf("?")!==-1){ //ff there is a query string involved
			    baseURL = url.substr(0, url.indexOf("?"));
	        }else{ 
	   		    baseURL = url;
	        }
	        
	        //**********************************************
	        //get html
	        
            TB_WIDTH = 680;
            TB_HEIGHT = 570;
            
            ajaxContentW = TB_WIDTH - 30;
            ajaxContentH = TB_HEIGHT - 45;
            
            tb_rendermovie(caption, ajaxContentW, ajaxContentH);
            tb_position_movie();
			//tb_presettings();
            
            $("#TB_movie_closeWindowButton").click(tb_remove_movie);
            $("#TB_movie_closeWindowImageButton").click(tb_remove_movie);
            $("#TB_movie_closeWindowLink").click(tb_remove_movie);
            
            var _rnd_url = baseURL + "?random=" + (new Date().getTime());
            
            $("#TB_ajaxContent").load(_rnd_url, onresultreceived);
                
           document.onkeyup = onkeydown_event;
           //document.onclick = tb_remove_movie;
   	} catch(e) {
		//nothing here
	}
}

function onresultreceived(response, status, xhr)
{
    if (status == "error") 
        return;
        //alert( "Sorry but there was an error: " + xhr.statusText + " " + xhr.status);
    
     //tb_position();
     $("#TB_movie_load").remove();
     //tb_init("#TB_ajaxContent a.embedbox");
     $("#TB_movie_window").css({display:"block"});
}


function tb_rendermovie(caption, width, height) {
    if( jQuery.browser.msie || jQuery.browser.opera || jQuery.browser.mozilla || jQuery.browser.safari)
    {
    
    	$("#TB_movie_window").append("<table>" + 
                         "<tr>"+ 
                         "<td><div id='TB_movie_caption'>"+caption+"</div>"+
                         "<div id='TB_closeAjaxWindow'><a href='#' id='TB_movie_closeWindowButton' title='"+txt_hint_close+"'><img id='TB_movie_closeWindowImageButton' alt='"+txt_hint_close+"' src='"+tb_pathToCloseLabelImage+"' /></a></div>"+
                         "<div><a href='#' id='TB_movie_closeWindowLink' title='"+ txt_hint_close+"'>" + text_close_link + "</a></div></td>" +
                         "</tr>" + 
                         "<tr>" +
                         "<td><div id='TB_ajaxContent' class='movie' style='width:"+width+"px;height:"+height+"px'></div></td>"+
                         "</tr>" +
                         "<tr><td></td></tr>"+
                         "</table>");
    	
    }
  
}

function tb_remove_movie() {
 	$("#TB_movie_imageOff").unbind("click");
	$("#TB_movie_closeWindowButton").unbind("click");
	$("#TB_movie_window").fadeOut("fast",function(){$('#TB_movie_window,#TB_movie_overlay,#TB_movie_HideSelect').trigger("unload").unbind().remove();});
	$("#TB_movie_load").remove();
	if (typeof document.body.style.maxHeight == "undefined") {//if IE 6
		$("body","html").css({height: "auto", width: "auto"});
		$("html").css("overflow","");
	}
	document.onkeydown = "";
	document.onkeyup = "";
	return false;
}

function tb_position_movie() {
    var pagesize = tb_getPageSize();
	var x = pagesize[0];
	var y = pagesize[1];
	
	var deltX = (TB_WIDTH - x) /2;
	var deltY = (TB_HEIGHT - y) /2;
	
	if( deltX < 0)
        $("#TB_movie_window").css({marginLeft: '-' + parseInt((TB_WIDTH/2),10) + 'px', width: TB_WIDTH + 'px'});
    else
        $("#TB_movie_window").css({marginLeft: '-' + parseInt(x/2,10) + 'px'});
        
	if ( !(jQuery.browser.msie && jQuery.browser.version < 7)) { // take away IE6
	    if( deltY < 0)
		    $("#TB_movie_window").css({marginTop: '-' + parseInt((TB_HEIGHT / 2),10) + 'px'});
		else
		   $("#TB_movie_window").css({marginTop: '-' + parseInt((y / 2),10) + 'px'});
	}
}


