/**
 * File Description: Amalgamated custom javascript.
 *
 * @package WordPress
 * @subpackage amalgamated
 * @since amalgamated 1.0
 */

var amalgamated;
amalgamated = amalgamated || {
	/* variable definitions */
	heroWrapper:null,
	heroContainer:null,
	heroImg:null,
	heroFrame:null,
	projects:null,
	culture :null,
	profile:null,
	category:null,
	hero:function(){  // homepage hero controls
		var imagewidth, imageheight;
		amalgamated.heroWrapper = $('#hero_height');
		amalgamated.heroContainer =	$('#hero ul');
		amalgamated.heroImg = $('.hero-image:first-child');
		amalgamated.heroFrame = $(".hero_frame ");
		$.fn.cycle.defaults.speed   = 900;
		$(amalgamated.heroContainer).cycle({
			fx:      'scrollHorz',
			timeout:  7000,
			delay:  -1,
			easing:  'backinout',
			next:   '#next', 
		    prev:   '#prev',
			/*pager:  '#pages',*/
			pause:         0,
	 	   /*	pagerAnchorBuilder: function(idx, slide) { 
        		return '#pages span:eq(' + idx + ') a'; 
   			},*/
   			cssBefore: {  
			   left: 0,  
			   top:  0,  
			   width:  amalgamated.heroImg.width(),  
			   height:  amalgamated.heroImg.height(),  
			   opacity: 0, 
			   zIndex: 1 
		   }, 
		   animOut: {  
			   opacity: 0  
		   }, 
		   animIn: {  
			   left: 0,  
			   top:  0,  
			   width:  amalgamated.heroImg.width() +1,  
			   height:  amalgamated.heroImg.height() +1,  
			   opacity: 0, 
			   zIndex: 1  
		   }, 
		   cssAfter: {  
			   zIndex: 0 
		   },
		});

		function resizer() {
			imageheight = amalgamated.heroImg.height();
			imagewidth = amalgamated.heroImg.width();
			if(imageheight < 1 ){ 
				imageheight = 388;
			}
			if($(window).width() < 640 && $(window).width() > 330){
				imageheight = 440;
			}	
			if($(window).width() < 330){
				imageheight = 450;
			}				
			
			console.log(imageheight);	
					
			amalgamated.heroWrapper.animate({height: imageheight});
			amalgamated.heroContainer.animate({height: imageheight});
			amalgamated.heroFrame.animate({width:amalgamated.heroFrame.next().width()-8, height:imageheight});
 		}
		var resizeTimer = null;
		$(window).bind('resize', function() {
			if (resizeTimer) clearTimeout(resizeTimer);
			resizeTimer = setTimeout(resizer, 100);
		});
		// update the size of the fancybox layout when the window changes size.
		window.onload = function(){ resizer();}
		
		$(amalgamated.heroContainer).touchwipe({
			wipeLeft: function() {
				$(amalgamated.heroContainer).cycle("next");
			},
			wipeRight: function() {
				$(amalgamated.heroContainer).cycle("prev");
			}
		});
		
		
		$('#prev_wrap').click(function(){
			var visible = $(amalgamated.heroContainer).find('li:visible .hero-image a');
			var target 	= visible.attr('target');
			var href 	= visible.attr('href'); 
			if(target === '_blank'){
				window.open(href,'name','height=768,width=1024');
				if (window.focus) {newwindow.focus()}
			} else {
				window.location = href;
			}
			return false;
		});
		

        document.addEventListener("orientationChanged", resizer);

		
		
	},
	layerFilter:function(){
		
		
		// remove title & alt tags for links on the homepage
		//amalgamated.removeTitleAttributes();


		amalgamated.projects =	$("li.project");
		amalgamated.culture  =	$("li.collaborative_culture");	
		amalgamated.profile	 =	$("li.profile");
		amalgamated.category =	$("li.category");
		amalgamated.mainNav  =  $("ul.nav li");
		var action = '';
		
		amalgamated.mainNav.hover(
			function(){
				if($(this).hasClass('about-us')){ 
					amalgamated.faderEventHandler('profile','out');
				}
				
				if($(this).hasClass('our-approach')){
					amalgamated.faderEventHandler('culture','out');
				}
				
				if($(this).hasClass('our-work')){
					amalgamated.faderEventHandler('project','out');
				}

				if($(this).hasClass('on-our-minds')){
					amalgamated.faderEventHandler('category','out');
				}
								

			
			},
			function(){
				if($(this).hasClass('about-us')){ 
					amalgamated.faderEventHandler('profile','in');
				}
				
				if($(this).hasClass('our-approach')){
					amalgamated.faderEventHandler('culture','in');
				}
				
				if($(this).hasClass('our-work')){
					amalgamated.faderEventHandler('project','in');
				}

				if($(this).hasClass('on-our-minds')){
					amalgamated.faderEventHandler('category','in');
				}		
			}
		);
	
	
	
		amalgamated.projects.hover(
			function(){
				amalgamated.faderEventHandler('project','out');
			},
			function(){
				amalgamated.faderEventHandler('project','in');
			}
		);
		amalgamated.culture.hover(
			function(){
				amalgamated.faderEventHandler('culture','out');
			},
			function(){
				amalgamated.faderEventHandler('culture','in');
			}
		);
		amalgamated.profile.hover(
			function(){
				amalgamated.faderEventHandler('profile','out');
			},
			function(){
				amalgamated.faderEventHandler('profile','in');
			}
		);
		amalgamated.category.hover(
			function(){
				amalgamated.faderEventHandler('category','out');
			},
			function(){
				amalgamated.faderEventHandler('category','in');
			}
		);		
	},	
	faderTimer:null,
	faderEventHandler:function(o, direction){
		if(!direction){ direction = 'out';}
		if (amalgamated.faderTimer) clearTimeout(amalgamated.faderTimer);
		action = 'amalgamated.fade_'+direction+'("'+o+'")';
		amalgamated.faderTimer = setTimeout("eval(action)", 250);	
	},

	fade_out:function(obj){
		var duration = 500;
		switch(obj){
			case 'project':
				amalgamated.projects.animate({opacity:1}, duration);
				amalgamated.culture.animate({opacity:.5}, duration);
				amalgamated.profile.animate({opacity:.5}, duration);
				amalgamated.category.animate({opacity:.5}, duration);
			break;
			case 'culture':
				amalgamated.culture.animate({opacity:1}, duration);
				amalgamated.projects.animate({opacity:.5}, duration);
				amalgamated.profile.animate({opacity:.5}, duration);
				amalgamated.category.animate({opacity:.5}, duration);
			break;
			case 'profile':
				amalgamated.profile.animate({opacity:1}, duration);
				amalgamated.culture.animate({opacity:.5}, duration);
				amalgamated.projects.animate({opacity:.5}, duration);
				amalgamated.category.animate({opacity:.5}, duration);
			break;			
			case 'category':
				amalgamated.category.animate({opacity:1}, duration);
				amalgamated.culture.animate({opacity:.5}, duration);
				amalgamated.profile.animate({opacity:.5}, duration);
				amalgamated.projects.animate({opacity:.5}, duration);
			break;
		}
	},
	fade_in:function(obj){
		var duration = 500;
		switch(obj){
			case 'project':
				amalgamated.culture.animate({opacity:1}, duration);
				amalgamated.profile.animate({opacity:1}, duration);
				amalgamated.category.animate({opacity:1}, duration);
			break;
			case 'culture':
				amalgamated.projects.animate({opacity:1}, duration);
				amalgamated.profile.animate({opacity:1}, duration);
				amalgamated.category.animate({opacity:1}, duration);
			break;
			case 'profile':
				amalgamated.culture.animate({opacity:1}, duration);
				amalgamated.projects.animate({opacity:1}, duration);
				amalgamated.category.animate({opacity:1}, duration);
			break;			
			case 'category':
				amalgamated.culture.animate({opacity:1}, duration);
				amalgamated.profile.animate({opacity:1}, duration);
				amalgamated.projects.animate({opacity:1}, duration);
			break;
		}
	},
	detail:function(){
		// search the dom for images that are larger than 656px width
		// give these images the 'popped' class
		$('#the_selected_project img').each(function(index) {
			var img = new Image();
			var jqImg = $(this);
			img.src = $(this).attr('src');
			img.onload = function(){
				if(this.width > 656){ 
					if(jqImg.hasClass('alignleft')){
						jqImg.wrap('<span class="plus_wrap alignleft"><a rel="popped" class="clickPop" href="'+img.src+'"></a><span class="plus">&nbsp;</span></span>');
						jqImg.removeClass('alignleft')
					} else if(jqImg.hasClass('alignright')){ 
						jqImg.wrap('<span class="plus_wrap alignright"><a rel="popped" class="clickPop"  href="'+img.src+'"></a><span class="plus">&nbsp;</span></span>');
						jqImg.removeClass('alignright')
					} else {
						jqImg.wrap('<span class="plus_wrap"><a rel="popped" class="clickPop"  href="'+img.src+'"></a><span class="plus">&nbsp;</span></span>');
					}
				}
			}
			delete img;
		});
		// load fancybox after we've updated the DOM.
		window.onload = function(){	
			$("a[rel=popped]").fancybox({
				'autoScale'			: true,
				'overlayShow'	: true,
				'transitionIn'	: 'elastic',
				'transitionOut'	: 'elastic'
			});	
		};

	},
	about:function(){
		  window.onload = function(){
			$("a[rel=popUp]").fancybox({
				'autoScale'			: true,
				'overlayShow'	: true,
				'transitionIn'	: 'elastic',
				'transitionOut'	: 'elastic'
			});	  
	  };
	},
	profile:function(){
		window.onload = function(){	
			$("a.video").fancybox({
				'width'				: 656,
				'height'			: 400,
        		'autoScale'     	: true,
        		'transitionIn'		: 'none',
				'transitionOut'		: 'none',
				'type'				: 'iframe'
			});	
			$("a[rel=profile_img]").fancybox({
				'autoScale'			: true,
				'overlayShow'	: true,
				'transitionIn'	: 'elastic',
				'transitionOut'	: 'elastic'
			});	
		};	
	},
	tracking:function(){
	 // :)
	},
	removeTitleAttributes:function(){
		$('img').each(function(){
			$(this).attr('title', '');	
			$(this).attr('alt', '');	
		});		
	},
	init:function(){
		/*try{Typekit.load({
			loading: function() {
			// Javascript to execute when fonts start loading
			},
			active: function() {
			  // As soon as the fonts are active, fade in the example
			  // Don't fade in browsers that don't do proper opacity, like IE
			  if (jQuery.support.opacity) {
				$('.wf-active #page' ).css('visibility', 'visible').hide().fadeIn();
			  } else {
				$('.wf-active #page').css('visibility', 'visible');
			  }
			 
			},
			inactive: function() {
			// Javascript to execute when fonts become inactive
			}
		});}catch(e){}
		*/ 
		amalgamated.removeTitleAttributes();
	}
};



