jQuery(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');
		$(this).css('background-image','url(../img/layout/minus.gif)');
	}, function() {
	  	$('div#team-hidden').hide();
		$(this).css('color','');
		$(this).css('background-image','url(../img/layout/plus.gif)');
	});

	$('a.junior-expand').toggle(function() {
		$('div#junior-hidden').show();
		$(this).css('color','#fff');
		$(this).css('background-image','url(../img/layout/minus.gif)');
	}, function() {
	  	$('div#junior-hidden').hide();
		$(this).css('color','');
		$(this).css('background-image','url(../img/layout/plus.gif)');
	});

	$('a.artists-expand').toggle(function() {
		$('div#artists-hidden').show();
		$(this).css('color','#fff');
		$(this).css('background-image','url(../img/layout/minus.gif)');
	}, function() {
	  	$('div#artists-hidden').hide();
		$(this).css('color','');
		$(this).css('background-image','url(../img/layout/plus.gif)');
	});

	$('a.legends-expand').toggle(function() {
		$('div#legends-hidden').show();
		$(this).css('color','#fff');
		$(this).css('background-image','url(../img/layout/minus.gif)');
	}, function() {
	  	$('div#legends-hidden').hide();
		$(this).css('color','');
		$(this).css('background-image','url(../img/layout/plus.gif)');
	});
    
    // 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
	var current_hidden_image_index = 0;
	$(".image_hidden img").hide();
	$(".image_hidden img:eq("+current_hidden_image_index+")").show();
	$(".color li").each( function(){
		$(this).bind("click", function(){
			var $this = $(this);
			$(".image_hidden img:eq("+current_hidden_image_index+")").fadeOut(500, function(){
				var i = ( $(".color li").index( $this ) );
				$(".image_hidden img:eq("+i+")").fadeIn();
				current_hidden_image_index = i;
			});
			
		});
	});
	
	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(/wp-content/themes/discrete/images/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 = $(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) + (-20) + 'px';
				$('#modal img').hide();

				
				$("#overlay").css({
					'display': 'block',
					'width': doc_width + 'px',
					'height': doc_height + 'px'
				});
				$("#modal").animate({
					'left': center_image,
					'top':$(window).scrollTop() + 120,
					'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() + 120
				}, 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(/wp-content/themes/discrete/images/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();
		});
	}
	
	function nugget_click(){
		
		var s = $("#showing-nugget");
		var p = $(this).next("div").clone();
		
		s.html("");
		
		s.append(p);
		
		p.show();
		
		return false;
	}
	
	
	//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(nugget_click);
		
		
		var old_nugget = function(){
			// Create Modal Object
			
			
			
			
			$('div#modal').remove();
			
			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(/wp-content/themes/discrete/images/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) + (-20) + 'px',
				opacity:'0'
			});*/
			//console.debug( $(this).next().html() );
			var clone = $(this).next().clone();
			var scale_width = $('#modal').width();
			var center_image = ((doc_width - scale_width) / 2) + 'px';
			
			$('div#modal').append(  clone ).css({
				background:'#fff',
				padding:'20px',
				position:'absolute',
				'top':$(window).scrollTop() + 75,
				'left': center_image,
				'display' : 'none'
				
			});
			
			$('div#modal').fadeIn(1000);
			
			//var img = $(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() + 150,
					'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() + 75
				}, 50);
			}
		});
		
		$("#overlay").click(function() {
			$("#overlay").hide();
			$("#modal").remove();
		});
		
		$("#modal").live('click', function() {
			$("#overlay").hide();
			$("#modal").remove();
		});
	}
});  
