// Funcoes para aumentar/diminuir fonte
$.fontSizeChange = function(element){
	var originalFontSize = $(element).css('font-size');
	// Reset Font Size
	$(".resetFont").click(function(){
		$.cookie('font_size', originalFontSize); // set cookie
		$(element).css('font-size', originalFontSize);
		return false;
	});
	// Increase Font Size
	$(".increaseFont").click(function(){
		var currentFontSize = $(element).css('font-size');
		var currentFontSizeNum = parseFloat(currentFontSize, 10);	
		var newFontSize = currentFontSizeNum*1.2;
		if (newFontSize < 60) {
			$.cookie('font_size', newFontSize); // set cookie
			$(element).css('font-size', newFontSize);
		};
		return false;
	});
	// Decrease Font Size
	$(".decreaseFont").click(function(){
		var currentFontSize = $(element).css('font-size');
	 	var currentFontSizeNum = parseFloat(currentFontSize, 10);
		var newFontSize = currentFontSizeNum*0.8;
		if (newFontSize > 8) {
			$.cookie('font_size', newFontSize); // set cookie
			$(element).css('font-size', newFontSize);
		};
		return false;
	});
};

// Funcao que seta o tamanho da fonte de acordo com o cookie
$.fontSizeSet = function(element){
	$(element).css('font-size', $.cookie('font_size'));
};

// Link direto na combo
function changeLocation(obj){
	if(obj.value != "") window.location = obj.value;
};

/*
	Open Popup
	rel=external é capturada no click do link
	$("a[@rel=external]").click
	Os parametros devem ser passados no atributo class do link
	
	Se não forem abre uma blank pegando o href 
	<a href="http://www.wunderman.com.br" rel="external">
	
	Para passar os atributos deve ser seguido o exemplo abaixo:
	<a href="http://www.wunderman.combr" class="link-item {name:'wunderman',width:300,height:300,options:'scrollbars=no',center:true}" rel="external">Lorem</a>
	
	Lista de atributos:
		- name é o nome do pop entre apostrofos
		- width é o largura do pop
		- height é o altura do pop
		- options são as features entre apostrofos, separadas por vírgula
		- center, true se ele tiver que ficar centralizado e false se não
		
	A função deve ser chamada dentro do init
	$.openPopup();
	
*/
$.openPopup = function() {
	var top, left, href, width, height, options, center, features;
	top = '';
	left = '';
	$("a[@rel=external]").click(function(){
		var pop = $(this).metadata();
		center = pop.center || false;
		if (center) {
	        left = (screen.availWidth - pop.width) / 2;
	        top = (screen.availHeight - pop.height) / 2;
	    };
		
		href    = $(this).attr('href');
		name    = ((pop.name)? + pop.name : '');
		width   = ((pop.width)? ',width='  + pop.width : '');
		height  = ((pop.height)? ',height=' + pop.height : '');
		options = ((pop.options)?',' + pop.options : '');
		
		if(top==''){
			window.open(href);
		}
		else{
			features = width+height+options;
			window.open(href,'pop_'+name,'left='+left+',top='+top+features); 
		};
		return false;
	});
};

$.fn.populateCombo = function(to,initialPath,file,xmlNode,typeText,typeValue){
	$(this).bind('change',function(){
	
		var xmlPath = '';
		switch(xmlNode){
			case 'city':
				xmlPath = initialPath + $('#selCountry').val().toLowerCase()  + '/cities/'+ this.value.toLowerCase()+file;
				typeValue = 'name';
				break;
			case 'state':
				xmlPath = initialPath + this.value.toLowerCase() + file;
				break;
				
		}
		
		$(to).find('option')[0].title=$(to).find('option')[0].text;
		$(to).find('option')[0].text = 'Loading...';
		
		$(to).find('option').each(function(i){
			if(i>0)$(this).remove();
			
		});
		$(to).attr('disabled','disabled');
		
		if (jQuery.trim(this.value) == '') {
			$(to).find('option')[0].text=$(to).find('option')[0].title;
			return false;
		};
		
		ajaxPopulate(to,xmlPath,xmlNode,typeText,typeValue);
		//$(this).unbind('change');
	});
	
};


ajaxPopulate = function (to,xmlPath,xmlNode,typeText,typeValue){
	if(typeText==undefined)typeText='name';
	if(typeValue==undefined)typeValue='abbr';
	
	$.ajax({	
		url: xmlPath,
		cache: false,
		success: function(returnXML){
			if ($(to).find('option').length == 1) {
				$(returnXML).find(xmlNode).each(function(i){
					$(to).append('<option value="' + $(this).attr(typeValue) + '">' + $(this).attr(typeText) + '<\/option>');
				});
			}
			$(to).removeAttr('disabled');	
		}
	});
	$(to).find('option')[0].text=$(to).find('option')[0].title;
	
};

String.prototype.replaceAll = function(from, to){
    var str = this;
    var pos = str.indexOf(from);
	while (pos > -1){
		str = str.replace(from, to);
		pos = str.indexOf(from);
		

	}

    return (str);
};

function replaceAccent(str) {
	var s=str.toString().toLowerCase();
	var rExps=[ /[\xC0-\xC2]/g, /[\xE0-\xE2]/g,
	/[\xC8-\xCA]/g, /[\xE8-\xEB]/g,
	/[\xCC-\xCE]/g, /[\xEC-\xEE]/g,
	/[\xD2-\xD4]/g, /[\xF2-\xF4]/g,
	/[\xD9-\xDB]/g, /[\xF9-\xFB]/g ];
	var repChar=['A','a','E','e','I','i','O','o','U','u'];

	for(var i=0; i<rExps.length; i++)s=s.replace(rExps[i],repChar[i]);

	return s;
}

//mostra thick box no validator ou diretamente do BE
controlMessage = function(caption,content){
	var container = $('<div class="controlMessage-container"></div>')
	container.append((content)?content:$('<ul class="errors"></ul>'));
	var caption = caption;
	
	this.add = function(message,tag,klass){
		$(((klass)?klass:'.errors'),container).append($('<'+((tag)?tag:'li')+'>'+message+'</'+((tag)?tag:'li')+'>'));
		return this;
	};
	
	this.clean = function(klass){
		$(((klass)?klass:'.errors'),container).children().remove();
		return this;
	};
	
	this.show = function(width,height){
		dynamic_box(caption,container.clone(),width,height);
		return this;
	};
	return this;
}

if(!console || (!console.info || !console.dir))
{
	var console = {
		info : function(msg){
			alert(msg);
		},
		dir : function(obj){
			console.info(obj.toString());
		}
	}
}

controlPage = function(objs,bt_prev,bt_next,bt_class){
	var spots = $(objs);
	var current_spot = 0;
	var bt_prev = bt_prev;
	var bt_next = bt_next;
	var bt_class = bt_class;
	
	this.next = function(){
		if(current_spot+1 < spots.length)
		{
			$(spots[current_spot]).hide();
			current_spot = current_spot+1;
			$(spots[current_spot]).show();
		}
		if(current_spot+1 == spots.length)
		{
			$(bt_next).addClass(bt_class);
		}
		 
		$(bt_prev).removeClass(bt_class);
	}

	this.preview = function(){
		if(current_spot > 0)
		{
			$(spots[current_spot]).hide();
			current_spot = current_spot-1;
			$(spots[current_spot]).show();   
		}
		if(current_spot == 0)
		{
			 $(bt_prev).addClass(bt_class);
		}
		
		$(bt_next).removeClass(bt_class);
	}
	
	$(bt_next).bind('click',this.next);
	$(bt_next).attr('href','javascript:void(0);');
	$(bt_prev).bind('click',this.preview);
	$(bt_prev).attr('href','javascript:void(0);');
	
}