// JavaScript Document

	
$(document).ready(function() { 
	
	var contentLoaded=false;
	
	initPage=function(){
		navigation(currentPage);
		
		
		
		$("#artwork a").hover(
		  function () {
			id=$(this).attr('id');
			html='<span class=\"title\">' + paintings[id].title + '</span>  <span class="year">' + paintings[id].year + '</span><br />';
            html+='' + paintings[id].size + ' ' + paintings[id].medium;
			$("#painting_tip #block").html(html);
					
			var textWidth=$("#painting_tip").width();
			var pos = $(this).offset();  
			var width = $(this).width()/2-28;
		    
			if(pos.left>$(window).width()/2){
				$("#painting_tip").css( { "left": (pos.left - textWidth + 55) + "px", "top":pos.top-66 + "px" } );
				$("#painting_tip").removeClass("left");
				$("#painting_tip").addClass("right");
			}else{
				//show the menu directly over the placeholder
				$("#painting_tip").css( { "left": (pos.left + width) + "px", "top":pos.top-66 + "px" } );
				
				$("#painting_tip").removeClass("right");
				$("#painting_tip").addClass("left");
			}	
			$("#painting_tip").show();			
		  }, 
		  function () {
		 	$("#painting_tip").hide();
		  }
		);
	
	};
	

// Event handlers
	$.address.init(function(event) {
		currentPage=event.value;
	}).change(function(event) {
	    if(contentLoaded==true){
			navigation(event.value);
		}
	});

				
	// Simple log
	var log = function(msg) {
		if (!$('.log').length) {
			$('<div class="log"></div>').appendTo('body');
		}
		$('.log').append(msg.replace(/^([^:]*):(.*)$/, '<p><b>$1:</b> <span class="$1">$2</span></p>'))
			.attr({scrollTop: $('.log').attr('scrollHeight')})
			.find('p:nth-child(even)').addClass('even');					
	}
	

	// Actions for back button	
	function navigation(v){
		//alert("HELLO"+v);
		if(v=="News" || v=="Contact" || v=="CV" || v=="Press"){
			 temp="#navigation ."+v;
			
			
			 $("#navigation a.on").removeClass("on");
			 $(temp).addClass("on");
			 $("#gallery").hide();
			 $("#navigation #info").hide();
			 id="#content #"+$(temp).text();
			 $("#content .section").hide();
			 $(id).show();
			 currentPage=$(temp).text();		
		 }else{
			temp="#"+v;
		 
		 // Set info
			 id=v;
			 setPainting(id);
			 
			 // Center Fullsize
			 //centerGallery();
			 
			 // Reset Navigation
			 $("#navigation a.on").removeClass("on");
			 $("#gallery").show();
			 $("#navigation #info").show();
			 $("#content .section").hide();
		 }
			 
		}
	
	
	/*  Painting Class Stucture  */
	
	function painting(title,medium,size,year,thumbnail,fullsize) {
		this.title=title;
		this.medium=medium;
		this.size=size;
		this.year=year;
		this.thumbnail=thumbnail;
		this.fullsize=fullsize;
	}
	
	
	/*  Create Paintings  */
	
	var paintings=new Array()
	$.getJSON(
		"includes/gallery.php?id=1",
		function(data){
			var html="";
			$.each(data.gallery, function(i,user){
				paintings[i]=new painting(user.title,user.medium,user.size, user.year, user.thumbnail, user.fullsize);
				html+='<a href="#' + i + '" id="' + i +'"><img src="' + paintings[i].thumbnail + '" /></a>';
			});
			$(html).appendTo("#artwork .shell");
			contentLoaded=true;
			initPage();
		
		}
	);
	
/*  Center Paintings  */ 
$(window).resize(function(){ //centerGallery(); 
});
	   
	   
	   function centerGallery(){	  
		  $("#gallery img").center();
		  $("#gallery").center();
	   }
	   
	   
	  function setPainting(id){
	  		if(id=="/"){
			  id=0;
			}
			$("#info").html("Loading...");
			$("#gallery").html("");
			//alert(paintings[id].fullsize);
			
			// Insert preloaded image after it finishes loading
			$('<img />')
				.attr({src: paintings[id].fullsize, alt: paintings[id].title , id: "gallery_image"})
				.load(function(){
					$("#gallery").html( $(this) );
					html='<span class="title">' + paintings[id].title + '</span>';
					html+=paintings[id].medium+'<br/>';
					html+=paintings[id].size+'<br/>';
					html+=paintings[id].year;
					$("#info").html(html);
					currentPainting=id;
					currentPage="Gallery";
					//centerGallery();
				});
			
		    //html='<img src="' + paintings[id].fullsize + '" alt="' + paintings[id].title + '" id="gallery_image"/>';
			//$("#gallery").html(html);	
			
		}	

		   

	
	/*  Show work - Artwork Click  */
	   
	   $("#artwork a").click(
	   	  function(){
			 // Set info
			 id=$(this).attr('id');
			 setPainting(id);
			 
			 // Center Fullsize
			 //centerGallery();
			 
			 // Reset Navigation
			 $("#navigation a.on").removeClass("on");
			 $("#gallery").show();
			 $("#navigation #info").show();
			 $("#content .section").hide();
			 return false;
	  	  }
	   );
	   
	
	   
	/*  Show artwork - Artwork Hover */
	   
	
	/*  Page Remote -   */
		 
		/*  
	   $("#navigation a").click(
	   	  function(){
			 $("#navigation a.on").removeClass("on");
			 $(this).addClass("on");
			 $("#gallery").hide();
			 $("#navigation #info").hide();
			 id="#content #"+$(this).text();
			 $("#content .section").hide();
			 $(id).show();
			 currentPage=$(this).text();
			 return false;
	  	  }
	   );
	   */
 
	
	/*
	$(document).keydown(alertKeyPress);
	function alertKeyPress(e) {
	
	if(e.keyCode==40){
		
	}else if(e.keyCode==38){
		if(currentPage=="Gallery"){
			$("#navigation #contact").Trigger('click');
		}
	   
	}
	
	if(e.keyCode==39){    
		if(currentPainting+1!=paintings.length){
			currentPainting++;
			setPainting(currentPainting);
		}
    }else if(e.keyCode==37){
		if(currentPainting!=0){
			currentPainting--;
			setPainting(currentPainting);
		}
	}};

	*/
	
    });
	

