/**
 * (C) Copyright 2007 Rits. All rights reserved.
 * Use is subject to license terms.
 */

if(typeof Rits == "undefined") {
	var Rits = {};
}

/**
 * Util
 *
 * @category   Rits
 * @package    Rits
 * @copyright  Copyright (c) 2007 Rits Comunicacao & Teconologia. (http://www.rits.com.br) 
 */
Rits.Util = {
		
	/**
	 * Opens a popup window
	 *
	 * @param string href
	 * @param int width
	 * @param int height
	 * @param string name
	 * @param int top
	 * @param int left
	 * @param string scrollbars
	 * @param string toolbar
	 * @param string location
	 * @param string status
	 * @param string menubar
	 * @param string resizable
	 * @param string dependent
	 */
	linkPopup: function(href, width, height, name, top, left, scrollbars, toolbar, location, status, menubar, resizable, dependent) {
		
		// href
		if(!href) {
			href = '/';
		}
		// width
		if(!width) {
			width = 400;
		}
		// height
		if(!height) {
			height = 300;
		}
		// window name
		if(!name) {
			name = '_window';
		}
		// top
		if(!top) {
			top = (screen.width - width)/2;
		}
		// left
		if(!left) {
			left = (screen.height - height)/2;
		}
		// scrollbars
		if(!scrollbars) {
			scrollbars = 'yes';
		}
		// toolbar
		if(!toolbar) {
			toolbar = 'no';
		}
		// location
		if(!location) {
			location = 'no';
		}
		// status
		if(!status) {
			status = 'no';
		}
		// menubar
		if(!menubar) {
			menubar = 'no';
		}
		// resizable
		if(!resizable) {
			resizable = 'no';
		}
		// dependent
		if(!dependent) {
			dependent = 'yes';
		}

		settings = 'height='+height+',width='+width+',top='+top+',left='+left+',scrollbars='+scrollbars+',toolbar='+toolbar+',location='+location+',status='+status+',menubar='+menubar+',resizable='+resizable+',dependent='+dependent;		
		var win = window.open (''+href+'', ''+name+'', settings);		
		
		if (parseInt(navigator.appVersion) >= 4) {
			win.window.focus();
		}
	},	
	
	/**
	 * Returns a cookie
	 *
	 * @param string name
	 * @return string
	 */
	getCookie: function(name) {
		var start = document.cookie.indexOf( name + "=" );
		var len = start + name.length + 1;
		if ((!start) && (name != document.cookie.substring( 0, name.length))) {
			return null;
		}
		if (start == -1) return null;
		var end = document.cookie.indexOf(";", len);
		if (end == -1) end = document.cookie.length;
		return unescape(document.cookie.substring(len, end));		
	},
	
	/**
	 * Sets a cookie
	 *
	 * @param string name
	 * @param string value
	 * @param string expires
	 * @param string path
	 * @param string domain
	 * @param boolean secure
	 */
	setCookie: function(name, value, expires, path, domain, secure) {
		// set time, it's in milliseconds
		var today = new Date();
		today.setTime( today.getTime() );
	
		/*
		if the expires variable is set, make the correct
		expires time, the current script below will set
		it for x number of days, to make it for hours,
		delete * 24, for minutes, delete * 60 * 24
		*/
		if (expires) {
			expires = expires * 1000 * 60 * 60 * 24;
		}
		var expires_date = new Date( today.getTime() + (expires) );
	
		document.cookie = name + "=" +escape( value ) +
		( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
		( ( path ) ? ";path=" + path : "" ) +
		( ( domain ) ? ";domain=" + domain : "" ) +
		( ( secure ) ? ";secure" : "" );		
	},
	
	/**
	 * Removes a cookie
	 *
	 * @param string name
	 * @param string path
	 * @param string domain
	 */
	delCookie: function(name, path, domain) {
		if (getCookie(name)) {
			document.cookie = name + "=" +
			( ( path ) ? ";path=" + path : "") +
			( ( domain ) ? ";domain=" + domain : "" ) +
			";expires=Thu, 01-Jan-1970 00:00:01 GMT";
		}		
	},	
	
	/**
 	 * Reloads a window
	 */
	reload: function(win) {
		if(!win) {
			win = window;
		}
		win.location.reload();
	},

	/**
	 * Sets a refresh
	 *
	 * @param int time
	 */
	refresh: function(time) {
		setTimeout("Rits.Util.reload()", time);
	},

	/**
	 * Sets the window status
	 *
	 * @param string message
	 */
	setStatus: function(message) {
		window.status = message;
	},

	/**
	 * Updates parent and close window
	 */
	updateParent: function() {
		if (window.opener && !window.opener.closed) {
			Rits.Util.reload(window.opener);			
		}
		self.close();
	},

	/**
	 * Preloads images
	 */
	preloadImages: function() {
		
		if(document.images){ 
			if(!document.preload) {
				document.preload = new Array();
			}
		}
		var i;
		var j = document.preload.length;		

		for(i=0; i<arguments.length; i++) {
			if (arguments[i].indexOf("#") != 0) {
				document.preload[j] = new Image();
				document.preload[j++].src = arguments[i];
			}
		}
	},
	
	/**
	 * Modifies url parameters and reloads
	 *
	 * @param string name
	 * @param string value
	 * @param string url
	 * @param boolean ret
	 * @return string
	 */
	modifyUrlParameter: function(name, value, url, ret) {		
		
		// url
		if(!url) {
			url = location.href;
		}

		if(url.indexOf('?' + name + '=') != -1 || url.indexOf('&' + name + '=') != -1) {
			var replace = new RegExp(name + '=[^&]*');						
			url = url.replace(replace, name + '=' + escape(value));
		} else {
			if(url.indexOf('?') != -1) {
				url = url + '&' + name + '=' + escape(value);
			} else {
				url = url + '?' + name + '=' + escape(value);
			}
		}
		
		if(!ret) {
			location.href = url;
		}
		
		return url;
	},
		
	changeTab: function(n, t, prefixMenu, prefixBox) {
		var total = t*2 + 1;
		var sel = n*2;
		
		var div;
		
		for(var i=1; i<=total; i++) {
			div = $(prefixMenu+i);
			
			if(i == sel) { //center
				div.className = 'tabMenuItem_on';
			} else if(i == sel-1) { //left
				if(div.className == 'tab_left_off' || div.className == 'tab_left_on') {
					div.className = 'tab_left_on';
				} else {
					div.className = 'tab_off_on';
				}
			} else if(i == sel+1) { //right
				if(div.className == 'tab_right_off' || div.className == 'tab_right_on') {
					div.className = 'tab_right_on';
				} else {
					div.className = 'tab_on_off';
				}			
			} else {
				if(div.className == 'tabMenuItem_on') {
					div.className = 'tabMenuItem_off';
				} else if(div.className == 'tab_left_on') {
					div.className = 'tab_left_off';
				} else if(div.className == 'tab_right_on') {
					div.className = 'tab_right_off';
				} else if(div.className == 'tab_on_off' || div.className == 'tab_off_on'){
					div.className = 'tab_off_off';
				}
			}
		}
		
		for(var i=1; i<=t; i++) {
			div = $(prefixBox+i);
			
			if(i == n) {
				div.style.display = '';
			} else {
				div.style.display = 'none';
			}
		}
	},
	
	appendHtml: function(div, html) {
		var e = $(div);
		e.innerHTML += html;	
	},
	
	openFile: function(url) {
		window.top.opener.SetUrl(url);
		window.top.close();
		window.top.opener.focus();
	},
	
	changeDisplay: function(element) {
		if($D.getStyle(element, 'display') == 'none') {
			$D.setStyle(element, 'display', '');	
		} else {
			$D.setStyle(element, 'display', 'none');	
		}		
	}
	
}
