$(function(){
	var isIE6 = $(document.getElementsByTagName('html')[0]).hasClass('ie6');
	if (!isIE6) $("select").selectBox();

	/* after for subnav */
	var ie67 = $('.ie7 .tc-left nav ul li.selected a, .ie6 .tc-left nav ul li.selected a, .ie6 #main-header #tln li.selected, .ie7 #main-header #tln li.selected, .ie6 #main-header #trn li.selected, .ie7 #main-header #trn li.selected');
	ie67.each(function(){
		$('<span class="after"></span>').appendTo($(this));
	});
	
	
	$('.ie6 #nav-home a, .ie6 #nav-contact a').addClass('png_bg');
	$('.ie6 #harrow-left, .ie6 #harrow-right').addClass('png_bg');
	$('.ie6 .tc-left nav ul li.selected a .after').addClass('png_bg');
	
	var formitems = $('footer form input, footer form textarea');
	var noplaceholder = (Modernizr.input.placeholder) ? false : true;
	
	if(noplaceholder){
		formitems.each(function(){
			var i = $(this);

			i.val(i.attr('placeholder'))
			.focus(function() {
		        if (this.value == i.attr("placeholder")) {
		          this.value = "";
					i.removeClass('incomplete')
		        }
		    })
			.blur(function() {
		      if (this.value == "") {
		          this.value = i.attr("placeholder");
					i.addClass("incomplete")
		      }
		    }).addClass('incomplete');
		})
	}
	$('#contact-form').submit(function(){
		
		var fm = $('footer #msg');
		if (fm.length < 1) {
			fm = $('<div id="msg"></div>');
			fm.appendTo('footer');
		}
		fm.css('display','none');
		if(noplaceholder){
			formitems.each(function(){
				var i = $(this);
				if(i.val() == i.attr("placeholder")) i.val('');
			});
		}
		var superserial = $(this).serialize();
		
		if(noplaceholder){
			formitems.each(function(){
				var i = $(this);
				if(i.val() == "") i.val(i.attr("placeholder"));
			});
		}
			$.post($(this).attr('action'),superserial,function(data){
				if (data.complete==false) {
					
					$('#msg').removeClass('complete').html(data.msg).css('display','none').fadeIn('slow');
				} else {
					$('#msg').addClass('complete').html(data.msg).css('display','none').fadeIn('slow');
					$('#contact-form input[type="text"]').val('');
					$('#contact-form textarea').val('');
					$('#contact-form input[type="checkbox"]').each(function(){
						this.checked = 0;
					});
					$('#contact-form input[type="text"]').each(function(){
						this.focus();
						this.blur();
					});
					$('#contact-form textarea').each(function(){
						this.focus();
						this.blur();
					});
					if (!isIE6) {
						
						$("#contact-form select").selectBox('value', 0);
					}
					if (isIE6) {
						$('#contact-form select').each(function(){
							this.selectedIndex = 0;
						});
					}
				
					if(noplaceholder){
						formitems.each(function(){
							var i = $(this);
							if(i.val() == "") i.val(i.attr("placeholder"));
						});
					}	
				}
			},'json');
			return false;
		});
	$('#nav-contact a').click(function(e){
		e.preventDefault();
		$(window).scrollTo($('#contact-form'), 800 );
	});
	
	if ($('#home-layout').length>0) {
		$('#carousel').simpleCarousel(
				{
					view:'#indicator',
					next_link :'#harrow-right',
					prev_link :'#harrow-left', 
					duration : 800,
					interval: 7000,
					autoplay : true,
					complete:function(ob,e) {
						$('#indicator ul li').each(function(){
							var i = $(this);
							i.text('');
							var newa = $('<a href="#"></a>');
							$('#indicator,#carousel').each(function(){
								$(this).css('visibility','visible')
							})
							newa.appendTo(i);
							newa.click(function(event){
								event.preventDefault();
								e.gotoNumber($(this).parent().prevAll().length);
							});
						});
					}
				}
		);
	}
	
	$.fancybox.hideActivity();
	$("a[rel=work]").fancybox({
		'overlayColor' : '#FFFFFF',
		'overlayOpacity' : 0.8,
		'titlePosition'		: 'inside',
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'cyclic' :true,
		'changeSpeed' : 0,
		'titleShow' : false,
		'showNavArrows' : false,
		onStart : function() {
			$('#fancybox-content #harrow-left').remove();
			$('#fancybox-content #harrow-right').remove();
			$('#fancybox-content .indicator,#fancybox-content .carousel, #fancybox-content .modal-group').each(function(){
				$(this).css('visibility','hidden')
			})
		},
		onComplete  :   function() {
            $('#fancybox-content .indicator').empty();
			$('<div id="harrow-left"><a href="#">Left</a></div>').appendTo('#fancybox-content');
			$('<div id="harrow-right"><a href="#">Right</a></div>').appendTo('#fancybox-content');
			if (isIE6) DD_belatedPNG.fix("#fancybox-content #harrow-left,#fancybox-content #harrow-right");
			$('#fancybox-content .carousel').simpleCarousel(
					{
						view:'#fancybox-content .indicator',
						prev_link :'#fancybox-content #harrow-left', 
						next_link :'#fancybox-content #harrow-right', 
						duration : 800,
						interval: 7000,
						autoplay : false,
						complete:function(ob,e) {
							
							$('#fancybox-content .indicator ul li').each(function(){
								var i = $(this);
								i.text('');
								var newa = $('<a href="#"></a>');
								$('#fancybox-content .indicator,#fancybox-content .carousel, #fancybox-content .modal-group').each(function(){
									$(this).css('visibility','visible')
								});
								
								newa.appendTo(i);
								newa.click(function(event){
									event.preventDefault();
									e.gotoNumber($(this).parent().prevAll().length);
								});
							});
						}
					}
			);
        }
	});
	if ($('#work-thumbs').length>0) {
		$('#work-thumbs a').mouseenter(function(){
			$(this).find('img:eq(0)').fadeIn('slow');
		});
		$('#work-thumbs a').mouseleave(function(){
			$(this).find('img:eq(0)').fadeOut('slow');
		});
		if (document.location.hash.indexOf('#')==0) {
			var item = $('a[href="'+document.location.hash+'"]');
			if (item.length > 0) {
				item.trigger('click');
				$(window).scrollTo($('#container'), 10 );
			}
		}
		
	}
	if ($('.people').length>0) {
		
		$('.people a').mouseenter(function(){
			$(this).find('img:eq(0)').fadeIn('slow');
		});
		$('.people a').mouseleave(function(){
			$(this).find('img:eq(0)').fadeOut('slow');
		});
		$("a[rel=people]").fancybox({
			'overlayColor' : '#FFFFFF',
			'overlayOpacity' : 0.8,
			'titlePosition'		: 'inside',
			'transitionIn'		: 'none',
			'transitionOut'		: 'none',
			'cyclic' :true,
			'changeSpeed' : 0,
			'titleShow' : false
		});
	}
});
