var CMSAPP = {}

CMSAPP.init = function () {
  for (prop in this) {
	if (typeof this[prop].init == 'function') {
	  this[prop].init();
	}
  }
}

/**** dropdown pesquisa **/

CMSAPP.fancyCombobox1 = {
  init: function () {  
	$('#search_categorias').sSelect();
	$('#search_regiao').sSelect();
  }
}

/**** vertical align bind ***/

CMSAPP.myVerticalAlign = {
  init: function () {
	if(homePage==true)
	{
	  $('.vA').VerticalAlign();
	}
	
  }
}

/*** precisa de ajudar hover  ***/

CMSAPP.caixaIndex = {
  init: function () {
	/****** Caixa index ******/
	$("#precisa_ajuda").mouseout(function(){
		$("#precisa_ajuda h1").css("color","#00B0D8"); 
		$("#precisa_ajuda span").css("color","#999999");
		$("#precisa_ajuda").css("background","url(/img/but_precisaAjuda_normal.png) no-repeat");
	});
	$("#precisa_ajuda").hover(function(){
		$("#precisa_ajuda h1").css("color","white"); 
		$("#precisa_ajuda span").css("color","white");
		$("#precisa_ajuda").css("background","url(/img/but_precisaAjuda_over.png) no-repeat");
	});
	$("#precisa_ajuda h1").mouseout(function(){
		$("#precisa_ajuda h1").css("color","#00B0D8"); 
		$("#precisa_ajuda span").css("color","#999999");
		$("#precisa_ajuda").css("background","url(/img/but_precisaAjuda_normal.png) no-repeat");
	});
	$("#precisa_ajuda h1").hover(function(){
		$("#precisa_ajuda h1").css("color","white"); 
		$("#precisa_ajuda span").css("color","white");
		$("#precisa_ajuda").css("background","url(/img/but_precisaAjuda_over.png) no-repeat");
	});
	$("#precisa_ajuda span").mouseout(function(){
		$("#precisa_ajuda h1").css("color","#00B0D8"); 
		$("#precisa_ajuda span").css("color","#999999");
		$("#precisa_ajuda").css("background","url(/img/but_precisaAjuda_normal.png) no-repeat");
	});
	$("#precisa_ajuda span").hover(function(){
		$("#precisa_ajuda h1").css("color","white"); 
		$("#precisa_ajuda span").css("color","white");
		$("#precisa_ajuda").css("background","url(/img/but_precisaAjuda_over.png) no-repeat");
	});
  }
}

/*** resolução ecrã ***/

CMSAPP.myScreenResolution = {
  init: function () {
	  
	jQuery.event.add(window, "load", loadFrame);
	//jQuery.event.add(window, "resize", resizeFrame);
	
	function loadFrame() 
	{
	  
		var w = $(window).width();
		if(homePage==true)
		{
		  if(w < 1143)
		  {
			//$('#destaques').load('/premium/destaques_tipo1/r1');
		  }
		  
		  if(w > 1143 && w < 1301)
		  {
			$('#centro').load('ads/r2', function() {
			  $(".resolucao_2_home").css('display',(w > 1143 && w < 1301) ? "inline" : "none");
			  //$('#destaques').load('/premium/destaques_tipo1/r2');
			});
		  }
		  
		  if(w > 1301)
		  {
			$('#centro').load('ads/r3', function() {
			  $(".resolucao_3_home").css('display',(w > 1301) ? "inline" : "none");
			  //$('#destaques').load('/premium/destaques_tipo1/r3');
			});
			
		  }
		  
		}
		
		if(w < 1143 && homePage!=true)
		{
		  /*if(typeof premiumCatId != 'undefined')
			$('#centro .resolucao_1').load('/premium/destaques_tipo1/r1/' + premiumCatId);
		  else
			$('#centro .resolucao_1').load('/premium/destaques_tipo1/r1');*/
		  
		  $("#centro .resolucao_1").css('display',(w < 1143) ? "inline" : "none");
		  $("#centro").css('width', "622px");
		  $("#AdAddesc_tbl").css('width', "500px");
		  
		  $('.vA').VerticalAlign();
		  setTimeout(function() {
			
			settings = {
			  tl: { radius: 5 },
			  tr: { radius: 5 },
			  bl: { radius: 5 },
			  br: { radius: 5 },
			  antiAlias: true,
			  autoPad: true
			}
			
			$('.round_this').corner(settings);
		  }, 1500);
		  
		  
		}
		
		if(w > 1143 && w < 1301 && homePage!=true)
		{
		  /*if(typeof premiumCatId != 'undefined')
			$('#centro .resolucao_2').load('/premium/destaques_tipo1/r2/' + premiumCatId);
		  else
			$('#centro .resolucao_2').load('/premium/destaques_tipo1/r2');*/
		  
		  $("#centro .resolucao_2").css('display',(w > 1143 && w < 1301) ? "inline" : "none");
		  $("#centro").css('width', "781px");
		  $("#AdAddesc_tbl").css('width', "600px");
		  
		  $('.vA').VerticalAlign();
		  setTimeout(function() {
			
			settings = {
			  tl: { radius: 5 },
			  tr: { radius: 5 },
			  bl: { radius: 5 },
			  br: { radius: 5 },
			  antiAlias: true,
			  autoPad: true
			}
			
			$('.round_this').corner(settings);
		  }, 1500);
		  
		}
		
		if(w > 1301 && homePage!=true)
		{
		  
		  /*if(typeof premiumCatId != 'undefined')
			$('#centro .resolucao_3').load('/premium/destaques_tipo1/r3/' + premiumCatId);
		  else
			$('#centro .resolucao_3').load('/premium/destaques_tipo1/r3');*/
		  
		  $("#centro .resolucao_3").css('display',(w > 1301) ? "inline" : "none");
		  $("#centro").css('width', "940px");
		  $("#AdAddesc_tbl").css('width', "800px");
		  
		  $('.vA').VerticalAlign();
		  setTimeout(function() {
			
			settings = {
			  tl: { radius: 5 },
			  tr: { radius: 5 },
			  bl: { radius: 5 },
			  br: { radius: 5 },
			  antiAlias: true,
			  autoPad: true
			}
			
			$('.round_this').corner(settings);
		  }, 1500);
		  
		}
		
		
	}
	
  }
}

/*loading message */

CMSAPP.loadingMessage = {
  init: function () {
	$().ajaxStart(function() { $('#loading').show(); });
	$().ajaxStop(function() { $('#loading').hide();
		  $('.vA').VerticalAlign();
		  setTimeout(function() {
			
			settings = {
			  tl: { radius: 5 },
			  tr: { radius: 5 },
			  bl: { radius: 5 },
			  br: { radius: 5 },
			  antiAlias: true,
			  autoPad: true
			}
			
			$('.round_this').corner(settings);
		  }, 1500);
				 });
  }
}



/*** FIX IE7!!!!! ****/

/*CMSAPP.fixIE7 = {
  init: function () {  
	var zIndexNumber = 1000;
	$('div').each(function() {
		$(this).css('zIndex', zIndexNumber);
		zIndexNumber -= 10;
	});
	
	$('#debug-kit-toolbar').css('zIndex', 10000);
	
	
  }
}*/


/* Processa links com class .save */
CMSAPP.formSaveButtons = {
  init: function () {
	$('a.save').bind('click', function() {
	  	$(this).parents('form:first').submit();
	});
  }
}

/* Processa pesquisa */
CMSAPP.pesqSaveButtons = {
  init: function () {
	$('a.pesquisaTop').bind('click', function() {
	  if( $('#search_categorias').val().length != 0 )
	  	$(this).parents('form:first').submit();
	  else
		alert(selectCategoria);
	});
  }
}


CMSAPP.searchInputs = {
  init: function () {

	$('#termo_pesquisar').bind("click", function() {
		$('#termo_pesquisar').val('');
	});

  }
}

CMSAPP.notificationWindows = {
  init: function () {

	$(".close").click(
		function () {
			$(this).parent().fadeTo(400, 0, function () { // Links with the class "close" will close parent
				$(this).slideUp(600);
			});
			return false;
		}
	);

  }
}

/*** select chain ***/

CMSAPP.selectChain = {
  init: function () {

	var region = $('#AdRegionId');
	var city = $('#AdCityId');     

	 // note that we're assigning in reverse order
	 // to allow the chaining change trigger to work
	 region.selectChain({
		 target: city,
		 url: baseUrl + 'city/list_it',
		 data: { ajax: true }
	 }).trigger('click');
	 
	var category = $('#AdCategoryId');
	var subcategory = $('#AdSubcategoriesId');     

	 // note that we're assigning in reverse order
	 // to allow the chaining change trigger to work
	 category.selectChain({
		 target: subcategory,
		 url: baseUrl + 'subcat/list_it',
		 data: { ajax: true }
	 }).trigger('click');

  }
}

/*** input numbers only ***/

CMSAPP.numberOnly = {
  init: function () {
	$('input.numberOnly').bind('keypress', function(e) { 
	return ( e.which!=8 && e.which!=0 && (e.which<48 || e.which>57)) ? false : true ;
	})
  }
}

CMSAPP.historyback = {
  init: function () {
	$('a.historyback').bind("click", function() {
		history.back();
	});
  }
}

CMSAPP.print = {
  init: function () {
	$('a.print').bind("click", function() {
		javascript:window.print();
	});
  }
}

CMSAPP.moreSearched = {
  init: function () {
	$('a.mSearched').bind("click", function() {
	  var str = $(this).text();
	  $('#termo_pesquisar').val(str);
	  $('#termo_pesquisar').parents('form:first').submit();
	  //$('#searchForm').submit();
	});
  }
}

CMSAPP.clickOpen342 = {
  init: function () {
	$('.clickOpen').bind("click", function() {
	  var str = $(this).attr('alt');
	  $(this).attr('href',str);
	});
  }
}

CMSAPP.addAnuncio = {
  init: function () {
	$('#AdEmpresa1').bind("click", function() {
	  $('#AdNome').parent().hide('fast');
	  $('#AdEmpresaNome').parent().show('fast');
	  $('#AdNipc').parent().show('fast');
	  
	});
	$('#AdEmpresa0').bind("click", function() {
	  $('#AdEmpresaNome').parent().hide('fast');
	  $('#AdNipc').parent().hide('fast');
	  $('#AdNome').parent().show('fast');
	});
	var var_name = $('input:radio[name="data[Ad][sell]"]').val();
	if(!$("#AdEmpresa1").is(":checked")&&!$("#AdEmpresa0").is(":checked"))
	{
	  $('#AdEmpresaNome').parent().hide();
	  $('#AdNipc').parent().hide();
	  $('#AdNome').parent().hide();
	}
	if ($("#AdEmpresa1").is(":checked"))
	{
	  $('#AdNome').parent().hide();
	}
	if ($("#AdEmpresa0").is(":checked"))
	{
	  $('#AdEmpresaNome').parent().hide();
	  $('#AdNipc').parent().hide();
	}
	
  }
}

CMSAPP.clickOpen = {
  init: function () {
	$('a.pesqusaLeftSubmit').bind("click", function() {
	  var str = $(this).attr('alt');
	  $('#AdSubcategoriesId').val(str);
	  $('#AdSubcategoriesId').parents('form:first').submit();
	});
  }
}


$(document).ready(function () {
  CMSAPP.init();
});



//Recaptcha

 var RecaptchaOptions = {
    theme : 'clean'
 };
