/**
 * @author Andrius Jukna <andrius@evp.lt>
 */

/**
 * @param       jquery element      bindTo                  // where to bind onClick action, default .bindTo
 * @param       jquery element      ppElement               // popup content, default #ppElement
 * @param       jquery element      backgroundPpElement     // background, default #backgroundPpElement
 * @param       jquery element      ppCloseBtn              // popup close button, default #ppCloseBtn
 * @param       jquery element      writeTo                 // where to write response, default #writeTo
 * @param       full url            url                     // url request for popup
 * @param       array               params                  // params to send post method, when requesting popup
 */
var PP ={
    init: function(params){
        this.params = params;
        var $PP = this;
        
        $PP.ppStatus = 0;
        $PP.setDefault();
        $PP.getPp();
    },
    
    setDefault: function(){
        var $PP = this;
        
        if(!$PP.params.bindTo) $PP.params.bindTo = '.bindTo';
        if(!$PP.params.ppElement) $PP.params.ppElement = '#ppElement';
        if(!$PP.params.backgroundPpElement) $PP.params.backgroundPpElement = '#backgroundPpElement';
        if(!$PP.params.ppCloseBtn) $PP.params.ppCloseBtn = '#ppCloseBtn';
        if(!$PP.params.writeTo) $PP.params.writeTo = '#writeTo';
        if(!$PP.params.params) $PP.params.params = {};
        if(!$PP.params.urls) $PP.params.urls = {};
    },

    getPp: function(){
        var $PP = this;
    
        $($PP.params.bindTo).bind('click', function() {
            var clickedId = 'unknown';
            if($(this).attr('fake')) clickedId = $(this).attr('fake');
            var params = {};
            var postToUrl = $PP.params.url; 
            
            if($PP.params.urls[clickedId]) postToUrl = $PP.params.urls[clickedId];
            if($PP.params.params[clickedId]) params = $PP.params.params[clickedId]; 
            params.clicked_id = clickedId;
            
            $.post(postToUrl, params, function(res){
                $($PP.params.writeTo).html(res);
                $PP.centerPp();
                $PP.loadPp();
                $PP.bindPp();
                
                $($PP.params.ppElement).css({
			            "left": document.documentElement.clientWidth/2 - $('#ppElement').width()/2
			        });
                /* $($PP.params.ppElement).scrollTo(1000, 'easeOutBack', 20); */
            });
            /**
             * TODO aj: 2009-02-26
             * nice to have - when loading show something
             */
	        return false;
        });
        
        // esc keypress
        $(document).keypress(function(e){
            if(e.keyCode==27 && 1 == $PP.ppStatus){
                $PP.disablePp();
            }
        });
    },
    
    bindPp: function(){
        var $PP = this;
    
        // clicked close btn
        $($PP.params.ppCloseBtn).click(function(){
            $PP.disablePp();
            return false;
        });
        // clicked on other than popup
        $($PP.params.backgroundPpElement).click(function(){
            $PP.disablePp();
        });
        
        $('#ppElement').bind('mouseover', function(){
        	$('.ppClose').show();
        });
        $('#ppElement').bind('mouseout', function(){
        	$('.ppClose').hide();
        });
    },
    
    centerPp: function(){
        var $PP = this;
	    var windowWidth = document.documentElement.clientWidth;
	    var windowHeight = document.documentElement.clientHeight;
	    var ppHeight = $($PP.params.ppElement).height();
	    var ppWidth = $($PP.params.ppElement).width();
	    
	    var arrPageScroll = ___getPageScroll();
	    var arrPageSizes = ___getPageSize();
	    
	    $($PP.params.ppElement).css({
	        "position": "absolute",
	        "top": arrPageScroll[1] + (windowHeight * 0.04),
	        "left": windowWidth/2 - ppWidth/2
	    });
	    //only need force for IE6
	    
	    $($PP.params.backgroundPpElement).css({
	        "height": windowHeight
	    });
	    
	},
	
	loadPp: function(){
        var $PP = this;
        
	    if(0 == $PP.ppStatus){
	        $($PP.params.backgroundPpElement).css({
	            "opacity": "0.7"
	        });
	        $($PP.params.backgroundPpElement).fadeIn("slow");
	        $($PP.params.ppElement).fadeIn("slow");
	        $PP.ppStatus = 1;
	    }
	},
	
	disablePp: function (){
        var $PP = this;
        
	    if(1 == $PP.ppStatus){
	        $($PP.params.backgroundPpElement).fadeOut("slow");
	        $($PP.params.ppElement).fadeOut("slow");
	        $PP.ppStatus = 0;
	    }
	}
}

/**
 / THIRD FUNCTION
 * getPageScroll() by quirksmode.com
 *
 * @return Array Return an array with x,y page scroll values.
 */
function ___getPageScroll() {
	var xScroll, yScroll;
	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
		xScroll = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollTop) {	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
		xScroll = document.documentElement.scrollLeft;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
		xScroll = document.body.scrollLeft;	
	}
	arrayPageScroll = new Array(xScroll,yScroll);
	return arrayPageScroll;
};

/**
 / THIRD FUNCTION
 * getPageSize() by quirksmode.com
 *
 * @return Array Return an array with page width, height and window width, height
 */
function ___getPageSize() {
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}
	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
	return arrayPageSize;
};

