/*  
===============================================================================
WResize is the jQuery plugin for fixing the IE window resize bug
...............................................................................
                                               Copyright 2007 / Andrea Ercolino
-------------------------------------------------------------------------------
LICENSE: http://www.opensource.org/licenses/mit-license.php
WEBSITE: http://noteslog.com/
===============================================================================
*/
( function( $ ) {
	$.fn.wresize = function( f ) 
	{
		version = '1.1';
		wresize = { fired: false, width: 0 };

		function resizeOnce() 
		{
			if ( $.browser.msie ) {
				if ( ! wresize.fired ) { wresize.fired = true; }
				else  {
					var version = parseInt( jQuery.browser.version, 10 );
					wresize.fired = false;
					if ( version < 7 ) { return false; }
					else if ( version == 7 ) {
						//a vertical resize is fired once, an horizontal resize twice
						var width = $( window ).width();
						if ( width != wresize.width ) {
							wresize.width = width;
							return false;
						}
					}
				}
			}
			return true;
		}
		function handleWResize( e ) { if ( resizeOnce() ) { return f.apply(this, [e]); } }
		this.each( function() { if ( this == window ) { $( this ).resize( handleWResize ); } else { $( this ).resize( f ); } } );
		return this;
	};

} ) ( jQuery );



function doResize() {
	$('#storeView').css('width', ($(window).width() - 250) + 'px').css('height', ($(window).height() - 90) + 'px');
}

$(function() {
	$('ul.drawers').accordion({
		header: 'H2.drawer-handle',
		selectedClass: 'open',
		//event: 'mouseover',
		active: false,
		clearStyle: true,
		alwaysOpen: false,
		autoheight: false
	});

	$(window).wresize(doResize);
	doResize();
});

// findIt(url, [params])
var activeStore = null;
var showingError = false;

var lastLink = null;
var lastUrl = null;
var lastParams = null;

function findIt(link, url, params) {
	lastLink = link;
	lastUrl = url;
	lastParams = params;

	// Find search string
	var searchString = $('#searchString').val();

	// Validate search string
	if (searchString == '') {
		if (!showingError) {
			showingError = true;
			$('#searchError').html('Search text is empty!').hide().animate({ 'opacity': 'toggle' }, 1500).animate({ 'opacity': 'toggle' }, { duration: 1000, complete: function() { showingError = false; } });
		}
		return;
	}

	// Add selected class to selected store, and remove class from previously active store
	if (activeStore != null) { activeStore.removeClass('selected'); }
	activeStore = $(link).parent();
	activeStore.addClass('selected');

	// Send search to iframe (form or url)
	if (params) {
		var html = '<input type="hidden" name="' + params[0] + '" value="' + searchString + '" />';
		for (var i = 0; i < params[1].length; i++) { html += '<input type="hidden" name="' + params[1][i][0] + '" value="' + params[1][i][1] + '" />'; }
		$('#storeSearchForm').html(html).attr('action', url).submit();
	} else { $('#storeView').attr('src', url.replace('{kw}', searchString)); }

	// Try to save it to analytics
	if (typeof (pageTracker) != 'undefined') {
		pageTracker._trackPageview('/storesearch/' + escape($(activeStore).text()) + '/');
	}
}


function redoSearch() {
	if (lastLink != null) { findIt(lastLink, lastUrl, lastParams); }
	return false;
}