/* Requires the Jquery Library. 2009 Alan Languirand */
$(document).ready(function(){
            //User Data
        var uData = readCookie("alanlanguirand");
        if (uData != null) {
            //figure out what to do based on cookie content
 //           var firstVisit = true;
            var firstVisit = false;
            }
        else {
            //set a cookie and set default values
            var firstVisit = true;
            createCookie("alanlanguirand", "thanks for visiting!", 24);
            }
            
            
            
            $("#loading").fadeTo(5, .8);
            textSlideUp($(".imageOverlay"));
            textSlideUp($("#menuContent"), 22);
            
            
            $(window).load(function(){
                           $("#loading").fadeOut();
                           $("#loading").html("Loading Artwork . . .");
                           $(".focusArea").fadeIn(1000, function(){
                                    if (firstVisit == true) {
                                             $("#menuContent").css({top: "0px"}).animate({top: "-22px"});
                                             //slide menu up from bottom
                                             }
                                    else {
                                            //$("#menuContent").css({top: "-22px"});
                                             //just display menu
                                    }
                                    });
                        $('.imgDL').attr({"href": $('.overlayTrigger .image').attr("src")});
                        $(".images div.thumbnail img").each(function(){
			var cont = $(this).parent();
			var ratio = parseInt($(this).css("width")) / parseInt($(this).css("height"));
			if ($(this).css("height") < cont.css("height")){
				$(this).css({
					height: cont.css("height"),
					width: (ratio * parseInt(cont.css("width"))) + "px"
					});
			}
		});
                    });
            $(".focusArea").hide();
            if (firstVisit == true) {$("#menuContent").css({top: "0px"});}
            $(".thumbContainer div.main_thumbnail").hover(function(){$(this).css({"border-color": "gray"});}, function(){$(this).css({"border-color": "white"});});
            
            $(".images img").click(function(){
                    $("#loading").show();
                    var handle = $(this).attr("src"); 
                    var newHandle = handle.replace("thumb", "photo");
                    //alert(newHandle);
                        fitImg($(".focusArea").find(".image"), document.getElementById($(this).attr("id")), 650, 698);

                    $(".focusArea").find(".image").attr({"src": $(this).attr("src")});
                    var theImage = new Image();
                    theImage.src = newHandle;
		//alert();

		//fix heights and ratios!
		
		theImage.onload = function() {
                        $("#loading").fadeOut();
                        $(".focusArea").find(".image").attr({"src": newHandle});
                        fitImg($(".focusArea").find(".image"), theImage, 650, 698);

			};            
                    
                    }); //add click event to thumbnails
            
            $(".images div.thumbnail").hover(
                function(){
                    $(this).css({ border: "1px solid black" });
                 },
                 function(){
                    $(this).css({ border: "1px solid white" });
                    }
            );

		 
		
		function fitImg(obj, newImg, maxHeight, maxWidth) {
			var ratio = newImg.width/newImg.height;
			var newHeight = maxHeight;
			var newWidth = ratio * newHeight;
			
			if (newWidth > maxWidth) {
				newWidth = maxWidth;
				newHeight = (newImg.height/newImg.width) * newWidth;
			}
			
                        $('.imgDL').attr({"href": obj.attr("src")});
			newWidth = newWidth + "px";
			newHeight = newHeight + "px";
			obj.css({
				"width": newWidth,
				"height": newHeight
				}); 
			   
		}

        });
        
	
	
        function getTxtHeight(obj){
		// height is heading height + paragraph spaces + (lines * lineheight)
		// common errors show up in the line height. Change the last
		// number in the line. estimate the height of a heading
                var headHeight = 20;
		var heads = 1;
		//get he number of characters in the text of the div
                var chars = obj.text().length;
		//figure out how many paragraphs there are
                var paragraphs = obj.html().split("<p>").length - 1;
		//estimate line height
                var charsPerLine = parseInt(obj.css("width")) / (parseInt(obj.css("font-size")) /1.55);
                
		// height is heading height + paragraph spaces + (lines * lineheight)
                var height = Math.floor((headHeight * heads) + (paragraphs * 15) + ((chars / charsPerLine) * parseInt(obj.css("line-height"))));
                //if (indexPadding){height *= indexPadding;}
                return height;
            }
            
        function textSlideUp(obj, mode){
                
                var downHeight = obj.css("height");
                var downTop = obj.css("top");
                
                if (mode == "text" || mode == null){
                        var index = getTxtHeight(obj);
			var target = $(".overlayTrigger");
                }
                else { var index = mode; var target = obj;}
		
                var upHeight = parseInt(downHeight) + index + "px";
                var upTop = parseInt(downTop) - index + "px";
                
                 target.bind("mouseenter",function(){
                     obj.stop().animate({
                         height: upHeight,
                         top: upTop
                         
                         }, 600, "easeOutCubic");
                    
                   }).bind("mouseleave",function(){
                             obj.stop().animate({
                                 height: downHeight,
                                 top: downTop
                             }, 1300, "easeOutCubic");
                     
                   }); 
                
                }
                
function createCookie(name,value,hours) {
	if (hours) {
		var date = new Date();
		date.setTime(date.getTime()+(hours*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}