// VITOSALVATORE.COM
// desktop beta 0.1

$(document).ready(function(){

	
	///////////////////////////////
	// NASCONDO ELEMENTI ALL'AVVIO
	
	$('#works').hide();
	$('#close').hide();
	$('#next-back').slideUp(0);
	
	
	////////////////////
	//VARIABILE ED ARRAY
	var wWidth = null;
	var wHeight = null;
	var pagine = $('#picCont a').length -1;
	var currPage = null;
	var worksOpen = false;
			
			///////////////////////
			// Stage Size Functions
			$stageDim = function(){
				var n = $("body").css("width").length-2;
				var n2 = $("body").css("height").length-2;
				
				if ( $.browser.msie ) {
					//alert($("body").css("width").length);
					
					wW = $("body").css("width").substr(0, n);
         			wWidth = wW;
					wH = $("body").css("height").substr(0, n2);
         			wHeight = wH;
       
        			 } else {
          				wWidth = document.width;
						wHeight = window.innerHeight;
         		}
			}
			$stageDim();
			//alert("wWidth:_"+wWidth+"wHeight"+wHeight);
	
	//////////////////////////////////////
	// FUNZIONI DI RIDIMENSIONAENTO PAGINA
	
	
			var n = null;
			$dim = function(){
				var picWidth = $("#picCont").css("width");

				n = Math.round(parseFloat(picWidth)/230);
				
			
				sWidth = parseFloat(picWidth)/n;
				//alert("picWidth:_"+picWidth+" sWidth"+sWidth+" n->"+n);
				
				// cambio dimensioni div anteprime

				$(".sqSize").css({
							"width":sWidth,
							"height":sWidth
				});
				
				$("#filter").css({
					"margin-left":sWidth+10,
					"margin-top":sWidth-40
				});
			}
			
			$dim();
			
			
			// avvio evento resize
			$(window).resize(function() {
				
				$stageDim();
				if(worksOpen != true){
				$dim();
				}
				$picDim();
				
			});
			
   //////////////////////////////
   // FUNZIONE RIDIMENZIONA FOTO
			
	$picDim = function(){
	var imgWidth = $("#works #cont img").width();
	var imgHeight = $("#works #cont img").height();
	var percW = wWidth/imgWidth;
	var percH = wHeight/imgHeight;
	var minPerc = null;
	
	//alert("imgWidth: "+imgWidth+" imgHeight:"+imgHeight+" percW: "+percW);
					
		if($('#works #cont img').attr("title") != "web"){
			//alert("percW: "+percW+" wHeight: "+wHeight);
			var minPerc = Math.min(percW, percH)*100;
			var ph2 = wHeight/2;
			var ih2 = imgHeight/200*minPerc;
			var iY = ph2-ih2;
			
			$('#works #cont img').width(Math.round(imgWidth/100*minPerc));
			$('#works #cont img').height(Math.round(imgHeight/100*minPerc));
			$('#works #cont img').css({'margin-top':Math.round(iY)+'px'});
			
			}else{
			var minPerc = Math.max(percW, percH)*100;
			var pw2 = wWidth/2;
			var iw2 = imgWidth/200*minPerc;
			var ph2 = wHeight/2;
			var ih2 = imgHeight/200*minPerc;
			var iY = ph2-ih2;
			var iX = pw2-iw2;
			//alert("minPerc: "+minPerc+" ih2: "+ih2+" iY: "+iY);
			
			$('#works #cont img').width(Math.round(imgWidth/100*minPerc));
			$('#works #cont img').height(Math.round(imgHeight/100*minPerc));
			$('#works #cont img').css({'margin-top':Math.round(iY)+'px','margin-left':Math.round(iX)+'px' });
			//setTimeout(chiudiInfo,3000);
			
					}	
	
					$('#next-back').css({
							"margin-top":wHeight/2-25+"px"
					})
				}
		//alert("imgWidth->"+imgWidth+" imgHeight->"+imgHeight);
			
			///////////////////////////
			// FUNZIONE CARICA IMMAGINI
			
			function loadNext(st){
				currPage = st;
				//alert(currPage);
				var page = $('#picCont a:eq('+st+')').attr('href');
				//alert(arr[st]);
				$("#works #cont").remove();
				$('#works').append('<div id="cont"></div>');
				$('#works #cont').empty().addClass('loading');
				$("#works").slideDown("Blind", function () {
       				$("#close").slideDown("Blind", 0);
					$('#works #cont').load(page, {}, function (stato) { 
					
					$('#works #cont img').hide();
					
					
					var img = $("#works #cont img")[0]; // Get my img elem
	
					$("<img/>") // Make in memory copy of image to avoid css issues
						.attr("src", $(img).attr("src"))
						.load(function() {
							$("#works #cont img").attr("width", this.width);
							$("#works #cont img").attr("height", this.height);
							$('#content').removeClass('loading');
							$picDim();
							$("#next-back").slideDown("Blind", 0);
							worksOpen = true;
							$("#works #cont img").fadeIn();
					});
					//$('#works #cont img').removeAttr('title');
					
						}
					);
      			});
			setTimeout(sposta,500);
			
			
		}
			
			
			//////////
			// BOTTONI
			
			// click anteprime
			$('#picCont a').click(function(e){
			e.preventDefault();	
			$("body").css({
							"overflow":"hidden"
				});
			var contentData=$(this).attr('tabindex');
			loadNext(contentData);
				
			});
			
			// sposta pagina dopo aver cliccato sulle anteprime
			function sposta(){
				$(window).scrollTo($('#works'),500,'y');
			}
			
		    
			
			// chiudi gallery
			$('#close img').click(function(e){
				e.preventDefault();
				$("#close").slideUp("Blind");
				$("#next-back").slideUp("Blind");
				$("#works").slideUp("Blind",function(){
					worksOpen = false;
						$("body").css({
							"overflow":"auto"
				});
					$stageDim();
					$dim();
					});
				$(window).scrollTo($('#logo'),500,'y');
			});
			
			$('#next-back #next').click(function(e){
					
				e.preventDefault();
				if(currPage!=pagine){
					currPage+=1;
					loadNext(currPage);
					}else{
						currPage=0;
						loadNext(currPage);
					}
				
			});
			
			$('#next-back #back').click(function(e){
				e.preventDefault();
					e.preventDefault();
				if(currPage!=0){
					currPage-=1;
					loadNext(currPage);
					}else{
						currPage=pagine;
						loadNext(currPage);
					}
				
			});
			
			/////////////////////
			// KEYBOARD CONTROL
			
			$(document).keyup(function(e) {
				if(worksOpen == true){
  				//alert(e.which);
				switch(e.which){
					case 37:
					if(currPage!=0){
					currPage-=1;
					loadNext(currPage);
					}else{
						currPage=pagine;
						loadNext(currPage);
					}
					
					break;
					case 39:
					if(currPage!=pagine){
					currPage+=1;
					loadNext(currPage);
					}else{
						currPage=0;
						loadNext(currPage);
					}
					
					break;
					case 38:
					$("#close").slideUp("Blind");
					$("#next-back").slideUp("Blind");
					$("#works").slideUp("Blind",function(){
						worksOpen = false;
							$("body").css({
								"overflow":"auto"
					});
						$stageDim();
						$dim();
						});
					$(window).scrollTo($('#logo'),500,'y');	
				}
				}
			});
	
	
	
	
			////////////////////
			// FILTRI
			var toFilter = null;
			
			 $('#filter li a').click(function(e){
				//e.preventDefault();
				$('#filter li a').fadeTo("fast", 0.5);
				$(this).fadeTo("slow", 1);
				
				var toFilter = $(this).attr("title");
				$("#picCont div").fadeTo("fast", 1);
				if(toFilter != "all"){
				$("#picCont div[title!="+toFilter+"]").fadeTo("slow", 0.10);
				/*$("#picCont div").filter(function (index) {
                      
					  return $(this).attr("title") != toFilter;
                
				})
				.fadeTo("slow", 0.10);*/
				}

			});
			 
	});
