﻿/// <reference path="../../ScriptLibrary/jQuery/1.2.6/jquery-vsdoc.js" />
/// <reference path="../../ScriptLibrary/jQuery/plugins/jqModal.js" />

var _Mask;
var _PageIsFullyLoaded = false;
$(document).ready(function() {
    _Mask = document.getElementById("Mask");
    _Mask = (_Mask) ? _Mask.value : 0;
    $("#footer").hide();
    //$(window).resize(AdjustLayout);
    $("#Master_Menu_NavMenu").clickMenu();
    $("#header_menu").css("visibility", "visible");
    if ($.browser.msie == true) {
        $("#header_outer").css("margin-top", "8px");
    }
    else {
        $("#header_outer").center();
    }
    if (document.getElementById("Header_CompanyLookup")) {
        $("#Header_CompanyLookup").click(OpenCompanyLookup).css("visibility", "visible");
    }
    Header_SearchInit();
});
function OpenCompanyLookup(lnk) {
    lnk = lnk || this;
    var title = lnk.title || "Company Lookup";
    $("#Header_CompanyLookup_Content").html("").attr("src", "/common/controls/info/Card_PopUp.aspx");
    $("#Header_CompanyLookup_Title_Text").html(title);
    $("#Header_CompanyLookup_Window").jqm({ modal: true, toTop: true }).jqmShow();
    $("#Header_CompanyLookup_Close").unbind().click(function() {
        $("#Header_CompanyLookup_Content").attr("src", "").html("");
        $("#Header_CompanyLookup_Window").jqm().jqmHide()
    });
    return false;
}

$(window).load(function() {
    AdjustLayout();
    _PageIsFullyLoaded = true;
});

function Header_SearchInit() {
    var myClose = function(hash){
        hash.w.fadeOut('3000', function(){
            hash.o.remove();
        });
    };
    var openInIframe = function(hash) {
        var newWidth = 0, newHeight = 0, newLeft = 0, newTop = 0;
        var $trigger = $(hash.t);
        var $modal = $(hash.w);
        var myUrl = $trigger.attr('href');
        var myTitle = $trigger.attr('title');
        var $modalContent = $("iframe", $modal);

        $modalContent.html('').attr('src', myUrl);
        $('#Header_SearchTitleText').text(myTitle);

        var queryString = null;
        if (myUrl) {
            myUrl = (myUrl.lastIndexOf("#") > -1) ? myUrl.slice(0, myUrl.lastIndexOf("#")) : myUrl;
            queryString = (myUrl.indexOf("?") > -1) ? myUrl.substr(myUrl.indexOf("?") + 1) : null;
        }

        if (queryString != null && typeof queryString != 'undefined') {
            var queryVarsArray = queryString.split("&");
            for (var i = 0; i < queryVarsArray.length; i++) {
                if (unescape(queryVarsArray[i].split("=")[0]) == 'width') {
                    newWidth = queryVarsArray[i].split("=")[1];
                }
                if (escape(unescape(queryVarsArray[i].split("=")[0])) == 'height') {
                    newHeight = queryVarsArray[i].split("=")[1];
                }
            }
            // let's run through all possible values: 90%, nothing or a value in pixel
            if (newHeight != 0) {
                if (newHeight.indexOf('%') > -1) {
                    newHeight = Math.floor(parseInt($(window).height()) * (parseInt(newHeight) / 100));
                }
                newTop = Math.floor(parseInt($(window).height() - newHeight) / 2);
            }
            else {
                newHeight = $modal.height();
            }
            if (newWidth != 0) {
                if (newWidth.indexOf('%') > -1) {
                    newWidth = Math.floor(parseInt($(window).width() / 100) * parseInt(newWidth));
                }
                newLeft = Math.floor(parseInt($(window).width() / 2) - parseInt(newWidth) / 2);

            }
            else {
                newWidth = $modal.width();
            }
            // do the animation so that the windows stays on center of screen despite resizing
            $modal.jqmShow().animate({
                width: newWidth,
                height: newHeight,
                top: newTop,
                left: newLeft,
                marginLeft: 0
            }, 2000);
        }
        else { // don't do animations
            $modal.jqmShow();
        }
    }
                
    $("#Header_SearchWindow").jqm({
        modal: true,
        trigger: 'a.Header_SearchLink',
        target: '#Header_SearchContent',
        onHide: myClose,
        onShow: openInIframe
    });
    
    $("button.Header_SearchClose").click(function() {
        $("#Header_SearchWindow").jqmHide();
    });
};


/**
* @author Alexandre Magno
* @desc Center a element with jQuery
* @version 1.0
* @example
* $("element").center({
*
* 		vertical: true,
*      horizontal: true
*
* });
* @obs With no arguments, the default is above
* @license free
* @param bool vertical, bool horizontal
* @contribution Paulo Radichi
*
*/
jQuery.fn.center = function(params) {
    var options = {
        vertical: true, horizontal: true
    }
    op = jQuery.extend(options, params);
    return this.each(function() {
        var $self = jQuery(this);                                               //initializing variables
        var width = $self.width();                                              //get the dimensions using dimensions plugin
        var height = $self.height();
        var paddingTop = parseInt($self.css("padding-top"));                    //get the paddings
        var paddingBottom = parseInt($self.css("padding-bottom"));
        var borderTop = parseInt($self.css("border-top-width"));                //get the borders
        var borderBottom = parseInt($self.css("border-bottom-width"));
        var mediaBorder = (borderTop + borderBottom) / 2;                       //get the media of padding and borders
        var mediaPadding = (paddingTop + paddingBottom) / 2;
        var positionType = $self.parent().css("position");                      //get the type of positioning
        var halfWidth = (width / 2) * (-1);                                     // get the half minus of width and height
        var halfHeight = ((height / 2) * (-1)) - mediaPadding - mediaBorder;
        var cssProp = {                                                         // initializing the css properties
            position: 'absolute'
        };
        if (op.vertical) {
            cssProp.height = height; cssProp.top = '50%'; cssProp.marginTop = halfHeight;
        }
        if (op.horizontal) {
            cssProp.width = width; cssProp.left = '50%'; cssProp.marginLeft = halfWidth;
        }
        if (positionType == 'static') {                                         //check the current position
            $self.parent().css("position", "relative");
        }
        $self.css(cssProp);                                                     //aplying the css
    });
};
