
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=400, width=700, 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.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;
        }
    }
}

/**
 * Clock - simple DOM
 */
CMS.clockFast = function(elementId) {
	window.onload = function() {
		window.setInterval(function() {
			var date = new Date();
			var hours = date.getHours();
			var seconds = date.getSeconds();
			var minutes = date.getMinutes();
			document.getElementById(elementId).innerHTML = (hours >= 0 && hours <= 9 ? "0" + hours : hours) + ":" + (minutes >= 0 && minutes <= 9 ? "0" + minutes : minutes) + ":" + (seconds >= 0 && seconds <= 9 ? "0" + seconds : seconds);
		}, 1000);
	};
}

/**
 * Clock - prototype
 * 
 * Example: $('clock').replace(new CMS.Clock());
 */
CMS.Clock = Class.create({
	format : "#{hour}:#{minute}:#{second}",
	
	initialize: function(options) {
		Object.extend(this, options);
		this.createElement();
		this.createTimer();
	},
	
	createElement: function() {
		this.element = new Element("span");
	},

	updateElement: function() {
		var date = new Date();
		
		this.element.update(this.format.interpolate({
			hour:	date.getHours(),
			minute:	date.getMinutes().toPaddedString(2),
			second:	date.getSeconds().toPaddedString(2)
		}));
	},

	createTimer: function() {
		window.setInterval(this.updateElement.bind(this), 1000);
	},
	
	toElement: function() {
		return this.element;
	}
});


/**
 * Text roll down
 */
CMS.TextRollDown = Class.create();
CMS.TextRollDown.prototype = {
    
	initialize: function(text, textMore, anchor){
        
		this.text = $(text);
		this.textMore = $(textMore);
        this.anchor = $(anchor);
        
        Event.observe(this.anchor, "click", this.rollDown.bind(this));
    },
    
    rollDown: function(event){
    	new Effect.BlindDown(this.textMore.id, {duration: 1.0});
    	var anchor = event.findElement();
    	Event.stopObserving(anchor, "click");
    	anchor.hide();
    	event.stop();
    }
};

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

	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");
			}
		});
    }
};