
CMS = Class.create();

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

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

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

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

/**
 * Panel projects
 */
CMS.projects = Class.create();
CMS.projects.prototype = {
    initialize: function(project, items){
		this.project = project;
		this.activeProject = items.first();
		this.items = items;
		this.itemIndex = 0;
		
		// Set events for projects list
		$(this.project.list).select('li').each(function(item) {						
			Event.observe(item, 'click', function(event){
				var number = parseInt(item.firstDescendant().innerHTML);
				this.goTo(event, number);
				Event.stop(event);
			}.bind(this), false);
		}.bind(this));
		
		// Preload image
		this.items.each(function(item) {
			var image = new Image();
			image.src = item.get("img");
		})
		
		// Set other events
		Event.observe(this.project.next, 'click', this.goNext.bindAsEventListener(this), false);
		Event.observe(this.project.show, 'click', this.show.bindAsEventListener(this), false);
		Event.observe(this.project.previous, 'click',this.goPrevious.bindAsEventListener(this), false);
    },
    
	goTo: function(event, number){
		if (this.itemIndex != (number - 1)) {
			this.itemIndex = (number - 1);
			this.reloadProject();
		}
    },
	
	goNext:  function(event){
        if ((this.itemIndex + 1) < this.items.size()) {
			this.itemIndex++;
			this.reloadProject();
		}
			
    },
	
	goPrevious: function(event){
        if (this.itemIndex > 0) {
			this.itemIndex--;
			this.reloadProject();
		}
    },
	
	show: function(event){
        window.location.href = this.getActiveProject().get('url');
    },
	
	getActiveProject: function() {
		return this.items[this.itemIndex];
	},
	
	reloadProject: function() {
		var project = this.getActiveProject();
		
		this.setActiveSpan();
		
		var elements = [this.project.itemName, this.project.itemDescribe, this.project.itemPhoto];
		
		elements.each(function(element) {
			new Effect.Opacity($(element), {
				duration: 0.5,
				from: 1.0,
				to: 0
			});
		});
		
		setTimeout(function() {
			$(this.project.itemName).innerHTML = project.get("name");
			$(this.project.itemDescribe).innerHTML = project.get("describe");
		
			$(this.project.itemPhoto).setAttribute("src", project.get("img"));
			$(this.project.itemHref).setAttribute("href", project.get("url"));
		
		}.bind(this), 500);
		
		setTimeout(function() {
			
			var elements = [this.project.itemName, this.project.itemDescribe, this.project.itemPhoto];
			elements.each(function(element){
				new Effect.Opacity($(element), {
					duration: 0.5,
					from: 0,
					to: 1.0
				});
			}.bind(this))
		}.bind(this), 700);
	},
	
	setActiveSpan: function(){
        
		$(this.project.list).select('li').each(function(item) {						
			var span = item.firstDescendant();
			var number = parseInt(span.innerHTML);
			
			if(number == (this.itemIndex + 1))
				span.addClassName("active");
			else
				span.removeClassName("active");
		}.bind(this));
    }
}

