var jackoMaker = function() {
	  
  var preActive = 'homeNav', active = 'homeNav', currentWorks = 'architectural';
  
  return {
	  
	  overlaysOff: function() {
		  jQuery("#screen, #contacts, #clients, #bio").hide();	
	  },
  
	  screenSize: function(size) {
		  if (size == 'big') {
			  jQuery("#screen").css({'top' : '0px', 'left' : '0px', 'width' : '100%', 'height' : '100%'});
		  } else {
			  jQuery("#screen").css({'top' : jQuery("#image_wrap > img").offset().top+'px', 'left' : jQuery("#image_wrap > img").offset().left+'px', 'width' : '671px', 'height' : '465px'});
		  }
	  },
	  
	  // Getters and Setters
	  getActive: function() { return active; },
	  setActive: function(thisActive) { 
	  	if (thisActive == 'homeNav' || thisActive == 'worksNav') preActive = thisActive; 
		active = thisActive; 
	  },	  
	  getCurrentWorks: function() { return currentWorks; },
	  setCurrentWorks: function(thisCurrentWorks) { currentWorks = thisCurrentWorks; },
	  getPreActive: function() { return preActive; }
  };
};

jQuery.noConflict()(function(){
							 
	var jacko = jackoMaker();
	currentImage = "0";
	
	jQuery.fn.jackNav = function(params) { 
		this.each(function() {
			jQuery(this).bind("mouseover", function() { jQuery("#"+jQuery(this).attr("id")+" > img").attr("src",params.on); });
			jQuery(this).bind("mouseout", function() { 
				if (params.type=='primary') {
					jQuery(this).attr("id") != jacko.getActive() ? jQuery("#"+jQuery(this).attr("id")+" > img").attr("src",params.off) : jQuery("#"+jQuery(this).attr("id")+" > img").attr("src",params.on); 
				} else {
					jQuery(this).attr("id") != jacko.getCurrentWorks()+'Nav' ? jQuery("#"+jQuery(this).attr("id")+" > img").attr("src",params.off) : jQuery("#"+jQuery(this).attr("id")+" > img").attr("src",params.on); 
				}
			});
			jQuery(this).bind("click", function() { 
				if (params.type=='primary') {
					jacko.setActive(jQuery(this).attr("id"));
					jQuery(".navButton").trigger("mouseout");
					if (jQuery(this).attr("id") == 'worksNav') jQuery('#homeNav').show();
					if (jQuery(this).attr("id") == 'homeNav') jQuery('#homeNav').hide();
				} else {
					jacko.setCurrentWorks(jQuery(this).attr("id").substring(0, jQuery(this).attr("id").indexOf('Nav')));
					jQuery(".navButton").trigger("mouseout");
					jQuery("#"+jacko.getCurrentWorks()+" img:eq(0)").trigger("click");
				}
				
			});
		});
	}; 
	
	jQuery("#worksNav").jackNav({ off: 'images/works.png', on: 'images/works_on.png', type: 'primary' });
	jQuery("#contactsNav").jackNav({ off: 'images/contacts.png', on: 'images/contacts_on.png', type: 'primary' });
	jQuery("#clientsNav").jackNav({ off: 'images/clients.png', on: 'images/clients_on.png', type: 'primary' });
	jQuery("#bioNav").jackNav({ off: 'images/bio.png', on: 'images/bio_on.png', type: 'primary' });
	jQuery("#homeNav").jackNav({ off: 'images/home.png', on: 'images/home_on.png', type: 'primary' });
	
	jQuery("#architecturalNav").jackNav({ off: 'images/architectural.png', on: 'images/architectural_on.png', type: 'works' });
	jQuery("#commercialNav").jackNav({ off: 'images/commercial.png', on: 'images/commercial_on.png', type: 'works' });
	jQuery("#fashionNav").jackNav({ off: 'images/fashion.png', on: 'images/fashion_on.png', type: 'works' });
	jQuery("#portraitsNav").jackNav({ off: 'images/portraits.png', on: 'images/portraits_on.png', type: 'works' });
	jQuery("#personalNav").jackNav({ off: 'images/personal.png', on: 'images/personal_on.png', type: 'works' });
	jQuery("#videoNav").jackNav({ off: 'images/video.png', on: 'images/video_on.png', type: 'works' });
	
	jQuery("#contacts, #clients, #bio").bind("click", function() {
		jacko.getPreActive() == 'homeNav' ? jQuery("#homeNav").trigger("click") : jQuery("#worksNav").trigger("click");
	});
	jQuery("#screen").bind("click", function() {
		jacko.overlaysOff();
		jacko.setActive(jacko.getPreActive());
		jQuery(".navButton").trigger("mouseout");
		//== 'homeNav' ? jQuery("#homeNav").trigger("muoseout") : jQuery("#worksNav").trigger("click");
	});
	

	jQuery("#worksNav").bind("click", function() {
		jacko.overlaysOff();
		jQuery("#home").hide();
		jQuery("#works").show();
		jQuery(".scrollable").hide(); 
		jQuery("#"+jacko.getCurrentWorks()).show();
		jQuery("#"+jacko.getCurrentWorks()+" img:eq("+currentImage+")").trigger("click");
		jQuery("#"+jacko.getCurrentWorks()+"Nav").trigger("click");
		jacko.screenSize('small');
	});	
	
	jQuery("#contactsNav").bind("click", function() {
		jacko.overlaysOff();
		jQuery("#screen, #contacts").show();
	});	
	
	jQuery("#clientsNav").bind("click", function() {
		jacko.overlaysOff();
		jQuery("#screen, #clients").show();
	});
	
	jQuery("#bioNav").bind("click", function() {
		jacko.overlaysOff();
		jQuery("#screen, #bio").show();
	});
	
	jQuery("#homeNav").bind("click", function() {
		jacko.overlaysOff();
		jacko.screenSize('big');
		jQuery("#home").show();
		jQuery("#works").hide();
		jQuery("#homeStrip").show();
	});
	
	jQuery(".worksNav").bind("click", function() {
		jQuery(".scrollable").hide();
		jQuery("#"+jQuery(this).attr("id").substring(0, jQuery(this).attr("id").indexOf('Nav'))).show();
	});
	
	j=0;
	for(i=0;i<imageData.images.length;i++) {
		if(imageData.images[i].featured=='yes') {
			jQuery("#homeStrip .items").append("<img class='"+imageData.images[i].category+"' src='bethumb/bethumb.aspx?h=50&w=75&img=../"+imageData.images[i].loRes+"' />");
			if(i==0) {
				jQuery("#home_image_wrap").find("img").attr("src", 'bethumb/bethumb.aspx?h=195&w=282&img=../'+imageData.images[i].loRes);
			}
		}
	}
	jQuery("#homeStrip").show();
	jQuery("#homeStrip img:eq(1)").trigger("click");
	//jQuery.timer(10000, function (timer) {
 		//nextFeature(1, timer);
 	//});
	
	
	
	for(i=0;i<imageData.images.length;i++) {
		jQuery("#"+imageData.images[i].category+" .items").append("<img src='bethumb/bethumb.aspx?h=50&w=75&img=../"+imageData.images[i].loRes+"' />");
	}
	
	jQuery("div.scrollable").scrollable({ size:11 });
	
	jQuery(".items img").click(function() {
		jacko.getActive() != 'homeNav' ? wrap = jQuery("#image_wrap") : wrap = jQuery("#home_image_wrap");	
		jacko.getActive() != 'homeNav' ? url = "images/ajax-loader.gif" : url = "images/ajax-loader-small.gif";
		wrap.find("img").attr("src", url);
		url = jQuery(this).attr("src");
		url = url.substring(url.indexOf('img=')+7);
		if(url.substring(0,5) == 'flash') {
			flowplayer("image_wrap", "flash/flowplayer-3.1.4.swf", getThisHi(url));
		} else {
			if (jacko.getActive() == 'homeNav') {
				url='bethumb/bethumb.aspx?h=195&w=282&img=../'+url;
			}
			var img = new Image();
			img.onload = function() {
				wrap.find("img").attr("src", url);
			};
			img.src = url;
		}
	});
	
	jQuery("#homeStrip img:eq("+currentImage+")").trigger("click");
	
	jQuery("#home_image_wrap img").click(function() {
		thisurl = jQuery(this).attr("src").substring(jQuery(this).attr("src").indexOf('../')+3);
		for(i=0;i<imageData.images.length;i++) {
			if (imageData.images[i].loRes == thisurl) {
				thisCategory = imageData.images[i].category;
			}
    	}
    	jQuery("#worksNav").trigger("click");
		jQuery("#"+thisCategory+"Nav").trigger("click");
		i=0;
		jQuery("#"+thisCategory+" .items img").each(function(i) {
			if(jQuery(this).attr("src").substring(jQuery(this).attr("src").indexOf('../')+3) == thisurl) {
				jQuery(this).trigger('click');
				currentImage = i;
			}
			i++;
		});
    	//window.open(url);
	});
	
	jQuery("#image_wrap img").click(function() {
		url = jQuery(this).attr("src");
		
		for(i=0;i<imageData.images.length;i++) {
			if (imageData.images[i].loRes == url) {
				url = imageData.images[i].hiRes;
			}
    	}
    	window.open(url);
	});
	
	getThisHi = function(lo) {
		for(i=0;i<imageData.images.length;i++) {
			if (imageData.images[i].loRes == lo) {
				result = imageData.images[i].hiRes;
			}
    	}
    	return result;
	}
	
});

nextFeature = function(position, timer) {
	jQuery("#homeStrip img:eq("+position+")").trigger("click");
	timer.stop();
	jQuery.timer(5000, function (timer) {
 		nextFeature(position+1, timer);
 	});
}

/*
 *
 *	jQuery Timer plugin v0.1
 *		Matt Schmidt [http://www.mattptr.net]
 *
 *	Licensed under the BSD License:
 *		http://mattptr.net/license/license.txt
 *
 */
 
 jQuery.timer = function (interval, callback)
 {

	var interval = interval || 100;

	if (!callback)
		return false;
	
	_timer = function (interval, callback) {
		this.stop = function () {
			clearInterval(self.id);
		};
		
		this.internalCallback = function () {
			callback(self);
		};
		
		this.reset = function (val) {
			if (self.id)
				clearInterval(self.id);
			
			var val = val || 100;
			this.id = setInterval(this.internalCallback, val);
		};
		
		this.interval = interval;
		this.id = setInterval(this.internalCallback, this.interval);
		
		var self = this;
	};
	
	return new _timer(interval, callback);
 };





