
Event.observe(window, 'load', function() {
	CMS.resizeImageContainers("round-image");
});

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=510, width=550, 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));
        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");
			}
		});
    }
});

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

/**
 * Resize image containers
 */
CMS.resizeImageContainers = function(className) {
	
	var documentBody = Element.extend(document.getElementsByTagName("body")[0]);
	var divs  = documentBody.select("." + className);
	
	divs.each(function(div) {
		var img = div.select("img").first();
		div.setStyle({'width': img.getWidth() + 'px'});
	});
}
