$(document).ready(function() {

	if(typeof initRotator == 'function') {
		initRotator();
	}
	
	//closes modal window when mouse clicks outside modal
	$('.ui-widget-overlay').live('click', function() { $('div[class*=ui-dialog-content]').dialog('close'); } );
	
	$('#french').click(function() {
		var currentUrl = location.href;
		var newUrl = '';
		var frenchPrefix = '/eyeglasses/fr/';
		var englishPrefix = '/eyeglasses/';
		var frenchRegex = new RegExp(frenchPrefix);
		var englishRegex = new RegExp(englishPrefix);
		
		if(currentUrl.match(frenchRegex)) {
			newUrl = currentUrl.replace(frenchRegex, englishPrefix);
		}
		else {
			newUrl = currentUrl.replace(englishRegex, frenchPrefix);
		}
		
		location.href = newUrl;
		
		return false;
	});
	
	
	$('.mainnav li').click(function(event) {
		if($(this).find('a').length > 0 && !$(this).is('.expand-only')) {
			window.location = $(this).find('a:first').attr('href');
			return false;
		}
	});
	
	$('.flyout').click(function(event) { //make sure flyout doesn't send click by itself when clicked
		event.stopPropagation();
		return false;
	});
	
	$('.expand-only, .expand-only > a').click(function(event) {
		
		//get <li> that was clicked even if it was the <a> tag
		var li = $(this);
		if($(this).is('a')) {
			li = $(this).parent();
		}

		// get the href of the parent navigation section
		var parentHref = li.parents('li.sub:first').prev().find('a:first').attr('href');
		
		// get the current page id
		var currentPage = document.URL;
		currentPage = currentPage.substring(currentPage.lastIndexOf('/') + 1);
		$('#navform input[name=prevform]').val(currentPage);

		// if current page is a parent category, expand the navigation without navigating away
		if (currentPage == parentHref) {		
			//find the expandable <ul>
			var expandable = li.next().children('ul');
			
			//hide the old expandable list and show the new one
			if(expandable.length > 0) {
				$('.subnav3:visible').slideUp();
				expandable.slideDown();
			}
	
			event.stopPropagation(); //ensure this is only executed for the either the <a> or <li> (and not both)
		}else {
			// navigate to the parent category
			$('#navform').attr('action', parentHref);
			
			// record the the current category the user is in, so that we can expand the appropriate section on the next page
			$('#navform #prevform').val(li.find('a').text());
			
			// navigate
			$('#navform').submit();
		}
		
		event.stopPropagation(); //ensure this is only executed for the either the <a> or <li> (and not both)
		return false;
	});

	if( $('.topnavigation').length )
	{
		$('.headerbar').append($('.topnavigation'));
		$('.flyout li:last-child').addClass('last');
		$('.flyout li:first-child').addClass('first');
		$('.mainnav li:last-child').addClass('last');
	}
	
	$('.show-flyouts .mainnav').children('li').hover(function() {
		$(this).find('ul.flyout').show();
		$(this).find('a.ls').css('color','#304E88');
	}, function() {
		$(this).find('ul.flyout').hide();
		$(this).find('a.ls').css('color','#999');
	});
	
	
	$('.printbtn').click(function() {
		var offerId = $(this).attr('id').match(/\d+/);
		var url = '/webapp/wcs/stores/servlet/SOOffersPopupView?storeId=' + storeId + '&offerId=' + offerId;
		window.open(url,'couponwindow','toolbar=no, directories=no,width=615, height=400');	
		return false;
	});
	
	$('.breadcrumbs a:not(:last)').each(function() {
		var a = $(this);
		a.addClass('bcone'); //add underline 
	});
	
	$('.expand-only > a').each(function() {
		var link = $(this);
		var breadcrumb = $('.breadcrumbs li:last').prev().find('a');
		if (breadcrumb.text() == $(link).text()) {
			breadcrumb.attr('href', link.closest('ul.subnav2').prev().find('a').attr('href'));
			breadcrumb.click(function() {
				link.click();
				return false;
			});
		}
	});
	
	
	$('.breadcrumbs a:not(.bcone)').removeAttr('href').addClass('last'); //remove the link from the last breadcrumb
	
	//get today's date and append to all ShareThis locations
	var date = new Date();
	$('.st_sharethis').attr('st_url', document.location.href + '?date=' + date.getFullYear() + '-' + (date.getMonth()+1) + '-' + date.getDate());

});
