
$(function(){
	// popup window animation time (miliseconds)
	var popup_animation_delay = 300;
	
	// window position before open popup window (will be restored after popup close)
	var window_position = 0;
	
	// window height (in case if popup window is bigger than current window size then main window should be stretched)
	var original_height = 0;
	var new_height = 0;
	
	// check current window size and compare with popup
	if ( $('.product_container').height() < $('#lookbook_product_large_white_container').height() ) {
		original_height = $('#lookbook_product_large_black_container').height();
		new_height = Math.abs(parseInt($('#lookbook_product_large_white_container').css('height')) + 200);
	}
	
	// landing page navigation links actions
	$('.nav a').mouseover(function() {
		var thisindex = $('.nav a').index(this);
		$('.nav a').css('background-position', 'bottom left');
		$(this).css('background-position', 'top left');
	});
	
	// landing page images actions
	$('.landing_images_container a').each(function() {
		$(this).mouseenter(function() {
			$(this).children().eq(0).fadeTo('normal', 0.2);
			$(this).children().eq(1).fadeIn('normal');
			if ( $(this).children().eq(0).hasClass('left_image') ) {
				$('.nav a.landing_mainline_link').trigger('mouseover');
			}
			else {
				$('.nav a.landing_1971_link').trigger('mouseover');
			}
		});
		
		$(this).mouseleave(function() {
			$(this).children().eq(1).fadeOut('normal');
			$(this).children().eq(0).fadeTo('normal', 1);
		});
	});
	
	// 'Back to top' link action
	$('#back_to_top').click(function() {
		$('html,body').animate({ scrollTop : 0}, 400, 'swing');
		return false;
	});
	
	//
	$('.product_container img[src$=.jpg]').hover(
		function() {
			$(this).parent().parent().css('background-color', '#000000');
			$(this).fadeTo(200, 0.6);
		},
		function() {
			$(this).fadeTo(500, 1.0); 
		}
	);
	
	$('.product_container img').dbtips({
		text    : 'CLICK TO SHOP',
		offsetX : -10,
		offsetY : 40
	});
	
	function nextPrevAjaxAction(next_or_prev) {
		var product_url = '';
		
		if ( next_or_prev == 'next' ) {
			product_url = $('#lookbook_product_large_next_url').text();
		}
		else if ( next_or_prev == 'prev' ) {
			product_url = $('#lookbook_product_large_prev_url').text();
		}
		
		if ( product_url ) {
			$('#lookbook_product_large_white_container').html($('#lookbook_product_large_white_container_default').html());
			
			unbindCloseButtonActions();
			bindCloseButtonActions();
			
			showLoader();
			
			$.ajax({
				type	:	'post',
				url		:	product_url + 'ajax/',
				success	:	function(data) {
					if ( data ) {
						unbindCloseButtonActions();
						
						unbindAddThis();
						
						hideLoader();
						
						$('#lookbook_product_large_white_container').html(data);
						
						refreshPrevNextActions();
						
						bindCloseButtonActions();
						
						bindAddThis();
					}
					else {
						hideLoader();
						$('#lookbook_product_large_close_button').trigger('click');
					}
				},
				error	:	function() {
					hideLoader();
					$('#lookbook_product_large_close_button').trigger('click');
				}
			 });
			
		}
		
		return false;
	}
	
	
	// look image click action (open popup window)
	$('.product_container a').click(function() {
		var product_url = $('span', $(this)).text();
		
		if ( product_url ) {
			setDefaults();
			
			window_position = $(window).scrollTop();
			$('html,body').animate({ scrollTop : 0}, 400, 'swing');
			
			unbindCloseButtonActions();
			bindCloseButtonActions();
			
			showLoader();
			$('#lookbook_product_large_black_container').fadeIn(popup_animation_delay);
			$('#lookbook_product_large_white_container').fadeIn(popup_animation_delay);
			
			$.ajax({
				type	:	'post',
				url		:	product_url + 'ajax/',
				success	:	function(data) {
					if ( data ) {
						unbindCloseButtonActions();
						
						unbindAddThis();
						
						hideLoader();
						
						$('#lookbook_product_large_white_container').html(data);
						
						refreshPrevNextActions();
						
						bindCloseButtonActions();
						
						bindAddThis();
					}
					else {
						hideLoader();
						$('#lookbook_product_large_close_button').trigger('click');
					}
				},
				error	:	function() {
					hideLoader();
					$('#lookbook_product_large_close_button').trigger('click');
				}
			 });
			
		}
		
		return false;
	});
	
	/**
	 * Set default values for popup window
	 */
	function setDefaults() {
		if ( original_height < new_height ) {
			$('#lookbook_product_large_black_container').css('height', new_height + 'px');
			$('#lookbook_content').css('height', (new_height - 30) + 'px');
		}
		
		$('#lookbook_product_large_white_container').html(
			$('#lookbook_product_large_white_container_default').html()
		);
	}
	
	/**
	 * Close popup window
	 */
	function closePopup() { 
		$('#lookbook_product_large_white_container').fadeOut(popup_animation_delay);
		$('#lookbook_product_large_black_container').fadeOut(popup_animation_delay, function() {
			if ( original_height < new_height ) {
				$('#lookbook_product_large_black_container').css('height', original_height + 'px');
				$('#lookbook_content').css('height', original_height + 'px');
			}
			
			$('html,body').animate({ scrollTop : window_position}, 400, 'swing');
		});
		return false;
	}
	
	/**
	 * Show AJAX loader
	 */
	function showLoader() {
		$('#lookbook_product_large_white_container').css(
			'background', 
			'#fff url(/images/ajax-loader_large_solid.gif) no-repeat center center'
		);
	}
	
	/**
	 * Hide AJAX loader
	 */
	function hideLoader() {
		$('#lookbook_product_large_white_container').css(
			'background', 
			'#fff'
		);
	}
	
	function refreshPrevNextActions() {
		$('#lookbook_product_large_prev').unbind('click');
		$('#lookbook_product_large_prev').bind('click', function() {
			nextPrevAjaxAction('prev');
			return false;
		});
		
		$('#lookbook_product_large_next').unbind('click');
		$('#lookbook_product_large_next').bind('click', function() {
			nextPrevAjaxAction('next');
			return false;
		});
		
		
		$('#lookbook_product_large_prev').unbind('mouseenter');
		$('#lookbook_product_large_next').unbind('mouseenter');
		
		$('#lookbook_product_large_prev, #lookbook_product_large_next').bind('mouseenter', function() {
			$('#at15s').hide();
		});
	}
	
	function unbindCloseButtonActions() {
		$('#lookbook_product_large_close_button').unbind('click');
		$('#lookbook_product_large_close_button').unbind('mouseenter');
		$('#lookbook_product_large_close_button').unbind('mouseleave');
	}
	
	function bindCloseButtonActions() {
		$('#lookbook_product_large_close_button').bind('click', function() {
			closePopup();
			return false;
		});
		$('#lookbook_product_large_close_button').bind('mouseenter', function() {
			$('img', this).attr('src', $('img', this).attr('src').replace('_off', '_on'));
			$('#at15s').hide();
		});
		$('#lookbook_product_large_close_button').bind('mouseleave', function() {
			$('img', this).attr('src', $('img', this).attr('src').replace('_on', '_off'));
		});
	}
	
	function unbindAddThis() {
		// do nothing just right now
	}
	
	function bindAddThis() {
		$('.addthis_button').attr('onmouseover', 'return addthis_open(this, \'\', \''+window.location+'\', \''+document.title+'\');');
		$('.addthis_button').attr('onclick', 'return addthis_sendto();');
	}
	
});
