EFFECT_SPEED = 200;

(function($){
	$.fn.hashValue = function() {
		var href = this.attr('href');
		return href ? href.replace(/^.*#/, '') : href;
	}

	// custom css expression for a case-insensitive contains()
	jQuery.expr[':'].Contains = function(a,i,m){
		return (a.textContent || a.innerText || "").toUpperCase().indexOf(m[3].toUpperCase())>=0;
	};

	$.fn.extend({
		setFilter: function (list) { // header is any element, list is an unordered list
			// create and add the filter form to the header
			$(this)
			.change( function () {
				var filter = $(this).val();
				if (filter.length > 0){
					list.addClass("filtered");
				} else {
					list.removeClass("filtered");
				}
				if(filter) {
					// this finds all links in a list that contain the input,
					// and hide the ones not containing the input while showing the ones that do
					list.find("a:not(:Contains(" + filter + "))").parent().hide();
					list.find("a:Contains(" + filter + ")").parent().show();
				} else {
					list.find("li").show();
				}
				return false;
			})
			.keyup( function () {
				// fire the above change event after every letter
				$(this).change();
			})
			.data('filtered', true)
		}
	});
	
	// Search Input
	$.fn.searchInput = function () {
	
		$(this).append('<a class="clear-button"></a>');
		$(this).children(':input').each(function(){
			var input = $(this);
			input.bind('keypress keyup change paste cut', onSomethingChanged);
			trigger($(this));
			input.siblings('.clear-button').click(function(){
				input.val('');
				trigger(input);
				input.change();
			})
		})

		function onSomethingChanged() {
			trigger($(this), true);
		}

		function trigger(input, setFocus) {
			if (input.val().length > 0 && !input.hasClass('empty')){ // has text & not .empty class as placeholder value
				showClearButton(input, setFocus);
			} else {
				hideClearButton(input, setFocus);
			}
		}

		function showClearButton(input, setFocus) {
			if (input.attr('has_clearable_button') != '1') {
				input.attr('has_clearable_button', '1');
				input.siblings('.clear-button').show();
				if (setFocus && setFocus != undefined) {
					input.focus();
				}
			}
		}

		function hideClearButton(input, setFocus) {
			if (input.attr('has_clearable_button') == '1') {
				input.removeAttr('has_clearable_button');
				input.siblings('.clear-button').hide();
			}
			if (setFocus && setFocus != undefined) {
				input.focus();
			}
		}

		return this;
	}
	
})(jQuery);

$(function(){

	// menu
	var selected = $('#site-menu li.selected');
	var opened = $('.products-nav.opened').attr('name');
	var menu_opened = opened;
	var hide_current = function (isAnimated) {
		var menu = $('#' + menu_opened + '_menu');
		var item = $('#' + menu_opened);
		item.removeClass('opened');
		if (isAnimated) {
			menu.slideUp(EFFECT_SPEED);
		} else {
			menu.hide();
		}
		menu_opened = null;
		$.cookie('menuOpened', null, { path: '/' });
	}
	$('#category-menu li:not(.line)').each(function(){
		var item = $(this);
		var menuId = item.attr('id');
		var menu = $('#' + menuId + '_menu');
		var btnClose = menu.find('a.close');
		if (menuId == menu_opened) {
			item.addClass('opened');
		}
		item.click(function(){
			if (menuId == menu_opened && menu.is(':visible')) {
				menu.slideUp(EFFECT_SPEED, function(){
					menu_opened = null;
					item.removeClass('opened');
					$.cookie('menuOpened', null, { path: '/' });
				});
			} else {
				if (menu_opened) {
					hide_current();
					menu.show();
				} else {
					menu.slideDown(EFFECT_SPEED);
				}
				item.addClass('opened');
				if (menuId != selected.attr('id')) {
					selected.addClass('current');
				} else {
					selected.removeClass('current');
				}
				menu_opened = menuId;
				$.cookie('menuOpened', 1, { path: '/' });
			}
			return false;
		});
		btnClose.click(function(){
			hide_current(true);
		})
	})
	$('#site-menu ul.root li, #site-menu ul.links li').click(function(){
		$.cookie('menuOpened', null, { path: '/' });
	})
	
	// announce
	$('.announce a.close').click(function(){
		$.cookie('hideAnnounce', 1);
		$(this).parent().slideUp(200);
	})
	
	// city search
	var s_input = $('#city_keyword_input');
	var f_content = $('#cities');
	s_input.parent().searchInput();
	s_input.setFilter(f_content);
	
});



  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-26637316-1']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();




