/*
 * Thickbox 3.1 - One Box To Rule Them All.
 * By Cody Lindley (http://www.codylindley.com)
 * Copyright (c) 2007 cody lindley
 * Licensed under the MIT License: http://www.opensource.org/licenses/mit-license.php
 *
 * and some hacked by tokuhirom.
*/

//add thickbox to href & area elements that have a class of .thickbox
var ThickBox = {};

ThickBox.pathToImage = "/thickbox/loadingAnimation.gif";

ThickBox.init = function (domChunk) {
    jQuery(domChunk).click(function() {
        var t = this.title || this.name || null;
        var a = this.href || this.alt;

        ThickBox.show(t,a);

        this.blur();

        return false;
    });
};

//function called when the user clicks on a thickbox link
ThickBox.show = function (caption, url) {
    try {
        if (typeof document.body.style.maxHeight === "undefined") {//if IE 6
            jQuery("body","html").css({height: "100%", width: "100%"});
            jQuery("html").css("overflow","hidden");
            if (document.getElementById("TB_HideSelect") === null) {//iframe to hide select elements in ie6
                jQuery("body").append(
                    "<iframe id='TB_HideSelect'></iframe><div id='TB_overlay'></div><div id='TB_window'></div>"
                );
                jQuery("#TB_overlay").click(ThickBox.remove);
            }
        } else {//all others
            if(document.getElementById("TB_overlay") === null){
                jQuery("body").append("<div id='TB_overlay'></div><div id='TB_window'></div>");
                jQuery("#TB_overlay").click(ThickBox.remove);
            }
        }
        
        if (ThickBox.detectMacXFF()) {
            jQuery("#TB_overlay").addClass("TB_overlayMacFFBGHack");//use png overlay so hide flash
        }else{
            jQuery("#TB_overlay").addClass("TB_overlayBG");//use background and opacity
        }
            
        if (caption===null) {caption="";}

        jQuery("body").append("<div id='TB_load'><img src='"+imgLoader.src+"' /></div>");//add loader to the page
        jQuery('#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 imgPreloader = new Image();
        imgPreloader.onload = function() {
            imgPreloader.onload = null;
            
            // Resizing large images - orginal by Christian Montoya edited by me.
            var pagesize = ThickBox.getPageSize();
            var x = pagesize[0] - 150;
            var y = pagesize[1] - 150;
            var imageWidth = imgPreloader.width;
            var imageHeight = imgPreloader.height;
            if (imageWidth > x) {
                imageHeight = imageHeight * (x / imageWidth); 
                imageWidth = x; 
                if (imageHeight > y) { 
                    imageWidth = imageWidth * (y / imageHeight); 
                    imageHeight = y; 
                }
            } else if (imageHeight > y) { 
                imageWidth = imageWidth * (y / imageHeight); 
                imageHeight = y; 
                if (imageWidth > x) { 
                    imageHeight = imageHeight * (x / imageWidth); 
                    imageWidth = x;
                }
            }
            // End Resizing
            
            jQuery("#TB_window").append("<a href='' id='TB_ImageOff' title='Close'><img id='TB_Image' src='"+url+"' width='"+imageWidth+"' height='"+imageHeight+"' alt='"+caption+"'/></a><div id='TB_caption'>"+caption+"<div id='TB_secondLine'></div></div><div id='TB_closeWindow'><a href='#' id='TB_closeWindowButton' title='Close'>close</a></div>"); 

            jQuery("#TB_closeWindowButton").click(ThickBox.remove);

            var width = imageWidth + 30;
            var height = imageHeight + 60;
            ThickBox.position(width, height);

            jQuery("#TB_load").remove();
            jQuery("#TB_ImageOff").click(ThickBox.remove);
            jQuery("#TB_window").css({display:"block"}); //for safari using css instead of show
        };

        imgPreloader.src = url;
    } catch(e) {
        //nothing here
    }
};

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

ThickBox.position = function (width, height) {
    jQuery("#TB_window").css({marginLeft: '-' + parseInt((width / 2),10) + 'px', width: width + 'px'});
    if ( !(jQuery.browser.msie && jQuery.browser.version < 7)) { // take away IE6
        jQuery("#TB_window").css({marginTop: '-' + parseInt((height / 2),10) + 'px'});
    }
};

ThickBox.getPageSize = function () {
    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;
};

ThickBox.detectMacXFF = function () {
    var userAgent = navigator.userAgent.toLowerCase();

    if (userAgent.indexOf('mac') != -1 && userAgent.indexOf('firefox')!=-1) {
        return true;
    }
};

ThickBox.init_next = function () {
    ThickBox.init('a.thickbox, area.thickbox, input.thickbox');//pass where to apply thickbox
};

jQuery(document).ready(function(){   
    ThickBox.init_next();

    // preload image
    imgLoader = new Image();
    imgLoader.src = ThickBox.pathToImage;
});

