/*
$('#featured-work ul').cycle({ 
    fx:         'fade', 
    speed:       2000,
    pager:      '#nav'
});*/


$(document).ready(function() {

	/* Slider */
	$('.slider').each(function() {
		
		var gallery = $(this).children('.slider .images');

		var images = gallery.children().children();
		
		var images_shown = Math.floor(gallery.width() / images.width());

		if(images_shown < images.length) {
			$(this)
				.prepend('<div class="prev">&nbsp</div>')
				.append('<div class="next">&nbsp</div>');
		}
	});
	$('.slider .prev, .slider .next').click(function() {
		var gallery = $(this).siblings('.slider .images');

		var controls = $(this).siblings('.slider .prev, .slider .next');

		var flickr = gallery[0];
		
		var images = gallery.children().children();
		
		if(flickr.current_image == undefined) {
			flickr.current_image = $(images[0]);
		}
		
		var advance = 2;
		
		var images_shown = Math.floor(gallery.width() / images.width());

		var total_images = images.length;
		
		var current_index = flickr.current_image.index();

		if($(this).hasClass('prev')) {
			
			if(current_index == 0) {
				//If we're at the beginning, go to the end minus the number of images shown.
				current_index = total_images - images_shown;
			} else {
				//If we're beyond the first image, cap it.
				current_index -= advance;
				
				if(current_index < 0) {
					current_index = 0;
				}
			}

		} else {
			
			if(current_index >= total_images - images_shown) {
				//If we move past the end, goto the beginning
				current_index = 0;
			} else {
				//cap the end off
				current_index += advance;
				
				if(current_index >= total_images) {
					current_index = total_images - images_shown;
				}
			}
		}

		flickr.current_image = images.eq(current_index);

		gallery.scrollTo(flickr.current_image, 300, {
			'axis': 'x'
		});

	}).mousedown(function() {
		return false;
	});
	
	/**
	 * EZ Slide show 
	 *
	 * @author Chad Thomas
	 * @copyright 
	 */
	$(document).ready(function() {
		
		$('.slideshow').each(function() {
			
			$(this).children('img, object, div').eq(0).siblings('img, object, div').hide();
			
			this.set_slide = function(slide, callback, end_callback) {

				var number_of_images = $(this).children('img, object').length;
				
				var end = false;
				
				if(slide < 0) {
					slide = number_of_images - 1;
				}
				if(slide >= number_of_images) {
					end = true;
					
					slide = 0;
				}
				
				var last_frame = null;
				if(slide != this.frame) {
					last_frame = $(this).children('img, object').eq(this.frame);
				}
				
				this.frame = slide;
				
				//Update counter
				$(this).children('.slideshow_counter').html((slide + 1) + ' of ' + number_of_images);
				
				//Update description
				if($(this).children('img, object').eq(this.frame).attr('flickr') != undefined) {
					$(this).children('.slideshow_description').html(
						'<a onclick="window.open(this.href); return false;" href="' + $(this).children('img, object').eq(this.frame).attr('flickr') + '">See image on Flickr</a>'
					);
				} else {
					if($(this).children('img, object').eq(this.frame).attr('alt')) {
						$(this).children('.slideshow_description').html(
							$(this).children('img, object').eq(this.frame).attr('alt').substring(0, 100)
						);	
					} else {
						$(this).children('.slideshow_description').html('');
					}
				}
				
				var slideshow = this;

				//progess slide
				if($(this).children('img, object').eq(this.frame)[0].tagName.toLowerCase() != 'object') {
					
					$(this).children('.slideshow_prev, .slideshow_next').css('right', '').css('left', '');
					
					$(this).children('img, object').eq(this.frame).hide().css('z-index', 2).fadeIn(1000, function() {

						slideshow.after_set_slide(last_frame, $(this), callback, end);

					});

				} else {
					
					//Flash object
					
					$(this).children('img, object').eq(this.frame).show();
					
					slideshow.after_set_slide(last_frame, $(this).children('img, object').eq(this.frame), callback, end);
					
					//Change the controls to be compatible with the video
					$(this).children('.slideshow_prev').css('right', '85%');
					$(this).children('.slideshow_next').css('left', '85%');
					
				}
				
				
				if(slideshow.autoresize) {
					this.adjust_resize();
				}
				
				
			}
			this.after_set_slide = function(last_frame, frame, callback, end) {
				
				//When done, hide the last image
				if(last_frame != null) {
					last_frame.hide();
				}
				
				frame.css('z-index', '0');

				if(callback != undefined) {
					callback(end);
				}
				
			}
			this.adjust_resize = function() {
				
				var frame = $(this).children('img, object').eq(this.frame);
				
				var width = frame.width();
				var height = frame.height();

				if(width > 0 && height > 0) {
					
					if(this.autoresize_offset_width > 0) {
						width += this.autoresize_offset_width;
					}
					if(this.autoresize_offset_height > 0) {
						height += this.autoresize_offset_height;
					}

					if ($.browser.msie && $.browser.version.substr(0,1)<=7) {
						$(this).parent().height(height);
					}
					
					$(this).animate({
						width: width,
						height: height
					}, 1000);

					if(this.autoAdjustParentMargin) {
						$(this).parent().animate({
							'margin-top': (height / 2) * -1,
							'margin-left': (width / 2) * -1 
						}, 1000);
					}

				} else {
					
					//Wait until image arives
					
					var slideshow = this;
					
					window.setTimeout(function() {
						slideshow.adjust_resize();
					}, 200);
					
				}
			}
			this.adjust_resize_for_all = function() {

				var maxwidth = 0;
				var maxheight = 0;
				
				var slideshow = this;
				
				$(this).children('img, object').each(function() {
					
					var width = $(this).width();
					var height = $(this).height();

					if(width > 0 && height > 0) {
					
						if(width > maxwidth) {
							maxwidth = width;
						}
						
						if(height > maxheight) {
							maxheight = height;
						}
					
					} else {
						
						//Wait until image arives
						window.setTimeout(function() {
							slideshow.adjust_resize_for_all();
						}, 200);
						
						return false;
						
					}
					
				});
				

				if(maxwidth == 0 || maxheight == 0) {
					return;
				}

				if(this.autoresize_offset_width > 0) {
					maxwidth += this.autoresize_offset_width;
				}
				if(this.autoresize_offset_height > 0) {
					maxheight += this.autoresize_offset_height;
				}

				if ($.browser.msie && $.browser.version.substr(0,1)<=7) {
					$(this).parent().height(maxheight);
				}
				
				$(this).css({
					width: maxwidth,
					height: maxheight
				});

				if(this.autoAdjustParentMargin) {
					$(this).parent().css({
						'margin-top': (maxheight / 2) * -1,
						'margin-left': (maxwidth / 2) * -1 
					});
				}
				
			}
			this.advance = function(frames, callback, end_callback) {
				
				if(this.frame == undefined) {
					this.frame = 0;
				}

				if(this.timer != undefined) {
					window.clearTimeout(this.timer);
					this.timer = undefined
				}
				
				this.set_slide(this.frame + frames, callback, end_callback);
			}
			this.play = function() {
				
				if(this.autohide != false) {
					
					$(this).children('.slideshow_play').hide();
					$(this).children('.slideshow_prev, .slideshow_next, .slideshow_bottom, ' + 
						'.slideshow_counter, .slideshow_description, .slideshow_playpause').show();
						
				}
				                   
				var slideshow = this;
				
				this.advance(1, 
					function(end) {
						
						if(end) {
							
							$(this).children('.slideshow_playpause').removeClass('playing');

							if(slideshow.autohide != false) {
								
								$(slideshow).children('.slideshow_play').show();

								$(slideshow).children('.slideshow_prev, .slideshow_next, .slideshow_bottom, ' +
									'.slideshow_counter, .slideshow_description, .slideshow_playpause').hide();
									
							}
							
						} else {
							
							slideshow.timer = window.setTimeout(function() {
								slideshow.play();
							}, 5000);
							
						}
					});
				
				$(this).children('.slideshow_playpause').addClass('playing');
			}
			this.pause = function () {
				this.advance(0);
				
				$(this).children('.slideshow_playpause').removeClass('playing');
			}
			this.prev = function() {
				this.advance(-1);
				
				$(this).children('.slideshow_playpause').removeClass('playing');
			}
			this.next = function() {
				this.advance(1);
				
				$(this).children('.slideshow_playpause').removeClass('playing');
			}

			this.init = function() {
				
				var number_of_images = $(this).children('img, object').length;
				
				if(number_of_images > 1) {
					
					$(this).prepend( 	'<div class="slideshow_prev"><div>&nbsp</div></div>'+    
										'<div class="slideshow_next"><div>&nbsp</div></div>');
										
				}
							
				$(this).prepend( 	'<div class="slideshow_bottom">&nbsp;</div>'+    
									'<div class="slideshow_counter">&nbsp;</div>'+    
									'<div class="slideshow_description">hai</div>'+  
									'<div class="slideshow_playpause">&nbsp;</div>'+    
									'<div class="slideshow_play">&nbsp;</div>');

				//Prime
				this.advance(0);
				
				//Initialize buttons
				$(this).children('.slideshow_prev').click(function() {
					$(this).parent()[0].prev();
				});
				$(this).children('.slideshow_next').click(function() {
					$(this).parent()[0].next();
				});
				$(this).children('.slideshow_playpause').click(function() {

					var slideshow = $(this).parent()[0];

					if($(this).hasClass('playing')) {
						slideshow.pause();
					} else {
						slideshow.play();
					}

				});
				$(this).children('.slideshow_play').click(function() {
					$(this).parent()[0].play();
				});

			}
			
			this.init();
			
			//Autostart?					
			//this.play();
			
		});
	});
	
	//Lightbox
	$('a.thework').click(function() {
		
		var images = $(this).parent().parent().children('li');
		
		var index = images.index($(this).parent());
		if(index > -1) {
			$('#project_lightbox').children('.slideshow').children('img, object').eq(index).siblings('img, object').hide();
			$('#project_lightbox').children('.slideshow')[0].set_slide(index);
		}
	
		$('#project_lightbox_container, #project_lightbox_shroud').show();
		
		//$('#project_lightbox').children('.slideshow')[0].adjust_resize_for_all();
		
		
		return false;
		
	});
	$('div#project_lightbox div.close a').click(function() {
		$('#project_lightbox_container, #project_lightbox_shroud').hide();
	});
	$('div#project_lightbox .slideshow').each(function() {
		this.autohide = false;
		this.autoresize = true;
		this.autoresize_offset_width = 2;
		this.autoresize_offset_height = 39;
		this.autoAdjustParentMargin = true;
		
		//Prime
		this.advance(0);
	});
	
	//Spotlights
	$('#spotlights').each(function() {
		
		//initialize
		$(this).children('.spotlight').hide().eq(0).show();
		
		$(this).fadeIn(1000);
		/*$('#shibboleth').animate({
			'padding-top': '30px'
		}, 500);*/
		
		//Add controls
		var spotlight_control = this;
		var spotlights = $(spotlight_control).children('.spotlight');
		
		if(spotlights.length < 2) {
			return;
		}
		
		for(var i = 0; i < spotlights.length; i++) {
			
			var spotlight = $(spotlights[i])
			
			$(this).children('#spotlight_controls').append('<a href="#' + spotlight.children('a').attr('href') + '">&nbsp;&nbsp;</a>');
		}
		
		
		
		$(this).children('#spotlight_controls').children('a').eq(0).addClass('selected');
		
		$(this).children('#spotlight_controls').children('a').click(function() {
			spotlight_control.set_spotlight($(this).index());
			
			return false;
		});
		
		//Setup switcher
		this.selected_index = 0;
		
		this.set_spotlight = function(index, timer) {
			
			$(this).children('#spotlight_controls').children('a')
				.removeClass('selected')
				.eq(index).addClass('selected');

			spotlights.eq(this.selected_index).fadeOut(250, function() {
				spotlights.eq(index).fadeIn(250, function() {
					//Just as a backup
					$(this).siblings('.spotlight').hide();
				});
			});
			
			spotlight_control.selected_index = index;
			
			if(spotlight_control.timer != undefined) {
				window.clearTimeout(spotlight_control.timer);
				spotlight_control.timer = undefined;
			}
			
			if(timer) {
				spotlight_control.timer = window.setTimeout(function() {
					
					var next = spotlight_control.selected_index + 1;
					
					if(next > spotlights.length - 1) {
						next = 0;
					}
					
					spotlight_control.set_spotlight(next, true);
					
				}, 5000);
			}
		}
		
		//autoplay
		this.set_spotlight(0, true);
		
	});

	$('#contact-maincontent form').submit(function() {
	  var good = true;
	  $('input.req').each(function() {
	    	if ($(this).val() =="") {
				$(this).attr("style","border: 1px solid #ff0303");
				$("#contact-maincontent em").attr("style","color: #ff0303");
				good = false;
			}
	   });
	   if ($(".static-browse h3").html() =="please select") {
			$(".static-browse").attr("style","border: 1px solid #ff0303");
			$("#contact-maincontent em").attr("style","color: #ff0303");
			good = false;
		}
	   return good;
	});
	
	$(".static-browse li a").click(function() {
       $(".static-browse h3").html($(this).html());
	   $("#00N30000000xyP9").val($(this).html());
	});
	
	$('#news-lightbox').dialog({
		autoOpen: false,
		modal: true,
		closeText: 'close x',
		width: 350,
		height: 250
		});
	
	$('#news-callout-button').click(function() {
		$('#news-lightbox').dialog('open');
		
		return false;
	});
});