$(document).ready(function(){
	
						   
$.fn.gallery = function(options){	


$("#blkshader").css("opacity",".5");	
	$('#captionshade').css("opacity",.5);	

					   
						   
	var defaults = {	
	//theID: ''
	};
	var options = $.extend(defaults, options); 					   
						   
						   
thumbdown=	options.thumbDown;					   
gallerybounds=options.theWidth;	
galleryheight=$(".featureimage .puthere").height();
//galleryheight=$("#secheadsect").height();
//alert(galleryheight);
thumbsize=options.thumbWidth;
thumbcount=8;
prevnextbutwidth=25;
shaderheight=75;
newleft=prevnextbutwidth;
gallspacer=(((gallerybounds-50)-(thumbcount*(parseInt(thumbsize)+2)))/(thumbcount-1))+(parseInt(thumbsize)+2);
//alert (gallspacer);
//gallspacer=gallspacer+(gallspacer/(thumbcount));


//alert("here troy"+gallerybounds);

$("#gallholder").css("width",gallerybounds+"px");	

$(".slidethumb").css("width",thumbsize+"px");
$(".slidethumb").css("height",thumbsize+"px");



						   
$("#morebutton").css("left",gallerybounds-25+"px");						   
						   
$("#morebutton").click(function() {
								slideleft();	 
								 });
$("#prevbutton").click(function() {
								slideright();	 
								 });

$("#hidewintext").click(function() {
								closewindow();	 
								 });

$("#showwintext").click(function() {
								openwindow();	 
								 });

$("#desc").css("opacity",0);


$("#infowinshader").css("opacity",.7);
myfadeinimage=$(".fadeinimage").children(".puthere");
myfeatureimage=$(".featureimage").children(".puthere");

firstthumbpos=$(".slidethumb:first").css("left");
lastthumbpos=$(".slidethumb:last").css("left");

$('#infowin').css("width",(gallerybounds)+"px");
$('#infowin').css("top",(galleryheight-shaderheight)+"px");
$('#infowin').css("height",shaderheight+"px");
$('#infowin').css("left","0px");
$("#desc").css("width",(gallerybounds)+"px");
//$("#desc").css("top",(galleryheight-shaderheight)+"px");
$("#desc").css("height",shaderheight+"px");
$("#desc").css("left","0px");

// start the slide left and slide right functions

startthepage();

function hideprevious(){
	$("#prevbutton").css("top",700);
}
function showprevious(){
	$("#prevbutton").css("top",(parseInt(thumbdown))+"px");
	//alert(thumbdown);
}
function hidenext(){
	$("#morebutton").css("top",700);
}
function shownext(){
	$("#morebutton").css("top",(parseInt(thumbdown))+"px");
	//alert(parseInt(thumbdown)+1);
}

function checkbuttons(){
	//alert ($(".slidethumb:first").css("left"));
	if (parseInt($(".slidethumb:first").css("left"))>=0){
		//alert("im hiding previous because first="+parseInt($(".slidethumb:first").css("left")));
	hideprevious();
	//alert("im here");
	
		
	} else {
	showprevious();		
	}
	
	if (parseInt($(".slidethumb:last").css("left"))<gallerybounds){
	hidenext();	
		
	} else {
	shownext();		
	
	}
	
}

function slideleft(){
	
	if(parseInt($(".slidethumb:eq("+thumbcount*9+")").css("left"))>0){
		nextleftthumb=thumbcount*10;
	}
	if(parseInt($(".slidethumb:eq("+thumbcount*8+")").css("left"))>0){
		nextleftthumb=thumbcount*9;
	}
	if(parseInt($(".slidethumb:eq("+thumbcount*7+")").css("left"))>0){
		nextleftthumb=thumbcount*8;
	}
	if(parseInt($(".slidethumb:eq("+thumbcount*6+")").css("left"))>0){
		nextleftthumb=thumbcount*7;
	}
	if(parseInt($(".slidethumb:eq("+thumbcount*5+")").css("left"))>0){
		nextleftthumb=thumbcount*6;
	}
	if(parseInt($(".slidethumb:eq("+thumbcount*4+")").css("left"))>0){
		nextleftthumb=thumbcount*5;
	}
	if(parseInt($(".slidethumb:eq("+thumbcount*3+")").css("left"))>0){
		nextleftthumb=thumbcount*4;
	}
	if(parseInt($(".slidethumb:eq("+thumbcount*2+")").css("left"))>0){
		nextleftthumb=thumbcount*3;
	}
	if(parseInt($(".slidethumb:eq("+thumbcount*1+")").css("left"))>0){
		nextleftthumb=thumbcount*2;
	}
	if(parseInt($(".slidethumb:eq(0)").css("left"))>0){
		nextleftthumb=thumbcount*1;
	}
	//alert($(".slidethumb").length);
	//alert("here troy"+thumbcount);
	newleft=(gallerybounds-50)*-1;
newleft=parseInt((gallspacer*thumbcount)-25)*-1;
newleft=25;

//$(".slidethumb").each(function(){

for(mythumbs=0;mythumbs<=$(".slidethumb").length-1;mythumbs++){

if(mythumbs<nextleftthumb){
	$(".slidethumb").eq(mythumbs).animate({left: "-500px"},{ queue:true, duration:500, easing: 'easeOutExpo'});
} else {

$(".slidethumb").eq(mythumbs).animate({left: newleft},{ queue:true, duration:500, easing: 'easeOutExpo'});

newleft=newleft+gallspacer;	
//alert(newleft);

}


}
setTimeout(function(){checkbuttons();}, 600);	

} // end of slide left



function slideright(){
	newleft=prevnextbutwidth;
	if(parseInt($(".slidethumb:eq(0)").css("left"))<0){
		nextleftthumb=0;
	}
	if(parseInt($(".slidethumb:eq("+thumbcount*1+")").css("left"))<0){
		nextleftthumb=thumbcount*1;
	}
	if(parseInt($(".slidethumb:eq("+thumbcount*2+")").css("left"))<0){
		nextleftthumb=thumbcount*2;
	}
	if(parseInt($(".slidethumb:eq("+thumbcount*3+")").css("left"))<0){
		nextleftthumb=thumbcount*3;
	}
	if(parseInt($(".slidethumb:eq("+thumbcount*4+")").css("left"))<0){
		nextleftthumb=thumbcount*4;
	}
	if(parseInt($(".slidethumb:eq("+thumbcount*5+")").css("left"))<0){
		nextleftthumb=thumbcount*5;
	}
	if(parseInt($(".slidethumb:eq("+thumbcount*6+")").css("left"))<0){
		nextleftthumb=thumbcount*6;
	}
	if(parseInt($(".slidethumb:eq("+thumbcount*7+")").css("left"))<0){
		nextleftthumb=thumbcount*7;
	}
	if(parseInt($(".slidethumb:eq("+thumbcount*8+")").css("left"))<0){
		nextleftthumb=thumbcount*8;
	}
	if(parseInt($(".slidethumb:eq("+thumbcount*9+")").css("left"))<0){
		nextleftthumb=thumbcount*9;
	}
	if(parseInt($(".slidethumb:eq("+thumbcount*10+")").css("left"))<0){
		nextleftthumb=thumbcount*10;
	}
	
	
	
	
	
	
	

	
	
	
	
	
//$(".slidethumb").each(function(){

for(mythumbs=0;mythumbs<=$(".slidethumb").length-1;mythumbs++){
goleft=newleft	
if(mythumbs>(nextleftthumb+thumbcount)){
	newleft=prevnextbutwidth-gallspacer;
	goleft=(gallerybounds+thumbsize)+"px";
} 
if(mythumbs<nextleftthumb){
	newleft=prevnextbutwidth-gallspacer;
	goleft="-"+(2*thumbsize)+"px";
}
$(".slidethumb").eq(mythumbs).animate({left: goleft},{ queue:true, duration:500, easing: 'easeOutExpo'});

newleft=newleft+gallspacer;	
//alert(newleft);


	
	
	

}



//});

setTimeout(function(){checkbuttons();}, 600);	

	
} // end of slideright






		
		
function clearallthumbs(){
	$(".slidethumb").each(function(){
			
$(this).css("opacity",.4);	
});
}		
function showthehidebutton(){
$("#hidewintext").css("left","0px");	
//alert($(".slidethumb:eq(9)").css("left"));
}
function hidethehidebutton(){
$("#hidewintext").css("left","-100px");	
}
function showtheshowbutton(){
$("#showwintext").css("left","0px");	
}
function hidetheshowbutton(){
$("#showwintext").css("left","-100px");	
}
function slidewindow(){
	
	if ($('#infowin').css("height")=="0px"){
		//alert ("troy"+$('#infowin').css("width"));
	openwindow();
	} else {
	closewindow();	
		
	}
	
}
function closewindow(){
	$('#captionblock').animate({left: "1200px"},{ queue:true, duration:1000, easing: 'easeOutExpo' });
	//hidethehidebutton();
	//hidethewords();
	//$('#infowin').stop().animate({height: "15px"},{ queue:true, duration:1000, easing: 'easeOutExpo', complete:showtheshowbutton });	
	//$('#infowin').animate({top: (galleryheight-15)+"px"},{ queue:false, duration:1000, easing: 'easeOutExpo', complete:showtheshowbutton });	

}
function openwindow(){
	//alert("im in open window");
	//hidetheshowbutton();
	//alert("hhhh"+$("#caption").html());
	if($("#caption").html()!=""){
	$('#captionblock').stop().animate({left: "50px"},{ queue:true, duration:1000, easing: 'easeOutExpo' });
	
	}
	//$('#infowin').animate({top: (galleryheight-shaderheight)+"px"},{ queue:false, duration:1000, easing: 'easeOutExpo' });
	//alert("im putting the window to"+(galleryheight-30)+"px");
	
	checkbuttons();	
	
	
}
function showthewords(){
	

	$('#desc').stop().animate({opacity: 1},{ queue:true, duration:500, easing: 'easeOutExpo', complete:showthehidebutton });	
	//alert("I should see the words and here is width"+$("#desc").css("width")+"here is top"+$("#desc").css("top")+"here is height"+$("#desc").css("height")+"here is left"+$("#desc").css("left")+"here is the html"+$("#desc").html());
}
function hidethewords(){
	

	$('#desc').stop().animate({opacity: 0},{ queue:true, duration:100, easing: 'easeOutExpo' });	
	
}

$("#showin").click(function() {
		//$('#infowin').toggleClass('newClass', 1000);
	slidewindow();
			
					
							});



function startthepage(){
	//alert("got in start page");
	$(".slidethumb").css("opacity",.4);

//$(".slidethumb").each(function(){
				//alert($(".slidethumb").length	);			 
for(mythumbs=0;mythumbs<=$(".slidethumb").length-1;mythumbs++){		
//alert("started thumb loop");
			
			

			//$(this).css("opacity",.4);	
			
			//$(this).animate({top: 500},{ queue:true, duration:1000 });
			//$(this).animate({left: newleft},{ queue:true, duration:500 });
			if(mythumbs==$(".slidethumb").length-1){
				
				$(".slidethumb").eq(mythumbs).animate({top: thumbdown+"px"},{ queue:true, duration:1000, easing: 'easeOutExpo' }).animate({left: newleft},{ queue:true, duration:500, easing: 'easeOutExpo',complete: openwindow});
			} else {
			$(".slidethumb").eq(mythumbs).animate({top: thumbdown+"px"},{ queue:true, duration:1000, easing: 'easeOutExpo' }).animate({left: newleft},{ queue:true, duration:500, easing: 'easeOutExpo' });
			}
			newleft=newleft+gallspacer;
			
			$(".slidethumb").eq(mythumbs).mouseover(function() {	
									if($(myfadeinimage).attr("src")!="/imagesNS/portfolio_imagesNS/"+(this.id)+".jpg"){
						$(this).animate({opacity: 1},{ queue:false, duration:250 });
									}
									   }); //end of mouse over
			
			$(".slidethumb").eq(mythumbs).mouseout(function() {	
									  if($(myfadeinimage).attr("src")!="/imagesNS/portfolio_imagesNS/"+(this.id)+".jpg"){
						$(this).animate({opacity: .4},{ queue:false, duration:250 });
									  }
									   }); //end of mouse out
			
			
			
								  
	$(".slidethumb").eq(mythumbs).click(function() {
						   
	if($(myfadeinimage).attr("src")!="/imagesNS/portfolio_imagesNS/"+(this.id)+".jpg"){
		
		clearallthumbs();
						 //  $('#infowin').removeClass('newClass', 1000);
		//$("#myhide").attr("src", "http://www.sofia-rose.com/archives1208/large/"+(this.id)+".jpg");				   
						   
		closewindow();	
		//$(".slidethumb").eq(mythumbs).css("opacity",1);
		$(this).css("opacity",1);
		
		myloadlayer=$("#loader");
		//$(myfadeinimage).attr("src", "imagesNS/realgallims/"+(this.id)+".jpg");
		$(myfadeinimage).attr("src", "/imagesNS/portfolio_imagesNS/"+(this.id)+".jpg");
		$(myloadlayer).css("top",200);
		myseeker=(this.id);
		
		$(myfadeinimage).load(function(){
									  // alert("im here and myseeker ="+myseeker);
								$(myloadlayer).css("top",-425);	   
		$(myfeatureimage).stop(true).animate({opacity: 0},{ queue:false, duration:1000, complete:function(){
																							   
			$(myfeatureimage).attr("src",$(myfadeinimage).attr("src"));
			$(myfeatureimage).css("opacity",1);
			//checkcaption(
			//start get the new text
		
		
		
		
	$.get("/portserver.php", { ID: ""+(myseeker)+"" },
   function(data){
	newdata= data.split("|");
   // alert("Data Loaded: "+(myseeker) + data);
	//$("#desc").html(newdata[1]);
	$("#caption").html(newdata[1]);
	//alert(newdata[1]);
	openwindow();
	
   });
		
		
		// end get the text
			
			
			} // end of complete function
		 }); // end of animate
		
		
		
		
		
		
		
		
		
									   }); //end of load function
			
	}
	});	//end of onclick		
			
			
			
			
			
			
			
			
			
			
			
//}); //end of slidethumb each

} //end of new by num thumbslides

} // end of start the page
						   
						   
						   
} //end of declared function






function switchgall(whichgall){
		
		$(".secheadsect").load("gallerypusher.php?makegal=11",rebuild);
		
		
	}	

$(".tabbuttons a").click(function(){
		
		switchgall(11);
		
		
		
		
		
	});
	
	function rebuild(){
		//alert("rebuilding");
		
		$("#gallholder").css("top","0px");
		
		//$(".slidethumb").css("left","-250px");
		
		$("#gallholder").gallery({
theWidth: "980",
thumbWidth: "75",
thumbDown: "367"
});
		
		
		
		
		
		
		
		
	}// end of rebuild









						   
						   
}); //end of jquery
