jQuery.preloadImages = function() {
	for(var i = 0; i<arguments.length; i++) {
		jQuery("<img>").attr("src", arguments[i]);
	}
}

$.preloadImages('../images/logo_over.gif');
$.preloadImages('../images/trommel_animiert.gif');

$(window).bind("resize", function(){
	w = $(window).width();
	h = $(window).height();
	if (w < 910 || h < 540) {
		showResBox();
	} else {
		hideResBox();
	}
});

$(document).ready(function() {
	
	// Site resolution
	var w = $(window).width();
	var h = $(window).height();
	
	if (w < 910 || h < 540) {
		showResBox();
	} else {
		hideResBox();
	}
	
	
	// Subnav effekt
	
	if ($('ul.subnav') && $('ul.subnav').hasClass('start') == false) {
		
		$('ul.subnav').hide().slideDown(1200);
		
		//$('ul.subnav').css('top','-25px').animate({top:'0px'}, 1000);
		
		//$('ul.subnav').css("left","-"+$('ul.subnav').innerWidth()+"px");
		//$('ul.subnav').animate({left:'41px'}, 1500);
	}
	
	
	
	// Background change
	/*
	var randnumber = Math.round(10*Math.random());
	var bg_pic = "";
	
	     if (randnumber >= 0 && randnumber < 2)  bg_pic = "bg_browser.gif";
	else if (randnumber >= 2 && randnumber < 5)  bg_pic = "bg_browser_2.gif";
	else if (randnumber >= 5 && randnumber < 8)  bg_pic = "bg_browser_3.gif";
	else bg_pic = "bg_browser_4.gif";
	
	$('body').css('background-image','url(images/'+bg_pic+')')
	*/
	
	
	// Logo MO
	$('img.logo_left').hover(function() {
		$(this).attr('src','images/logo_over.gif');
	}, function() {
		$(this).attr('src','images/logo.gif');
	});
	
	// Startseite Trommel
	if ($('#trommel_animiert')) {
		$('#trommel_animiert').hover(function(){
			$(this).addClass('trommel');
		}, function() {
			$(this).removeClass('trommel');
		})
	}
	
	// Portfolio images
	if ( $('.right_wrapper').hasClass('portfolio') ) {
		
		$('.portfolio img.portfolio').css('cursor','pointer');
		
		$('.portfolio img.portfolio').hover(function() {
			$('#portfolio_text').html($(this).attr('text'));
			var img_src_orig = $(this).attr('src');
			var img_src_neu  = img_src_orig.replace(/.jpg/,"_over.jpg");
			$(this).attr('src',img_src_neu);
		}, function() {
			$('#portfolio_text').html('');
			var img_src_orig = $(this).attr('src');
			var img_src_neu  = img_src_orig.replace(/_over.jpg/,'.jpg');
			$(this).attr('src',img_src_neu);
		});
		
		$('.right_wrapper').find('img').each(function(el) {
			var img_src_orig = $(this).attr('src');
			var img_src_neu  = img_src_orig.replace(/.jpg/,"_over.jpg");
			setTimeout(function() { $.preloadImages('../'+img_src_neu) }, 500);
		});
		
		$('.portfolio img.portfolio').click(function(){
			var link_to = $(this).attr('src');
			link_to = link_to.replace(/.jpg/,"");
			link_to = link_to.replace(/_over/,"");
			link_to = link_to.split("/");
			link_to = link_to[link_to.length-1];
			window.location.href='portfolio.detail.php?pid='+link_to;
		});
		
	}
	
	// Clients box handling
	$('ul.clients').find('li a').click(function(el) {
		$('ul.clients').find('li span').removeClass('active');
		$(this).parent().addClass('active');
		$(this).blur();
	});
	
	
	
});



// CLIENTS BOXEN

var was_visible = false;

function showbox(cid) {
	
	var speed = 500;
	
	// Get infos
	var el = $('#'+cid);
	
	var info = el.find('div.info').html();
	var statement = el.find('div.statement').html();
	var portfolio = el.find('div.portfolio').html();
	
	var box = $('#box');
	
	
	// Hide box if shown
	if (box.css("right") == "0px") {
		hidebox(speed);
		was_visible = true;
	}
	
	//if (was_visible == true) speed = 0;
	
	window.setTimeout(function(){
		// Reset box
		box.find('div.text').html();
		box.find('div.text_statement').html();
		
		// Set info
		$('#box .text').html(info);
		// Set statement
		$('#box .text_statement').html(statement);
		
		
		// Show statement
		if (statement != "") {
			var statement_link = box.find('a.statement');
			statement_link.show();
			statement_link.click(function(){ showstatement(); });
		}
		
		// Show portfolio
		if (portfolio != "") {
			var portfolio_link = box.find('a.portfolio');
			portfolio_link.show();
			portfolio_link.click(function(){ location.href="portfolio.detail.php?pid="+portfolio; });
			
		}
		
		
		box.animate( { right:"0px" }, speed );
		
   }, speed);
}

function hidebox() {
	var speed = 500;
	$('ul.clients').find('li span').removeClass('active');
	var box = $('#box');
	box.animate( { right:"-255px" }, speed );
	box.find('a.statement').hide();
	box.find('a.info').hide();
	box.find('.text_statement').hide();
	box.find('.text').show();
	was_visible = false;
}

function showstatement() {
	var speed = 700;
	$('#box .text').slideUp(speed);
	setTimeout(function(){ $('#box .text_statement').slideDown(speed); }, speed);
	$('#box a.statement').hide();
	$('#box a.info').show().click(function(){ showinfo(); });
}

function showinfo() {
	var speed = 700;
	$('#box .text_statement').slideUp(speed);
	setTimeout(function(){ $('#box .text').slideDown(speed); }, speed);
	$('#box a.info').hide();
	$('#box a.statement').show().click(function(){ showstatement(); });
}


// Resolution
function showResBox() {
	$(".overall_wrapper").hide();
	$("#res").show();
}
function hideResBox() {
	$("#res").hide();
	$(".overall_wrapper").show();
}



// OVERLAYS

function openOverlay(page) {
	
	$('#overlay').show();
	$('#overlay_'+page).show();
	
}

function closeOverlay(page) {
	
	$('#overlay').hide();
	$('#overlay_'+page).hide();
	
}


