var homeSpeed = 400;
var workSpeed = 400;
var workImgSpeed = 250;
var workID;
var aboutSpeed = 400;
var aboutID;
var newsSpeed = 400;
var contactSpeed = 400;

jQuery(document).ready(function(){
	// Home page sliding nav
	/*if(jQuery('ul.homeNav').length){
		jQuery('a.title').click(function(){
			if(jQuery(this).closest('li').attr('class') == 'active'){
				homeClose('ul.homeNav');
			}else{
				homeClose('ul.homeNav');
				homeOpen(jQuery(this));
			}
			return false;
		});
		jQuery('a.close').click(function(){
			homeClose('ul.homeNav');
			return false;
		});
	}*/
	
	// Work Category Projects fading
	if(jQuery('ul.workCatSelect').length){
		jQuery('ul.workCatSelect li a').mouseenter(function(){
			workID = jQuery(this).attr('id').substring(3,4);
			
			if(jQuery('ul.workCatProjects li.active').attr('id') == 'cat_'+workID){
				return false;
			}else{
				jQuery('ul.workCatSelect li a.active').removeClass('active');
				jQuery(this).addClass('active');
				
				jQuery('ul.workCatProjects li.active').fadeOut(workSpeed,function(){
					jQuery('ul.workCatProjects li.active').removeClass('active');
					jQuery('ul.workCatProjects li#cat_'+workID).addClass('active');
					jQuery('ul.workCatProjects li#cat_'+workID).fadeIn(workSpeed);
				});
			}
		});
	}
	
	// Work header project listing scrollable
	if(jQuery('div.scrollableProjects').length)
		jQuery('div.scrollableProjects').scrollable({circular:false,keyboard:false,next:'.pnext',prev:'.pprev'});
	
	// Work single image gallery, hover removes boxes
	if(jQuery('div.scrollable').length){
		var _gallerycount = 1;
		
		jQuery('div.scrollable').scrollable({circular:true,speed:0,keyboard:false});
		
		var _total = jQuery('div.scrollable ul.items li').length-2;
		jQuery('div.workHeader div.num span.total').html(_total);
		
		jQuery('div.galleryNav a.next').click(function(){
			_gallerycount++;
			if(_gallerycount >= _total+1){
				_gallerycount = 1;
			}
		});
		jQuery('div.galleryNav a.prev').click(function(){
			_gallerycount--;
			if(_gallerycount <= 0){
				_gallerycount = _total;
			}
		});
		jQuery('div.galleryNav a').click(function(){
			jQuery('div.workHeader div.num span.cur').html(_gallerycount);
			return false;
		});
		
		//jQuery('div.scrollable').scrollable({circular:true,speed:0}).navigator('div.nav');
		//jQuery('div.nav a:last').addClass('last');
		
		//galleryWidth = jQuery('div.galleryNav').width();
		//jQuery('div.galleryNav').css('left',535-(galleryWidth/2));
		
		jQuery('div.workHeader').mouseenter(function(){
			jQuery('div.workInfo').fadeOut(workImgSpeed);
		}).mouseleave(function(){
			jQuery('div.workInfo').fadeIn(workImgSpeed);
		});
	}
	
	// About sliding
	if(jQuery('div.aboutContent').length){
		jQuery('div.titleBox a').click(function(){
			aboutOpen(jQuery(this),5);
			return false;
		});
		
		jQuery('ul.aboutLuz a').click(function(){
			aboutOpen(jQuery(this),10);
			return false;
		});
		
		jQuery('a.close').click(function(){
			jQuery('a.close').fadeOut(aboutSpeed,function(){
				jQuery('a.close').css('top',0);
			});
			jQuery('div.select').fadeOut(aboutSpeed,function(){
				jQuery('div.select').css('left',0);
			});
			aboutClose();
			jQuery('.aboutFooterWrapper').animate({top:716},aboutSpeed);
			return false;
		});
	}
	
	// News sliding
	if(jQuery('ul.newsList').length){
		jQuery('a.viewmore').toggle(function(){
			var newHeight = jQuery('ul.newsList').height() + 65;
			jQuery('a.viewmore').html('View less<span>-</span>');
			jQuery('div.newsContent').animate({height:newHeight},newsSpeed);
		},function(){
			jQuery('a.viewmore').html('View more<span>+</span>');
			jQuery('div.newsContent').animate({height:215},newsSpeed);
		});
	}
	
	// News Category box height
	if(jQuery('div.newsFooter.cat').length){
		var boxHeight1 = jQuery('div.contentBox.black').height();
		var boxHeight2 = jQuery('div.contentBox.white').height();
		if(boxHeight1 > boxHeight2)
			jQuery('div.contentBox.white').height(boxHeight1);
		else
			jQuery('div.contentBox.black').height(boxHeight2);
	}
	
	// News Category display
	if(jQuery('ul.newsCatContent').length){
		jQuery('ul.newsCatContent ul').masonry();
	}
	
	// News Category sliding
	if(jQuery('ul.newsCatContent').length){
		jQuery('ul.newsCatContent .content.prior').hide();
		
		jQuery('a.title').click(function(){
			if(jQuery(this).closest('li').attr('class') == 'active'){
				homeClose('ul.newsCatContent');
			}else{
				homeClose('ul.newsCatContent');
				homeOpen(jQuery(this));
			}
			return false;
		});
		jQuery('a.close').click(function(){
			homeClose('ul.newsCatContent');
			return false;
		});
	}
	
	// Contact sliding
	if(jQuery('div.contactForm').length){
		jQuery('a.title').click(function(){
			if(jQuery('div.contactForm').hasClass('active')){
				contactClose();
			}else{
				contactOpen();
			}
			return false;
		});
		jQuery('a.general').click(function(){
			if(jQuery('div.contactForm').hasClass('active')){
				jQuery('input#subject').val('General Inquiry');
			}else{
				contactOpen();
				jQuery('input#subject').val('General Inquiry');
			}
			return false;
		});
		jQuery('a.employment').click(function(){
			if(jQuery('div.contactForm').hasClass('active')){
				jQuery('input#subject').val('Employment Inquiry');
			}else{
				contactOpen();
				jQuery('input#subject').val('Employment Inquiry');
			}
			return false;
		});
		jQuery('a.close').click(function(){
			contactClose();
			return false;
		});
		
		inputFB('#name', '* Name');
		inputFB('#email', '* Email');
		inputFB('#message', '* Message');
	}
});

function homeOpen(anchor){
	jQuery(anchor).closest('li').addClass('active');
	jQuery(anchor).siblings('div.content').slideDown(homeSpeed);
}

function homeClose(selector){
	jQuery('li.active').removeClass('active');
	jQuery(selector).find('div.content').slideUp(homeSpeed);
}

function aboutOpen(selector,size){
	aboutID = selector.attr('id').substring(size,size+1);
			
	if(!jQuery(selector).hasClass('active')){
		aboutClose();
		
		jQuery('a#aboutlink_'+aboutID).addClass('active');
		jQuery('a#name_'+aboutID).addClass('active');
		jQuery('div#about_'+aboutID).addClass('active');
		jQuery('div#about_'+aboutID).slideDown(aboutSpeed);
		cssTop = (jQuery('div#about_'+aboutID+' div.copy').height()+102)/2-22;
		
		footerTop = jQuery('div#about_'+aboutID+' div.copy').height()+102+716;
		jQuery('.aboutFooterWrapper').animate({top:footerTop},aboutSpeed);
		
		jQuery('div.select').fadeIn(aboutSpeed);
		if(aboutID == 1)
			jQuery('div.select').animate({left:125},aboutSpeed);
		else if(aboutID == 2)
			jQuery('div.select').animate({left:466},aboutSpeed);
		else if(aboutID == 3)
			jQuery('div.select').animate({left:812},aboutSpeed);
			
		jQuery('a.close').animate({top:cssTop},aboutSpeed);
		jQuery('a.close').fadeIn(aboutSpeed);
	}
}

function aboutClose(){
	jQuery('div.wrapper.active').slideUp(aboutSpeed);
	jQuery('.active').each(function(){
		if(!jQuery(this).closest('ul').hasClass('mainNav'))
			jQuery(this).removeClass('active');

	});
}

function contactOpen(){
	jQuery('div.contactForm').addClass('active');
	jQuery('div.contactForm').animate({height:370},contactSpeed);
}

function contactClose(){
	jQuery('div.contactForm').removeClass('active');
	jQuery('div.contactForm').animate({height:36},newsSpeed);
}

// Input focus/blur function
function inputFB(){
	var _selector = arguments[0];
	var _value = arguments[1];
	if(arguments.length == 3)
		var _pw = arguments[2];
	else
		var _pw = null;

	jQuery(_selector).live('focus', function(){
		if (jQuery(this).val() == _value) {
			if (_pw) {
				var _id = jQuery(this).attr('id');
				jQuery(this).parent('span').html('<input id="'+_id+'" class="text selected" name="'+_id+'" type="password" value="" />');
				jQuery(_selector).focus();
			} else {
				jQuery(this).val('');
				jQuery(this).addClass('selected');
			}
		}
	});
	jQuery(_selector).live('blur', function(){
		if (jQuery(this).val() == '') {
			if (_pw) {
				var _id = jQuery(this).attr('id');
				jQuery(this).parent('span').html('<input id="'+_id+'" class="text" name="'+_id+'" type="text" value="'+_value+'" />');
			} else {
				jQuery(this).removeClass('selected');
				jQuery(this).val(_value);
			}
		}
	});
}
