$(document).ready(function(){  
	
	// This function just slows down the each loop so that I can have time to animate one item at a time.
	
    $.fn.slowEach = function(interval, callback){
        var array = this;
        if(!array.length) return;
        var i = 0;
        next();
        function next(){
            if( callback.call( array[i], i, array[i] ) !== false ){
                if( ++i < array.length ){
                    setTimeout( next, interval );
                }
            }
        }
    };

	$('a.team-two-expand').toggle(function() {
		$('div#team-hidden').show();
		$(this).css('color','#fff');
	}, function() {
	  	$('div#team-hidden').hide();
		$(this).css('color','');
	});
    
    // Calls the homepage animation

	if($('div.promo').length > 0){
		$('div#content div').css("opacity","0");
		$('div#content div').slowEach(150, function() {
			$(this).animate({'opacity':'1'},500);	
		});
		$(function() { $('div.promo').cycle({timeout: 6000, speed: 500}); }); 
	}
		
	// Loads saved image location for variant colors into main product view
	
	if($('div.color').length > 0) {
		$('.color a').click(function(){
			var ref = $(this).attr('rel');
			var tit = $(this).attr('title');
			$('.product_img').animate({'opacity':'0'}, 400, function() {
				$('.product_img').attr('src', ref).attr('alt',tit).attr('title',tit);
				$('.product_img').animate({'opacity':'1'},400);
			});
			$('.color a').each(function() {
				if($(this).hasClass('on')) {
					$(this).removeClass('on');
				}
			});
			$(this).addClass('on');
			return false;
		});	
	}
	
	if($('div.product').length > 0) {
		 $("div.product img").lazyload({         
		     placeholder : "img/layout/grey.gif"
		 });	
	}
	
	if($('div.press_content').length > 0) {
		var start_width = (($(document).width() - 40) / 2) + 'px';
		$('#modal').css('left',start_width);
		
		$('div.press_content a').click(function() {
			// Create Modal Object
			var doc_width = $(document).width();
			var doc_height = $(document).height();
			var win_width = $(window).width();
			var win_height = $(window).height();
			
			$('div.press_content').append($('<div></div>').attr('id','loading'));
			$('div#loading').css({
				background: 'url(img/layout/loader.gif) #fff no-repeat center center',
				position: 'absolute',
				left: ((win_width - 40) / 2) + 'px',
				top: ((win_height - 40) / 2) + 'px',
				width: '32px',
				height: '32px',
				padding: '4px'
			});
			
			$('div.press_content').append($('<div></div>').attr('id','modal'));
			$('div#modal').append($('<img src="" />')).css({
				background:'#fff',
				padding:'20px',
				position:'absolute',
				top:'400px',
				left:((doc_width - 40) / 2) + 'px',
				opacity:'0'
			});
			var img = "img/content/press/large/" + $(this).attr('rel');
			
			$('#modal img').attr('src', img).load(function() {
				$('div#loading').remove();
				var scale_width = $('#modal').width();
				var scale_height = $('#modal').height();
				var center_image = ((doc_width - scale_width) / 2) + 'px';
				$('#modal img').hide();

				
				$("#overlay").css({
					'display': 'block',
					'width': doc_width + 'px',
					'height': doc_height + 'px'
				});
				$("#modal").animate({
					'left': center_image,
					'top':$(window).scrollTop() + 70,
					'height': scale_height,
					'width': scale_width,
					'opacity':1
				},100, function(){
					$('#modal img').fadeIn('fast');
				});
			});
			return false;
		});
		
		$(window).scroll(function() {
			if($('#modal').length > 0) {
				$("#modal").animate({
					'top':$(window).scrollTop() + 70
				}, 50);
			}
		});
		
		$("#overlay").click(function() {
			$("#overlay").hide();
			$("#modal").remove();
		});
		
		$("#modal").live('click', function() {
			$("#overlay").hide();
			$("#modal").remove();
		});
	}
	
	//News Popup
	
	if($('div.news').length > 0) {
		var start_width = (($(document).width() - 40) / 2) + 'px';
		$('#modal').css('left',start_width);
		
		$('div.news a.pop').click(function() {
			// Create Modal Object
			var doc_width = $(document).width();
			var doc_height = $(document).height();
			var win_width = $(window).width();
			var win_height = $(window).height();
			
			$('div.news').append($('<div></div>').attr('id','loading'));
			$('div#loading').css({
				background: 'url(img/layout/loader.gif) #fff no-repeat center center',
				position: 'absolute',
				left: ((win_width - 40) / 2) + 'px',
				top: ((win_height - 40) / 2) + 'px',
				width: '32px',
				height: '32px',
				padding: '4px'
			});
			
			$('div.news').append($('<div></div>').attr('id','modal'));
			$('div#modal').append($('<img src="" />')).css({
				background:'#fff',
				padding:'20px',
				position:'absolute',
				top:'400px',
				left:((doc_width - 40) / 2) + 'px',
				opacity:'0'
			});
			var img = "img/content/" + $(this).attr('rel');
			
			$('#modal img').attr('src', img).load(function() {
				$('div#loading').remove();
				var scale_width = $('#modal').width();
				var scale_height = $('#modal').height();
				var center_image = ((doc_width - scale_width) / 2) + 'px';
				$('#modal img').hide();

				
				$("#overlay").css({
					'display': 'block',
					'width': doc_width + 'px',
					'height': doc_height + 'px'
				});
				$("#modal").animate({
					'left': center_image,
					'top':$(window).scrollTop() + 70,
					'height': scale_height,
					'width': scale_width,
					'opacity':1
				},100, function(){
					$('#modal img').fadeIn('fast');
				});
			});
			return false;
		});
		
		$(window).scroll(function() {
			if($('#modal').length > 0) {
				$("#modal").animate({
					'top':$(window).scrollTop() + 70
				}, 50);
			}
		});
		
		$("#overlay").click(function() {
			$("#overlay").hide();
			$("#modal").remove();
		});
		
		$("#modal").live('click', function() {
			$("#overlay").hide();
			$("#modal").remove();
		});
	}
	
	//Gallery Popup
	
	if($('div.gallery_content').length > 0) {
		var start_width = (($(document).width() - 40) / 2) + 'px';
		$('#modal').css('left',start_width);
		
		$('div.gallery_content a').click(function() {
			// Create Modal Object
			var doc_width = $(document).width();
			var doc_height = $(document).height();
			var win_width = $(window).width();
			var win_height = $(window).height();
			
			$('div.gallery_content').append($('<div></div>').attr('id','loading'));
			$('div#loading').css({
				background: 'url(img/layout/loader.gif) #fff no-repeat center center',
				position: 'absolute',
				left: ((win_width - 40) / 2) + 'px',
				top: ((win_height - 40) / 2) + 'px',
				width: '32px',
				height: '32px',
				padding: '4px'
			});
			
			$('div.gallery_content').append($('<div></div>').attr('id','modal'));
			$('div#modal').append($('<img src="" />')).css({
				background:'#fff',
				padding:'20px',
				position:'absolute',
				top:'400px',
				left:((doc_width - 40) / 2) + 'px',
				opacity:'0'
			});
			var img = "img/content/gallery/large/" + $(this).attr('rel');
			
			$('#modal img').attr('src', img).load(function() {
				$('div#loading').remove();
				var scale_width = $('#modal').width();
				var scale_height = $('#modal').height();
				var center_image = ((doc_width - scale_width) / 2) + 'px';
				$('#modal img').hide();

				
				$("#overlay").css({
					'display': 'block',
					'width': doc_width + 'px',
					'height': doc_height + 'px'
				});
				$("#modal").animate({
					'left': center_image,
					'top':$(window).scrollTop() + 40,
					'height': scale_height,
					'width': scale_width,
					'opacity':1
				},100, function(){
					$('#modal img').fadeIn('fast');
				});
			});
			return false;
		});
		
		$(window).scroll(function() {
			if($('#modal').length > 0) {
				$("#modal").animate({
					'top':$(window).scrollTop() +40
				}, 50);
			}
		});
		
		$("#overlay").click(function() {
			$("#overlay").hide();
			$("#modal").remove();
		});
		
		$("#modal").live('click', function() {
			$("#overlay").hide();
			$("#modal").remove();
		});
	}
});  