/*
js effektit
*/
$(document).ready(function(){
  var txt = "";
  /* nappeja */
  
  //alareunan haku
  $('#footer #site-search-submit').click(function() {
	$('#footer #site-search form').submit();
  });
  
  //alareunan lähin tarmo
  $('#tarmo-search-submit').click(function() {
	var address = $('#tarmo-search #address').val();
	var city = $('#tarmo-search #postal').val();
	var locale =  $('#locale').text();
	window.location = '/lahin_tarmosi/'+locale+'/tarmo_kartalla/?adr='+address+','+city;
  });
  
  
  /* logosta etusivulle */
  $('a#go-home').click(function(){
	window.location = '/';
  });
  
  //kauppias-sivujen login
  $('#intra-login-submit').click(function() {
	$('#intra-login').submit();
  });
   /* lähin tarmo -search */
  $('#site-search a.submit').click(function(){
	$(this).parent('form').submit();
  });
  $('#scroll input.custom').click(function(){
	txt = $(this).val();
	$(this).val('');
  });
  $('#scroll input.custom').focusout(function(){
	if($(this).val() == '')
	{
	  $(this).css('color','#ffffff');
	  $(this).val(txt);
	  $(this).animate({ color: "#999999" }, 500);
	}
  });
  
  /* login auki/kiinni */
  $('a#login-kauppiassivut').toggle(
	function() {
	  $('#login-box').slideDown('fast');
	  $(this).addClass('active');
	},
	function() {
	  $('#login-box').slideUp('fast');
	  $(this).removeClass('active');
	}
  );
  
  /* onko uutislista sidebarissa vai ei */
  $('#scroll div.newslist_wrapper').each(function() {
	if(!$(this).parents().hasClass('sidebar'))
	{
	  $('table.newslist',$(this)).attr('class','newsInContent');
	}
  });
  
});

/* before ready */
/* tyhjät slotit */
function hideEmptySlots() {
  $('#scroll .hide-if-empty').each(function(i){
	  /* jos vain kuva tai flash, ei tyhjä */
	  var hasImg = false;
	  if($(this).find('img').attr('src')) {
		hasImg = true;
	  }
	  if($(this).find('object').length>0) {
		hasImg = true;
	  }

	  if(($(this).is(':empty') || jQuery.trim($(this).text()) == '') && !hasImg)
	  {
		  $(this).hide();
	  }
	  
  });
  $('div[title]').removeAttr('title');
}

/* tyhjät slotit */
function makeLovely(pagex) {
  var emt;
  switch(pagex) {
	/*
	  Etusivulla poistetaan tyhjistä listoista otsikot
	*/
	case 'home-lists':
	  $('#scroll div.navigo-list').each(function() {
		if($(this).find('div').is(':empty') || jQuery.trim($(this).find('div').text()) == ''){
		  $(this).hide();
		}
	  });
	break;
	/*
	 Pysyvästi-edullista / tarjous sivulla haetaan otsikkokuvan attribuuteista
	 sivun otsikkoteksti sekä aliotsikko.
	*/
	case 'pysyvasti-edullista':
	  var groupCount = 0;
	  $('div.tarjous-group').each(function(i){
		emt = $(this);
		var img = $('.header-image img',emt);
		
		//jossei kuvaa, skipataan
		if(img.length>0) {
		  var headerHolder = $('div.u4-image-caption',emt);
		  var header = headerHolder.text()
		  var subheader = img.attr('alt');
		  var textHolder = $('<div class="titleHolder"></div>');
		  if(header.length>0) {
			textHolder.append('<h1 class="cufon">'+header+'</h1>');
			headerHolder.remove();
		  }
		  if(subheader.length>0) {
			textHolder.append('<h5>'+subheader+'</h5>');
		  }
		  emt.prepend(textHolder);
		  
		  /* kuva divin bg:ksi */
		  var divHeight = parseFloat(img.height())+6;
		  emt.find('div.header-image').css('background','transparent url('+img.attr('src')+') no-repeat right center');
		  emt.find('div.header-image').css('height',divHeight+'px');
		  emt.find('.header-image div').remove();
		  
		  /* tuotetaulu */
		  $('.content-body table tr',emt).each(function() {
			
			/* borderit hiiteen */
			$('td:last',$(this)).addClass('no-b-r');
			
			/* navigo-siivousta */
			$('td',$(this)).each(function() {
			  var img = $(this).find('img');
			  $(this).html(img);
			});
		  });
		  groupCount = i;
		}
		else {
		  $(this).hide();
		}
	  });
	  
	  $('#pysyvasti-edullista #products .product').each(function(index) {
		if(index % 5 === 4) {
		  $(this).addClass('no-b-r');
		}
		if($.trim($('.product-text1',$(this)).text()) == '') {
		  $('.product-text1',$(this)).remove();
		}
	  });
	break;
	
  }
  
  
 
}


/* funktiot */
function enchanceSlots() {
  /* buff - slotinfo */
  $('#scroll div.edit-mode').tooltip({ effect: "fade",relative:true});
  
  $('#scroll div.edit-mode').each(function(i){
	  /* fix - navigo slotspacing ie8 */
	  $(this).children('div').children('img').hide();
  
	  /*
	  $(this).children('div').children('div').first('div').hover(
		  function(){
			  $(this).css('background-color','#ffffff');
		  },
		  function() {
			  $(this).css('background-color','#e1e1e1');
		  }
	  );
	  */
  });
  return true;
}
