$(window).load(function() {
	
	// add a last class to the 3rd item from each row
	$("#latest_work li:nth-child(3n), #portfolio_items li:nth-child(3n)").addClass("last");
	
	var lis = $("#latest_work li");
	$("#latest_work li").hover(
	  function () {
		$(this).find('.description-overlay').animate({top:'20px'}, 300, 'easeOutExpo');
	  },
	  function () {
		$(this).find('.description-overlay').animate({top:'140px'}, 200, 'easeOutExpo');
	  }
	);
	
	// portfolio item animation
	var portfolio_items = $("#portfolio_items .tab_content li");
	var portfolio_items_height = $("#portfolio_items .tab_content li").css('height');

	portfolio_items.each(function(el){
		$(this).find('.description-overlay').css({'height':portfolio_items_height,'top':portfolio_items_height});
	})
	
	portfolio_items.hover(
	  function () {
		$(this).find('.description-overlay').animate({top:0}, 300, 'easeOutExpo');
	  },
	  function () {
		$(this).find('.description-overlay').animate({top: portfolio_items_height}, 200, 'easeOutExpo');
	  }
	);
	
	// add a plus to the services & clients lists
	$('#mainbottom li, #sidebar ul.menu li').prepend("<span>+</span>");
	
	// display random testimonial
	var list = $(".testimonials ul li").toArray();
	var elemlength = list.length; 
	var randomnum = Math.floor(Math.random()*elemlength);
	var randomitem = list[randomnum];
	$(randomitem).css("display", "block");

	//initialize the menu
//	$(function($){ $("ul.sf-menu").supersubs({minWidth:13, maxWidth:30, extraWidth:0}).superfish({hoverClass:'sfHover', pathClass:'sf-active', pathLevels:0, delay:500, animation:{height:'show'}, speed:'def', autoArrows:1, dropShadows:0}) });


	
	// init infield labels
	$("#newsletter-form label, .searchbox label, #commentform label, #contact-form label ").inFieldLabels();
	
	// FAQ Code
	$('#faqs h3').each(function() {
		var tis = $(this), state = false, answer = tis.next('div').hide().css('height','auto').slideUp();
		tis.prepend("<span>+</span> ")
		tis.click(function() {
		  state = !state;
		  answer.slideToggle(state);
		  tis.toggleClass('active',state);
		  if(tis.hasClass('active')) {
			  tis.find('span').text('→');
		  } else {
			  tis.find('span').text('+');
		  }
		});
	}); // end each faqs
	
	
        /**
	//twitter
	$("#twitter").getTwitter({
		// change here the Twitter username
		userName: "envato",
		numTweets: 2,
		loaderText: "Loading tweets...",
		slideIn: false,
		showHeading: true,
		headingText: "Latest Tweets",
		showProfileLink: true
	});
        **/
});

var documentOverlay = {
  show: function () {
    
      var color = '#777777';
//      var opacity = 0.5;
      var headerHeight = 65;
      // ---------------------
      // ---------------------
      var o = document.getElementById('doc_overlay');
      if (!o) {

          o = document.createElement('div');
          o.id = "doc_overlay";
          documentOverlay.style(o, {
              height: ($(document).height() - headerHeight) + 'px',
              background: color
			  
          });
          document.getElementsByTagName('body')[0].appendChild(o);
      } else {
          documentOverlay.style(o, {
              background: color || '#000',
              height: $(document).height() - headerHeight + 'px',
              display: 'block'
			 
          });
      }
  },
  hide: function () {
      var o = document.getElementById('doc_overlay');
      o.style.display = 'none';
  },
  style: function (obj, s) {
      for (var i in s) {
          obj.style[i] = s[i];
      }
  }
};

$(document).ready(function() {
  
  // dropdown menu effect
  $('.header-menu > li').hover(
    function() {
      $(' > a', this).addClass('hover');
      
      if($(this).has('.dropdown-menu').length) {
        documentOverlay.show();
        $('.dropdown-menu', this).show();
      }
      
    },
    function() {
      $('.dropdown-menu', this).hide();
      $(' > a', this).removeClass('hover');
      documentOverlay.hide();
  });
  
  // tabs
  $('#tabs .tab-headers a').click(function() {
    $('#tabs .active').removeClass('active');
    $(this).parent().addClass('active');
    $('#tabs .tab').hide();
    $($(this).attr('href')).show();
    return false;
  });
  $('#tabs .more').click(function() {
    var next = $(this).closest('.tab').next();
    if(next.length) {
      var id = next.attr('id');
      $('#tabs .tab-headers a[href=#' + id + ']').click();
    }
    else {
      // uncomment if need to return to first tab
//      $('#tabs .tab-headers a[href=#tab1]').click();
    }
    return false;
  });



  // ingredient tabs
  $('#itabs .itab-headers a').click(function() {
    $('#itabs .shown').removeClass('shown');
    $(this).parent().addClass('shown');
    $('#itabs .itab').hide();
    $($(this).attr('href')).show();
    return false;
  });
  

});


//combobox events
function changeLocale(newLocale) {
  // 'locale' request parameter is changed to newLocale, or set
  var newLocation;
  var locationArray = location.href.split('?');
  if(locationArray.length == 1) {
    newLocation = location.href + '?locale=' + newLocale;
  }
  else {
    var params = locationArray[1].split('&')
    for(var i=0; i < params.length; i++) {
      var index = params[i].indexOf('locale=');
      if(index == 0) {
        params[i] = 'locale=' + newLocale;
        newLocation = locationArray[0] + '?' + params.join('&');
        break;
      }
      else {
        newLocation = location.href + '&locale=' + newLocale;
      }
    }
  }
  location.href = newLocation;
}

function changeCountry(newCountry) {
  var newLocale = $('#lang .value span').text() + '_' + newCountry;
  changeLocale(newLocale);
}

function changeLang(newLang) {
  var newLocale = newLang + '_' + $('#country .value span').text();
  changeLocale(newLocale);
}

function getLocale() {
  var locale = $.cookie("locale")? $.cookie("locale") : 'en_US';
  var locationArray = location.href.split('?');
  if(locationArray.length != 1) {
    var params = locationArray[1].split('&')
    for(var i=0; i < params.length; i++) {
      var index = params[i].indexOf('locale=');
      if(index == 0) {
        var paramArray = params[i].split('=');
        locale = paramArray[1];
        break;
      }
    }
  }
  return locale;
}

function getLang() {
  var locale = getLocale();
  var array = locale.split('_');
  var lang = array[0];
  return lang;
}

function getCountry() {
  var locale = getLocale();
  var array = locale.split('_');
  var country = array[1];
  return country;
}
