/*
function hideLoadShow(filename, target)
{
    $("#description").stop();
    $("#description").hide("slide", { direction: "left" }, 500, load(filename,target));
}
*/

function load(filename, target)
{
    
    var static_path         = "_lib/static/";
    var portfolio_path      = "portfolio_descriptions/";
    var default_extension   = ".txt"
    
    $.ajax({
        url : static_path+portfolio_path+filename+default_extension,
        success : function (data) {
            $("#"+target).html(data);
            /* $("#description").show("slide", { direction: "right" }, 500); */
        }
    });
}
function colorize(t)
{
	if (/color/.test($(t).attr("src")) == false) {
    	var newSrc = $(t).attr("src").replace(".jpg","-color.jpg");
    	$(t).attr("src",newSrc);
    }
}

function greyscale(t)
{
	if (/color/.test($(t).attr("src")) == true) {
    	var newSrc = $(t).attr("src").replace("-color.jpg",".jpg");
    	$(t).attr("src",newSrc);
    }
}

function submitContact(who, how, what)
{
    $.ajax({
        type: "POST",
        url: "_lib/ajax/submitContact.php",
        data: "who="+who+"&how="+how+"&what="+what,
        success: function(msg)
        {

            // hide ajax loader and contact form
            $("#activeContact").hide();            
            
            // reset button
            $("#submitContact").css("background-image","url(_lib/images/button_OUT.png)");
            
            // success!
            if(msg == 0)
            {
                $("#contact").slideUp("slow");
            
                // slide in thank you text
                $("#contactThanks").slideDown("slow");
            
                //set timer to auto-close and fade in window content
                setTimeout("$('#contactThanks').slideUp('slow');",6000);
                setTimeout("$('#window').animate({opacity: '1'},2000);",6000);
            
                // reset switch
                $("#switch").attr("src","_lib/images/switch_up.jpg");
            }
            // errorz!!!!!!!
            else
            {
                var errors = Array();
                errors[1]  = "Please let us know who you are.";
                errors[2]  = "Please let us know how you would like to be contacted.";
                errors[3]  = "Interested in setting up a blog? A website for your business? Please let us know what we can do for you.";
                
                $("#errorText").html(errors[msg]);
                
                if($("#error").css("display") != "none")
                {
                    $("#errorText").effect("pulsate", { times:1 }, 500);
                }
                
                // slide in error text
                $("#error").slideDown("slow");
            }
        }
    });
}

$(document).ready(function(){
    
    //check for anchor in page URL; auto-load description
    myURL = window.location.href;
    toLoad = myURL.replace(/.*#(.*)/, "#$1");
    
	load("_default","description");
	
    /* 
    if (toLoad == myURL) {
    	load("_default","description");
    } else {
		try {
			colorize(toLoad);
        	load($(toLoad).attr("id"),"description");
        	$(toLoad).siblings(".zoom").css("visibility","visible");
        } catch(err) {
        	load("_default","description");
        }
    }
	*/
	
    // ajax contact form submit
    $("#submitContact").click(function () {
        try
        {
            $("#submitContact").css("background-image","url(_lib/images/button_IN.png)");
        }
        catch(err) { alert(err) }
		$("#sendText").show();
    	$("#sendText").css({"margin-left":"20px"});
		$("#sendText").animate({marginLeft: "20px"},300)
			   .animate({marginLeft: "100px"},{queue: false, duration: 1200})
			   .fadeOut(1000);
			   
        // $("#activeContact").show();
        submitContact($("#who").val(), $("#how").val(), $("#what").val());      
    });

    // allow use to close the thanks div
    $("#contactThanksClose").click(function () {
            $("#contactThanks").slideUp("slow");
            $("#window").animate({opacity: "1"},2000);
    });
    
    // slide toggle contact form
    $("#contactSlideContainer").click(function () {
    	if($("#switch").attr("src") == "_lib/images/switch_up.jpg") {
    		$("#switch").attr("src","_lib/images/switch_down.jpg");
    		$("#window").animate({opacity: "0.1"},2000);
    	} else {
    		$("#switch").attr("src","_lib/images/switch_up.jpg");
    		$("#window").animate({opacity: "1"},2000);
    	}
    	
    	//show "*click*"
    	$("#clickText").css({"margin-top":"-20px"});
		$("#clickText").show();
		$("#clickText").animate({marginTop: "-30px"},300)
					   .animate({marginTop: "-60px"},{queue: false, duration: 1200})
					   .fadeOut(1000);
		//$("#clickText").hide();
		
    	
        if($("#contact").is(":hidden"))
        {
            // simple slide down
            $("#contact").slideDown("slow");
        } else {
            $("#contact").slideUp("slow");
        }          
    });
            
    $("#head").bind("click", function(e){
        load("_default","description");
        $('.colorize').each( function() {
            greyscale($(this));
            $(this).siblings(".zoom").css("visibility","hidden");
        });
        document.location = "#home";
    });
    
    $(".colorize").bind("click", function(e){        
        $('.colorize').each( function() {
            greyscale($(this));
            $(this).siblings(".zoom").css("visibility","hidden");
        });

        colorize($(this));
        /* load($(this).attr("id"),"description"); */
        
        //show the preview icon onclick
        $(this).siblings(".zoom").css("visibility","visible");
    });
    
    $(".colorize").bind("mouseover", function(e){
        colorize($(this));
    });
    
    $(".colorize").bind("mouseout", function(e){
    	if ($(this).siblings(".zoom").css("visibility") == "hidden"){
        	greyscale($(this));
        }
    });
    
    $(".zoom").bind("click", function(e){
    	tb_show($(this).siblings("img").attr("alt"), "_lib/images/full_"+$(this).siblings("img").attr("id")+".jpg", null);
    });
    
    jQuery.preloadImages = function()
    {
        for(var i = 0; i<arguments.length; i++)
        {
            jQuery("<img>").attr("src", arguments[i]);
        }
    }
    
    $.preloadImages("_lib/images/button_OUT.png","_lib/images/thumb_colonialpva-color.jpg","_lib/images/thumb_cowan-color.jpg","_lib/images/thumb_gladnick-color.jpg","_lib/images/thumb_jeremymack-color.jpg","_lib/images/thumb_livylu-color.jpg","_lib/images/thumb_salonchr-color.jpg","_lib/images/switch_down.jpg","_lib/images/button_IN.png","_lib/images/close.png");
});