/**
 * @author Sebastian Romero April-03-2008
 * @projectDescription Utilities for Page
 * @version 1.0
 * @notes Put methods for page only
 */
var Page = {
    /*
     Gets the position of the scroll
     */
    getPageScroll: function(){
        var num_scrollX = 0, num_scrollY = 0;
        if (typeof(window.pageYOffset) == 'number') {
            num_scrollY = window.pageYOffset;
            num_scrollX = window.pageXOffset;
        }
        else 
            if (document.body && (document.body.scrollLeft || document.body.scrollTop)) {
                num_scrollY = document.body.scrollTop;
                num_scrollX = document.body.scrollLeft;
            }
            else 
                if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
                    num_scrollY = document.documentElement.scrollTop;
                    num_scrollX = document.documentElement.scrollLeft;
                }
        return {
            'x': num_scrollX,
            'y': num_scrollY
        };
    },
    /*
     Gets the size of the Page
     */
    getWindowSize: function(){
        var xScroll, yScroll, windowWidth, windowHeight;
        if (window.innerHeight && window.scrollMaxY) {
            xScroll = document.body.scrollWidth + window.scrollMaxX;
            yScroll = window.innerHeight + window.scrollMaxY;
        }
        else 
            if (document.body.scrollHeight > document.body.offsetHeight) {
                xScroll = document.body.scrollWidth;
                yScroll = document.body.scrollHeight;
            }
            else {
                xScroll = document.body.offsetWidth;
                yScroll = document.body.offsetHeight;
            }
        if (self.innerHeight) {
            windowWidth = self.innerWidth;
            windowHeight = self.innerHeight;
        }
        else 
            if (document.documentElement && document.documentElement.clientHeight) {
                windowWidth = document.documentElement.clientWidth;
                windowHeight = document.documentElement.clientHeight;
            }
            else 
                if (document.body) {
                    windowWidth = document.body.clientWidth;
                    windowHeight = document.body.clientHeight;
                }
        if (yScroll < windowHeight) {
            pageHeight = windowHeight;
        }
        else {
            pageHeight = yScroll;
        }
        if (xScroll < windowWidth) {
            pageWidth = windowWidth;
        }
        else {
            pageWidth = xScroll;
        }
        return {
            'pageWidth': pageWidth,
            'pageHeight': pageHeight,
            'windowWidth': windowWidth,
            'windowHeight': windowHeight
        };
    },
    /*
     * Preloads Imanages on Page
     */
    preloadImages: function(){
        var d = document;
        if (d.images) {
            if (!d.MM_p) 
                d.MM_p = new Array();
            var i, j = d.MM_p.length, a = MM_preloadImages.arguments;
            for (i = 0; i < a.length; i++) {
                if (a[i].indexOf("#") != 0) {
                    d.MM_p[j] = new Image;
                    d.MM_p[j++].src = a[i];
                }
            }
        }
    },
    /*
     * Find Offset To Body
     */
    findOffsetBody: function(obj){
        var curleft = 0;
        var curTop = 0;
        if (obj.offsetParent) {
            while (1) {
                curleft += obj.offsetLeft;
                if (!obj.offsetParent) 
                    break;
                obj = obj.offsetParent;
            }
        }
        else 
            if (obj.x) {
                curleft += obj.x;
                curTop += obj.y;
            }
        return {
            top: curTop,
            left: curleft
        };
    },
    
    /*
     * Creates a Modal Page
     */
    createModal: function(anim){
        var modal = ($(".modal").length == 0) ? $(document.createElement("div")) : $(".modal");
        var obj_windowSize = Page.getWindowSize();
        modal.addClass("modal");
        modal.css({
            position: "absolute",
            opacity: 0.5,
            filter: 'alpha(opacity=' + (50) + ')',
            backgroundColor: "#000",
            top: "50%",
            left: "50%",
            zIndex: 1000
        });
        modal.css({
            width: 2,
            height: 2,
            marginTop: 2 / 2 * -1,
            marginLeft: 2 / 2 * -1
        });
        if (anim) {
            modal.animate({
                width: obj_windowSize.pageWidth,
                height: obj_windowSize.pageHeight,
                marginTop: obj_windowSize.pageHeight / 2 * -1,
                marginLeft: obj_windowSize.pageWidth / 2 * -1
            }, 400);
        }
        else {
            Page.setStyleToModal(modal);
        }
        $("body").append(modal);
        $(window).bind("resize", function(){
            Page.setStyleToModal($(".modal"));
        });
        return modal;
    },
    
    setStyleToModal: function(modal){
		var obj_windowSize = Page.getWindowSize();
        modal.css({
            width: "100%",
            height: obj_windowSize.pageHeight,
            top: "0px",
            left: "0px",
			marginTop: "0px",
            marginLeft: "0px"
        });
    }
    
};

/**
 * popup plug in for Partsdirect
 * @param {Object} params
 * @example $(document.body).popup({isModal:true, html:"testing..."})
 */
jQuery.fn.popup = function(params){
	
	var scope = this;
	var current_popup;
	var popupPrams = params;
	var boo_anim = PartsDirect.animateComponents;
	var timeout;
	/**
	 * 
	 * Paramaters object, this evaluates all the parameter for the popup
	 * Add functionality new functionality here
	 */
	var parameters  = {
		/**
		 * Adds the modal window 
		 * @param {Boolean} e
		 */
		isModal : function(e){
			if(e){
				void Page.createModal();
			}
		},
		/**
		 * Changes the popup width
		 * @param {Number} pWidth
		 */
		width : function(pWidth){
			pWidth = parseInt(pWidth);
			var method = (boo_anim)?"animate":"css";
			$(".popup")[method]({"width" : pWidth, "margin-left" : (pWidth/2)*-1});
		},
		/**
		 * Gives the popup the loader look and feal
		 * @param {Boolean} e
		 */
		isLoader : function(e){
			if(e){
				var html = "Loader";
				parameters.html($("#interstitialSearch").html());
			}
		},
		/**
		 * Loads and External html as content
		 * @param {Object} ajaxcontent
		 */
		ajaxContent : function(ajaxcontent){
			if(ajaxcontent.url){
				$.ajax({url: ajaxcontent.url, 
						success: function(html){ 
								parameters.html(html);
							}
						});
			}
		},
		/**
		 * Loads HTML Content into the Popup
		 * @param {String} html
		 */
		html : function(html){
			$(".content", $(".popup")).append(html);
			$(".content_popup", $(".popup")).css({height:$(".content_popup", $(".popup")).height() - 10});
			void centerPopup()
		}, 		
		/**
		 * Fix the popup on the center
		 * @param {Boolean} e
		 */
		fixCenter : function(e){
			$(window).scroll(function(event){
				clearTimeout(timeout);
				timeout = setTimeout(function(){
					void centerPopup(boo_anim);
				}, 500)
				
			});	
		}
	};
	/**
	 * 
	 * @param {HtmlObject / JqueryObject} element
	 */
	var popup = function(element){
		if($(".popup").length == 0){
			current_popup = scope.createPopup();
			$(document.body).append(current_popup);
		} else 
			current_popup = $(".popup");
		for(var propertie in popupPrams){
			if(parameters[propertie]){
				parameters[propertie](popupPrams[propertie]);
			}	
		}
	};
	
	/**
	 * 
	 * This function creates the html for the scroll bar
	 * @return {JqueryObject} 
	 */
	this.createPopup = function(){
		var div_popup = $(document.createElement("div"));
		var popup_content = "<div class=\"header_popup\"><span></span></div>";
		div_popup.addClass("popup");
		popup_content += "<div class=\"content_popup\">";
		popup_content += "<div class=\"header_content\"><span></span></div>";
		popup_content += "<div class=\"content\"></div>";
		popup_content += "</div>";
		popup_content += "<div class=\"footer_popup\"><span></span></div>";
		div_popup.html(popup_content);
		return div_popup;					
	};
	
	
	/**
	 * Private Methods
	 */
	var centerPopup = function(animate){
		var method = (animate)?"animate":"css";
		var scrollPos = Page.getPageScroll();
		if(current_popup){
			if(scrollPos.y == 0){
				current_popup[method]({top:"50%", marginTop:(current_popup.height()/2)*-1});
			} else {
				var windowSize = Page.getWindowSize();
				current_popup[method]({top:scrollPos.y + ((windowSize.windowHeight - current_popup.height())/2), marginTop:0});
			}
		}
	};
	
	/**
	 * Constructor Call
	 */
    this.each(function(){
        popup(this);
    });
};
