/**
 * All rights reserved
 * 
 * @author: Jakub Kozicki kuba.kozicki@gmail.com
 * @version 0.6
 */

CMS = Class.create();

/**
 * Open new window
 */
CMS.open = function(event) {
	event = Event.extend(event);
	element = event.findElement();
	element = element.nodeName.toLowerCase() != "a" ? element.up("a") : element;
	window.open(element.getAttribute("href"));
	event.stop();
}

/**
 * Set location
 */
CMS.setLocation = function(sUrl){
    window.location.href = sUrl;
}

/**
 * Add to favourites
 */
CMS.addToFavourites = function(sName, sUrl) {
	if(window.sidebar)
		window.sidebar.addPanel(sName, sUrl , "");
	else if(window.external)
		window.external.AddFavorite(sUrl, sName)
}

/**
 * Send e-mail
 */
CMS.mail = function(sUser, sSite, bUrl, bShowMail, sClassName) {
	if(typeof sClassName == "undefined")
		sClassName == "";
	
	document.write(bUrl ? '<a class="' + sClassName + '" href=\"mailto:' + sUser + '@' + sSite + '\">' : '');
	document.write((bShowMail ? sUser + '@' + sSite : '') + (bUrl ? '</a>' : ''));
}


/**
 * Show pop up
 */
CMS.openPopup = function(sUrl){
	var sWidth = window.screen.width;
	var sHeight = window.screen.height;
	var oWindow = window.open(sUrl, "popupWindow", "height=450, width=940, left="+parseInt((sWidth-700)/2)+", top="+parseInt((sHeight-400)/2)+", scrollbars, resizable");
	oWindow.focus();
}

/**
 * Decorate anchors
 */
CMS.DecorateAnchors = Class.create({

	initialize: function(content){
        
		var anchors = $(content).select("a");
		anchors.each(function(anchor) {
			
			if(/^.*\.(doc|docx)$/.test(anchor.getAttribute("href")) == true) {
				anchor.addClassName("anchor_word");
			}
			
			if(/^.*\.(xls|xlsx)$/.test(anchor.getAttribute("href")) == true) {
				anchor.addClassName("anchor_excel");
			}
			
			if(/^.*\.(ppt|pptx)$/.test(anchor.getAttribute("href")) == true) {
				anchor.addClassName("anchor_powerpoint");
			}
			
			if(/^.*\.pdf$/.test(anchor.getAttribute("href")) == true) {
				anchor.addClassName("anchor_pdf");
			}
		});
    }
});

/**
 * Print
 */
CMS.print = function(iTimeout){
	if(window.print) {
		setTimeout(function() {
			window.print();
		}, iTimeout);
	}
}


/**
 * Submenu
 */
CMS.Submenu = Class.create();
CMS.Submenu.prototype = {
    initialize: function(menu){
        this.menu = $(menu);
        this.li = this.menu.select("li");
        
        if(this.li instanceof Array) {
        	for(i = 0; i < this.li.length; i++) {
        		var li = $(this.li[i]);
        		Event.observe(li, 'mouseover', this.mouseover.bindAsEventListener(this));
        		
        		var childs = li.childElements();
        		for(j = 0; j < childs.length; j++) {
        			Event.observe($(childs[j]), 'mouseover', this.mouseover.bindAsEventListener(this));
        		}
        		
            	Event.observe(li, 'mouseout', this.mouseout.bindAsEventListener(this));
            	
            	// Funkcja otrzymana interpolacja Lagrange'a
            	x = li.getWidth();
            	var y = (-0.0012904506650784197)*(x-143.0)*(x-270.0)+(-0.0015405682985279014)*(x-51.0)*(x-270.0)+0.0029842160140941287*(x-51.0)*(x-143.0);
            	
            	li.select("div.submenu").each(function(element) {
            		$(element).setStyle({'left' : y + 'px'});
            	});
        	}
        }
    	
    },
    
    mouseover: function(event){
    	var element = event.element();
    	if(element.nodeName.toLowerCase() != "li") {
    		element = element.up("li");
    	}
    	
    	element.select("div.submenu").each(function(element) {
    		$(element).show();
    	});
    	
    },
    
    mouseout: function(event){
        this.li.each(function(li) {
        	li.select("div.submenu").each(function(element) {
        		$(element).hide();
        	});
        });
    }
}


