
$(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;

	var lightbox = $('<div id="lookbook_product_large_black_container" style="display:none;"></div>')
						.append($('<div id="lookbook_product_large_black_container_inside"></div>'))
						.append($('<div id="lookbook_product_large_white_container" style="display:none;"></div>'))
						.append(
							$('<div id="lookbook_product_large_white_container_default" style="display:none;"></div>')
							.append(
								$('<div class="lookbook_product_large_close_button"></div>')
								.append($('<a href="#" style="text-decoration:none;">CLOSE X</a>'))
							)
							.append($('<div class="clear"></div>'))
						);

	$('#wrapper').append(lightbox);

	// this is IE6 hack
	if ( $('#wrapper').height() < 1000 ) {
		$('#lookbook_product_large_black_container').css('height', '1000px');
	}
	
	// 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 images actions
	$('.landing_images_container a').each(function() {
		$(this).mouseenter(function() {
			$(this).children().eq(0).fadeOut(400);
		});
		
		$(this).mouseleave(function() {
			$(this).children().eq(0).fadeIn(400);
		});
	});
	
	//
	$('.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();

						unbindClickToClose();
						
						unbindAddThis();
						
						hideLoader();
						
						$('#lookbook_product_large_white_container').html(data);

						if ('undefined' != typeof(track)) {
							track.lookbookLookView($('#lookbook_product_large_image').find('img').attr('alt'));
						}
						
						refreshPrevNextActions();
						
						bindCloseButtonActions();

						bindClickToClose();
						
						bindAddThis(product_url);
					}
					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 ) {
			if ('undefined' != typeof(track)) {
				track.lookbookLookView($(this).find('img').attr('alt'));
			}

			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();

						unbindClickToClose();

						unbindAddThis();
						
						hideLoader();
						
						$('#lookbook_product_large_white_container').html(data);
						
						refreshPrevNextActions();
						
						bindCloseButtonActions();

						bindClickToClose();
						
						bindAddThis(product_url);
					}
					else {
						hideLoader();
						$('.lookbook_product_large_close_button').trigger('click');
					}
				},
				error	:	function() {
					hideLoader();
					$('.lookbook_product_large_close_button').trigger('click');
				}
			 });
			
		}
		
		return false;
	});


	/**
	 * Auto open popup window if user passed product URL
	 */
	if ( /product\/[0-9]+\//.test(window.location.href) ) {
		$('.product_container div.product_mainline a span, .product_container div.product_1971 a span').each(function(){
			if ( $(this).text() == window.location.href || $(this).text() == window.location.href.replace(/\?(.)*$/, '') ) {
				$(this).parent().trigger('click');
				return false;
			}
			return true;
		});
	}
	
	/**
	 * Set default values for popup window
	 */
	function setDefaults() {
		if ( original_height < new_height ) {
			$('#lookbook_product_large_black_container').css('height', (new_height - 75) + 'px');
			$('#lookbook_content').css('height', (new_height - 50) + '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 350px'
		);
	}
	
	/**
	 * 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');
	}
	
	function bindCloseButtonActions() {
		$('.lookbook_product_large_close_button').bind('click', function() {
			closePopup();
			return false;
		});
	}

	function bindClickToClose() {
		$('#lookbook_product_large_image img').dbtips({
			text    : 'CLICK TO CLOSE',
			offsetX : -10,
			offsetY : 40
		});
		$('#lookbook_product_large_image img').click(function(){
			closePopup();
			return false;
		});
	}

	function unbindClickToClose() {
		$('#lookbook_product_large_image img').unbind('dbtips').unbind('click');
	}
	
	function unbindAddThis() {
		// do nothing just right now
	}

	function bindAddThis(product_url) {
		 if ( window.addthis ) {
			window.addthis.ost = 0;
			window.addthis.ready();
			for (i=0; i<window.addthis.links.length; i++) {
				try {
					window.addthis.links[i].href = window.addthis.links[i].href.replace(/(url=)http%3A%2F%2F.*%2F/, '$1'+urlencode(product_url));
					window.addthis.links[i].share.url = product_url;
					window.addthis.links[i].share.baseURI = product_url;
				} catch(e) {}
			}
		}
		$('.share-icons a').each(function() {
			$(this).attr('onclick', 'return addthis_sendto()');
		});
		
		$('.addthis_button_facebook_like iframe').attr('src', $('.addthis_button_facebook_like iframe').attr('src').replace(/(href=)http%3A%2F%2F.*%2F/, '$1'+urlencode(product_url)));
	}

	function urlencode(str) {
		return escape(str).replace(/\+/g, '%2B').replace(/%20/g, '+').replace(/\*/, '%2A').replace(/\//g, '%2F').replace(/@/g, '%40');
	}
	
});

//Set Variable for Event Tracking
lookbook_page = true;

