/* Phil Gleeson
	- Phoenix Media Group
--------------------------------*/
jQuery.fn.centre = function () {
    this.css("position","absolute");
    this.css("top", ( $(window).height() - this.outerHeight() + 50 ) / 2);
    this.css("left", ( $(window).width() - this.outerWidth() ) / 2);
    return this;
}
jQuery.fn.vert_centre = function() {
	this.css("position", "absolute");
	this.css("top", ( $(window).height() - this.height() - 30 ) / 2+$(window).scrollTop() + "px");
	return this;
}
jQuery.fn.parent_centre = function() {
	this.css("position", "absolute");
	this.css("top", ( $("#hero-wrapper").height() - this.outerHeight() ) / 2 );
	return this;
}
$(function() {

	$('#hero-wrapper').centre();
	$('#menu_holder').vert_centre();
	$('#text-wrapper').parent_centre();
	//$('.appeasement-wrapper').centre();

	function imageResize() {  
		var screen_width = $(window).width();	
		var screen_height = $(window).height();	
		if ( screen_width <= 1280 || screen_height <= 850) {  
			$('.image-wrapper, .hero-piece, #hero-wrapper').css('width', 768).css('height', 512);
			$('#slidePrev, #slideNext').css('width', 384).css('height', 512);
			$('.image-wrapper-img').attr('width', 768);
			$('.image-wrapper-img').attr('height', 512); 
			$('.appeasement-wrapper').css('top', 70);
			$('#hero-wrapper').centre();
		} else {
			$('.image-wrapper, .hero-piece, #hero-wrapper').css('width', 960).css('height', 640);
			$('#slidePrev, #slideNext').css('width', 480).css('height', 640);
			$('.image-wrapper-img').attr('width', 960);
			$('.image-wrapper-img').attr('height', 640); 
			$('.appeasement-wrapper').css('top', 135);
			$('#hero-wrapper').centre();
		}
	}
	
	$('#pattern_overlay').height($(document).height());

	$('ul.slider').hover(function() {
		$(this).css({ width: 250 });
		$("#menu_holder").css({width: 250 });	
	}, function() {
		$(this).css({ width: 50 });
		$("#menu_holder").css({width: 50 });
	});

	$('ul.slider li a').hover(function() {	
		$(this).stop().animate({		
			left: 0 }, {
			duration: 500,
			easing: 'easeOutCirc'
		});	
	}, function() {
		$(this).stop().animate({
			left: -230 }, {
			duration: 300,
			easing: 'easeOutQuart'
		});	
	});

	$("ul.slider").mousemove(function(e) {	
		if($("#menu_holder").height() < $("ul.slider").height()) {
			var distance = e.pageY - $("#menu_holder").offset().top;
			var percentage = distance / $("#menu_holder").height();
			var targetY = -Math.round(($("ul.slider").height() - $("#menu_holder").height()) * percentage );
			$('ul.slider').animate({top: [targetY+"px", "easeOutCirc"]}, { queue: false, duration: 400 });
		}		
	});
	
	/*---Ajax Loading---*/

	$('a.ajax').live('click', function() {				   
		
		$('ul.slider li a').removeClass('active');
		$(this).addClass('active');		

		var toLoad = $(this).attr('href');
		$('.hero-piece').fadeOut(600, function() {			
			$('.image-wrapper').cycle('destroy');
			$('.loader').remove();
			$('#hero-wrapper').prepend('<div class="loader"></div>');
			$('.loader').fadeIn(250, function() {
				loadContent();
			});				
		});
		$('ul.hero-paging').fadeOut(250);
		$('.hero-piece-description').remove();
		$('#arrow-next').hide();
		$('#arrow-prev').hide();
		$('#supersized').hide();
		$('#supersized img').remove();	

		window.location.hash = $(this).attr('rel').substr(0,$(this).attr('rel').length);
		
		function loadContent() {
			$('.hero-piece-description').hide();
			$('.hero-piece').load(toLoad + " .piece-wrapper", showNewContent);	
			$('.bg-tmp').load(toLoad + " .background img", getAttr);
		}	
		function getAttr() {
			$.slide = $('.bg-tmp').children('img').first().attr('src');
		}
		function showNewContent() {
			$('.hero-piece-description').hide();
			$('#arrow-next').hide();
			$('#arrow-prev').hide();
			$('ul.hero-paging').hide();
			$('.loader').delay(1000).fadeOut(500, function() {
				$('.hero-piece').fadeIn(500);
				$('.hero-piece-description').fadeIn(500);
				$('ul.hero-paging').fadeIn(500);
				Cufon.replace('h2, h3', { fontFamily: 'Baskerville'	});
				Cufon.replace('.appeasement-wrapper h1', { fontFamily: 'Helvetica Neue LT Pro', fontWeight: 750	});
				$("#text-wrapper").parent_centre();
				$("#text-wrapper").fadeIn(500);
				$('.image-wrapper').children('img').each(function() {
					$(this).addClass('image-wrapper-img');
				});
				imageResize();				
				$('.image-wrapper').cycle({ 
					fx:     'custom',
					sync: 0, 
					cssBefore: { 
						opacity: 0,
						display: 'block' 
					}, 
					animIn:  { 
						opacity: 1
					}, 
					animOut: {  
						opacity: 0
					}, 
					cssAfter:{
						display:'none'
					},
					speed:  800, 
					timeout: 0,
					prev: '#slidePrev',
					next: '#slideNext',
					easing: 'easeOutQuart',
					pager:  '.hero-paging', 
					pagerAnchorBuilder: function(idx, slide) { 
						return '<li><a href="#"><img src="' + slide.src + '" width="74" height="50" /></a></li>'; 
					},
					before: function onBefore() { 
						imageResize();
						$('.hero-piece-description').hide();
						$('.hero-piece-description').html('<p></p>'); 
						
						$.heroTitle = $(this).attr('title');	
						$.curSlide = $('#supersized').find('img').attr('title');
						$('.bg-tmp').children('img').each(function() {
							if( $(this).attr('title') == $.heroTitle) {
								$.newSlide = $(this).attr('src');
								$.slideTitle = $(this).attr('title');
							}
						});
						if ( $.heroTitle != $.curSlide ) {
							$.slide = $.newSlide;
							$("#supersized").children('img').fadeOut(500, function() {
								$(this).remove();
								supaSize();
							});						
						}						
						
					}, 
					after: function onAfter() {
						$('.hero-piece-description').fadeIn(400);
						$('.hero-piece-description').append('<p>' + this.alt + '</p>');
						
						if ( $(this).hasClass('link') ) {
							$('.links').find('a').each(function() {
								if( $(this).attr('alt') == $.heroTitle) {
									$.newLink = $(this).attr('href');
								}								
							});
							$('.external-link').fadeIn(300);
							$('.external-link').find('a').attr('href', $.newLink);
						} else {
							$('.external-link').fadeOut(300);						  
						}						
					}					
				});		

				var $container = $('#container');
					$container.isotope({
						itemSelector : '.element',
						getSortData : {
							category : function( $elem ) {
							return $elem.attr('data-category');
						},
						name : function ( $elem ) {
							return $elem.find('.name').text();
						}
					}
				});					  
			
				if ($('.image-wrapper') && ($('.image-wrapper').children().length == 1)) {
					$('.hero-piece-description p').html( $('.image-wrapper').find('img').attr('alt') );
					$('#slideNext').hide();
					$('#slidePrev').hide();	
					if ( $('.image-wrapper').children('img').hasClass('link') ) {
						$('.links').find('a').each(function() {
							$.newLink = $(this).attr('href');								
						});
						$('.external-link').fadeIn(300);
						$('.external-link').find('a').attr('href', $.newLink);
					} else {
					  $('.external-link').fadeOut(300);							  
					}
				} else {										
					$('#slideNext').show();
					$('#slidePrev').show();
					$(".tooltip-prev").tipsy({			
						gravity: 'e',
						fade: true
					});	
					$(".tooltip-next").tipsy({			
						gravity: 'w',
						fade: true
					});	
				}
				
				supaSize();
			});
		}	
		function supaSize() {				
			$.supersized({
				min_width: 1200,
				min_height: 800,
				vertical_center: 0,
				horizontal_center: 0,	
				slides: [ {image : $.slide} ]
			});	
			$('#supersized').delay(500).fadeIn(500);
			$('#supersized').find('img').addClass('active');
			$('#supersized').find('img').attr('title', $.slideTitle);
			$(window).trigger('load')
		}				
		return false;
	});		
	imageResize();
	
	$(window).resize(function(){
		$('#hero-wrapper').centre();
		$('#menu_holder').vert_centre();
		$('#text-wrapper').parent_centre();
		$('#pattern_overlay').height($(document).height());
		imageResize();
	});
	
    if (!window.location.hash) {
		$("#text-wrapper").fadeIn(800);
	} else {
		$("#text-wrapper").hide();
	}
	
	var hash = window.location.hash.substr(1);
	
    var rel = $('a.ajax').each(function() {
        var rel = $(this).attr('rel');
		var href = $(this).attr('href');		
		
        if(hash===rel.substr(0,rel.length)) {
            var toLoad = href;	
			
			$('ul.slider li a').removeClass('active');
			$(this).addClass('active');			
			
			$('.hero-piece').hide();
			$('.image-wrapper').cycle('destroy');
			$('.loader').remove();
			$('#hero-wrapper').prepend('<div class="loader"></div>');
			$('.loader').fadeIn(250, function() {;
				loadContent();
			});	
				
			$('.hero-piece-description').remove();
			$('#slideNext').hide();
			$('#slidePrev').hide();
			$('#supersized').hide();
			$('#supersized').find('img').remove();	
			
			function loadContent() {
				$('.hero-piece-description').hide();
				$('.hero-piece').load(toLoad + " .piece-wrapper", showNewContent);
				$('.bg-tmp').load(toLoad + " .background img", getAttr);
			}			
			function getAttr() {
				$.slide = $('.bg-tmp').children('img').first().attr('src');				
			}			
			function showNewContent() {				
				$('.hero-piece-description').hide();
				$('#arrow-next').hide();
				$('#arrow-prev').hide();
				$('ul.hero-paging').hide();
				$('.loader').delay(1000).fadeOut(500, function() {
					$('.hero-piece').fadeIn(500);
					$('.hero-piece-description').fadeIn(500);
					$('ul.hero-paging').fadeIn(500);					
					$("#text-wrapper").parent_centre();
					Cufon.replace('h2, h3', { fontFamily: 'Baskerville'	});
					Cufon.replace('.appeasement-wrapper h1', { fontFamily: 'Helvetica Neue LT Pro', fontWeight: 750	});
					$("#text-wrapper").fadeIn(500);
					$('.image-wrapper').children('img').each(function() {
						$(this).addClass('image-wrapper-img');
					});
					imageResize();					
					$('.image-wrapper').cycle({ 
						fx:     'custom',
						sync: 0, 
						cssBefore: { 
							opacity: 0,
							display: 'block' 
						}, 
						animIn:  { 
							opacity: 1
						}, 
						animOut: {  
							opacity: 0
						}, 
						cssAfter:{
							display:'none'
						},
						speed:  800, 
						timeout: 0,
						prev: '#slidePrev',
						next: '#slideNext',
						easing: 'easeOutQuart',
						pager:  '.hero-paging', 
						pagerAnchorBuilder: function(idx, slide) { 
							return '<li><a href="#"><img src="' + slide.src + '" width="74" height="50" /></a></li>'; 
						},
						before: function onBefore() { 
							imageResize();
							$('.hero-piece-description').hide();
							$('.hero-piece-description').html('<p></p>'); 
							
							$.heroTitle = $(this).attr('title');	
							$.curSlide = $('#supersized').find('img').attr('title');
							$('.bg-tmp').children('img').each(function() {
								if( $(this).attr('title') == $.heroTitle) {
									$.newSlide = $(this).attr('src');
									$.slideTitle = $(this).attr('title');
								}
							});
							if ( $.heroTitle != $.curSlide ) {
								$.slide = $.newSlide;
								$("#supersized").children('img').delay(2000).fadeOut(400, function() {
									$(this).remove();
									supaSize();
								});						
							}					
						}, 
						after: function onAfter() {
							$('.hero-piece-description').fadeIn(400);
							$('.hero-piece-description').append('<p>' + this.alt + '</p>');
							if ( $(this).hasClass('link') ) {
								$('.links').find('a').each(function() {
									if( $(this).attr('alt') == $.heroTitle) {
										$.newLink = $(this).attr('href');
									}								
								});
								$('.external-link').fadeIn(300);
								$('.external-link').find('a').attr('href', $.newLink);
							} else {
							  $('.external-link').fadeOut(300);							  
							}
						}					
					});
					
					if( rel.substr(0,rel.length) == 'portfolio' ) {	
						var $container = $('#container');
							$container.isotope({
								itemSelector : '.element',
								getSortData : {
									category : function( $elem ) {
									return $elem.attr('data-category');
								},
								name : function ( $elem ) {
									return $elem.find('.name').text();
								}
							}
						});
						var $optionSets = $('ul#sort'),
						$optionLinks = $optionSets.find('a');					
						$optionLinks.click(function(){
							var $this = $(this);
							// don't proceed if already selected
								if ( $this.hasClass('selected') ) {
								return false;
							}
							var $optionSet = $this.parents('.option-set');
							$optionSet.find('.selected').removeClass('selected');
							$this.addClass('selected');
							
							// make option object dynamically, i.e. { filter: '.my-filter-class' }
							var options = {},
							key = $optionSet.attr('data-option-key'),
							value = $this.attr('data-option-value');
							// parse 'false' as false boolean
							value = value === 'false' ? false : value;
							options[ key ] = value;
							if ( key === 'layoutMode' && typeof changeLayoutMode === 'function' ) {
								// changes in layout modes need extra logic
								changeLayoutMode( $this, options )
							} else {
								// otherwise, apply new options
								$container.isotope( options );
							}					
							return false;
						});
					}
												
					if ($('.image-wrapper') && ($('.image-wrapper').children().length == 1)) {
						$('.hero-piece-description p').html( $('.image-wrapper').find('img').attr('alt') );
						$('#slideNext').hide();
						$('#slidePrev').hide();	
						if ( $('.image-wrapper').children('img').hasClass('link') ) {
							$('.links').find('a').each(function() {
								$.newLink = $(this).attr('href');								
							});
							$('.external-link').fadeIn(300);
							$('.external-link').find('a').attr('href', $.newLink);
						} else {
						  $('.external-link').fadeOut(300);							  
						}
					} else {										
						$('#slideNext').show();
						$('#slidePrev').show();
						$(".tooltip-prev").tipsy({			
							gravity: 'e',
							fade: true
						});	
						$(".tooltip-next").tipsy({			
							gravity: 'w',
							fade: true
						});	
					}
					
					$('#supersized').find('img').first().remove();
					supaSize();					
				});
			}			
			function supaSize() {				
				$.supersized({
					min_width: 1200,
					min_height: 800,
					vertical_center: 0,					
					horizontal_center: 0,				
					slide_captions: 1,
					slides: [ {image : $.slide} ]
				});	
				$('#supersized').delay(500).fadeIn(500);
				$('#supersized').find('img').addClass('active');
				$('#supersized').find('img').attr('title', $.slideTitle);
				$(window).trigger('load');				
			}
        }		
    });
});
/* Contact Form
-----------------------*/
function submit_form() {
	$('#submitbutton').attr("disabled", true);  
	var name = $("input#name").val();
	var email = $("input#email").val();
	var phone = $("input#phone").val();
	var message = $("textarea#message").val();
	
	$('.form-wrapper').load('contact-us/contact-form.html', {'name':name, 'email':email, 'phone':phone, 'message':message, 'formid':'contact-form'}, contactLoad);
	
	function contactLoad() {
		
	}		
}
