/**
 * @author Kozicki Jakub
 * @copyright Kozicki Jakub <kuba.kozicki@gmail.com>
 */

var CMS = {};

/**
 * Open new window
 */
CMS.open = function(event) {
	element = event.target;
	while(element.nodeName.toLowerCase() != "a") {
		element = element.parentNode;
	}
	window.open(element.getAttribute("href"));
	event.stopPropagation();
}

/**
 * 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=500, left="+parseInt((sWidth-700)/2)+", top="+parseInt((sHeight-400)/2)+", scrollbars, resizable");
	oWindow.focus();
}

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

/**
 * Search form
 */
CMS.SearchForm = function(form, field, inputSubmit, emptyText){
    this.form = jQuery(form);
    this.inputSubmit = jQuery(inputSubmit);
    this.field = jQuery(field);
    this.emptyText = emptyText;
    
    this.form.submit(function(event) {
        if (that.field.attr('value') == that.emptyText || that.field.attr('value') == '') {
        	event.preventDefault();
        }
    });
    
    this.field.focus(function(event){
        if (that.field.attr('value') == that.emptyText) {
            that.field.attr('value', '');
        }
    });
    
    this.field.blur(function(event){
        if (that.field.attr('value') == '') {
            that.field.attr('value', that.emptyText);
        }
    });
    
    var that = this;
    
    this.field.blur();
};

/**
 * 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);
	};
}

/**
 * Decorate anchors
 */
CMS.DecorateAnchors = function(content) {
	var anchors = jQuery(content).find("a");
	
	jQuery.each(anchors, function() {

		if(/^.*\.(doc|docx)$/.test(this.getAttribute("href")) == true) {
			jQuery(this).addClass("anchor_word");
		}
		
		if(/^.*\.(xls|xlsx)$/.test(this.getAttribute("href")) == true) {
			jQuery(this).addClass("anchor_excel");
		}
		
		if(/^.*\.(ppt|pptx)$/.test(this.getAttribute("href")) == true) {
			jQuery(this).addClass("anchor_powerpoint");
		}
		
		if(/^.*\.pdf$/.test(this.getAttribute("href")) == true) {
			jQuery(this).addClass("anchor_pdf");
		}
	});
};

