/**
 * @author Aubrey
 */
var ActiveGallery = function(){
    this.cats = {};
    this.pics = {};
    this.SlideAmount = 0;
    this.app = "ActiveGallery";
}

ActiveGallery.prototype.showImage = function(iPicture, iPage){
    var ag_picture, ag_text, ag_picpages, x, xy;
    
    ag_picture = $("#ag_picture")[0];
    ag_text = $("#ag_text")[0];
    ag_picpages = $("#ag_picpages")[0];
	
	var oAgImage=this.pics[iPicture];
	if (!oAgImage) {
		return false;
	}
	//alert(oAgImage["longDesc"]);
	//alert(oAgImage["img"]);	
	
	/*	
	$(".portfolio_text").each(function() {
		if (oAgImage && oAgImage["longDesc"]) {
			$(".portfolio_text").hide("slow");

		} else {
			if (oAgImage && oAgImage["longDesc"]) {
				this.innerHTML=oAgImage["longDesc"];
				$(".portfolio_text").show("slow");
			} else {
				$(".portfolio_text").hide("slow");
			}
		}
	});
	*/

	$(".portfolio_text").hide("slow");
	
    
    //$("#ag_picture").show("fast");
    
    if (iPage == null) {
        iPage = "";
    }
    else 
        if (iPage == 0) {
            iPage = "";
        }
        else 
            if (iPage == 1) {
                iPage = "";
            }
    if (ag_picture) {
        sImage = this.pics[iPicture]["img" + iPage];
        ag_picture.innerHTML = "<img id='ag_pic' height='500' src='" + sImage + "'>";
    }
    if (ag_text) {
        sHTML = this.pics[iPicture]["overlay_html"];
        ag_text.innerHTML = sHTML;
    }
    thisPic = this.pics[iPicture]["img2"];
    sPicPages = ""
    if (thisPic) {
        sPicPages = "<center><b>Other Pictures At This Site</b><br>";
        sPicPages += "<table align='center'><tr>";
        xy = 0;
        for (var x = 1; x <= 10; x++) {
            thisPic = this.pics[iPicture]["img" + x];
            thisTxt = this.pics[iPicture]["link" + x];
            if (x == 1) {
                thisPic = this.pics[iPicture]["img"];
                thisTxt = this.pics[iPicture]["link"];
            }
            if (!thisTxt) {
                thisTxt = "";
            }
            if (thisPic) {
                xy++;
                sPicPages += "<a class='ag_picturepage' href='#' align='center'>";
                sPicPages += "<img height='50'  width='85'  alt='" + sPicGallery + "' title='" + sPicGallery + "'  src='" + thisPic + "' border='0'  onclick='showImage(" + iPicture + "," + x + ");';\" src='" + thisPic + "'><br>" + thisTxt + "";
                sPicPages += "</a>";
                if (xy == 4) {
                    xy = 0;
                    sPicPages += "</tr><tr>";
                }
            }
        }
        sPicPages += "</tr></table></center>";
    }
    if (ag_picpages) {
        ag_picpages.innerHTML = sPicPages;
    }

	if ($("#watermark_layer") && $("#watermark_layer")[0] && $("#watermark_layer")[0].style && $("#watermark_layer")[0].style.backgroundImage) {
		//$("#watermark_layer")[0].style.backgroundImage = sImage;
	}
    
    //$("#ag_picture").show("slow");


}

ActiveGallery.prototype.slideThumbs = function(iSlideAmount){

    iNewSlideAmount = parseInt(this.SlideAmount) + parseInt(iSlideAmount);
    if (iNewSlideAmount > -1) {
        $("#click_prev").animate({
            opacity: "0"
        }, "650", "swing");
    }
    else {
        $("#click_prev").animate({
            opacity: "1"
        }, "650", "swing");
    }
    if (iNewSlideAmount < -8) {
        $("#click_next").animate({
            opacity: "0"
        }, "650", "swing");
    }
    else {
        $("#click_next").animate({
            opacity: "1"
        }, "650", "swing");
    }
    if (iNewSlideAmount > 0) {
        return false;
    }
    if (iNewSlideAmount < -16) {
        return false;
    }
    this.SlideAmount = iNewSlideAmount;
    iPxToMove = this.SlideAmount * 79;
    var sSlideAmount = "" + parseInt(iPxToMove) + "px";
    $("#ag_thumbs").animate({
        marginLeft: sSlideAmount
    }, "650", "swing");
    
    
    return;
}

ActiveGallery.prototype.preloadThumbs = function(){
    for (var iPicture in this.pics) {
        oPicture = this.pics[iPicture];
        if (oPicture["thumb"]) {
            sThumbSrc = oPicture["thumb"];
        }
        else {
            sThumbSrc = oPicture["img"];
        }
        this.cacheImage(sThumbSrc);
    }
}

ActiveGallery.prototype.cacheImage = function(sImage){
    //var myimg=new Image();
    //myimg.src=sImage;	
    return;
}


ActiveGallery.prototype.preloadImg = function(){
    for (var iPicture in this.pics) {
        oPicture = this.pics[iPicture];
        sThumbSrc = oPicture["img"];
    }
}


ActiveGallery.prototype.createGallery = function(sGallery){
	this.currentCat=sGallery;
	//alert("Thumb Page " + this.gals.page);

    var ag_tags = $("#ag_tags")[0];
    if (!ag_tags) {
        //fireunit.ok(false, "Tags Div");
    }
    var ag_thumbs = $("#ag_thumbs")[0];
    if (!ag_thumbs) {
        //fireunit.ok(false, "Thumbs Div");
    }
    var strThumbs = "";
    var iDefImage = -1;
    var cntImg = 0;
    
    // Galleries
    strThumbs = "";
    
	
	var maxPic=(oAG.gals.page*oAG.gals.pageSize);
	var minPic=(maxPic-oAG.gals.pageSize)+1; /* Needs to be +1, because you don't start counting at zero */


	var lastPic=0;
	for (var iPicture in this.cats) {
		lastPic++;
	}
	var lastPage=Math.ceil(lastPic/oAG.gals.pageSize);
	
	$("#g_prev").show("slow");
	$("#g_next").show("slow")
	    
    var tt = 0;
    for (var iPicture in this.cats) {
        tt++;
		if (tt >= minPic && tt <= maxPic && this.cats[iPicture]["title"]!="In Progress" ) {
	        if (oAG.gals.page <= 1) {
				$("#g_prev").hide("slow");
				oAG.gals.page=1;
			}	
			if (oAG.gals.page >= lastPage ) {		
				$("#g_next").hide("slow")
				oAG.gals.page=lastPage;
				
			}
			oPicture = this.cats[iPicture];
			sPicGallery = this.cats[iPicture]["gallery"];
			sPicTitle = this.cats[iPicture]["title"];
			sPicImg="";
			if (this.pics[iPicture] && this.pics[iPicture]["img"]) {
				sPicImg = this.pics[iPicture]["img"];
			} else if (this.pics[iPicture] && this.pics[iPicture]["img2"]) {
				sPicImg = this.pics[iPicture]["img2"];
			}
			this.cacheImage(sPicImg);
			if (sPicGallery == sGallery) {
				sStyle = "border: 3px #090 solid;";
				sPicImg = this.cats[iPicture]["img"];
				this.cacheImage(sPicImg);
			}
			else {
				sStyle = "border: 3px #CCC solid;";
			}
			sStyle = "border: 3px #888 solid";
			strThumbs += "<div style='float: left; width: 140px; height: 225px; margin: 5px; font-size: 10px; '  class='pic_details' data-cat='" + sPicGallery + "'>";
			
			if (this.pics[iPicture]) {
				sThumbData = this.pics[iPicture]["thumbdata"];
				aThumbData = sThumbData.split("|");
				//if (aThumbData.length == 8) {
				if (isNumeric(aThumbData[0]) && isNumeric(aThumbData[1]) && isNumeric(aThumbData[2]) && isNumeric(aThumbData[3]) && isNumeric(aThumbData[4]) && isNumeric(aThumbData[5]) && isNumeric(aThumbData[6]) && isNumeric(aThumbData[7]) && isNumeric(aThumbData[8])) {
				//oDrag.style.left = aThumbData[0] + "px";
				//oDrag.style.top = aThumbData[1] + "px";
				//oDrag.style.width = aThumbData[2] + "px";
				//oDrag.style.height = aThumbData[3] + "px";
				//oImgThumb.style.left = aThumbData[4] + "px";
				//oImgThumb.style.top = aThumbData[5] + "px";
				//oImgThumb.style.width = aThumbData[6] + "px";
				//oImgThumb.style.height = aThumbData[7] + "px";
				//oImgThumbSrc.style.width = aThumbData[6] + "px";
				//oImgThumbSrc.style.height = aThumbData[7] + "px";
				//$("#slider").slider("value", aThumbData[8]);
				}
				//}  		
			}
			
			strThumbs += "<div style='height: 175px; overflow: hidden; " + sStyle + "'>";
			
			var iFound = 0;
			for (var iPicture2 in this.pics) {
				sCatPicGallery = this.pics[iPicture2]["gallery"];
				sCatFilePath = this.pics[iPicture2]["filepath"];
				if (sCatPicGallery == sPicGallery && iFound == 0) {
					iFound = 1;
					//  left:
					//strThumbs += "<div style=';position: relative; left: " + Math.floor(aThumbData[4]) + "px; top: " + Math.floor(aThumbData[5]) + "px; width: " + Math.floor((aThumbData[6]/100)*175) + "px;  height: " + Math.floor((aThumbData[7]/100)*175) + "px'><img id='ag_thumb_" + cntImg + "' class='ag_thumb' src='thumbnail.php?width=" + Math.floor((aThumbData[6]/100)*175) + "&height=" + (aThumbData[7]/100)*175 + "&pimg=" + this.pics[iPicture2]["thumb"] + "' onclick='oAG.showImage(" + iPicture + ",0);' height='" + (aThumbData[7]/100)*175 + "'></div>";
					
					strThumbs += "<div style=';position: relative;'><img id='ag_thumb_" + cntImg + "' class='ag_thumb' src='" + sCatFilePath + "_thumb.jpg' onclick='oAG.showImage(" + iPicture + ",0);' height='225'></div>";
				}
			}
			strThumbs += "</div>";
			strThumbs += "<span style='color: #FFF; margin: 0px; font-family: Palatino Linotype, Book Antiqua, Palatino, serif; font-size: 12px; '>" + sPicTitle + "</span>";
			strThumbs += "</div>";
		}
    }
    
    //  && !$(".pic_details")[0]
    if (ag_tags) {
        ag_tags.innerHTML = strThumbs;
    }
    $("#new_gal").click(function(){
        $("#pic_left").animate({
            opacity: "0",
            left: "-800px"
        }, 1000);
        $("#pic_right").animate({
            opacity: "0",
            left: "300px"
        }, 1000);
        $("#pic_cats_left").animate({
            opacity: "1",
            left: "0px"
        }, 1000);
        $("#pic_cats_right").animate({
            opacity: "1",
            left: "0px"
        }, 1000);
		oAG.thumbs.page=1;
    });
    $(".pic_details").click(function(){
        oAG.createThumbs(this.getAttribute("data-cat"))
        $("#pic_cats_left").animate({
            opacity: "0",
            left: "-800px"
        }, 1000);
        $("#pic_cats_right").animate({
            opacity: "0",
            left: "300px"
        }, 1000);
        $("#pic_left").animate({
            opacity: "1",
            left: "0px"
        }, 1000);
        $("#pic_right").animate({
            opacity: "1",
            left: "0px"
        }, 1000);
		oAG.thumbs.page=1;
    });
    
    
}



ActiveGallery.prototype.createThumbs = function(sGallery){
	
	//alert(sGallery);
	//alert("Thumb Page " + oAG.thumbs.page);
	
	this.currentCat=sGallery;
    var ag_tags = $("#ag_tags")[0];
    if (!ag_tags) {
        //fireunit.ok(false, "Tags Div");
    }
    var ag_thumbs = $("#ag_thumbs")[0];
    if (!ag_thumbs) {
        //fireunit.ok(false, "Thumbs Div");
    }
    var strThumbs = "";
    var iDefImage = -1;
    var cntImg = 0;

	var maxPic=oAG.thumbs.page*oAG.thumbs.pageSize;
	var minPic=maxPic-(oAG.thumbs.pageSize);

	lastPic=0;
    for (var iPicture in oAG.pics) {
		sPicGallery = oAG.pics[iPicture]["gallery"];
		//alert(sPicGallery + " " + sGallery)
		if (sPicGallery == sGallery) {
			lastPic++
		}
	}		
	//alert(lastPic);		
	var lastPage=Math.ceil(lastPic/oAG.thumbs.pageSize);
    //alert("Thumbs " + oAG.thumbs.page + " " + lastPage);
    var tt = 0;

	$("#t_prev").show("slow");
	$("#t_next").show("slow");
	
    // Thumbs
    for (var iPicture in this.pics) {
        oPicture = this.pics[iPicture];
        sPicGallery = this.pics[iPicture]["gallery"];
        if (sPicGallery == sGallery) {
			tt++;

			if (tt >= minPic && tt <= maxPic) {

		        if (oAG.thumbs.page <= 1) {
					$("#t_prev").hide("slow");
					oAG.thumbs.page=1;
				}	
				if (oAG.thumbs.page >= lastPage ) {		
					$("#t_next").hide("slow")
					oAG.thumbs.page=lastPage;
				}	
			
				cntImg++;
				if (iDefImage == -1) {
					iDefImage = iPicture;
				}
				sThumbSize = "";
				for (var iCat in this.cats) {
					sCatPicGallery = this.cats[iCat]["gallery"];
					if (sCatPicGallery == sGallery) {
						$("#d_gallery_name")[0].innerHTML = this.cats[iCat]["title"];
						$("#d_gallery_long")[0].innerHTML = this.cats[iCat]["longDesc"];
						
						if (this.cats[iCat]["architect"]!="") {
							$("#d_gallery_architect")[0].innerHTML ="<br><div style='color: #666; font-size: 12px; padding: 1px;'><i>Architect:</i></div>" + this.cats[iCat]["architect"];
						} else {
							$("#d_gallery_architect")[0].innerHTML ="";
						}
						if (this.cats[iCat]["designer"]!="") {
							$("#d_gallery_designer")[0].innerHTML = "<br><div style='color: #666; font-size: 12px; padding: 1px;'><i>Designer:</i></div>" + this.cats[iCat]["designer"];
							
						} else {
							$("#d_gallery_designer")[0].innerHTML = "";
						}
						
						sThumbSize = " width='" + this.cats[iCat]["thumbwidth"] + "' height='" + this.cats[iCat]["thumbheight"] + "' ";
					}
				}
				if (oPicture["thumb"]) {
					sThumbSrc = oPicture["thumb"];
				}
				else {
					sThumbSrc = oPicture["img"];
				}
				sThumbData = this.pics[iPicture]["thumbdata"];
				aThumbData = sThumbData.split("|");
				//if (aThumbData.length == 8) {
				if (isNumeric(aThumbData[0]) && isNumeric(aThumbData[1]) && isNumeric(aThumbData[2]) && isNumeric(aThumbData[3]) && isNumeric(aThumbData[4]) && isNumeric(aThumbData[5]) && isNumeric(aThumbData[6]) && isNumeric(aThumbData[7]) && isNumeric(aThumbData[8])) {
				//oDrag.style.left = aThumbData[0] + "px";
				//oDrag.style.top = aThumbData[1] + "px";
				//oDrag.style.width = aThumbData[2] + "px";
				//oDrag.style.height = aThumbData[3] + "px";
				//oImgThumb.style.left = aThumbData[4] + "px";
				//oImgThumb.style.top = aThumbData[5] + "px";
				//oImgThumb.style.width = aThumbData[6] + "px";
				//oImgThumb.style.height = aThumbData[7] + "px";
				//oImgThumbSrc.style.width = aThumbData[6] + "px";
				//oImgThumbSrc.style.height = aThumbData[7] + "px";
				//$("#slider").slider("value", aThumbData[8]);
				}
				//}  
				strThumbs += "<div style='width: 68px; height: 85px; margin-right: 4px;  margin-bottom: 4px; overflow: hidden; float: left;'><a href='#'>";
				strThumbs += "<div style='position: relative; left: " + Math.floor((aThumbData[4] / 100) * 85) + "px; top: " + Math.floor((aThumbData[5] / 100) * 85) + "px; width: " + Math.floor((aThumbData[6] / 100) * 85) + "px;  height: " + Math.floor((aThumbData[7] / 100) * 85) + "px'><img id='ag_thumb_" + cntImg + "' class='ag_thumb' src='thumbnail.php?width=" + Math.floor((aThumbData[6] / 100) * 85) + "&height=" + (aThumbData[7] / 100) * 85 + "&pimg=" + sThumbSrc + "' onclick='oAG.showImage(" + iPicture + ",0);' height='" + (aThumbData[7] / 100) * 85 + "'></div>";
				strThumbs += "</a></div>";
				
				sPicPages = "";
			}
		}
        if (cntImg == 20) {
            //break;
        }
    }
    if (ag_thumbs) {
        ag_thumbs.innerHTML = strThumbs;
    }
    
    if (iDefImage == -1) {
        iDefImage = 0;
    }
    
    // This selects the first image automatically
    this.showImage(iDefImage, "");
    
    
}

function thumbPrev() {
	oAG.thumbs.page=oAG.thumbs.page-1;
	oAG.createThumbs(oAG.currentCat);
}

function thumbNext(){
	oAG.thumbs.page = oAG.thumbs.page + 1;
	oAG.createThumbs(oAG.currentCat);
}

function galPrev() {
	oAG.gals.page=oAG.gals.page-1;
	oAG.createGallery(oAG.currentCat);	
}

function galNext() {
	oAG.gals.page=oAG.gals.page+1;
	oAG.createGallery(oAG.currentCat);	
}

function isNumeric(value) {
  if (value == null || !value.toString().match(/^[-]?\d*\.?\d*$/)) return false;
  return true;
}
//ActiveGallery.addItem=function(){
//	return null;
//}

oAG = new ActiveGallery();
oAG.thumbs={};
oAG.gals={};
oAG.gals.page=1;
oAG.gals.pageSize=8;
oAG.thumbs.page=1;
oAG.thumbs.pageSize=12;




