var cur_bg = 0;
var err_timeout;
var prt_interval;

$(document).ready(function() {
	//-- Right side links
	$('#right div.block p a:first').click(function() {
		toggleContent();
	});
	$('#right div.block p a:eq(1)').click(function() {
		toggleBG('next');
	});
	$('#right div.block p a:eq(2)').click(function() {
		toggleBG('previous');
	});
	$('#right div.block p a:last').click(function() {
		toggleBG('random');
	});
	//-- Menu links hover function
	$('#header #menu ul li a')
		.css( {backgroundPosition: "left 35px"} )
		.mouseover(function(){
			$(this).stop().animate({backgroundPosition:"(left 94px)"}, {duration:500})
		})
		.mouseout(function(){
			$(this).stop().animate({backgroundPosition:"(right 35px)"}, {duration:300, complete:function(){
				$(this).css({backgroundPosition: "left 35px"})
			}})
		})
	//-- Assign target to appropriate links
	$("a[rel*='license']").click(function() {
		this.target = "_blank";
	});
	$("a[rel*='download']").click(function() {
                this.target = "_blank";
        });
	$("a[rel*='external']").click(function() {
		this.target = "_blank";
	});
	//-- Assign javascript:void(0); to links with # as href
	$("a[href='#']").attr("href", "javascript:void(0);");
	//-- Start Portfolio, change each 5 seconds
	prt_interval = setInterval( "togglePortfolio()", 5000 );
	//-- Show Errors, make them disappear after 5 seconds or on click.
	$('#errors').click(function() {
		closeErrors();
	});
	toggleErrors();
	err_timeout = setTimeout( "toggleErrors()", 5000 );
	if ($.cookie("bg") >= 0 && $.cookie("bg") <= 17) cur_bg = $.cookie("bg");
	$.cookie("bg", cur_bg, { path: '/', expires: 7 });
});

function toggleContent() {
	$('#content').toggle('fast');
	$('#footer').toggle('fast');
	if ($('#right div.block p a:first').text() == js_lang['hide_content']) {
		$('#right div.block p a:first').text(js_lang['show_content']);
	} else {
		$('#right div.block p a:first').text(js_lang['hide_content']);
	}
}

function toggleBG(option) {
	if (!option) option = 'next';

	$('#loading').fadeIn('fast');

	switch (option) {
		case 'next' :
			cur_bg++;
			if (cur_bg > 17) cur_bg = 0;
		break;
		case 'previous' :
			cur_bg--;
			if (cur_bg < 0) cur_bg = 17;
		break;
		case 'random' :
			tmp_v = cur_bg;
			while (cur_bg == tmp_v) {
				cur_bg = rand(0,17);
			}
		break;
	}

	$.cookie("bg", cur_bg, { path: '/', expires: 7 });

	img_path = '/images/bg/' + cur_bg + '.jpg';

	var tempImage = new Image();

	jQuery(tempImage).load( function(){
		$('#wrapper').css({'background-image':'url(' + img_path + ')'});
		$('#loading').fadeOut('fast');
	});

	tempImage.src = img_path;
}

function togglePortfolio() {
	var $active = $('#portfolio a.active');

	if ( $active.length == 0 ) $active = $('#portfolio a:last');

	var $next =  $active.next().length ? $active.next()
		: $('#portfolio a:first');

	$active.addClass('last-active');

	$next.css({opacity: 0.0})
		.addClass('active')
		.animate({opacity: 1.0}, 1000, function() {
			$active.removeClass('active last-active');
		});
}

function toggleErrors() {
	$('#errors').slideToggle('slow');
}

function closeErrors() {
	$('#errors').slideToggle('fast');
	clearTimeout(err_timeout);
}

function preloadImgs() {
	for(var i = 0; i<arguments.length; i++) {
		var tempImage = new Image();
		tempImage.src = arguments[i];
	}
}

function rand(min,max) {
	return Math.floor((Math.random() * (max-min+1))+min);
}

