$(document).ready(function(){
	$('#s').focus(function(){
		$(this).val('');
	});
	$('.current_page_item').removeClass('current_page_item').addClass('back-to-homepage').html('<a href="/">Back to Exsel Homepage</a>');
	
	$('.sidebar h1.title').each(function(key, value){
		text = $(this).text().toLowerCase().replace(/\s/, "-");
		$(this).attr('id', 'sidebar-' + text );
	});
	
	var li = $('.featured-projects ul#canvas li');
	var slideTarget = 0;
	var newsli = $('.news-projects ul#newscanvas li');
	var newsslideTarget = 0;
	$(li).each(function(key, value){
		if(li.length < 2) {
			$('.-previous-button, .next-button').css('display', 'none');
		}
		var href = $('img:first',li[key]).parent().attr('href');
		var img = new Image();
		img.src = href;
		
		$(li[key]).css('width', '909px');
		firstTitle = $('.featured-photo-the-content img:first' , li[key]).attr('title');
		firstDescription = $('.featured-photo-the-content .wp-caption-text:first' , li[key]).text();
		$('.featured-photo-the-content .current-featured-title', li[key]).text(firstTitle);
		$('.featured-photo-the-content .current-featured-description', li[key]).text(firstDescription);
		featuredContentHeight = $('.featured-photo-the-content').height();
		$(img).load(function(){
			$('.current-featured-photo', li[key]).html('<img src="'+ href +'" alt=""/>');
			
			if(key == 0) {
				sliderHeight = Math.max(featuredContentHeight, img.height);
				$('.featured-projects .slider').css('height', sliderHeight);
			}
		});
	});
	$(newsli).each(function(key, value){
		if(newsli.length < 2) {
			$('.news-previous-button, .news-next-button').css('display', 'none');
		}
		var href = $('img:first',newsli[key]).parent().attr('href');
		var img = new Image();
		img.src = href;
		
		$(newsli[key]).css('width', '909px');
		firstTitle = $('.featured-photo-the-content img:first' , newsli[key]).attr('title');
		firstDescription = $('.featured-photo-the-content .wp-caption-text:first' , newsli[key]).text();
		$('.featured-photo-the-content .current-featured-title', newsli[key]).text(firstTitle);
		$('.featured-photo-the-content .current-featured-description', newsli[key]).text(firstDescription);
		featuredContentHeight = $('.featured-photo-the-content').height();
		$(img).load(function(){
			$('.current-featured-photo', newsli[key]).html('<img src="'+ href +'" alt=""/>');
			
			if(key == 0) {
				sliderHeight = Math.max(featuredContentHeight, img.height);
				$('.news-projects .slider').css('height', sliderHeight);
			}
		});
	});
	
	$('ul#canvas').css('width', li.length * 909);
	$('ul#newscanvas').css('width', newsli.length * 909);
	
	$('.previous-button').click(function(){
		slideTarget--;
		
		if(slideTarget < 0) slideTarget = li.length -1;
		slideTo(slideTarget, li, $('#canvas'));
	});
	$('.next-button').click(function(){
		slideTarget++;
		
		if(slideTarget > li.length-1) slideTarget = 0;
		slideTo(slideTarget, li, $('#canvas'));
	});
	
	
	$('.news-previous-button').click(function(){
		newsslideTarget--;
		
		if(newsslideTarget < 0) newsslideTarget = newsli.length -1;
		slideTo(newsslideTarget, newsli, $('#newscanvas'));
	});
	$('.news-next-button').click(function(){
		newsslideTarget++;
		
		if(newsslideTarget > newsli.length-1) newsslideTarget = 0;
		slideTo(newsslideTarget, newsli, $('#newscanvas'));
	});
	
	function slideTo(target, lis, canvas) {
		targetlLiFirstImgHeight = $('img:first', lis[target]).height();
		featuredContentHeight = $('.featured-photo-the-content', lis[target]).height();
		var resizeHeight = Math.max(targetlLiFirstImgHeight, featuredContentHeight);
		
		$(canvas).animate({
			left: - target * 909
		},800, 'easeInOutQuint');
		closestslider = $(canvas).closest('.slider');
		$(closestslider).animate({
			height: resizeHeight + 'px'
		},800, 'easeInOutQuint');
	}
	// canvas A img
	$('#canvas li a img, #newscanvas li a img').click(function(){
		imghref = $(this).parent().attr('href');
		img = new Image();
		img.src = imghref;
		
		photoContainer = $(this).parent().parent().parent();
		targetPhotoContainer = $(this).parent().parent();
		title = $(this).attr('title');
		copy = $('.wp-caption-text', targetPhotoContainer).text();
		
		$('.current-featured-title', photoContainer).text(title);
		$('.current-featured-description', photoContainer).text(copy);
		if ($.browser.msie) {
			$(img).ready(function(){
				targetcancas = $(photoContainer).closest('.canvas');
				targetlis = $('li', targetcancas); 
				$('.current-featured-photo', targetlis[slideTarget]).html('<img src="'+ imghref +'" alt=""/>');
				featuredContentHeight = $(photoContainer).parent().height();
				resizeHeight = Math.max(img.height, featuredContentHeight);
				closestslider = $(photoContainer).closest('.slider');
				$(closestslider).animate({
					height: resizeHeight + 'px'
				},800, 'easeInOutQuint');
			});
		}
		else {
			$(img).load(function(){
				targetcancas = $(photoContainer).closest('.canvas');
				targetlis = $('li', targetcancas); 
				$('.current-featured-photo', targetlis[slideTarget]).html('<img src="'+ imghref +'" alt=""/>');
				featuredContentHeight = $(photoContainer).parent().height();
				resizeHeight = Math.max(img.height, featuredContentHeight);
				closestslider = $(photoContainer).closest('.slider');
				$(closestslider).animate({
					height: resizeHeight + 'px'
				},800, 'easeInOutQuint');
			});
		}
		
		return false;
	});
	
	$('#news-ul li').each(function(key, value) {
		$(this).append('<a href="#" class="right news-more-link">more..</a>');
	});
	
	var lastli;
	var smallnewsimagesrc;
	$('#news-ul li .news-more-link').click(function(){
		newsli = $(this).parent();
		if(lastli && ($(lastli).index() != $(newsli).index())) {
			$('.news-image-container a', lastli).html('<img src="' + smallnewsimagesrc + '" />');
			$('.news-content-container', lastli).css('height', '90px');
			$('.news-content-container', lastli).css('width', '600px');
			$('.news-more-link', lastli).text('more..');
			lastli = null;
		}
		lastli = newsli;
		newscontentcontainer = $('.news-content-container', newsli);
		newsimagecontainer = $('.news-image-container', newsli);
		if($(newscontentcontainer).height() == '90') {
			$(newscontentcontainer).css('height', 'auto');
			$(newscontentcontainer).css('width', '320px');
			$(this).text('less..');
			smallnewsimagesrc = $('a img', newsimagecontainer).attr('src')
			bigimage = $('a', newsimagecontainer).attr('href');
			$('a', newsimagecontainer).html('<img src="' + bigimage + '" />');
		}
		else {
			$(newscontentcontainer).css('height', '90px');
			$(newscontentcontainer).css('width', '600px');
			$('.news-image-container a', lastli).html('<img src="' + smallnewsimagesrc + '" />');
			$(this).text('more..');
			
			lastli = null;
		}
		return false;	
	});
	
	$('.news-image-container a').click(function(){
		return false;
	});
		
	// direction link
	$('#direction-toggle').click(function(){
		$('#direction-container').toggle();
		
		if($('#direction-container').css('display') == 'block') $(this).html('Directions<span>(-)</span>');
		else $(this).html('Directions<span>(+)</span>');
		
		return false;
	});
	
	// service-read-more link
	$('#services-read-more').click(function(){
		$('#services-list-container').toggle();
		
		return false;
	});
	$('#client-list-link').click(function(){
		$('#clients-list ul ').toggle();
		
		if($('#clients-list ul').css('display') == 'block') $(this).html('Our Clients - Partial Listing (Past and Present)');
		else $(this).html('Our Clients');
		return false;
	});
	
});
