/**
 * All rights reserved
 * 
 * @uthor: 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=755, left="+parseInt((sWidth-700)/2)+", top="+parseInt((sHeight-400)/2)+", scrollbars, resizable");
	oWindow.focus();
}

/**
 * Search form
 */
CMS.SearchForm = Class.create();
CMS.SearchForm.prototype = {
    initialize: function(form, field, fieldSubmit, emptyText){
        this.form = $(form);
        this.buttonSubmit = fieldSubmit;
        this.field = $(field);
        this.emptyText = emptyText;
        
        Event.observe(this.form, 'submit', this.submit.bind(this));
        
        if(this.buttonSubmit instanceof Array) {
        	for(i = 0; i < this.buttonSubmit.length; i++)
	        	Event.observe($(this.buttonSubmit[i]), 'click', this.click.bind(this));
        } else {
        	Event.observe($(this.buttonSubmit), 'click', this.click.bind(this));
        }
        Event.observe(this.field, 'focus', this.focus.bind(this));
        Event.observe(this.field, 'blur', this.blur.bind(this));
        this.blur();
    },
    
    submit: function(event){
        if (this.field.value == this.emptyText || this.field.value == '') {
            Event.stop(event);
            return false;
        }
        return true;
    },
    
    click: function(event){
        if (this.submit(event) && !this.form.action.blank()) 
            this.form.submit();
    },
    
    focus: function(event){
        if (this.field.value == this.emptyText) {
            this.field.value = '';
        }
        
    },
    
    blur: function(event){
        if (this.field.value == '') {
            this.field.value = this.emptyText;
        }
    }
}

/**
 * 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");
			}
			
			if(/^.*\.(jpg|JPG)$/.test(anchor.getAttribute("href")) == true) {
				
				if(anchor.select("img").size() == 0)
					anchor.addClassName("anchor_image");
				
			}
		});
    }
});

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


var arVersion = navigator.appVersion.split("MSIE")
var version = parseFloat(arVersion[1])

CMS.fixPNG = function(myImage) 
{
    if ((version >= 5.5) && (version < 7) && (document.body.filters)) 
    {
       var imgID = (myImage.id) ? "id='" + myImage.id + "' " : ""
	   var imgClass = (myImage.className) ? "class='" + myImage.className + "' " : ""
	   var imgTitle = (myImage.title) ? 
		             "title='" + myImage.title  + "' " : "title='" + myImage.alt + "' "
	   var imgStyle = "display:inline-block;" + myImage.style.cssText
	   var strNewHTML = "<span " + imgID + imgClass + imgTitle
                  + " style=\"" + "width:" + myImage.width 
                  + "px; height:" + myImage.height 
                  + "px;" + imgStyle + ";"
                  + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
                  + "(src=\'" + myImage.src + "\', sizingMethod='scale');\"></span>"
	   myImage.outerHTML = strNewHTML	  
    }
}