var c3d = function() {
	return {
		toggleval : function(el, str) {
			//var el = 'input#q';
			var a = str;
			var t = $(el).val();
			if (t == a) {
				$(el).val('').css('color', '#000');
			} else {
				if (t.length == 0 ) {
					$(el).val(a).css('color','#ccc');
				}
			}
		}
	};
}();

$.tools.addTabEffect("mySlide", function(tabIndex) { // this points to the API
	var tabs = this;
	this.getCurrentPane().slideUp(250, function(){
		tabs.getPanes().eq(tabIndex).slideDown(250);
	});
	
});

jQuery.fn.slideFadeToggle = function(speed, easing, callback) {
        return this.animate({height: 'toggle'}, speed, easing, callback);  
      };
$(document).ready(function(){
	
   	$('input#q').focus(function(){c3d.toggleval('input#q', "Search Architecture");}).blur(function(){c3d.toggleval('input#q', "Search Architecture");});
	// Open external links in new window
	$('a[href^=http]').not('a[href*=hdr.corporate3]').attr({target: "_blank", title: "Opens in a new window"});
	
	
	//Featured Projects
	$('.cycle img').tooltip({
				effect: "slideup"
				,opacity: 0.8
				,position: ['bottom','center']
				,offset: [-62,0]
						 });
	$('div.cycle').after('<ul id="pager"></ul>').cycle({
				pager:'#pager'
				, pagerAnchorBuilder: function(idx, slide) {
					return '<li><a href="#">' + (idx + 1) + '</a></li>'; 
				} 
				,pagerEvent: 'click' 
			    ,pauseOnPagerHover: true 
				,pause: true
						 });
	/* $('ul#pager').append('<li class="norm">Featured Projects</li>'); */
	
	// Add Vertical Bars to News listing
	$('span.date', '#sidebar').after('&nbsp; |');



// News Pagination
//	$('div.newslist div').css('display','none');
	$('div.newslist div:first').show();
	$('div.newslist').after('<ul id="newspager"></ul>').after('<br />').cycle({
				timeout:0,
				speed:200,
				fx:'scrollHorz',
				pager:'#newspager'
				,startingSlide: Math.floor($(".newslist div").index($("div.curr").parent()) / 5 ) > -1 ? Math.floor($(".newslist div").index($("div.curr").parent()) / 5) : 0
				, pagerAnchorBuilder: function(idx, slide) {
					return '<li><a href="#">' + (idx + 1) + '</a></li>'; 
				} 
				,pagerEvent: 'click'
	});
	// News Columns
	if($("body").hasClass("news")) {
		var l = $("#sidebar");
		var d = $("#copy");
		if(l.height() > d.height()) {
			d.height(l.height() + 25);
		}
	}
	
	
	
	
	// AJAX pages
	
	$('a#closeOverlay').click(function(){
		doOverlay();
	});
	$('a#closeProject').click(closeProject);


	// CHECK FOR EXTANT HASH
	
	if(location.hash.length > 1) {
		var l = location.hash;
		if(location.hash.indexOf('projects') > 0) {
			var loadFromHash = true;
			doLocation('/portfolio/all/', true);
			$("div.pad:not(.project)", '#other').css('display','none');

			doProject(l.substring(1));
		} else {
			doLocation(l.substring(1), true);
		}
	}
	
	
	$('a[rel=overlay]').each(function(){
		var segment = $(this).parent().parent().parent().attr("class");
		var goto = $(this).attr('href');
		var loc = window.location.toString();
		if(loc.indexOf(segment) < 0) {
			$(this).attr('href', '/' + segment + '/' + '#' + goto);
		} else {
			$(this).attr('href', '#' + goto).click(function(){
				var goto = ($(this).attr('href').indexOf('/portfolio/specialty') >= 0) ? '/portfolio/specialty/' : $(this).attr('href').substring(1) ;
				doLocation(goto, !($('#full').is(":visible"))); 
			});
		}
	});
	$("#specArc").attr('href', "/home/#/portfolio/specialty/");
	
	// ROLLOVER MENU
	var curpage = $("ul#lowernav").attr("class");
	$("#lowernav li a").not('#lowernav li ul li a').mouseenter(function(){
		var $this = $(this).parent();
		clearTimeout(timeoutId);
		$('#lowernav li ul li').not($this.children('ul').children('li')).stop(true, true).animate({ height: 'hide'}, 500);
		$this.children('ul').children('li').animate({ height: 'show'}, 500);
	});
	
	$("#lowernav li ul").mouseenter(function(){
		clearTimeout(timeoutId);
	});
	
	$("#lowernav > li").mouseleave(function(){
		var $this = $(this);
		if($this.attr("class") != curpage) {
			
			var archdown = function(){
					$("#lowernav li ul li:visible").animate({ height: 'hide'},250);
					$('li.' + curpage + ' ul li').animate({ height: 'show'},150);
				}
			timeoutId = setTimeout(archdown,500);
		}
	});
	if(curpage === "news") {
		curpage = "home";
	}
	$('li.' + curpage + ' ul li').show(0);
	$('body').addClass('jsready');
	
	
	
});// END document.ready()

var timeoutId;
function doOverlay() {
		//alert(el.href);
		if(loadFromHash) {
		}
		
		if($('#full').is(":visible")) { // close
			$('div.cycle').cycle('resume');
			$('.over').animate({ opacity: 'toggle'}, 1000, false, function(){
				$('#full').animate({ height: 'toggle'}, 1000);
			});

			if (slimlist)
			{
			slimlist.videoElt.innerHTML = "";
			}


		} else { // open
			$('div.cycle').cycle('pause');
			$('#full').animate({ height: 'toggle'}, 1000, false, function(){
				$('.over').animate({ opacity: 'toggle'}, 1000);
			});
			
		}
		return false;
	}
	
function doLocation(goto, toggle) {
		
		$.ajax({
			type: "GET",
			url: goto,
			dataType: "html",
			success: function(html){
				//alert(html);
				$(".over div.pad div.overcontent").empty().append(html);
					doPortfolio();
				pageTracker._trackPageview(goto);
			},
			error: function(XMLHttpRequest, textStatus, errorThrown) {
				$(".over div.pad div.overcontent").empty().append('<h2>' + textStatus + '</h2>');

			}
		
		});
			if(toggle) {doOverlay();}

			if($('div.pad.project').is(":visible")) {
				$("div.pad.project", "#other").fadeOut(500,function(){
					$('div.pad:not(.project)', '#other').fadeIn(500);
					location.hash = portfoliohash;
				});
			}
		location.hash = goto;
		portfoliohash = goto;
		return false;
}
var api;
var portfoliohash;
var speed = 500;
var loadFromHash = false;
function doPortfolio(){
	
	$("div.contactsheet div a").each(function(){
		var goto = $(this).attr('href');
		if(goto.indexOf('#') < 0 ) { $(this).attr('href', '#' + goto); }
	}).hover(function(){
		$('#caption span').stop(true, true).empty().append($(this).siblings("b").html()).fadeIn(500);
	},function(){
		$('#caption span').stop(true, true).fadeOut(500, function(){$('#caption span').empty()});
	}).click(function(){
		doProject($(this).attr('href').substring(1))
	});
	
	$('a[rel=filter]').click(function(){
		doLocation($(this).attr('href'), false);
		return false;
	});
	api = $("div.contactsheet").scrollable({ 
 
    // one configuration property 
    size: 7, 
 	items: '#projects',
	api:true,
	clickable:false,
	disabledClass: "disabled"
    // ... the rest of the configuration properties 
	});
	
	if( api && api.getSize() <= api.getConf().size) {
		$('.nextPage').addClass('disabled');
	}
	
	
	$('input#portfolioq').focus(function(){c3d.toggleval('input#portfolioq', "Search Projects");}).blur(function(){c3d.toggleval('input#portfolioq', "Search Projects");});
	$('form#portfoliosearch').submit(function(){
		var q = $('#portfolioq').val();
		doLocation('/portfolio/all/?q=' + q);
		return false
	});
	
	///SET UP Contact FORM
	$('input[type=text], textarea', "#contactform").focus(function(){
		$(this).prev().fadeTo(250, .3);
	}).blur(function(){
		if($(this).val() == '') {
			$(this).prev().fadeTo(250,1);
		} else {
			$(this).prev().fadeTo(250,0);
		}
	})
	
	$('#contactform').validate( {
		submitHandler: function(form) {
			$(form).ajaxSubmit();
	    },
		messages: {
			name: "Please specify your name"
			,email: {
				required: "Please enter your e-mail address"
				,email: "Please enter a valid e-mail address"
			}
			,verify: {
				required: "Please verify your e-mail address",
				email: "Please enter a valid e-mail address"
			}
			,captcha: "Please prove that you are not a robot"
			,city	: "Please provide your mailing address"
			,state	: "Please provide your mailing address"
			,zip	: "Please provide your mailing address"
			,country: "Please provide your mailing address"
			,phone	: {
				required: "Please enter your phone number"
				,phone	: "Please enter a valid phone number"
			}
			,subject: "Please provide a subject for your message"
			,message: "Please enter your comments here"
		}
		
	} );
	
	// Sitemap Folding
	
	$(".sitemap li ul").hide().parent().prepend('<a class="expand" href="#"><img src="/images/plus.gif" alt="+"></a> ').children("a.expand").click(function(){
		var $this = $(this);
		//console.log($this.html());
		if ( $this.html() == '<img src="/images/plus.gif" alt="+">' ) {
				$this.html('<img src="/images/minus.gif" alt="-">');
		} else {
			$this.html('<img src="/images/plus.gif" alt="+">');	
		}
		$this.parent().children("ul").toggle();
		return false;
	});
	
	return api;
}
function doProject(goto) {
	portfoliohash = location.hash;
	$.ajax({
			type: "GET",
			url: goto,
			dataType: "html",
			success: function(html){
				//alert(html);
				if(loadFromHash) {speed = 0}
				$("div.pad:not(.project)", '#other').fadeOut(speed, function(){
					$('div.pad.project').fadeIn(speed);
					$(".project .foliocontent", '#other').empty().append(html).fadeIn(speed, foliocycle);
					$("#more").click(function(){
											  
						var targetOffset = $('div.more').fadeIn(1000).offset().top;
       					$('html,body').animate({scrollTop: targetOffset}, 500);
						$(this).fadeOut();
						return false;
					});
				});
				//pageTracker._trackPageview(goto);
			},
			error: function(XMLHttpRequest, textStatus, errorThrown) {
				
				$("div.pad:not(.project)", '#other').fadeOut(speed, function(){
					$('div.pad.project').fadeIn(speed);
					$(".project .foliocontent", '#other').empty().append('<h2>' + textStatus + '</h2>').fadeIn();
				});
				
			}
		
		});

		location.hash = goto;
		return false;
}
var images;
function foliocycle() {
	//console.log('foliocycle');
	images = $(".portfoliocycle").scrollable({
			size: 1,
			api:true,
			prev: '.prev',
			next: '.next',
			loop: true,
			navi: 'ul#cyclenav',
			onBeforeSeek: function(){
				if($f()) {
					$f().unload();
				}
			},
			easing:"easeOutQuad",
			speed:600
			
	});
	$(".portfoliocycle .items a").flowplayer("swf/flowplayer-3.1.1.swf", {
		clip: {
			scaling: 'fit'
		},
		logo: {
			fullscreenOnly: true
		},
		plugins: {
		   controls: {
			  progressGradient: 'medium',
			  backgroundGradient: 'medium',
			  volumeSliderColor: '#000000',
			  borderRadius: '5',
			  buttonOverColor: '#728B94',
			  timeColor: '#ffffff',
			  backgroundColor: '#454545',
			  tooltipTextColor: '#ffffff',
			  durationColor: '#b0b0b0',
			  progressColor: '#303030',
			  volumeSliderGradient: 'none',
			  timeBgColor: '#555555',
			  tooltipColor: '#5F747C',
			  bufferGradient: 'none',
			  buttonColor: '#b3b3b3',
			  sliderGradient: 'none',
			  bufferColor: '#9e9e9e',
			  sliderColor: '#8a8a8a',
			  height: 20,
			  opacity: 1.0
		   }
		}
	}); 
}



function switchEffect() {
	images = $(".portfoliocycle").scrollable({
			api:true
	});
	images.getConf().easing = 'linear';
	
	return false;
	
	
}

function toTop() {
	$('html,body').animate({scrollTop: 0}, 500)
}
function closeProject() {
	$("div.pad.project", "#other").fadeOut(500,function(){
					$('div.pad:not(.project)', '#other').fadeIn(500);
					location.hash = portfoliohash;
					if($f()) {
						$f().unload();
					}
				});
	toTop();
}


function startslide() {
	
	return 1;
	$(".newslist div").index($("span.curr").parent());
}
