var CMSAPP = {}

CMSAPP.init = function () {
  for (prop in this) {
	if (typeof this[prop].init == 'function') {
	  this[prop].init();
	}
  }
}


/*** stylish-select ***/

CMSAPP.myDropDown = {
  init: function () {  

	$('#my-dropdown3').sSelect({ddMaxHeight: '600px'}).change(function(){
	  $(this).parents('form:first').submit();
	  });
	
	$('#myDropdownSublinguas').sSelect({ddMaxHeight: '300px'}).change(function(){
	  $(this).parents('form:first').submit();
	  });
	
	$('#my-dropdown4').sSelect({ddMaxHeight: '300px'});
  }
}

/****
 *ashvin
 **/

CMSAPP.ashvin = {
  init: function () {  

	$('#ashvin').val(L33t);
	
  }
}




/*** lava lamp ***/

/*CMSAPP.lavaLamp = {
  init: function () {  
	$(".lavaLampWithImage").lavaLamp({
		fx: "backout",
		speed: 700,
		hover: function(event, menuItem) {
			return true;
		}
	});
  }
}*/


/*CMSAPP.subMenu = {
  init: function () {  
	$("#menu_container ul li").hover(function(){  
	$(this).find('ul:first').show(200);  
	}, function(){  
	  $(this).find('ul:first').hide(200);
	});	
  }
}*/

/*** FIX IE7!!!!! ****/

CMSAPP.fixIE7 = {
  init: function () {  
	var zIndexNumber = 1000;
	$('div').each(function() {
		$(this).css('zIndex', zIndexNumber);
		zIndexNumber -= 10;
	});
  }
}

/*** Fontsize  ****/

CMSAPP.myFontsize = {
  init: function () {
	
	var minSize = 10;
	var maxSize = 12;

	$("#a_menos").click(function(){
		$('#wrapper_container p').each(function() {
			var currentSize = parseInt($(this).css('font-size'));
			if(currentSize > minSize)
				$(this).css('font-size', currentSize - 1);
		});
		$('#wrapper_container h2').each(function() {
			var currentSize = parseInt($(this).css('font-size'));
			if(currentSize > minSize)
				$(this).css('font-size', currentSize - 1);
		});
		$('#wrapper_container th').each(function() {
			var currentSize = parseInt($(this).css('font-size'));
			if(currentSize > minSize)
				$(this).css('font-size', currentSize - 1);
		});
		$('#wrapper_container td').each(function() {
			var currentSize = parseInt($(this).css('font-size'));
			if(currentSize > minSize)
				$(this).css('font-size', currentSize - 1);
		});
		$('#wrapper_container li').each(function() {
			var currentSize = parseInt($(this).css('font-size'));
			if(currentSize > minSize)
				$(this).css('font-size', currentSize - 1);
		});
	});
	
	$("#a_mais").click(function(){
		$('#wrapper_container p').each(function() {
			var currentSize = parseInt($(this).css('font-size'));
			if(currentSize < maxSize)
				$(this).css('font-size', currentSize + 1);
		});
		$('#wrapper_container h2').each(function() {
			var currentSize = parseInt($(this).css('font-size'));
			if(currentSize < maxSize)
				$(this).css('font-size', currentSize + 1);
		});
		$('#wrapper_container th').each(function() {
			var currentSize = parseInt($(this).css('font-size'));
			if(currentSize < maxSize)
				$(this).css('font-size', currentSize + 1);
		});
		$('#wrapper_container td').each(function() {
			var currentSize = parseInt($(this).css('font-size'));
			if(currentSize < maxSize)
				$(this).css('font-size', currentSize + 1);
		});
		$('#wrapper_container li').each(function() {
			var currentSize = parseInt($(this).css('font-size'));
			if(currentSize < maxSize)
				$(this).css('font-size', currentSize + 1);
		});
	});

  }
}

CMSAPP.staffThumb = {
  init: function () {  

	$(".staff_box div.thumb").hover(function() {
		$(this).css({'z-index' : '10'}); /*Add a higher z-index value so this image stays on top*/ 
		$(this).find('img').addClass("hover").stop() /* Add class of "hover", then stop animation queue buildup*/
			.animate({
				marginTop: '-5px', /* The next 4 lines will vertically align this image */ 
				marginLeft: '-5px',
				width: '70px', /* Set new width */
				height: '103px', /* Set new height */
				padding: '5px'
			}, 200); /* this value of "200" is the speed of how fast/slow this hover animates */
	
		} , function() {
		$(this).css({'z-index' : '0'}); /* Set z-index back to 0 */
		$(this).find('img').removeClass("hover").stop()  /* Remove the "hover" class , then stop animation queue buildup*/
			.animate({
				marginTop: '0', /* Set alignment back to default */
				marginLeft: '0',
				width: '66px', /* Set width back to default */
				height: '99px', /* Set height back to default */
				padding: '3px'
			}, 400);
	});

  }
}

CMSAPP.animateCountrys = {
  init: function () {  

	/*$("#nationalPagesSelection li").hover(function() {
		$(this).css({'z-index' : '10'});
		$(this).find('img').addClass("hover").stop() 
			.animate({
				marginTop: '-5px', 
				marginLeft: '0'
			}, 200); 
	
		} , function() {
		$(this).css({'z-index' : '0'}); 
		$(this).find('img').removeClass("hover").stop()  
			.animate({
				marginTop: '0', 
				marginLeft: '0'
			}, 400);
	});*/

  }
}




/*** marquee ***/

CMSAPP.marquee = {
  init: function () {  
	  $("#marquee1").marquee({yScroll: "left",
							 fxEasingShow: "swing",
							 showSpeed: 300,
							 pauseSpeed: 400,
							 scrollSpeed: 35,
							 fxEasingScroll: "linear"});

  }
}

/***** tabs ******/

CMSAPP.tabs = {
  init: function () {  

	setTimeout( function() { 
		$('.content-box .content-box-content div.tab-content').hide(); // Hide the content divs
		$('ul.content-box-tabs li a.default-tab').addClass('current'); // Add the class "current" to the default tab
		$('.content-box-content div.default-tab').show(); // Show the div with class "default-tab"		   
	}, 1000 );


	
	$('.content-box ul.content-box-tabs li a').click( // When a tab is clicked...
		function() { 
			$(this).parent().siblings().find("a").removeClass('current'); // Remove "current" class from all tabs
			$(this).addClass('current'); // Add class "current" to clicked tab
			var currentTab = $(this).attr('href'); // Set variable "currentTab" to the value of href of clicked tab
			$(currentTab).siblings().hide(); // Hide all content divs
			$(currentTab).show(); // Show the content div with the id equal to the id of clicked tab
			return false; 
		}
	);
  }
}

/*** corner ***/

CMSAPP.corner = {
  init: function () {
	
	$('.redondo').corner();
  }
}


/**** vertical align bind ***/

CMSAPP.myVerticalAlign = {
  init: function () {
	
	$('.vA').VerticalAlign();
  }
}

/**** load news homepage ***/

CMSAPP.homeNews = {
  init: function () {  
	/*if(isHome == true && siteRoot != 'http://pt.nbbrokers.com/por/')
	{
		$.ajax({
			url: siteRoot + 'home/home_news/3',
			cache: false,
			dataType: "html",
			success: function(data) {
				$("#newsData").html(data);
				$("#newsData").slideDown('slow');
			}
		});
	}
	if(isHome == true && siteRoot == 'http://pt.nbbrokers.com/por/')
	{
		$.ajax({
			url: siteRoot + 'home/home_news/2',
			cache: false,
			dataType: "html",
			success: function(data) {
				$("#newsData").html(data);
				$("#newsData").slideDown('slow');
			}
		});
	}*/
	if(isHome == true)
	{
		$.ajax({
			url: siteRoot + 'home/home_news/3',
			cache: false,
			dataType: "html",
			success: function(data) {
				$("#newsData").html(data);
				$("#newsData").slideDown('slow');
			}
		});
	}
	if(isNationalPage == true)
	{
		$.ajax({
			url: siteRoot + 'home/home_news/5',
			cache: false,
			dataType: "html",
			success: function(data) {
				$("#newsData").html(data);
				$("#newsData").slideDown('slow');
			}
		});
	}
  }
}

CMSAPP.homeAnim = {
  init: function () {  
	if(isHome == true)
	{

		//Custom settings
		var style_in = 'easeOutBounce';
		var style_out = 'jswing';
		var speed_in = 800;
		var speed_out = 300;	
	
		//Top and bottom
		var	top = $('.qitem').height() * (-1); 
		var	bottom = $('.qitem').height(); 
	
		$('.qitem').each(function () {
	
			//retrieve all the details of the image before removing it
			url = $(this).find('a').attr('href');
			img = $(this).find('img.slicer').attr('src');
			alt = $(this).find('img.slicer').attr('img');
			width = $(this).width() / 4; 
			height = $(this).height(); 
			
			//remove the image and append 4 div into it
			$('img.slicer', this).remove();
			$(this).append('<div class="bar1"></div><div class="bar2"></div><div class="bar3"></div><div class="bar4"></div>');
			
			//set the image as background image to all the bars
			$(this).children('div').css('background-image','url('+ img + ')');
	
			//Divide the image into 4 bars and rebuild the image
			$(this).find('div.bar1').css({top:0, left:0, width:width, height:height, backgroundPosition:'0 0' });	
			$(this).find('div.bar2').css({top:0, left:width, width:width, height:height, backgroundPosition:(width*-1) + 'px 0' });	
			$(this).find('div.bar3').css({bottom:0, left:width*2, width:width, height:height, backgroundPosition:(width*-2) + 'px 0' });	
			$(this).find('div.bar4').css({bottom:0, left:width*3, width:width , height:height, backgroundPosition:(width*-3) + 'px 0' });
	
		}).hover(function () {
			
			//Move those bar, 1st and 3rd move upward, 2nd and 4th move downward
			$(this).find('div.bar1').stop(false, true).animate({top:top - 1}, {duration:speed_out, easing:style_out});	
			$(this).find('div.bar2').stop(false, true).animate({top:bottom + 1}, {duration:speed_out, easing:style_out});	
			$(this).find('div.bar3').stop(false, true).animate({top:top - 1}, {duration:speed_out, easing:style_out});	
			$(this).find('div.bar4').stop(false, true).animate({top:bottom + 1}, {duration:speed_out, easing:style_out});	
					
		},
		
		function () {
	
			//Back to default position
			$(this).find('div.bar1').stop(false, true).animate({top:0}, {duration:speed_in, easing:style_in});	
			$(this).find('div.bar2').stop(false, true).animate({top:0}, {duration:speed_in, easing:style_in});	
			$(this).find('div.bar3').stop(false, true).animate({top:0}, {duration:speed_in, easing:style_in});	
			$(this).find('div.bar4').stop(false, true).animate({top:0}, {duration:speed_in, easing:style_in});	
		
		}).click (function () {
			
			//make the whole box clickable
			//window.location = $(this).find('a').attr('href');	
		});

	}
  }
}

/*** ***/



/**** ajax oportunidades - pag principal ***/

CMSAPP.myOportunidades = {
  init: function () {
	
	if(isBuySell == true)
	{
	  /* national / international / all */
	  
	  $('#national_businesses').click(function(event) {
		$("#content_sale").load(siteRoot + 'home/sale',{showtype:"national"});
		$("#content_wanted").load(siteRoot + 'home/wanted',{showtype:"national"});
	  });
	  
	  $('#international_businesses').click(function(event) {
		$("#content_sale").load(siteRoot + 'home/sale',{showtype:"international"});
		$("#content_wanted").load(siteRoot + 'home/wanted',{showtype:"international"});
	  });
	  
	  $('#all_businesses').click(function(event) {
		$("#content_sale").load(siteRoot + 'home/sale',{showtype:"all"});
		$("#content_wanted").load(siteRoot + 'home/wanted',{showtype:"all"});
	  });
	
	/** site 1st load **/
	
	  if(showNacionais == true)
	  {
		var showType = 'national';
		$("#barra_tipo_negocios #national_businesses").attr("checked", "checked");
	  }
	  else
	  {
		var showType = 'international';
		$("#barra_tipo_negocios #international_businesses").attr("checked", "checked");
	  }
	  
		$("#content_sale").load(siteRoot + 'home/sale',{showtype: showType}, function() {
			$("#content_sale").slideDown('slow');
		});
		$("#content_wanted").load(siteRoot + 'home/wanted',{showtype: showType}, function() {
			$("#content_wanted").slideDown('slow');
		});
		
	  /*$("#content_sale").load(siteRoot + 'home/sale',{showtype: showType});
	  $("#content_wanted").load(siteRoot + 'home/wanted',{showtype: showType});*/
	  
	  $('#content_sale a[href*=/sort:], #content_sale a[href*=/page:]').livequery('click', function(){
		  $('#content_sale').load($(this).attr('href'));
		  return false;
	  });
	  
	  $('#content_wanted a[href*=/sort:], #content_wanted a[href*=/page:]').livequery('click', function(){
		  $('#content_wanted').load($(this).attr('href'));
		  return false;
	  });
	  
	  $('a#search_destaques').click(function(event) {
		  $("#barra_tipo_negocios input").removeAttr("checked");
		  var my_dropdown4 = $('#my-dropdown4').attr("value");
		  var oportunidadesQuery = $('#oportunidadesQuery').attr("value");
		  $("#content_sale").load(siteRoot + 'home/sale',{country:my_dropdown4, search_query:oportunidadesQuery});
		  $("#content_wanted").load(siteRoot + 'home/wanted',{country:my_dropdown4, search_query:oportunidadesQuery});
	  });
	}
	
  }
}

/* Processa links com class .save */
CMSAPP.formSaveButtons = {
  init: function () {
	$('a.save').bind('click', function() {
	  	$(this).parents('form:first').submit();
	});
  }
}

/*loading message */

CMSAPP.loadingMessage = {
  init: function () {
	$().ajaxStart(function() { $('#loading').show(); });
	$().ajaxStop(function() { $('#loading').hide(); });
  }
}

/*notifications messages */

CMSAPP.closeNotification = {
  init: function () {
	
	$('div.close-notification').bind('click', function() {
		$(this).fadeTo(400, 0, function () {
			$(this).slideUp(400);
		});
	});
	
  }
}

CMSAPP.nationalPagesArchive = {
  init: function () {  
	$("#nationalPagesArchive ul li.year").click(function(){
		$(this).addClass("selected");
		$(this).find('ul:first').show(200);
	});	
  }
}

CMSAPP.selectCountrysOffice = {
  init: function () {
	
	$("#selectCountrysOffice").click(function() {
		$('#displayCountrysList').show('fast');
	  });
	
	$("#closeME2").click(function() {
		$('#displayCountrysList').hide('fast');
	  });
  }
}

CMSAPP.partnersAutocomplete = {
  init: function () {
	
	$("#autoComplete").autocomplete(siteRoot + 'partners/search',
	{
		minChars: 2,
		cacheLength: 800,
		onItemSelect: selectItem,
		onFindValue: findValue,
		formatItem: formatItem,
		autoFill: false
	});
  }
}


CMSAPP.uiTabs = {
  init: function () {
	$( "#tabs" ).tabs();
  }
}


$(document).ready(function () {
  CMSAPP.init();
});


function selectItem(li) {
	findValue(li);
}

function findValue(li) {
	if( li == null ) return alert("No match!");

	// if coming from an AJAX call, let's use the product id as the value
	if( !!li.extra ) var sValue = li.extra[0];

	// otherwise, let's just display the value in the text box
	else var sValue = li.selectValue;
	
	window.location = partnerUrl + sValue;

	//alert("The value you selected was: " + partnerUrl + sValue);
}

function formatItem(row) {
	if(row[1] == undefined) {
		return row[0];
	}
	else {
		return row[0];
		//return row[0] + " (id: " + row[1] + ")";
	}
}
