/**
 * @author jesse
 */

var feature_idx = 0;

$(document).ready(function() {
 	
	$('#ContentContainer').hide();
 	
	getFeatures();
	getClients();
	
	$('textarea').autogrow();
	
	$('.nextfeature').click(function() {
		feature_idx++;
		$('.fc:visible').fadeOut();
		$('#fc' + (feature_idx % featureTables.length)).fadeIn();
	});
	
	$('a.ajax').live('click', function(e) {
		
		e.preventDefault();
		SlideContent($(this).attr('href'));
	
	});
	
	$('a.submenu').live('click', function(e) {
		
		e.preventDefault();
		FadeContent($(this).attr('href'));
	
	});
	
	$('a.contact-button').click(function() {
		$('#ContactName').focus();
	});
	
	$('#MainMenu a').live('click', function() {
		
		if($('#SubMenuLabel').is(':hidden')) {
			$('#SubMenuLabel').fadeIn();
		}
		
		$('#MainMenu a img').each(function() {
			$(this).attr('src', $(this).attr('src').replace(/_on.gif/, '.gif'));
		});
		
		$(this).find('img').attr('src', $(this).find('img').attr('src').replace(/.gif/, '_on.gif'));
		var target = $(this).attr('href').split('.')[0];
		
		$('.sub:visible').fadeOut();
		
		$('#' + target + ' img').each(function() {
			$(this).attr('src', $(this).attr('src').replace(/_on.gif/, '.gif'));
			
		});
		$('#' + target + ' img:first').each(function() {
			$(this).attr('src', $(this).attr('src').replace(/.gif/, '_on.gif'));
		});
		$('#' + target).fadeIn();
	});
	
	$('#SubMenu a').live('click',function() {
		
		$('#SubMenu a img').each(function() {
			$(this).attr('src', $(this).attr('src').replace(/_on.gif/, '.gif'));
		});
		
		$(this).find('img').attr('src', $(this).find('img').attr('src').replace(/.gif/, '_on.gif'));
	});
	
	$('.work_results').click(function() {
		
		buildThumbs($(this).attr('href').split('#')[1]);
		if($('#WorkDetails').is(':visible')) {
			$('#ThumbNails').slideDown();
			$('#WorkDetails').slideUp();
		}
	});
	
	$('.showdetails').live('click',function() {
		
		var det = $(this);
		$('#ThumbNails').slideUp(200, function() {
			buildDetails(det.attr('href').split('#')[1]);
		});
		$('#WorkDetails').slideDown();
	});
	
	$('.show-thumbs').live('click', function() {
		
		$('#ThumbNails').slideDown();
		$('#WorkDetails').slideUp();
	});
	
	$('#Gallery').live('mouseover', function() {
		$('#GalleryControls').show();
		UpdateControls();
	});
	
	$('#Gallery').live('mouseout', function() {
		$('#GalleryControls').hide();
	});
	
		
	$('.rollover').live('mouseover', function() {
	    $(this).attr('src', $(this).attr('src').replace(/_on/,'').replace(/.gif/, '_on.gif'));
	});
	
	$('.rollover').live('mouseout',function() {
	    $(this).attr('src', $(this).attr('src').replace(/_on.gif/,'.gif'));
	});
	
	$('.close-content').click(function() {
		$('#ContentContainer').slideUp();
		$('#MainMenu').find('img').each(function(){
			$(this).attr('src', $(this).attr('src').replace(/_on.gif/, '.gif'));
		});
		$('.sub:visible').fadeOut();
		$('#SubMenuLabel').fadeOut();
	});
	

 });
 
 
 function SlideContent(url) {
	$('#ContentContainer').slideUp(200, function() {
		$.ajax({
		  url: url,
		  cache: true,
		  success: function(html){
		    $("#ContentContainer").html(html);
			$('#ContentContainer').slideDown();
		  }
		});
	});
 }
 
 function FadeContent(url) {
 	
	$.ajax({
	  url: url,
	  cache: true,
	  success: function(html){
	  	$('#ContentContainer').fadeOut(200, function() {
		    $("#ContentContainer").html(html);
			$('#ContentContainer').fadeIn();
		});
	  }
	});
 }
 
 var currentThumbs = [];
 var currentProject = 0;
 
 function buildThumbs(section) {
 	
	$('#ThumbNails').fadeOut(200, function() {
		
		var idx = 0;
		$('.thumb').hide();
		
		currentThumbs = [];
		currentProject = 0;
		
		var HTML = '';
		
		var client_count = clients.size();
		
		clients.each(function() {
			var i = 0;
			if($(this).attr('sections').indexOf(section) > -1) {
				i++;
			}
		});
		
		
		clients.each(function() {
	
			if($(this).attr('sections').indexOf(section) > -1) {
				HTML += '<div class="client-container"><div id="Client' + idx + '" class="thumb"><a id="Link' + idx + '" href="#' + idx + '" class="showdetails"><img id="Thumb' + idx + '" src="' + $(this).attr('tn') + '" /></a><div class="thumb-title"><span class="small-red-label">CLIENT</span> <span id="Title' + idx + '" class="work-title">' + $(this).attr('title') + '</span></div></div></div>';
				currentThumbs[idx] = $(this).attr('id');
				idx++;		
			}		
		});
			
		
		HTML += '<div class="c"></div>'
		$('#ThumbNails').html(HTML);
	
		$('#ThumbNails').fadeIn();
	});
 }
 
 function nextProject() {
	$('#WorkDetails').fadeOut(200, function() {
	 	currentProject++;
		currentProject = currentProject % currentThumbs.length;
	 	buildDetails(currentProject);
		$('#WorkDetails').fadeIn();
	});
 }
 
 function prevProject() {
	$('#WorkDetails').fadeOut(200, function() {
	 	currentProject--;
		if(currentProject < 0) {
			currentProject += currentThumbs.length;
		}
		currentProject = currentProject % currentThumbs.length;
	 	buildDetails(currentProject);
		$('#WorkDetails').fadeIn();
	});
 }
 
 var CurrentGalleryItem = 0;
 var GalleryLength = 0;
 
 function buildDetails(idx) {
	currentProject = idx;
 	var client = $(clientXML).find("client[id='" + currentThumbs[idx] + "']:first");
	
	
	if (client.length > 0) {
		var client = $(client[0]);
		$('#ClientTitle').html(client.attr('title'));
		$('#ClientType').html(client.attr('type'));
		$('#ClientRole').html(client.find('role:first').text());
		$('#ClientMoreInfo').html(client.find('moreinfo:first').text());
		
		var btnHTML = '';
		if (client.attr('btn')) {
			btnHTML = '<img src="' + client.attr('btn') + '" />';
			if(client.attr('url')) {
				btnHTML = '<a href="' + client.attr('url') + '" target="_blank">' + btnHTML + '</a>';
			}
			btnHTML = '<br/>' + btnHTML;
		}
		$('#ClientButton').html(btnHTML);
		
		CurrentGalleryItem = 0;
		GalleryLength = 0;
		
		$('#GalleryImages').animate({ 
        	left: CurrentGalleryItem * 601 * -1
    	}, 600 );
		
		var GALLERY_HTML = '<ul>';
		client.find('img').each(function() {
			GALLERY_HTML += '<li><img src="' + $(this).attr('src') + '" /></li>';
			GalleryLength++;
		});
		GALLERY_HTML += '</ul>';
		
		$('#GalleryImages').html(GALLERY_HTML);
	}
 }
 
 function UpdateControls() {
 	
	$('#MoveLeft').show();
	$('#MoveRight').show();
	
	if(CurrentGalleryItem == 0) {
		showLeft = false;
		$('#MoveLeft').hide();
	}
	
	if(CurrentGalleryItem == (GalleryLength - 1)) {
		showRight = false;
		$('#MoveRight').hide();
	}
 }
 
 function galleryLeft(){
 	if(CurrentGalleryItem > 0) {
		CurrentGalleryItem--;
		$('#GalleryImages').animate({ 
        	left: CurrentGalleryItem * 601 * -1
    	}, 600 );
	}
	UpdateControls();
 }
 
 function galleryRight(){
 	if(CurrentGalleryItem < GalleryLength - 1) {
		CurrentGalleryItem++;
		$('#GalleryImages').animate({ 
        	left: CurrentGalleryItem * 601 * -1
	    }, 600 );
	}
	UpdateControls();
 }

