jQuery.preloadImages = function() {
	for(var i = 0; i<arguments.length; i++) {
		jQuery("<img>").attr("src", arguments[i]);
	}
}

var overlay_html = ' \
<div id="overlay"><ul id="overlay_menu"><li><a href="/" id="menu_overlay_works"></a></li><li><a href="/contact" id="menu_overlay_contact"></a></li><li><a href="/blog" id="menu_overlay_blog"></a></li></ul></div> \
<div id="project_pagination"> \
	<a href="#" id="project_prev" class="prev_pagination">‹</a> \
	<a href="#" id="project_next" class="next_pagination">›</a> \
</div> \
<div id="project"> \
	<a id="project_close">[X]</a> \
	<div id="project_content">  \
		<ul id="project_paging"> \
		</ul> \
		<div id="project_image_holder"> \
		<img src="" width="548" height="335" alt="" id="project_image" /> \
		<img src="" width="548" height="335" alt="" id="project_image_alt" /> \
		</div> \
		<div id="project_description"> \
			<h2></h2> \
			<p></p> \
			<a href="#" id="project_description_visit" target="_blank">Visit site</a> \
		</div> \
	</div> \
</div> \
';

List = {
	TOTAL : 1,
	DISPLAY : 3,
	STEP : 1,
	CURRENT_PAGE : 1,
	URL : null,
	SPEED : 400,
	WAIT : 100,
	ACTIVE : true,
	
	animateOutNext : function(i) {
		$('#cases_list li.page_'+List.STEP+':eq('+i+')').animate({left : '-1000px'}, List.SPEED);
		if (i==2) {
			List.STEP++;
			List.animateInNext(0);
			List.updatePaging();
		} else {
			setTimeout("List.animateOutNext('"+(parseInt(i)+1)+"')",List.WAIT);
		}
	},	
	animateInNext : function(i) {
		$('#cases_list li.page_'+List.STEP+':eq('+i+')').animate({left : (i*291) + 'px'}, List.SPEED, 'easeOutCirc');
		if (i==2) {
			List.updateNumbers();
		} else {
			setTimeout("List.animateInNext('"+(parseInt(i)+1)+"')",List.WAIT);
		}
	},	
	animateOutPrev : function(i) {
		$('#cases_list li.page_'+List.STEP+':eq('+i+')').animate({left : '1500px'}, List.SPEED);
		if (i == 0) {
			List.STEP--;
			List.animateInPrev(2);
			List.updatePaging();
		} else {
			setTimeout("List.animateOutPrev('"+(parseInt(i)-1)+"')",List.WAIT);
		}
	},	
	animateInPrev : function(i) {
		$('#cases_list li.page_'+List.STEP+':eq('+i+')').animate({left : (i*291) + 'px'}, List.SPEED, 'easeOutCirc');
		if (i == 0) {
			List.updateNumbers();
		} else {
			setTimeout("List.animateInPrev('"+(parseInt(i)-1)+"')",List.WAIT);
		}
	},
	init : function() {
		
		$('#case_numbers').append('<span class="tone"></span>');
		$('#case_next').hover(function() {
			if ($(this).css('opacity') < 1) return;
			$(this).animate({marginRight : '-5px'}, 200, 'swing', function() {
				$(this).animate({marginRight : '0'}, 200,'swing');
			});
		});		
		$('#case_prev').hover(function() {
			if ($(this).css('opacity') < 1) return;
			$(this).animate({marginLeft : '-5px'}, 200, 'swing', function() {
				$(this).animate({marginLeft : '0'}, 200,'swing');
			});
		});
		
		if ($('#case_list_holder').size() > 0) {
			List.TOTAL = parseInt($('#case_total').text());

			$('#case_list_holder').append('<div id="fade_right"></div>');
			$('#case_list_holder').append('<div id="fade_left"></div>');
			$('#case_prev').css({opacity:0.2});
			
			$('#case_next').click(function() {
				if (List.STEP*3 >= List.TOTAL || !List.ACTIVE) return false;
				List.ACTIVE = false;
				List.animateOutNext(0);
				return false;
				
			});
			$('#case_prev').click(function() {
				if (List.STEP == 1 || !List.ACTIVE) return false;
				List.ACTIVE = false;
				List.animateOutPrev(parseInt($('#cases_list li.page_'+List.STEP).size())-1);
				return false;
			});
		}
	},
	updatePaging : function() {
		if (List.STEP*3 >= List.TOTAL) {
			$('#case_next').css({opacity:0.2})
		} else {
			$('#case_next').css({opacity:1});
		}
		if(List.STEP > 1) {
			$('#case_prev').css({opacity:1})
		} else {
			$('#case_prev').css({opacity:0.2});
		} 
	},
	updateNumbers : function() {
		$('#case_current').text(((List.STEP*3)-2) + '-' + (List.STEP*3 > List.TOTAL ? List.TOTAL : List.STEP*3));
		List.ACTIVE = true;
	}
}

Overlay = {
	projects : eval(Projects),
	current : null,
	current_slide : 0,
	num_of_slides : 0,
	current_project : null,
	num_of_pages : 0,
	active : false,
	slideshow_active : false,
	slideshow_id : 0,
	current_slideshow : 0,
	paging_inactive :false,
	
	init : function() {
		$('a.item_link').click(function() {
			
			Overlay.current = parseInt($(this).attr('rel'));
			Overlay.num_of_pages = Overlay.projects.length;
			
			$('body').append(overlay_html);
			$('#overlay, #project, #project_pagination').hide();
			
			$('#project_image_alt').css({left : '548px'});
			if (Overlay.projects[Overlay.current].images.length > 0) {
				$('img#project_image').attr('src', Overlay.projects[Overlay.current].images[0].full);
			}
			$('#overlay').fadeIn('medium', function() {
				$('#project, #project_pagination').fadeIn();
			});
			
			Overlay.initProject(Overlay.current);
			Overlay.initPaging();
			Overlay.initPageLinks();
			
			function animatePrevArrow() {
				$('#project_prev').stop().animate({marginLeft : '-330px'}, 200, 'swing', function() {
					$(this).animate({marginLeft : '-325px'}, 200,'swing');
				});
			}			
			function animateNextArrow() {
				$('#project_next').stop().animate({marginLeft : '305px'}, 200, 'swing', function() {
					$(this).animate({marginLeft : '300px'}, 200,'swing');
				});
			}
			
			if (!Overlay.active) {
				$(document).bind('keydown', 'left',function (evt){
					if (!Overlay.prevPage()) return false;
					animatePrevArrow();
					return false;
				});
				$(document).bind('keydown', 'right',function (evt){
					if (!Overlay.nextPage()) return false;
					animateNextArrow();
					return false;
				});
				$(document).bind('keydown', 'esc',function (evt){return Overlay.close();});
				$(document).bind('keydown', 'up',function (evt){Overlay.slideshow_active=false;return Overlay.setSlide(Overlay.current_slide++ + 1);});
				$(document).bind('keydown', 'down',function (evt){Overlay.slideshow_active=false;return Overlay.setSlide(Overlay.current_slide-- - 1);});
			}

			$('#project_next').hover(function() {
				if ($(this).css('opacity') < 1) return;
				animateNextArrow();
			});		
			$('#project_prev').hover(function() {
				if ($(this).css('opacity') < 1) return;
				animatePrevArrow();
			});

			$('a#project_close').click(function() {Overlay.close()});
			Overlay.active = true;
			return false;
		});
	},
	close : function() {
		$('#project').fadeOut('medium', function() {
			$('#overlay').fadeOut('medium', function() {
				$(this).remove();
			});
			$('#project_pagination').remove();
			$(this).remove();
		});
		return false
	},	
	preload : function(i) {

		if(i+1 < Overlay.projects.length && Overlay.projects.length > 1 && Overlay.projects[i+1].images.length > 0) {
			$.preloadImages(Overlay.projects[i+1].images[0].full);
		}
		if(i > 0 && Overlay.projects[i-1].images.length > 0) {
			$.preloadImages(Overlay.projects[i-1].images[0].full);
		}
		return false
	},	
	setSlide : function(i, auto, id) {
		if (auto && (Overlay.current_slideshow != id || !Overlay.slideshow_active)) return;
		if (auto) i++;
		
		if (Overlay.num_of_slides == 1) return false;
		if(i < 0) i = Overlay.num_of_slides - 1;
		else if (i > Overlay.num_of_slides - 1) i = 0;		
		
		Overlay.current_slide = i;
		$('ul#project_paging li.current').removeClass('current');
		$('ul#project_paging li:eq('+i+')').addClass('current');
		
		$('img#project_image').fadeOut('fast', function() {
			$(this).attr('src', Overlay.current_project.images[i].full);
			$(this).fadeIn('fast');
		});
		if(Overlay.slideshow_active) {
			setTimeout('Overlay.setSlide('+ (Overlay.current_slide) +', true, ' + id + ')', 3000);
		}
		return false;
	},	
	startSlideshow : function() {
		if (Overlay.projects[Overlay.current].images.length > 1) {
			Overlay.slideshow_id++;
			Overlay.current_slideshow = Overlay.slideshow_id;
			if (!Overlay.slideshow_active) {
				Overlay.slideshow_active = true;
				setTimeout('Overlay.setSlide('+ Overlay.current_slide +', true, ' + Overlay.slideshow_id + ')', 6000);
			}
		}
	},
	initProject : function() {
		var project = Overlay.projects[Overlay.current];
		Overlay.current_project = project;
		Overlay.num_of_slides = project.images.length;
		Overlay.slideshow_active = false;
		Overlay.current_slide = 0;
		
		$('#project_description h2').html(project.title);
		$('#project_description p').html(project.content);
		if (project.url != null) {
			$('#project_description_visit').show();
			$('#project_description_visit').attr('href',project.url);
		} else {
			$('#project_description_visit').hide();
		}
		
		$('#project_description p').truncate( 200,{
	        chars: /\s/,
	        trail: [ '<a href="#" class="truncate_show"></a>...', '<a href="#" class="truncate_hide"></a>' ]
	    });
		Overlay.initContentToggle();

		$('ul#project_paging').empty();
		for(x in project.images) {
			$('ul#project_paging').append('<li>'+ (x+1) +'</li>');
			$.preloadImages(project.images[x].full);
		}
		$('ul#project_paging li').click(function() {
			Overlay.slideshow_active = false;
			Overlay.setSlide($('ul#project_paging li').index(this));
		});
		
		$('ul#project_paging li:eq(0)').addClass('current');
		Overlay.startSlideshow();
		Overlay.preload(Overlay.current);
	},
	changePage : function(dir) {
		if(dir == 'next') {
			var animLeft = '-548px';
			var cssLeft = '548px';
		} else {
			var cssLeft = '-548px';
			var animLeft = '548px';
		}
		$('#project_image').css({opacity:1});
		$('#project_image, #project_image_alt').stop();
		$('#project_image').animate({left : animLeft},'medium', function() {
			Overlay.paging_inactive = false;
		});
		$('#project_image_alt').show().css({left : cssLeft});
		if (Overlay.projects[Overlay.current].images[0] != undefined) {
			$('#project_image_alt').attr('src', Overlay.projects[Overlay.current].images[0].full);
		}
		$('#project_image_alt').animate({left : '0'},'medium', function() {
			var img = new Image();
			img.onload = function() {
				$('#project_image_alt').hide();
				$('#project_image').css({left : 0});
				$('#project_image').attr('src',$('#project_image_alt').attr('src'));
				Overlay.paging_inactive = false;
			}
			img.src = $('#project_image_alt').attr('src');
		});
		
		$('#project_description').animate({height:'80px'});
		Overlay.initProject();
		Overlay.current_slide = 0;
		Overlay.initPageLinks();
	},
	prevPage : function() {
		if(Overlay.current > 0 && !Overlay.paging_inactive) {
			Overlay.paging_inactive = true;			
			Overlay.current--;
			Overlay.initPageLinks();
			Overlay.changePage('prev');
		}
		return false;
	},
	nextPage : function() {
		if(Overlay.current < Overlay.num_of_pages-1 && !Overlay.paging_inactive) {
			Overlay.paging_inactive = true;			
			Overlay.current++;
			Overlay.initPageLinks();
			Overlay.changePage('next');
		}
		return false;
	},
	initPaging : function() {
		$('#project_prev').click(function() {
			return Overlay.prevPage();
		});
		$('#project_next').click(function() {
			return Overlay.nextPage();
		});
	},	
	initContentToggle : function() {
		$('.truncate_show').click(function() {
			$('#project_description').animate({height:'205px'});
			$('.truncate_hide').click(function() {
				$('#project_description').animate({height:'80px'});
			}
		)});
	},
	initPageLinks : function() {
		if(Overlay.current == 0) {
			$('#project_prev').css({opacity:0.3});
		} else {
			$('#project_prev').css({opacity:1});
		}
		if (Overlay.current == Overlay.num_of_pages-1) {
			$('#project_next').css({opacity:0.3});
		} else {
			$('#project_next').css({opacity:1});
		}
	}
}

Blog = {
	comments_url : '',
	init : function() {
		$('#posts .view_comments').click(function() {
			var id = $(this).attr('rel');
			if ($(this).hasClass('active')) {
				$('#comments_' + id).slideUp(function() {
					$(this).remove();
				});
				$(this).removeClass('active');
				$(this).slideDown('medium');
			} else {
				$(this).addClass('active');
				$(this).parent().parent().append('<span id="loader_' + id + '" class="loader"></span>');
				$('#post-' + id).append('<div id="comments_' + id + '"></div>');
				$('#comments_' + id).hide().load($(this).attr('href') + '?r=' + Math.random(), {}, function() {
				 	$('#loader_' + id).remove();
				 	$(this).slideDown('medium');
					Blog.comment_form(id);
				});
				
			}
			return false;
		});
	},
	submitted : false,
	comment_form : function(id) {
		
		$('#posts .comment_form').ajaxSubmit.debug = true;
		$('#posts .comment_form').ajaxForm({ 
			beforeSubmit : function() {
				$('#comment_form_' + id + ' .error').remove();
				$('#comment_form_' + id).fadeTo('fast',0.3);
			},
			success : function(responseText, statusText) {
				Blog.submitted = true;
				Blog.comment_form(id);
				$('#comments_' + id).fadeTo('medium', 0.3, function() {
					$('#comments_' + id).load($('#view_comments_' + id).attr('href') + '?r=' + Math.random(), {}, function() {
						$('#comments_' + id).fadeTo('medium', 1, function() {
							$(this).find('ul li:last').css({background : '#0f0f0f'});
							Blog.comment_form(id);
						});
					});
				});
			},
			error : function(error) {
				var match = /<p>(.*)<\/p>/.exec(error.responseText);
				// console.log(match[0]);
				$('#comment_form_' + id).append('<div class="error">' + match[0] + '</div>');
				$('#comment_form_' + id).fadeTo( 'medium', 1);
			}
		});		
	}
}

var Search = {
	DEFAULT_VALUE : 'Search',
	SEARCH_FIELD : "input#search_input_field",
	init: function() {
		if ($(Search.SEARCH_FIELD).val() == '') {
			$(Search.SEARCH_FIELD).val(Search.DEFAULT_VALUE);
		}
		$(Search.SEARCH_FIELD).focus(function() {
			if ($(this).val() == Search.DEFAULT_VALUE) {
				$(this).val('');
			}
		});	
		$(Search.SEARCH_FIELD).blur(function() {
			if ($(this).val().replace(/\s*/, '') == '') {
				$(this).val(Search.DEFAULT_VALUE);
			}
		});	
	}
}

function isdefined(variable) {return (typeof(window[variable]) == "undefined")?  false: true;}

function init() {
	Blog.init();
	List.init();
	Overlay.init();
	Search.init();
	
	$.preloadImages('/wp-content/themes/dareville/images/overlay.png');
	
	$('#contact address span').append('<span class="tone"></span>');
	
	var flashvars = {section : 'works'}
	
	if ($('#menu li:eq(1)').hasClass('current_page_item') || $('#menu li:eq(1)').hasClass('current_page_parent')) {
		flashvars.section = 'blog';
	} else if ($('#menu li:eq(2)').hasClass('current_page_item')) {
		flashvars.section = 'contact';
	}
	
	var params = {
		wmode : 'transparent',
		bgcolor : '000000'
	}
/*	$('#menu').hide();
	$('#header').prepend('<div id="header_flash"><div id="header_flash_holder"></div></div>');
	swfobject.embedSWF("/wp-content/themes/dareville/flash/header_winter.swf", "header_flash_holder", "1141", "356", "9.0.0", null, flashvars, params);
	*/
	$('#footer').prepend('<div id="footer_flash"><div id="footer_flash_holder"></div></div>');
	swfobject.embedSWF("/wp-content/themes/dareville/flash/footer.swf", "footer_flash_holder", "975", "540", "9.0.0", null, flashvars, params);
	
	// $('.post h2').append('<span class="tone"></span>');
}

$(document).ready(init);


