/* jLightBox (using jQuery library).
*--------------------------------------------*
*  @author : ukhome ( ukhome@gmail.com | ntkhoa_friends@yahoo.com )
*--------------------------------------------*
*  @released : 24-Mar-2009 : version 1.0
*--------------------------------------------*
*  @revision history : ( latest version : 1.0 )
*--------------------------------------------*
*      + 24-Mar-2009 : version 1.0
*          - released
*--------------------------------------------*
*/

/* package $lightbox.Core
*/

$lightbox = function (jEl, options) { //jEl = jquery element
    //fields
    var parent = this;
    this.isIE6 = (/MSIE 6\.0/).test(navigator.userAgent) && !(/MSIE 7\.0/).test(navigator.userAgent);
    this.handler = jEl;
    if ( this.handler != null ) { //call event is assign to link by jQuery.addLightBoxControl methods : automatic
        this.options = {
            internalContent: typeof( jEl.attr("rel") ) != "undefined" && jEl.attr("rel") != ""
                                 ? jEl.attr("rel")
                                 : null,
            externalContent: typeof( jEl.attr("href") ) != "undefined" && jEl.attr("href") != "" && jEl.attr("href") != "#"
                                 ? jEl.attr("href")
                                 : "",
            enableScrollbar: typeof(options.enableScrollbar) != "undefined"
                                 ? options.enableScrollbar
                                 : true,
            maxLightBoxHeight: options.maxLightBoxHeight
                                      ? options.maxLightBoxHeight
                                      : 800,
            closeBtn: options.closeBtnText && typeof(options.closeBtnText) != "undefined"
                       ? jQuery("<a href=\"#\" title=\"\" class=\"CloseBtn\"><span>" + options.closeBtnText + "</span></a>")
                       : null,					   
			themeClass: options.themeClass && options.themeClass != undefined
                       ? options.themeClass
                       : "",
			callback: options.callback && options.callback != undefined
                       ? options.callback
                       : undefined		
        }
    }
    else { //call directy from external link
        this.options = {
            internalContent: typeof( options.rel ) != "undefined" && options.rel != ""
                                 ? options.rel
                                 : null,
            externalContent: typeof( options.href ) != "undefined" && options.href != "" && options.href != "#"
                                 ? options.href
                                 : "",
            enableScrollbar: typeof(options.enableScrollbar) != "undefined"
                                 ? options.enableScrollbar
                                 : true,
            maxLightBoxHeight: options.maxLightBoxHeight
                                      ? options.maxLightBoxHeight
                                      : 800,
            closeBtn: options.closeBtnText && typeof(options.closeBtnText) != "undefined"
                       ? jQuery("<a href=\"#\" title=\"\" class=\"CloseBtn\">" + options.closeBtnText + "</a>")
                       : null,
			themeClass: options.themeClass && options.themeClass != undefined
                       ? options.themeClass
                       : "",
			callback: options.callback && options.callback != undefined
                       ? options.callback
                       : undefined		
        }
    }
    this.reload = false;
    this.isOpen = false;
    this.overlays;
    this.popup;
    if ( parent.isIE6 ) {
        this.applyPosition = "absolute";
        this.offsetTop = jQuery(document).scrollTop();
        this.offsetLeft = jQuery(document).scrollLeft();
    }
    else {
        this.applyPosition = "fixed";
        this.offsetTop = 0;
        this.offsetLeft = 0;
    }

    //MAIN
    if (  this.options.internalContent != null ) { //Image Type Prepare
    /*this.handler != null : in case call by external interface, the gallery mode is disable*/
        var group = this.options.internalContent.match(/\[.*\]$/);
        if ( group != null ) {
            if ( this.handler != null ) {
                var groupClassName = "Gallery_" + group.toString().replace(/[ \[ | \] ]/g, "").toUpperCase();
                this.options.internalContent = this.options.internalContent.replace(group, "");
                jQuery( this.options.internalContent ).addClass( groupClassName );
            }
            else {
                this.options.internalContent = this.options.internalContent.replace(group, "");
            }
        }
    }

    //create events handler
    new $lightbox.EventHandler(this);

    //create LightBox control
    new $lightbox.LightBoxControl(this, options/*optional: use in case call by external interface*/);

    //create Overlays
    this.createOverlays = function () {
        this.overlays = jQuery("<div class=\"Overlays\"></div>");
        if ( parent.isIE6 ) {
            this.offsetTop = jQuery(document).scrollTop();
            this.offsetLeft = jQuery(document).scrollLeft();
        }
        else {
            this.offsetTop = 0;
            this.offsetLeft = 0;
        }
        //overlays: properties
        this.overlays.css({
            width: jQuery(window).width(),
            height: jQuery(window).height(),
            position: this.applyPosition,
            zIndex: 1000,
            top: this.offsetTop,
            left: this.offsetLeft,
            display: "none"
        });
        this.overlays.appendTo("body");
        this.overlays.hide();

        //overlays: binding event
        this.overlays.bind("click", function () {
            parent.hideLightBox();
            return false;
        });
    }

    //create popup
    this.createPopup = function () {
        this.popup = jQuery("<div class=\"LightBoxPopup "+ this.options.themeClass +" FixPng\"><div class=\"LightBoxPopupContent\"></div></div>");
        this.popupContent = this.popup.find("div.LightBoxPopupContent:first");
        if ( this.options.closeBtn != null ) {
            this.popup.prepend(this.options.closeBtn);
            this.options.closeBtn.bind("click", function () {
                parent.hideLightBox();
                return false;
            });
        }
        //popup: properties
        this.popup.css({
            position: this.applyPosition,
            zIndex: 1001,
            display: "none"
        });
        this.popup.appendTo("body");
        this.popup.hide();
    }

    return this;
}
