/**
 * 
 * @author frbk.misc@gmail.com
 *
 */

/** Function that creates a cookie
 * 
 * @param String cookie name
 * @param Mixed cookie value
 * @param Integer cookie expiration in days from now
 */
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	} else expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

/** Function that reads a cookie
 * 
 * @param String cookie name
 */
function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

/** Function to return an array of elements marked with
 * a style class
 * 
 * @param Object The element that contains the desired child elements
 * @param String The type of tag to search for
 * @param String The style class name to search for
 */
function getElementsByClassName(oElm, strTagName, strClassName){
    var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
    var arrReturnElements = new Array();
    strClassName = strClassName.replace(/\-/g, "\\-");
    var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
    var oElement;
    for(var i=0; i<arrElements.length; i++){
        oElement = arrElements[i];      
        if(oRegExp.test(oElement.className)){
            arrReturnElements.push(oElement);
        }   
    }
    return (arrReturnElements)
}

var tmp_imgs = new Array();
var tot_imgs = 0
function preloadImage(title, src) {
	tmp_src = 'img/' + title + '/' + src;
	tmp_imgs[tot_imgs] = new Image();
	tmp_imgs[tot_imgs].src = tmp_src;
	
	tot_imgs++;
}

function writeImage(src, params) {
	var title = cookie ? cookie : getPreferredStyleSheet();
	
	if(title == null) {
		title = 'red';
	}
	
	preloadImage(title, src);
	
	var return_str = "<img src=\"img/" + title + "/" + src + "\" ";
	
	if(params != '' && params != undefined) {
		var params_obj = eval("(" + params + ")");
	
		for(param in params_obj) {
			return_str = return_str + param + "=\"" + params_obj[param] + "\" ";
		}
	}
	
	return_str = return_str + "/>";
	
	return return_str;
}

function correctPosition(oElement, oPos, oWhich) {
	while( oElement.offsetParent ) {
		oPos -= oElement['offset'+oWhich];
		oElement = oElement.offsetParent;
	}
	oPos += document.documentElement['scroll'+oWhich] ? document.documentElement['scroll'+oWhich] : document.body['scroll'+oWhich];
	return oPos;
}

function showLoading(tgt) {
	$(tgt).innerHTML = '<span class="loading"><span class="load_graph"></span>Carregando...</span>';
	
	// adjusts the scroll position for the new content
	window.scrollTo(0, 0);
	window.scroll(0, 0);
}

/** A function that is called whenever the user
	presses the back or forward buttons. This
	function will be passed the newLocation,
	as well as any history data we associated
	with the location. */
function handleHistoryChange(newLocation, historyData) {
	// use the history data to update our UI
	updateUI(newLocation, historyData);
}

/** A simple method that updates our user
	interface using the new location. */
function updateUI(newLocation, historyData) {
	
	if(newLocation.indexOf('portfolio') > -1) {
		menuLocation = 'portfolio';
	} else {
		menuLocation = newLocation;
	}
	
	// change the current content to
	// the one stored in the historyData object
	if (historyData != null) {
		// AJAX updater for the new content		
		//showLoading('content_holder');
		var historyContent = new Ajax.Updater('content_holder', historyData.url, {asynchronous:true, evalScripts:true, loading:showLoading('content_holder')});
		updateMenu(menuLocation);
	} else {
		//var historyContent = new Ajax.Updater('content_holder', '/', {asynchronous:true, evalScripts:true, loading:showLoading('content_holder')});
		updateMenu(menuLocation);
	}
}

/** A function to update the current selected
	section in a menu */
function updateMenu(nextSection) {
	//alert(nextSection + " != " + currentSection);
	if(nextSection != currentSection){
		if(currentSection != '' && currentSection != undefined && currentSection != null) {
			oldItem = $("bt_" + currentSection);
			oldClassNames = Element.classNames(oldItem);
			oldClassNames.set('menu_item');
		}
		
		if(nextSection != '' && nextSection != undefined && nextSection != null) {
			currentItem = $("bt_" + nextSection);
			currentClassNames = Element.classNames(currentItem);
			currentClassNames.set('menu_item_selected');
		}
		
		currentSection = nextSection;
	}
}