/**
 * Reiss: Navigation functions
 **/

	$(document).ready(function() {
	
		// Hide unwanted content -
		// do this here because otherwise dbselects doesn't work because they can't calculate elem height
		$('.hidden').hide();

		$('#subscribe_no').click(function(){
			$('#subscribe_extra').attr("checked", "");
		});

		$('#subscribeForm input:text').focus(function() {
			if ( $(this).attr('original') == undefined ) {
				$(this).attr('original', $(this).val()).val('');
			}
			if ( $(this).val() == $(this).attr('original') ) {
				$(this).val('');
			}
		});
		$('#subscribeForm input:text').blur(function() {
			if ( $(this).val() == '' ) {
				$(this).val($(this).attr('original'));
			}
		});

		$('#searchForm input:text').focus(function() {
			if ( $(this).attr('original') == undefined ) {
				$(this).attr('original', $(this).val()).val('');
			}
			if ( $(this).val() == $(this).attr('original') ) {
				$(this).val('');
			}
		});
		$('#searchForm input:text').blur(function() {
			if ( $(this).val() == '' ) {
				$(this).val($(this).attr('original'));
			}
		});

        $('#block_screen').css('display', 'block').hide();

        $('.new_nav .nav_main_block').hover(
            function () {
                $('#block_screen').height($('#wrapper').height()).fadeIn('fast');
            },
            function () {
                $('#block_screen').fadeOut('fast');
            }
        );

		var not_your_country_animation_in_progress = false;
		var not_your_country_link_animation_in_progress = false;
		var not_your_country_link_mouse_over = false;

		$('.not_your_country_link a').each(function() {
			$(this).parent().fadeIn();

			$(this).mouseenter(function() {
				not_your_country_link_mouse_over = true;
				
				if ( !not_your_country_link_animation_in_progress ) {
					not_your_country_link_animation_in_progress = true;
					
					$(this).parent().parent().css('zIndex', '1001');
					$('.not_your_country_popup', $(this).parent().parent()).fadeIn('normal', function(){
						not_your_country_link_animation_in_progress = false;
					});
				}
			});
			
			$(this).mouseleave(function() {
				not_your_country_link_mouse_over = false;
				
				if ( !not_your_country_link_animation_in_progress ) {
					not_your_country_link_animation_in_progress = true;
					
					$(this).parent().parent().css('zIndex', '');
					$('.not_your_country_popup', $(this).parent().parent()).fadeOut('normal', function() {
						not_your_country_link_animation_in_progress = false;
					});
				}
			});

			if ( $(this).hasClass('with_country_dropdown') ) {
				$(this).click(function(){
					$.fancybox({
						'overlayOpacity' : '0.4',
						'overlayColor'   : '#000000',
						'autoScale'      : false,
						'centerOnScroll' : true,
						'transitionIn'   : 'fade',
						'transitionOut'  : 'fade',
						'autoDimensions' : false,
						'width'          : 210,
						'height'         : 200,
						'showCloseButton': true,
						'content'        : $(this).parent().parent().find('.not_your_country_popup_top').html(),
						onComplete       : function() {
							$('#fancybox-inner .over_container').show();

							$('#fancybox-inner input[type=checkbox], #fancybox-inner input[type=radio]').unbind('checkbox');
							$('#fancybox-inner .not_your_link').find('span').remove();
							$('#fancybox-inner .not_your_link').find('input').attr('style', '');
							
							$('#fancybox-inner input[type=checkbox], #fancybox-inner input[type=radio]').checkbox({
								cls		:	'jquery-reiss-checkbox',
								empty	:	reiss_base_url+'images/empty.png'
							});
						},
						onCleanup        : function() {},
						onClosed         : function() {
							$('#fancybox-inner input[type=checkbox], #fancybox-inner input[type=radio]').unbind('checkbox');
							$('#fancybox-inner .not_your_link').find('span').remove();
							$('#fancybox-inner .not_your_link').find('input').attr('style', '');
						}
					});
					return false;
				});
			}
		});

		setInterval(function(){
			if ( not_your_country_link_mouse_over == false && $('.not_your_country_link a').is('a') ) {
				$('.not_your_country_link a').trigger('mouseleave');
			}
		}, 3000);

        $('#wrapper_top #country_top_selector_form .dbselect .listOptions').css('width', '135px');

		$('#wrapper_top #country_top_selector_form select').change(function() {
			var temp_base = $(this).val();

			// sheck if user should stay in current page after change the region
			if ( /from_url=$/.test(temp_base) ) {
				temp_base += urlencode(document.location.href.replace(reiss_base_url, temp_base.replace(/^(.*)\?(.*)/, '$1')));
			}
			
			$('#country_top_selector_form').attr('action', temp_base).submit();
		});

        $('.new_nav .nav_main_block .palette_category.is_child').remove();

		$('.new_nav .nav_dropdown_container').each(function(){
			var old_top = parseInt($(this).css('top')) + 'px';
			if (old_top=='0px') {
				old_top = '41px';
			}
			$(this).css('top', '-1000px').show();

			// Recalculate width and height for navigation
			var width = 0;
			$(this).find('.nav_block, .nav_block_divider').each(function(){
				width += Math.abs(parseInt($(this).width()));
				width += Math.abs(parseInt($(this).css('padding-left')));
				width += Math.abs(parseInt($(this).css('padding-right')));
				width += Math.abs(parseInt($(this).css('margin-left')));
				width += Math.abs(parseInt($(this).css('margin-right')));
			});
//			width += Math.abs(parseInt($(this).find('.nav_block, .nav_block_divider').length * 2));

            $(this).width(width + 'px').css('top', old_top);
			$(this).find('.nav_block, .nav_block_divider').height($(this).height());

			// total nav height current dropdown inc padding
			var height = $(this).height();
			height += Math.abs(parseInt($(this).css('padding-top')));
			height += Math.abs(parseInt($(this).css('padding-bottom')));

			// Apply shadow
            /*
			$(this)
				.append($('<div/>').addClass('nav_shadow_left fix_png').css({top:'30px', left:'-16px', height:(height-30)+'px'}))
				.append($('<div/>').addClass('nav_shadow_bottom fix_png').css({top:height+'px', left:'-1px', width:(width-14)+'px'}))
				.append($('<div/>').addClass('nav_shadow_top_left_corner fix_png').css({top:'15px', left:'-16px'}))
				.append($('<div/>').addClass('nav_shadow_bottom_left_corner fix_png').css({top:height+'px', left:'-16px'}))
				.append($('<div/>').addClass('nav_shadow_bottom_right_corner fix_png').css({top:height+'px', left:(width-15)+'px'}));
            */

			// Resize banners
			$(this).find('.palette_banner .banner').each(function(){
				var width = Math.abs(parseInt($(this).width()-$(this).find('.banner_images').width()));
				width -= Math.abs(parseInt($(this).find('.banner_text').css('padding-left')));
				width -= Math.abs(parseInt($(this).find('.banner_text').css('padding-right')));
				if (width) {
					$(this).find('.banner_text').width(width + 'px');
				}
			});
			
			$(this).hide();
		});

        //$('#block_screen').css('height', $(document).height());

		$('.new_nav .nav_main_block').mouseenter(function(){
			$(this).addClass('active').find('.nav_dropdown_container').show();
            //$('#block_screen').fadeIn('fast');
		});

		$('.new_nav .nav_main_block').mouseleave(function(){
			$(this).removeClass('active').find('.nav_dropdown_container').hide();
            //$('#block_screen').fadeOut('fast');
		});

        /*
        $(window).bind('resize', function() {
            $('#block_screen').css('height', $(window).height());
        });
        */

		$('.new_static_nav .palette_category.is_parent').each(function(){
			if ($(this).next().hasClass('is_child')) {
				$(this).addClass('parent_closed');
			} else {
				$(this).removeClass('is_parent');
			}
		});

		// Make arrow allow to show/hide sub categories
		$('.new_static_nav .palette_category.is_parent').click(function(){
			openCloseSubNav($(this).find('.nav_category a'));
		});
		
		// Highlight current active category navigation based on URL and href of anchor tag
		openCloseSubNav($('.new_static_nav .nav_category a[href*=' + location.pathname + ']:eq(0)'));
		
		
		if ( $('.wrapper_center_black .recently_viewed_title img').length ) {
			$('.wrapper_center_black .recently_viewed_title img').attr(
				'src',
				$('.wrapper_center_black .recently_viewed_title img').attr('src').replace(/recently\_viewed\.gif/, 'recently_viewed_white.gif')
			);
		}

		$('.filter_header, .filter_arrow').click(function(){
			var thisElem = $(this);
			thisElem.addClass('active');
			
			$(this).parent().parent().find('.filter_dropdown').slideToggle('normal', function(){
				$(this).width('100%');
				thisElem.removeClass('active');
			});
			$(this).parent().parent().find('.filter_arrow').toggleClass('down');
			
			refreshLeftBarHeight();
		});
		
		refreshLeftBarHeight();

		$('form#search_filters input[type=checkbox]').click(function() {
			window.setTimeout(function(){$('form#search_filters').submit()}, 100);
		});

	});

	
	function refreshLeftBarHeight() {
		if ($('.category_content_right').length) {
			if ($('.category_content_right').height() < $('#category_content').height()) {
				$('.category_content_right').css('min-height', $('#category_content').height() + 'px');	
			}
		}
        if ($('.search_content_right').length) {
			if ($('.search_content_right').height() < $('#product_window').height()) {
				$('.search_content_right').css('min-height', $('#product_window').height() + 'px');	
			}            
        }
	}
	
	function openCloseSubNav(current_static_nav_elem) {
		var current_static_nav_elem_block =  current_static_nav_elem.parent().parent();
		var current_parrent = current_static_nav_elem_block.find('span.parent').text();
		current_static_nav_elem.addClass('active');
		if (current_static_nav_elem_block.hasClass('is_parent')) {
			if (current_static_nav_elem_block.hasClass('parent_closed')) {
				// Open parent category element
				current_static_nav_elem_block.removeClass('parent_closed').addClass('parent_opened');
				
				// Make child categories visible
				current_static_nav_elem_block.siblings('.is_child.parent_of_' + current_parrent).show();
			} else if (current_static_nav_elem_block.hasClass('parent_opened')) {
				// Close parent category element
				current_static_nav_elem_block.removeClass('parent_opened').addClass('parent_closed');
				
				// Make child categories invisible
				current_static_nav_elem_block.siblings('.is_child.parent_of_' + current_parrent).hide();
			}
		} else if (current_static_nav_elem_block.hasClass('is_child')) {
			// Open parent category element
			current_static_nav_elem_block.siblings('.is_parent.parent_of_' + current_parrent).removeClass('parent_closed').addClass('parent_opened');
			
			// Make child categories visible
			current_static_nav_elem_block.show().siblings('.is_child.parent_of_' + current_parrent).show();
		}
		
	}
	
