var files;

$('.faq_categoria').live('click', function(){
	var id = this.id.split('-');
	$('#perguntas-'+id[1]).slideToggle();
});

$(window).bind('load',function(){
	$('#boxListCarrousel').jCarouselLite({
		btnNext: '.btNext',
		btnPrev: '.btPrevious',
		visible: 3,
		circular: false
	});
});

$(document).ready(function(){
	zebrarTable('.shopGrid','#f6f6f6');
	zebrarTable('.gridList','#f6f6f6');
	startToolTip();
	//ATIVA BOTAO DO HEADER QUE CARREGA A LISTA DO CONTAINER
	$('#popupContainer').overlay({
		center:false,
		buttonClose: '.exitContainer',
		callbackOpen:function(){
			$.ajax({
					dataType: 'html',
					type: 'GET',
					url: 'frontend/shop/shop-action.php',
					data: 'action=grid',
					success: function(list){ $('#infoContainer').toggle().html(list); zebrarTable('.shopGrid','#f6f6f6'); startToolTip('C'); startDetalhesTable(); startShipping(); }
				});
		},
		callbackClose: function(){ $('#infoContainer').toggle().empty()}
	});

	$('#headerLogin').overlay({
		center:false,
		buttonOpen: '#btLogin',
		callbackOpen:function(){ $("#headerBoxLogin").toggle(); },
		callbackClose:function(){ $("#headerBoxLogin").toggle(); }
	});

	/*
	$('#headerRegister').overlay({
		center:false,
		buttonOpen: '#btRegister',
		callbackOpen:function(){ $("#headerBoxRegister").toggle(); },
		callbackClose:function(){ $("#headerBoxRegister").toggle(); }

	});
	*/
	startDetalhes();
	startDetalhesTable();
	startShipping();
/*
* BOTOES DE PAGINAÇÃO
*/
	$('#btPag2cm').click(function(){
		paginacao(this,'#2cm');
		return false;
	});
	$('#btPag3cm').click(function(){
		paginacao(this,'#3cm');
		return false;
	});
	$('#btPagnew').click(function(){
		paginacao(this,'#new');
		return false;
	});
	$('#btPagvisited').click(function(){
		paginacao(this,'#visited');
		return false;
	});
	$('#btPagleftover').click(function(){
		paginacao(this,'#leftover');
		return false;
	});
	$('#btPagrecommends').click(function(){
		paginacao(this,'#recommends');
		return false;
	});
	$('#btPagmaterial').click(function(){
		paginacao(this,'#material');
		return false;
	});

	$('#swfUpload').overlay({
		buttonOpen: '.openSWF',
		buttonClose: '.btclose',
		center:true,
		callbackOpen: function(){ $('#swfUpload').toggle(); }
	});
	
	$('body').overlay({
		buttonOpen: '.btTopic.delet',
		buttonClose:'.btclose',
		center:true
	});

});


	settings = {
		flash_url : "script/swfupload/Flash/swfupload.swf",
		upload_url: "script/swfupload/upload.php",
		post_params: {"PHPSESSID":PHPSESSID},
		file_size_limit : "100 MB",
		file_types : "*.*",
		file_types_description : "All Files",
		file_upload_limit : 100,
		file_queue_limit : 0,
		custom_settings : {
			progressTarget : "fsUploadProgress",
			cancelButtonId : "btnCancel"
		},
		debug: false,

		// Button settings
		button_width: "85",
		button_height: "20",
		button_placeholder_id: "spanButtonPlaceHolder",
		button_text_left_padding: 12,
		button_text_top_padding: 3,
		button_window_mode: SWFUpload.WINDOW_MODE.TRANSPARENT,
		button_cursor: SWFUpload.CURSOR.HAND,		
		
		// The event handler functions are defined in handlers.js
		file_queued_handler : fileQueued,
		file_queue_error_handler : fileQueueError,
		file_dialog_complete_handler : fileDialogComplete,
		upload_start_handler : uploadStart,
		upload_progress_handler : uploadProgress,
		upload_error_handler : uploadError,
		upload_success_handler : uploadSuccess,
		upload_complete_handler : uploadComplete,
		queue_complete_handler : queueComplete	// Queue plugin event
	};

	//swfu = new SWFUpload(settings);


function imgRemove(e,id,location){
	$(e).parent().fadeOut(function(){ $(this).remove(); });
	location = (location == undefined )? 'tmp' : location; 
	$.ajax({
		url: 'frontend/forum/forum-action.php?action=imgRemove',
		type: 'POST',
		data: {location: location,id: id}
	});
	return false;
}
/*
* START PIROBOX
*/
function pirobox(){
	$().piroBox({
		  my_speed: 300, //animation speed
		  bg_alpha: 0.7, //background opacity
		  slideShow : 'true', // true == slideshow on, false == slideshow off
		  slideSpeed : 3 //slideshow
		  //close_all : '.piro_close' // add class .piro_overlay(with comma)if you want overlay click close piroBox
	});
}

function startToolTip(target){
	$("table tr[title]").each(function(){
		$(this).tooltip("destroy");
		$(this).tooltip({content: function(resp){
						var img = $(this).attr('title');
						img = "<img src='corta.php?i="+img+"&x=158&y=98' alt='Loading...' />";
						resp(img);
						},
			position: (target == 'C') ? {offset: "-580 -40"} : {offset: "-550 -70"}
		});
	});
}
/*
* BUNDLES DETALHES   at: "center top",my: "center bottom"
*/
//ATIVA O OVERLAY NOS BOTOES DETALHES DE PRODUTOS
function startDetalhes(_url,_bt){
	$('body').overlay({
		buttonOpen: _bt==undefined?'.details':_bt,
		buttonClose:'.btclose',
		ajaxUrl: _url!=undefined?_url:undefined,
		center:true,
		callbackOpen: ativaCarrosel,
		callbackClose: function(){ $('.piro_overlay').remove(); $('.pirobox_content').remove(); $('#imgCache').remove();}
	});
}

//ATIVA O OVERLAY AO RECEBER O ID DO PRODUTO VIA URL
function startDetalhesMailing(_id){
	$('body').overlay({
		buttonClose:'.btclose',
		ajaxUrl: "inc/bundle-detalhe.php?id="+_id,
		center:true,
		callbackOpen: ativaCarrosel,
		callbackClose: function(){ $('.piro_overlay').remove(); $('.pirobox_content').remove(); $('#imgCache').remove();}
	},true);
}



function startDetalhesTable(){
	$("table tr[id]").each(function(){
		var id = $(this).attr('id');
		startDetalhes("inc/bundle-detalhe.php?id="+id,"#"+id);
	});
}

//ATIVA O CARROSEL DO POPUP DETALHES
function ativaCarrosel(){
	$('#boxDetalheCarrousel').jCarouselLite({
		btnNext: '.btDetalheNext',
		btnPrev: '.btDetalhePrevious',
		visible: 6,
		circular: false
	});
	imgDestaque();
}

function imgDestaque(){
	$("#boxDetalheCarrousel li a").each(function(){
		$(this).click(function(){
			var img = $(this).attr('href');
			var id = $(this).attr('id');
			var linkAlvo = $("#boxImageDetalhe img").attr('src');
			var oldImg = getLinkImage(linkAlvo);
			linkAlvo = linkAlvo.replace(oldImg,img);
			$("#boxImageDetalhe img").attr('src',linkAlvo);

			linkAlvo = $("#boxImageDetalhe a").attr('href');
			oldImg = getLinkImage(linkAlvo);
			linkAlvo = linkAlvo.replace(oldImg,img);
			$("#boxImageDetalhe a").attr('href',linkAlvo);

			$(".btDownload").attr('href',files[id]);
			return false;
		});
	});
	pirobox();
}

function getLinkImage(src){
	var ini = src.indexOf("i=") + 2;
	var fim = src.indexOf("&x");
	return src.substring(ini,fim);
}



/*
* PAGINAÇÃO
*/
function paginacao(bt,alvo){
	var box = $(bt).parent();
	var botao = box.html();
	box.html("<span ><img src='img/load.gif' alt='load' /></span>");

	$.ajax({
		url: $(bt).attr('href'),
		type: 'POST',
		dataType: 'html',
		success: function(msg){
			$(alvo).append(msg);
			box.html(botao);
			ativaBtPag($(bt).attr('id'));
			startDetalhes();
			zebrarTable('.shopGrid','#f6f6f6');
			startToolTip();
			if (msg=='') $('.seeMore').hide();
		},
		error: function(obj,error){ alert('error');  }
	});
}

function ativaBtPag(id){

	switch(id){
		case 'btPagnew':
			$('#btPagnew').click(function(){
				paginacao(this,'#new');
				return false;
			});
		break;
		case 'btPagvisited':
			$('#btPagvisited').click(function(){
				paginacao(this,'#visited');
				return false;
			});
		break;
		case 'btPagleftover':
			$('#btPagleftover').click(function(){
				paginacao(this,'#leftover');
				return false;
			});
		break;
		case 'btPagrecommends':
			$('#btPagrecommends').click(function(){
				paginacao(this,'#recommends');
				return false;
			});
		break;
		case 'btPag2cm':
			$('#btPag2cm').click(function(){
				paginacao(this,'#2cm');
				return false;
			});
		break;
		case 'btPag3cm':
			$('#btPag3cm').click(function(){
				paginacao(this,'#3cm');
				return false;
			});
		break;
		case 'btPagmaterial':
			$('#btPagmaterial').click(function(){
				paginacao(this,'#material');
				return false;
			});
		break;

	}
}

function exibeMensagem(str,type){
	type = type==undefined ? 'formError' : type;
	//$("#content .row > p").remove();
	//$("#content .row > strong").remove();
	//$("#content .row > h1").after("<strong class='"+type+"'>"+str+"</strong>");
	if($(".formWarning").length > 0) $('.formWarning').remove();
	$("#msgSite").css('display','block').html("<strong class='"+type+"'>"+str+"</strong>");

}

function startShipping(){
	$('body').overlay({
		buttonOpen: '.shipping',
		buttonClose: '.shippingClosed',
		ajaxUrl:'frontend/shop/shipping-quote.php',
		center:true,
		callbackOpen:function(){
			$('#popupContainer').css({
				'-moz-opacity': 0.4,
				'opacity': 0.4,
				'filter':'alpha(opacity=40)'
			});
		},
		callbackClose:function(){
			$('#popupContainer').css({
				'-moz-opacity': 1,
				'opacity': 1,
				'filter':'alpha(opacity=100)'
			});
		}

	});
}


function sendShipping(strError){
	var strGet;

	if(strError==undefined){
		strGet = $('#formShippingQuote').serialize();
		$.ajax({
			dataType: 'html',
			type: 'GET',
			url: 'frontend/shop/shop-action.php',
			data: 'action=sendShipping&'+strGet,
			success: function(msg){
				$('#formShippingQuote > fieldset').html('<legend>Form to Shipping Quote</legend><div id="msgSuccess">Seus dados foram enviados com sucesso</div>');
				$('#shipping').css('height',150);
			}
		});
	}
}



/*
* UTILIDADES
*/
function zebrarTable(tbl,cor){
	$(tbl+" tr[style]").removeAttr('style');
	$(tbl+' tr:nth-child(2n)').css('background-color',cor);
}















