$(function () {
	// header inputs values
	var dvalues = {};
	$('.header input').each(function () {
		dvalues[$(this).attr('name')] = $(this).val();
	});

	$('.header input').focus(function () {
		// if the current value is the default one, remove it.
		var dvalue = dvalues[$(this).attr('name')];
		var cvalue = $(this).val();
		if (dvalue == cvalue) {
			$(this).val('');
		}
	}).blur(function () {
		// if the current value is empty, restore the default one.
		var dvalue = dvalues[$(this).attr('name')];
		var cvalue = $(this).val();
		if (cvalue == '') {
			$(this).val(dvalue);
		}
	});
	
	// header fake submit buttons
	$('.header .tnav form .click').click(function () {
		$(this).parents('form').submit();
	});

	// open external link in a new window
	$('a').live('click', function (e) {
		e.preventDefault();
		if ($(this).attr('rel') == 'portfolio') { 
			return false; 
		} else if ($(this).hasClass('langswitch') && $('a.alt-lang').size() == 1) {
			location.href = $('a.alt-lang').attr('href');
		} else if ($(this).hasClass('_nw')) {
			window.open($(this).attr('href'));
		} else if ($(this).attr('href').match(/^http:\/\/(www\.)?cinemaexcentris\.com/i)) {
			location.href = $(this).attr('href');
		} else if ($(this).attr('href').match(/^http/i)) {
			if (typeof _gaq != 'undefined') _gaq.push(['_trackEvent', 'Offsite Links', 'Click', $(this).attr('href')]);
			window.open($(this).attr('href'));
		} else {
			location.href = $(this).attr('href');
		}
	});

	// footer position 
	adjustfooter();
	$(window).resize(function () { adjustfooter(); });
	function adjustfooter () {
		var headerHeight = $('.header').height();
		var footerHeight = $('.footer').height();
		var contentHeight = $('.content').height();
		var windowHeight = $(window).height();
		
		if ((headerHeight + footerHeight + contentHeight) > windowHeight) {
			$('.footer').removeClass('forcebottom');
		} else {
			$('.footer').addClass('forcebottom');
		}
	}
});

$(document).ready(function(evt){
	var root = $('#socials'), items = $('.item', root), current = 0, process = false, left = parseInt(root.width());
	
	$('li:last', root).bind('click', function(evt){
		if(!process)
		{
			process = true;
			var index = (current == 0)? items.length-1 : current-1;
			var c = $(items[current]), t = $(items[index]);
			t.css({left: left});
			c.stop(true, true).animate({left: -1*left});
			t.stop(true, true).animate({left: 0}, {complete: function(){
				current = index;
				process = false;
			}});
		}
		return false;
	});
	
	$('li:first', root).bind('click', function(evt){
		if(!process)
		{
			process = true;
			var index = (isLast(current))? 0 : current+1;
			var c = $(items[current]), t = $(items[index]);
			t.css({left: -1*left});
			c.stop(true, true).animate({left: left});
			t.stop(true, true).animate({left: 0}, {complete: function(){
				current = index;
				process = false;
			}});
		}
		return false;
	});
	
	var isLast = function(index)
	{
		var r = true, len = items.length;
		if(index < len-1)
		{
			r = false;
		}
		
		return r;
	};
});

