var activeTab="t1";
var PopUpActive=false;
var pageWd=500;
var galleryList=new Array();
var GalleryNames
var ActiveId=0;
var stripWidth=0;
var loading=false;
function resetValues(){
	 $(".ThumbStrip").fadeOut('fast');
	$(".PicImage").fadeOut('fast', function() {
		$("#PageOver").fadeOut('fast', function() {
			PopUpActive=false;
 //galleryList=new Array();
 GalleryNames
 ActiveId=0;
 stripWidth=0;
 loading=false	
				$("#PageOver").remove();
			})
		});
 
}
function mkThumbs(GalleryName,id,tabId){
   			var GalleryDiv=$("#"+tabId).children(".thumbPanel");
			//console.log("GalleryDiv:"+tabId+" html :"+GalleryDiv.html())
               var Thumb = new Image();
               var ThumbDivs='<div class="ThumbDiv .LoadingImgs"></div>'
                var ThumbId=$(GalleryDiv).append(ThumbDivs);
				$(GalleryDiv).click(function (event){
							
									event.stopPropagation();
								});
               $(Thumb).load(function () {
                
                 
                    var scaler =120/Thumb.height
                    var wd =Math.round(Thumb.width*scaler);
                    //stripWidth+=(wd+10)

                    
                        // $(GalleryName).css("width",stripWidth+"px")
                       $(Thumb).css("width",Thumb.width+"px");
                       $(Thumb).css("height",Thumb.height+"px");
					   
                       GalleryDiv.children(".ThumbDiv").eq(id).append(Thumb);
                         GalleryDiv.children(".ThumbDiv").eq(id).data('id', id);
                        GalleryDiv.children(".ThumbDiv").eq(id).removeClass(".LoadingImgs")
                         GalleryDiv.children(".ThumbDiv").eq(id).css("width",Thumb.width+"px");
						 GalleryDiv.children(".ThumbDiv").eq(id).css("height",Thumb.height+"px");
                        GalleryDiv.children(".ThumbDiv").eq(id).mouseenter(function(){$(this).fadeTo('fast', .7)});
                          GalleryDiv.children(".ThumbDiv").eq(id).mouseleave(function() {$(this).fadeTo('fast', 1)});
                          GalleryDiv.children(".ThumbDiv").eq(id).click(function (event){
							 if( loading==false){
								 loading=true
								 MkGallery(GalleryName,tabId,$(this).data('id') )
                            //NextImg($(this).data('id'));
								}
                            event.stopPropagation();
                           })
                        $(Thumb).hide();
                         id=id+1
                       if(id<galleryList[tabId].imgs.length){
                       // console.log("Mkthumb id:"+id);
                       mkThumbs(GalleryName,id,tabId)

                       }
                        
                      
                       $(Thumb).fadeTo('slow', 1)
                 }).attr('src', "GalleryFolders/"+GalleryName+"/thumbs/"+galleryList[tabId].imgs[id].file);
                  $(Thumb).error(function() {
                    var ThumbError = new Image();
                    $(ThumbError).load(function () {
                           var scaler =75/ThumbError.height
                    var wd =Math.round(ThumbError.width*scaler);
                       stripWidth+=wd+10
//console.log("stripWidth:"+ stripWidth+" error wd:"+wd+"  id:"+id+ "current wd:"+ $(".ThumbInnerStrip").width())
                        
                         $(GalleryName).css("width",stripWidth+"px")
                       $(ThumbError).css("width",wd+"px");
                       $(ThumbError).css("height","75px");
                        GalleryDiv.children(".ThumbDiv").eq(id).append(ThumbError);
                        GalleryDiv.children(".ThumbDiv").eq(id).data('id', id);
                        GalleryDiv.children(".ThumbDiv").eq(id).removeClass(".LoadingImgs")
                         GalleryDiv.children(".ThumbDiv").eq(id).click(function (event){
							 if( loading==false){
								 loading=true
                            NextImg($(this).data('id'),tabId);
								}
                            event.stopPropagation();
                           })
                        $(ThumbError).hide();
                         id=id+1
                       if(id<galleryList.imgs.length){
                        //console.log("Mkthumb id:"+id);
                       mkThumbs(GalleryName,id)

                       }


                       $(ThumbError).fadeIn('normal');
                     }).attr('src', "LayoutImgs/errorPic.png");
                   });



}
function NextImg(id,tabId){
    //console.log("next id:"+id)
///remove old image
            $(".PicImage").children().fadeOut('fast',function() {
            
             })
            

//add new image
                   var img = new Image();

                   $(img).load(function () {
				   
				   $("#LoadingImgs").remove()
                   $(".PicImage").children().remove();
                   ActiveId=id;
                    $(img).hide();
                   var imgScale=0.8*pageWd
                   var wd
                   var hg
                    if(imgScale>img.width){
                    wd=img.width
                    hg=img.height
                       }else{
                    wd=img.width
                    hg=img.height
                        }
                       var top=$(window).scrollTop();
                   //console.log("des lg:"+galleryList.imgs[id].des.length)
                   var des='<div class="PicDes">'+galleryList[tabId].imgs[id].des+'</div>'
                    $(".PicImage").append('<div class="PicDes">'+galleryList[tabId].imgs[id].des+'</div>');
                    hg+=$(".PicDes").height()+16+10
                     $(".PicDes").remove();
				//	console.log("hg height:"+hg)
					if($(window).scrollTop()>700){
						window.scroll(0,0); 
					}
					$(img).css("width",img.width+"px");
                       $(img).css("height",img.height+"px");
                   $(".PicImage").animate({'width': wd,'height': (hg), 'top': ($(window).scrollTop()+20) + 'px'},'normal', function(){
							$(".PicImage").append('<div class="PicHeading">'+galleryList[tabId].imgs[id].title+'</div><div id="CloseGallery"></div>');
							$(".PicImage").append(img);
							$(".PicImage").append('<div id="PrevB"></div>');
							$(".PicImage").append('<div id="NextB"></div>');
							$(".PicImage").append('<div class="PicDes">'+galleryList[tabId].imgs[id].des+'</div>');
                          //$(".PicImage").css("height",(hg+$(".PicDes").height()+16)+"px")
                            $(img).click(function (event){
									event.stopPropagation();
								
								});
							$(img).fadeIn('normal', function() {
								loading=false
								$("#CloseGallery").click(function (event){
									resetValues()
									event.stopPropagation();
								});
								
								if(ActiveId==0){
										$("#PrevB").fadeTo('fast', 0.3) 
								}
								if((ActiveId)==galleryList[tabId].imgs.length-1){
										  $("#NextB").fadeTo('fast', 0.3)
								}
														$("#PrevB").click(function (event)
														 { 
															if(ActiveId>0){
															 NextImg(ActiveId-1,tabId)
														  }
														  event.stopPropagation();
														   });
														 $("#NextB").click(function (event)
														 {
															 if((ActiveId+1)<galleryList[tabId].imgs.length){
																 NextImg(ActiveId+1,tabId)
															}
															event.stopPropagation();
															});

								});
                    });
                    $(".ThumbStrip").animate({'top': ($(window).scrollTop()+40) + 'px'  });
                   }).attr('src', "GalleryFolders/"+GalleryNames+"/"+galleryList[tabId].imgs[id].file);
                   $(img).error(function() {
					   var Errorimg = new Image();
                    $(Errorimg).load(function () {
                           $(".PicImage").children().remove();
                   ActiveId=id;
                    $(Errorimg).hide();
                   var imgScale=0.8*pageWd
                   var wd
                   var hg
                    if(imgScale>img.width){
                    wd=Errorimg.width
                    hg=Errorimg.height
                       }else{
                    wd=(Errorimg.width/imgScale)*Errorimg.width
                    hg=(Errorimg.width/imgScale)*Errorimg.height
                        }
                       var top=$(window).scrollTop();
                   //console.log("des lg:"+galleryList.imgs[id].des.length)
                   var des='<div class="PicDes">'+galleryList.imgs[id].des+'</div>'
                    $(".PicImage").append('<div class="PicDes">'+galleryList[tabId].imgs[id].des+'</div>');
                    hg+=$(".PicDes").height()+16+10
                     $(".PicDes").remove();
					//console.log("hg height:"+hg)
					if($(window).scrollTop()>700){
						window.scroll(0,0); 
					}
                   $(".PicImage").animate({'width': wd,'height': (hg), 'top': ($(window).scrollTop()+20) + 'px'},'normal', function(){
							$(".PicImage").append('<div class="PicHeading">'+galleryList[tabId].imgs[id].title+'</div>');
							$(".PicImage").append(Errorimg);
							$(".PicImage").append('<div id="PrevB"></div>');
							$(".PicImage").append('<div id="NextB"></div>');
							$(".PicImage").append('<div class="PicDes">'+galleryList[tabId].imgs[id].des+'</div>');
                          //$(".PicImage").css("height",(hg+$(".PicDes").height()+16)+"px")
							$(Errorimg).fadeIn('normal', function() {
								loading=false
								$("#CloseGallery").click(function (event){
									resetValues()
									event.stopPropagation();
								});
								if(ActiveId==0){
										$("#PrevB").fadeTo('fast', 0.3) 
								}
								if((ActiveId)==galleryList.imgs.length-1){
										  $("#NextB").fadeTo('fast', 0.3)
								}
														$("#PrevB").click(function (event)
														 { if(ActiveId>0){
															 NextImg(ActiveId-1,tabId)
														  }
														  event.stopPropagation(); });
														 $("#NextB").click(function (event)
														 { if((ActiveId+1)<galleryList[tabId].imgs.length){
															 NextImg(ActiveId+1,tabId)
																}
																event.stopPropagation();
															});
								});
                    });
                    $(".ThumbStrip").animate({'top': ($(window).scrollTop()+40) + 'px'  });
                     }).attr('src', "Gallery/LayoutImgs/errorPic.png");
                   });



}
function MkGallery(GalleryName,tabId,PicId){
GalleryNames=GalleryName
var InsetHtml="";
   if(PopUpActive==false){
        
        InsetHtml='<div id="PageOver">';
        InsetHtml+='<div class="PicImage">';
                   InsetHtml+='</div>';
        InsetHtml+='</div>';
        $("body").append(InsetHtml);
		$("#PageOver").append('<div id="LoadingImgs"></div')
        $("#LoadingImgs").css("top",( $(window).scrollTop()/2)+"px");
		 //$("#LoadingImgs").css("left",( $(window).width()/2)+"px");
		PopUpActive=true;
        //
          
			
           
             $(".PicImage").click(function (event){
							
									event.stopPropagation();
								});
            
          
        //
		
        $('#PageOver').height(document.documentElement.scrollHeight);
        $('#PageOver').fadeIn('slow', function() {
                   NextImg(PicId,tabId)
                 $('#PageOver').click(function (event){
									resetValues()
								});
            });
   }

}

function getGallery(GalleryId,tabid,clicked){
//console.log("getting:"+"Gallery/GalleryFolders/"+GalleryId+"/piclist.lst")
$.get("GalleryFolders/"+GalleryId+"/piclist.lst",  function(data){
   //  console.log("retunring:"+data)
    // console.log(data.imgs[1].file); //  2pm
    galleryList[tabid]=data;
   //MkGallery("GalleryFolders/"+GalleryId)
mkThumbs(GalleryId,0,tabid)
 $(clicked).data('GalleryLoaded', true);
   }, "json");
}
function parseStg(stg){
// Damn pesky carriage returns...
  var txt =stg
    var txt = txt.replace(/(\r\n|\n|\r)/gm,"");
    
 // console.log("out: "+txt);
var obj = jQuery.parseJSON(txt);
return obj
}
function GalleryInit(){
//insert css

//rewrite events
$("li").attr("data-gallery", function (arr) {
         
        }).each(function () {
        // console.log("num of light boxs:"+ $(this).index());
            if($(this).index()>0){
				
				$("#"+$(this).attr("tab")).hide()
			}else{
				$("li").removeClass("active");
						$(this).addClass("active");
						
							
						
                        var stg=$(this).attr("data-gallery");
                        
                        var Folder=stg.substring(stg);
						var tabid=$(this).attr("tab");
                     //  console.log("open gallery folder:"+Folder);
					   if($(this).data('GalleryLoaded')!=true){
					   resetValues()
                       getGallery(Folder,tabid,this);
					   }
					//	console.log("activeTab:"+activeTab)
						$("#"+activeTab).fadeOut('fast',function() {
						 $("#"+tabid).fadeIn('slow')
						activeTab=tabid;
						})
			}
          //      console.log("..."+ $(this).html());
                $(this).click(function (event)
                 {
                        event.preventDefault();
						$("li").removeClass("active");
						$(this).addClass("active");
						
							
						
                        var stg=$(this).attr("data-gallery");
                        
                        var Folder=stg.substring(stg);
						var tabid=$(this).attr("tab");
                     //  console.log("open gallery folder:"+Folder);
					   if($(this).data('GalleryLoaded')!=true){
					   resetValues()
                       getGallery(Folder,tabid,this);
					   }
					//	console.log("activeTab:"+activeTab)
						$("#"+activeTab).fadeOut('fast',function() {
						 $("#"+tabid).fadeIn('slow')
						activeTab=tabid;
						})
						
					   
                });
              //  console.log("is light box");
          
          });

}

$(document).ready(function()
    {

    GalleryInit();
   if ($.browser.msie && jQuery.browser.version < 9) {
    $("#headingTxt").html('Intrinsic Media <br>To see this page as it was intended please download and use a real browser not this IE crap. </br><br>Try <a href="http://www.google.com/chrome/" >Google chrome</a> or <a href="http://www.mozilla.com/en-US/firefox/new/" >Firefox</a> or <a href="http://www.opera.com/" >Opera</a> or <a href="http://www.apple.com/safari/" >Safari</a>' )
  }
    });

