/* Array of rollover data. Format: [ id, image, rollover image, width, height ] */
var rollOvers = [
    [ "gun", "images/gun.jpg", "images/gun-over.jpg", "108", "56" ],
    [ "left-arrow", "images/arrow-left.png", "images/arrow-left.png", "10", "11" ],
    [ "right-arrow", "images/arrow-right.png", "images/arrow-right.png", "10", "11" ],
    [ "home", "images/logo.gif", "images/logo.gif", "10", "11" ]
]; 

var ajaxFrags = [
		 [ "gallery-1-photo-0", "gallery-1-photo-0.html" ],
		 [ "gallery-1-photo-1A", "gallery-1-photo-1A.html" ],
		 [ "gallery-1-photo-1", "gallery-1-photo-1.html" ],
		 [ "gallery-1-photo-2", "gallery-1-photo-2.html" ],
		 [ "gallery-1-photo-3", "gallery-1-photo-3.html" ],
		 [ "gallery-1-photo-4", "gallery-1-photo-4.html" ],
		 [ "gallery-1-photo-5", "gallery-1-photo-5.html" ],
		 [ "gallery-1-photo-6", "gallery-1-photo-6.html" ],
		 [ "gallery-1-photo-7", "gallery-1-photo-7.html" ],
		 [ "gallery-1-photo-8", "gallery-1-photo-8.html" ],
		 [ "gallery-1-photo-9", "gallery-1-photo-9.html" ],
		 [ "gallery-1-photo-10", "gallery-1-photo-10.html" ],
		 [ "gallery-1-photo-11", "gallery-1-photo-11.html" ],
		 [ "gallery-1-photo-12", "gallery-1-photo-12.html" ],
		 [ "gallery-1-photo-13", "gallery-1-photo-13.html" ],
		 [ "gallery-1-photo-14", "gallery-1-photo-14.html" ],
		 [ "gallery-1-photo-15", "gallery-1-photo-15.html" ],
		 [ "gallery-1-photo-16", "gallery-1-photo-16.html" ],
		 [ "gallery-1-photo-17", "gallery-1-photo-17.html" ],
		 [ "gallery-1-photo-18", "gallery-1-photo-18.html" ],
		 [ "gallery-1-photo-19", "gallery-1-photo-19.html" ],
		 [ "gallery-1-photo-20", "gallery-1-photo-20.html" ],
		 [ "gallery-2-photo-0", "gallery-2-photo-0.html" ],
		 [ "gallery-2-photo-1", "gallery-2-photo-1.html" ],
		 [ "gallery-2-photo-2", "gallery-2-photo-2.html" ],
		 [ "gallery-2-photo-3", "gallery-2-photo-3.html" ],
		 [ "gallery-2-photo-4", "gallery-2-photo-4.html" ],
		 [ "gallery-2-photo-5", "gallery-2-photo-5.html" ],
		 [ "gallery-2-photo-6", "gallery-2-photo-6.html" ],
		 [ "gallery-2-photo-7", "gallery-2-photo-7.html" ],
		 [ "gallery-2-photo-8", "gallery-2-photo-8.html" ],
		 [ "gallery-2-photo-9", "gallery-2-photo-9.html" ],
		 [ "gallery-2-photo-10", "gallery-2-photo-10.html" ],
		 [ "gallery-2-photo-11", "gallery-2-photo-11.html" ],
		 [ "gallery-2-photo-12", "gallery-2-photo-12.html" ],
		 [ "gallery-2-photo-13", "gallery-2-photo-13.html" ],
		 [ "gallery-2-photo-14", "gallery-2-photo-14.html" ],
		 [ "gallery-2-photo-15", "gallery-2-photo-15.html" ],
		 [ "gallery-2-photo-16", "gallery-2-photo-16.html" ],
		 [ "gallery-2-photo-17", "gallery-2-photo-17.html" ],
		 [ "gallery-2-photo-18", "gallery-2-photo-18.html" ],
		 [ "gallery-2-photo-19", "gallery-2-photo-19.html" ],
		 [ "gallery-2-photo-20", "gallery-2-photo-20.html" ],
		 [ "gallery-2-photo-21", "gallery-2-photo-21.html" ],
		 [ "gallery-2-photo-22", "gallery-2-photo-22.html" ],
		 [ "gallery-2-photo-23", "gallery-2-photo-23.html" ],
		 [ "gallery-2-photo-24", "gallery-2-photo-24.html" ],
		 [ "gallery-2-photo-25", "gallery-2-photo-25.html" ],
		 [ "gallery-2-photo-26", "gallery-2-photo-26.html" ],
		 [ "gallery-2-photo-27", "gallery-2-photo-27.html" ],
		 [ "gallery-3-photo-1", "gallery-3-photo-1.html" ],
		 [ "gallery-3-photo-2", "gallery-3-photo-2.html" ],
		 [ "gallery-3-photo-3", "gallery-3-photo-3.html" ],
		 [ "gallery-3-photo-4", "gallery-3-photo-4.html" ],
		 [ "gallery-3-photo-5", "gallery-3-photo-5.html" ],
		 [ "gallery-3-photo-6", "gallery-3-photo-6.html" ],
		 [ "gallery-3-photo-7", "gallery-3-photo-7.html" ],
		 [ "gallery-3-photo-8", "gallery-3-photo-8.html" ],
		 [ "gallery-3-photo-9", "gallery-3-photo-9.html" ],
		 [ "gallery-3-photo-10", "gallery-3-photo-10.html" ],
		 [ "gallery-3-photo-11", "gallery-3-photo-11.html" ],
		 [ "gallery-3-photo-12", "gallery-3-photo-12.html" ],
		 [ "gallery-3-photo-13", "gallery-3-photo-13.html" ],
		 [ "gallery-3-photo-14", "gallery-3-photo-14.html" ],
		 [ "gallery-3-photo-15", "gallery-3-photo-15.html" ],
		 [ "gallery-3-photo-16", "gallery-3-photo-16.html" ],
		 [ "gallery-3-photo-17", "gallery-3-photo-17.html" ],
		 [ "gallery-3-photo-18", "gallery-3-photo-18.html" ],
		 [ "gallery-3-photo-19", "gallery-3-photo-19.html" ],
		 [ "gallery-3-photo-20", "gallery-3-photo-20.html" ],
		 [ "gallery-3-photo-21", "gallery-3-photo-21.html" ],
		 [ "gallery-3-photo-22", "gallery-3-photo-22.html" ],
		 [ "gallery-3-photo-23", "gallery-3-photo-23.html" ],
		 [ "gallery-3-photo-24", "gallery-3-photo-24.html" ],
		 [ "gallery-3-photo-25", "gallery-3-photo-25.html" ],
		  [ "gallery-4-photo-0", "gallery-4-photo-0.html" ],
		 [ "gallery-4-photo-1", "gallery-4-photo-1.html" ],
		 [ "gallery-4-photo-2", "gallery-4-photo-2.html" ],
		 [ "gallery-4-photo-3", "gallery-4-photo-3.html" ],
		 [ "gallery-4-photo-4", "gallery-4-photo-4.html" ],
		 [ "gallery-4-photo-5", "gallery-4-photo-5.html" ],
		 [ "gallery-4-photo-6", "gallery-4-photo-6.html" ],
		 [ "gallery-4-photo-7", "gallery-4-photo-7.html" ],
		 [ "gallery-4-photo-8", "gallery-4-photo-8.html" ],
		 [ "gallery-4-photo-9", "gallery-4-photo-9.html" ],
		 [ "gallery-4-photo-10", "gallery-4-photo-10.html" ],
		 [ "gallery-4-photo-11", "gallery-4-photo-11.html" ],
		 [ "gallery-4-photo-12", "gallery-4-photo-12.html" ],
		 [ "gallery-4-photo-13", "gallery-4-photo-13.html" ],
		 [ "gallery-4-photo-14", "gallery-4-photo-14.html" ],
		 [ "gallery-4-photo-15", "gallery-4-photo-15.html" ],
		 [ "gallery-4-photo-16", "gallery-4-photo-16.html" ],
		 [ "gallery-4-photo-17", "gallery-4-photo-17.html" ],
		 [ "gallery-4-photo-18", "gallery-4-photo-18.html" ],
		 [ "gallery-4-photo-19", "gallery-4-photo-19.html" ],
		 [ "gallery-4-photo-20", "gallery-4-photo-20.html" ],
		 [ "gallery-4-photo-21", "gallery-4-photo-21.html" ],
		 [ "gallery-4-photo-22", "gallery-4-photo-22.html" ],
		 [ "gallery-4-photo-23", "gallery-4-photo-23.html" ],
		 [ "gallery-4-photo-24", "gallery-4-photo-24.html" ],
		 [ "gallery-4-photo-25", "gallery-4-photo-25.html" ],
		 [ "gallery-4-photo-26", "gallery-4-photo-26.html" ],
		 [ "gallery-4-photo-27", "gallery-4-photo-27.html" ],
		  [ "gallery-5-photo-1", "gallery-5-photo-1.html" ],
		 [ "gallery-5-photo-2", "gallery-5-photo-2.html" ],
		 [ "gallery-5-photo-3", "gallery-5-photo-3.html" ],
		 [ "gallery-5-photo-4", "gallery-5-photo-4.html" ],
		 [ "gallery-5-photo-5", "gallery-5-photo-5.html" ],
		 [ "gallery-5-photo-6", "gallery-5-photo-6.html" ],
		 [ "gallery-5-photo-7", "gallery-5-photo-7.html" ],
		 [ "gallery-5-photo-8", "gallery-5-photo-8.html" ],
		 [ "gallery-5-photo-9", "gallery-5-photo-9.html" ],
		 [ "gallery-5-photo-10", "gallery-5-photo-10.html" ],
		 [ "gallery-5-photo-11", "gallery-5-photo-11.html" ],
		 [ "gallery-5-photo-12", "gallery-5-photo-12.html" ],
		 [ "gallery-5-photo-13", "gallery-5-photo-13.html" ],
		 [ "gallery-5-photo-14", "gallery-5-photo-14.html" ],
		 [ "gallery-5-photo-15", "gallery-5-photo-15.html" ],
		 [ "gallery-5-photo-16", "gallery-5-photo-16.html" ],
		 [ "gallery-5-photo-17", "gallery-5-photo-17.html" ],
		 [ "gallery-5-photo-18", "gallery-5-photo-18.html" ],
		 [ "gallery-5-photo-19", "gallery-5-photo-19.html" ],
		 [ "gallery-5-photo-20", "gallery-5-photo-20.html" ],
		 [ "contact", "contact.html" ],
		 [ "home", "home.html" ],
		 [ "about", "about.html" ],
		 [ "clients", "clients.html" ]

];

apDivTarget = "";

/* Helper routines */

getrollOverbyid = function(idstr) {
    for (i=0;i<rollOvers.length;i++) {
	if (rollOvers[i][0] == idstr)
	    return i;
    }
    return -1;
}

getajaxFragbyid = function(idstr) {
    for (i=0;i<ajaxFrags.length;i++) {
    if (ajaxFrags[i][0] == idstr)
        return i;
    } 
    return -1;
}

/* End Helpers */

/* We just set up the CSS for the rollOvers */
setRollovers = function(obj) {
    $("a.rollOver", obj).each(function() {
	    idx = getrollOverbyid($(this).attr("id"));
	    if (idx != -1) {
		r = rollOvers[idx];
		$(this).html('<IMG ID="' + idx + '" SRC="' + r[1] + '" WIDTH="' +r[3]+ '" HEIGHT="' + r[4] +  '">');
		$(this).bind("mouseenter", function() {
			$("img", this).each(function() {
				$(this).attr("src", rollOvers[parseInt($(this).attr("id"))][2]);
			    });
		    });
		$(this).bind("mouseleave", function() {
			$("img", this).each(function() {
				$(this).attr("src", rollOvers[parseInt($(this).attr("id"))][1]);
			    });
		    });
	    }
		
    });
}

fadedOut = function() {
    $("#photos").load(apDivTarget, fadeBackIn);
}

performFade = function() {
    $("#photos").fadeTo(1200,0, fadedOut);
}

addHandlers = function() {
    setRollovers(this);
    setAjax(this);
}

fadeBackIn = function() {
    setRollovers(this);
    setAjax(this);
    $("#photos").fadeTo(1200, 1);
}

setAjax = function() {
    $("a.newPhoto").each(function() {
        
        var origHref = $(this).attr("href"); //IE6+7 add http://etc to the href att, so clean it up:
	    var cleanHref = ( origHref.indexOf("http://") != -1 ) ? origHref.substr(origHref.lastIndexOf("/")+1) : origHref; 
	    
	    idx = getajaxFragbyid(cleanHref);
	    if (!$(this).hasClass("ajaxSet")) {
	    $(this).bind("click", idx, function(e) {
	        if(e.data == -1) return false; //bail out when user clicks on category links (e.g.: javascript://)
			apDivTarget = ajaxFrags[e.data][1];
			performFade();
			return false;
		    });
		$(this).addClass("ajaxSet");
	    }
	});
}

$(document).ready(addHandlers);