// JavaScript Document

function get_picture(item_id, nr, maxord){
	$.post('php/get_image.php', {'iid':item_id, 'nr':nr}, function(data){
		idata=data.split('|||');
		$('#nav_'+item_id+' .current_pic').html(nr);
		if(nr>1){prv=nr-1;}else{prv=maxord;}
		if(nr<maxord){nxt=nr+1;}else{nxt=1;}
		$('#next_'+item_id).attr('href', 'javascript:get_picture('+item_id+','+nxt+','+maxord+')'); 
		$('#prev_'+item_id).attr('href', 'javascript:get_picture('+item_id+','+prv+','+maxord+')');
		$('#image_'+item_id+'_'+idata[0]).hide();
		$('#image_'+item_id+'_'+idata[0]).topZIndex();
		$('#image_'+item_id+'_'+idata[0]).fadeIn();
		$('#text_'+item_id+' p').html(idata[1]);
		 externalLinks();
	});	
}

function externalLinks() {
if (!document.getElementsByTagName) return;
var anchors = document.getElementsByTagName("a");
for (var i=0; i<anchors.length; i++) {
var anchor = anchors[i];
if (anchor.getAttribute("href") &&
anchor.getAttribute("rel") == "external")
anchor.target = "_blank";
}
}

function MO_imgChange(img){
	img.mouseover(function(){$(this).attr("src", $(this).attr("src").replace("_N", "_MO"))});
	img.mouseout(function(){$(this).attr("src", $(this).attr("src").replace("_MO", "_N"))});	
}

function get_target_offset($target){	
//alert($target);
	   $target = $target.length && $target  || $('[name=' + this.hash.slice(1) +']');			   
	   if ($target.length) {return($target.offset().left);}else{return(false);}
}

function scroll_to_loc(loc){
	$('html,body').animate({scrollLeft: loc},{ duration:"slow",  easing: "easeInOutSine"});
}

function changebg(newsrc){
	n=Math.round($.cookie('bg'));n++;
	$.cookie('bg', n);
	a=Math.round(n)-1;;
	oldid='#newimg'+a;
		$("#background").append('<img  class="bgimg" src="img/bg/'+ newsrc+'" id="newimg'+n+'"  alt="'+newsrc+'" />');
$('#newimg'+n).hide();
	$('#newimg'+n).fadeIn(750, function(){
	$(oldid).remove();
	})
}

function randombg(){	
	oldsrc=$('#newimg'+$.cookie('bg')).attr('src');;
	n=Math.floor(Math.random()*bgs.length)
	 newsrc=bgs[n];
	if(oldsrc!=newsrc){
		changebg(newsrc);		
	}
	else
	{randombg()};
}

function fadeInImages(){
	   $('.imagedivholder').each(function(index) {
			id=($(this).attr('id'));
			iid=($('#'+id+' img:eq(0)').attr('id'));
			$('#'+iid).fadeIn(500)
	});
}

$(document).ready(function(){

				   
externalLinks()
$.cookie('bg', 0);					

$(".bgimg").fadeIn();
pf=$('#portfolio_menu');



	setInterval('randombg()', 60000);			   
	$('#portfolio_menu').onselectstart=function(){return false;}
	
	 $('a[href*=#]').click(function() {	
	 	href=($(this).attr('href'));
		 for(i=0;i<items.length;i++){
			if(items[i]==href){
				if(i>0){prev=items[i-1];}else{prev=items[items.length-1];}
				if(i<items.length-1){next=items[i+1];}else{next=items[0];}	
			}		 
		 }
		scroll_to_loc(get_target_offset($(this.hash))-35);
		$('.arrow:eq(1)').attr('href', next);
		$('.arrow:eq(0)').attr('href', prev);
		return false;		
	 });
	 
		MO_imgChange($('.pfbutton img'));
		MO_imgChange($('.button img'));
		MO_imgChange($('.arrow img'));

	pf.mouseover(function () {
	 openPortFolio(pf);
	});
	
	pf.mouseout(function(){
	 closePortFolio(pf);
	});

	scroll_to_loc(-get_target_offset($('#overons'))-35); 
	//$('.imagedivholder').click(function(){
									//	my_id=$(this).parent().attr('id');
									//scroll_to_loc(get_target_offset($('#'+my_id))-35); 
									//	})
});

function closePortFolio(obj){ 
obj.stop();
obj.animate({'top':'-80px'})
}
function openPortFolio(obj){
	 obj.stop();
	obj.animate({'top':'0px'})
}
