/**
 * @author Fábio Matavelli <fabio.matavelli@reference.com.br>
 * @since Aug 26, 2009
 * @name /Reference/js/components.js
 */

var includes = new Array('jquery.ui.js','jquery.flash.js','jquery.corner.js','jquery.tinysort.js','jquery.tagcloud.js','jquery.thickbox.js','jquery.pwi.js','jquery.easing.js','jquery.fancybox.js','jquery.validate.js');
document.write('<scr'+'ipt src="/min/b=js&f='+includes.join(',')+'" type="text/javascript"></scr'+'ipt>');
document.write('<scr'+'ipt src="/js/jquery.mask.js"></scr'+'ipt>');

$.extend({
	msgFx: function(texto,tempo,icone) {
		var ico;
		switch(icone) {
			case 'erro':
				ico = "ico_error.gif";
			break;
			case 'alerta':
				ico = "ico_warning.gif";
			break;
			case 'info':
				ico = "ico_info.gif";
			break;
			case 'questao':
				ico = "ico_question.gif";
			break;
		}
		
		if((tempo == '') || !tempo) {
			tempo = 10000;
		}
		
		$('#mensagem').slideUp('fast',function() {
			$(this).html('<img src="/images/'+ico+'" border="0"> <span>'+texto+'</span>').slideDown('fast',function() {
				setTimeout(function() {
					$('#mensagem').slideUp();
				},tempo);
			}).effect("highlight",{
				color: "#ff0000"
			},2000);
		});
	},
	mensagem: {
		alerta: function(txt,tempo) {
			$.msgFx(txt,tempo,'alerta');
		},
		erro: function(txt,tempo) {
			$.msgFx(txt,tempo,'erro');
		},
		info: function(txt,tempo) {
			$.msgFx(txt,tempo,'info');
		},
		questao: function(txt,tempo) {
			$.msgFx(txt,tempo,'questao');
		},
		hide: function() {
			$('#mensagem').slideUp('fast');
		}
	}
});

$.extend({
	notify: {
		show: function(texto, tipo, callback) {
			var notification = $("#notification");
			notification.removeClass("sucess notice error");
			
			notification.css("top",$(window).scrollTop()).css("width",$(document).width());
			
			$("#notification-text").html(texto).addClass(tipo);
			
			notification.slideDown("normal", function() {
				setTimeout(function() {
					$.notify.hide(callback);
				},4000);
			});
		},
		hide: function(callback) {
			$("#notification").slideUp("slow");
			if(null != callback && (typeof callback == "function")) {
				callback();
			}
		}
	}
});

function doBusca(qr) {
	if(qr) {
		$('#search input[name=qr]').val(qr);
		location.href='/busca?qr='+escape(qr.replace(/\s/g,'+'));
		//$('#search_su').hide();
	} else {
		$.mensagem.erro('Você precisa digitar algum termo para buscar.');
	}
}

function sBZone(zoneID) {document.MAX_ct0 ='INSERT_CLICKURL_HERE';var m3_u = (location.protocol=='https:'?'https://referencemusic.websiteseguro.com/ext/ads/openx/www/delivery/ajs.php':'http://ads.reference.com.br/openx/www/delivery/ajs.php');var m3_r = Math.floor(Math.random()*99999999999);if (!document.MAX_used) document.MAX_used = ',';document.write ("<scr"+"ipt type='text/javascript' src='"+m3_u);document.write ("?zoneid="+zoneID+"&amp;target=_top");document.write ('&amp;cb=' + m3_r);if (document.MAX_used != ',') document.write ("&amp;exclude=" + document.MAX_used);document.write (document.charset ? '&amp;charset='+document.charset : (document.characterSet ? '&amp;charset='+document.characterSet : ''));document.write ("&amp;loc=" + escape(window.location));if (document.referrer) document.write ("&amp;referer=" + escape(document.referrer));if (document.context) document.write ("&context=" + escape(document.context));if ((typeof(document.MAX_ct0) != 'undefined') && (document.MAX_ct0.substring(0,4) == 'http')) {document.write ("&amp;ct0=" + escape(document.MAX_ct0));}if (document.mmm_fo) document.write ("&amp;mmm_fo=1");document.write ("'><\/scr"+"ipt>");}

$(document).ready(function() {
	$('#search form').submit(function() {
		doBusca($('#search input[name=qr]').val());
		return false;
	});
	
	$('.procurar_btn a').click(function() {
		doBusca($('#search input[name=qr]').val());
	});
	
	$('#search input[name=qr]').keyup(function() {
		if($(this).val().length > 1) {
			$.getJSON('/Busca.php?ajax=true&query='+escape($(this).val()),function(data) {
				if(data.total > 0) {
					$('#search_su').show().empty();
					$.each(data.resultado, function(i,item){
						$('#search_su').append('<span><a href="javascript:void(0);" onclick="javascript:doBusca(\''+item.qr+'\');">'+item.qr+'</a></span>');
					});
				} else {
					$('#search_su').hide().empty();
				}
			});
		} else {
			$('#search_su').hide().empty();
		}
	}).blur(function() {
		setTimeout(function() {
			$('#search_su').hide();
		},2000);
	});
	
	$('#banner_flash').flash({
		src: '/swf/genz_fluido.swf',
		width: '100%',
		height: 380,
		wmode: 'transparent',
		allowScriptAccess: 'always',
		salign: 't'
	}).css({
		'background-color': 'transparent',
		'z-index': '40',
		'height': 200
	});

	//if($.browser.msie) {
		$('#banner_flash').hover(function() {
			$(this).css({
				'z-index':'40'
			}).height(380);
			$('#flash_parent').css('z-index','40');
		},function() {
			$(this).css({
				'z-index':'1'
			}).height(200);
			$('#flash_parent').css('z-index','1');
		});
	/*} else {
		$('#banner_flash').css('z-index','40');
	}*/
		
	$('.menu_item').mouseover(function() {
		$('#submenu_'+$(this).attr('id')).show();
	}).mouseout(function() {
		$('#submenu_'+$(this).attr('id')).hide();
	});

	$('.menu_ttl a').click(function() {
		if(!$('.menu_itens_departamento:first').is(":hidden")) {
			$('.menu_ttl_departamento img').attr('src','/images/arrow_right.png');
		} else {
			$('.menu_ttl_departamento img').attr('src','/images/arrow_down.png');
		}
		
		$('.menu_itens_departamento').toggle("fast");
	});
});