var ie55 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 5.5") != -1);
var ie6 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 6.0") != -1);


/**
 * jQuery custom selectboxes
 * 
 * Copyright (c) 2008 Krzysztof Suszyński (suszynski.org)
 * Licensed under the MIT License:
 * http://www.opensource.org/licenses/mit-license.php
 *
 * @version 0.6.1
 * @category visual
 * @package jquery
 * @subpakage ui.selectbox
 * @author Krzysztof Suszyński <k.suszynski@wit.edu.pl>
**/
jQuery.fn.selectbox = function(options){
	
	/* Default settings */
	var settings = {
		className: 'selectbox',
		animationSpeed: 100,
		listboxMaxSize: 10,
		replaceInvisible: false,
		listTitle: ''
	};
	var commonClass = 'jquery-custom-selectboxes-replaced';
	var listOpen = false;
	var showList = function(listObj) {
		var selectbox = listObj.parents('.' + settings.className + '');
		if (jQuery.browser.msie && (ie55 || ie6)) {
			listObj.show();
			listOpen = true;
		} else {
			listObj.slideDown(settings.animationSpeed, function(){
				listOpen = true;
			});
		};
		selectbox.addClass('selecthover');
		jQuery(document).bind('click', onBlurList);
		return listObj;
	}
	var hideList = function(listObj) {
		var selectbox = listObj.parents('.' + settings.className + '');
		if (jQuery.browser.msie && (ie55 || ie6)) {
			listObj.hide();
			listOpen = false;
			selectbox.removeClass('selecthover');
		} else {
			listObj.slideUp(settings.animationSpeed, function(){
				listOpen = false;
				jQuery(this).parents('.' + settings.className + '').removeClass('selecthover');
			});
		};
		jQuery(document).unbind('click', onBlurList);
		return listObj;
	}
	var onBlurList = function(e) {
		var trgt = e.target;
		var currentListElements = jQuery('.' + settings.className + '-list:visible').parent().find('*').andSelf();
		if(jQuery.inArray(trgt, currentListElements)<0 && listOpen) {
			hideList( jQuery('.' + settings.className + '-list:visible') );
		}
		return false;
	}
	
	/* Processing settings */
	settings = jQuery.extend(settings, options || {});
	/* Wrapping all passed elements */
	return this.each(function() {
		var _this = jQuery(this);
		
		if(_this.filter(':visible').length == 0 && !settings.replaceInvisible)
			return;
		var replacement = jQuery(
			'<div class="' + settings.className + ' ' + commonClass + '">' +
				'<div class="selectbox-list ' + settings.className + '-list ' + commonClass + '-list">' +
					'<div class="selectbox-list-head" />' +
					'<div class="selectbox-list-inner" >' +
						'<div class="selectbox-list-body-bg" />' +
						'<div class="selectbox-list-body">' +
							'<div class="selectbox-list-title">' + settings.listTitle + '</div>' +
						'</div>' +
					'</div>' +
					'<div class="selectbox-list-foot">' +
						'<div class="selectbox-list-foot-inner" />' +
					'</div>' +
				'</div>' +
				'<span class="' + settings.className + '-current-wrap"><span class="' + settings.className + '-current-item" /></span>' +
			'</div>'
		);
		jQuery('option', _this).each(function(k,v){
			var v = jQuery(v);
			var listElement =  jQuery('<span class="selectbox-item value-'+v.val()+' item-'+k+'"><em class="selectbox-item-icon '+v.attr("class")+'"></em>' + v.text() + '</span>');
			listElement.click(function(){
				var thisListElement = jQuery(this);
				var thisReplacment = thisListElement.parents('.'+settings.className);
				var thisIndex = thisListElement[0].className.split(' ');
				for( k1 in thisIndex ) {
					if(/^item-[0-9]+$/.test(thisIndex[k1])) {
						thisIndex = parseInt(thisIndex[k1].replace('item-',''), 10);
						break;
					}
				};
				var thisValue = thisListElement[0].className.split(' ');
				for( k1 in thisValue ) {
					if(/^value-.+$/.test(thisValue[k1])) {
						thisValue = thisValue[k1].replace('value-','');
						break;
					}
				};
				thisReplacment
					.find('.' + settings.className + '-current-item')
					.text(thisListElement.text()).removeAttr("class").addClass(settings.className + '-current-item').addClass(v.attr("class"));
				thisReplacment
					.find('select')
					.val(thisValue)
					.triggerHandler('change');
				var thisSublist = thisReplacment.find('.' + settings.className + '-list');
				if(thisSublist.filter(":visible").length > 0) {
					hideList( thisSublist );
				}else{
					showList( thisSublist );
				}
			}).bind('mouseenter',function(){
				jQuery(this).addClass('listelementhover').children().addClass(v.attr("class")+ '-hover');
			}).bind('mouseleave',function(){
				jQuery(this).removeClass('listelementhover').children().removeClass(v.attr("class")+ '-hover');
			});
			jQuery('.selectbox-list-body', replacement).append(listElement);
			if(v.filter(':selected').length > 0) {
				jQuery('.'+settings.className + '-current-item', replacement).text(v.text()).removeAttr("class").addClass(settings.className + '-current-item').addClass(v.attr("class"));
			}
		});
		replacement.find('.' + settings.className + '-current-wrap').click(function(){
			var thisMoreButton = jQuery(this);
			var otherLists = jQuery('.' + settings.className + '-list')
				.not(thisMoreButton.siblings('.' + settings.className + '-list'));
			hideList( otherLists );
			var thisList = thisMoreButton.siblings('.' + settings.className + '-list');
			if(thisList.filter(":visible").length > 0) {
				hideList( thisList );
			}else{
				showList( thisList );
			}
		});
		
		_this.hide().replaceWith(replacement).appendTo(replacement);
		var thisListBox = replacement.find('.' + settings.className + '-list');
		var thisListBoxSize = thisListBox.find('.' + settings.className + '-item').length;
		if(thisListBoxSize > settings.listboxMaxSize)
			thisListBoxSize = settings.listboxMaxSize;
		if(thisListBoxSize == 0)
			thisListBoxSize = 1;	
		var thisListBoxWidth = Math.round(_this.width() + 5);
		if(jQuery.browser.safari)
			//thisListBoxWidth = thisListBoxWidth * 0.94;
		//replacement.css('width', thisListBoxWidth + 'px');
		thisListBox.css({
			//width: Math.round(thisListBoxWidth-5) + 'px',
			//height: thisListBoxSize + 'em'
		});
	});
}



$(document).ready(function(){

    // IE7 search button outline fix
    jQuery('#search-image').add(jQuery('#search-text')).attr("hidefocus", true);

	// Toggle 'Search In' Field
	toggle_search_restaurant();
	
	blur_form_fields();

});

// Toggle 'Search In' Field
function toggle_search_restaurant(){
	if(jQuery('#search-select').val() == '04'){
		jQuery('.search-rest-in').hide();
		jQuery('.search-field-rest').attr('disabled', true);
	} else{
		jQuery('.search-rest-in').show();
		jQuery('.search-field-rest').removeAttr('disabled');
	}
}


function blur_form_fields(){
	jQuery('.yform-field', '.yform').each(function(){
		var field = jQuery(this);
		var label_value = field.val();
		field.blur(function() {
			if(field.val() == ''){
				field.val(label_value);
				field.removeClass('yform-field-active');
			}
		});
		field.focus(function() {
			if(field.val() == label_value){
				field.val('');
				field.addClass('yform-field-active');
			}
		});
	});
}


function dropdown_menu(toggle_link, link_opened_classname, span_opened_classname, default_classname){
	var toggleList = toggle_link.next();
	
	toggleList.addClass('selectbox-menu');
	
	var toggleDropdowns = jQuery('.selectbox-menu');
	var toggleSpan = toggle_link.children('span');
	var openedLinkVisible = 'toggle-link-visible';
	
	var showDropdown = function(closedList) {
		var closedListLink = closedList.prev();
		var closedListSpan = closedListLink.children();
		var currentLinkClass = closedListLink.attr('class');

		closedList.attr('link-orig-class', currentLinkClass);
		closedListLink.addClass('toggle-link-visible');
		closedListLink.addClass(link_opened_classname);
		closedListSpan.addClass(span_opened_classname);
		
		closedList.slideDown(100, function(){
			jQuery(document).bind('click', blurDropdown);
		});
		
		return closedList;
	}
	
	var hideDropdown = function(openedList) {
		var openedListLink = openedList.prev();
		var openedListSpan = openedListLink.children();
		var origLinkClass = openedList.attr('link-orig-class');
		
		if (jQuery.browser.msie && (ie55 || ie6)) {
			openedList.hide();
			openedListLink.removeAttr('class');
			openedListLink.addClass(origLinkClass);
			openedListSpan.removeAttr('class');
			jQuery(document).unbind('click', blurDropdown);
		} else {
			openedList.slideUp(100, function(){
				openedListLink.removeAttr('class');
				openedListLink.addClass(origLinkClass);
				openedListSpan.removeAttr('class');
				jQuery(document).unbind('click', blurDropdown);
			});
		};
		return openedList;
	}
	
	var closeDropdown = function(openedList) {
		var openedListLink = openedList.prev();
		var openedListSpan = openedListLink.children();
		var origLinkClass = openedList.attr('link-orig-class');
		
		openedList.hide();
		openedListLink.removeAttr('class');
		openedListLink.addClass(origLinkClass);
		openedListSpan.removeAttr('class');
		jQuery(document).unbind('click', blurDropdown);
		
		return openedList;
	}
	
	var blurDropdown = function(e) {
		var trgt = e.target;
		var currentListElements = jQuery('.selectbox-menu:visible').parent().find('*').andSelf();
		var toggleOpenedDropdowns = jQuery('.selectbox-menu:visible')
		if(jQuery.inArray(trgt, currentListElements)<0 && toggleOpenedDropdowns) {
			hideDropdown(toggleOpenedDropdowns);
		}
		return false;
	}
	
	if(!toggle_link.hasClass('toggle-link-visible')){
		if(toggleDropdowns.is(':visible')){
			jQuery(document).unbind('click');
			closeDropdown(jQuery('.selectbox-menu:visible'));
		}
		showDropdown(toggleList);
	} else if(toggle_link.hasClass('toggle-link-visible')){
		hideDropdown(toggleList);
	}
	return false;
}




/**
 * jQuery Ajax Rater Plugin
 *
 * This rater is based on the code Ritesh Agrawal did. Unfortunatly his CSS and the hover technique breaks in some browsers.
 * So i thought, why not use the best CSS star-rater known to man kind and throw it in the mix.
 * I have used the CSS and technique from Komodo Media since it is stable and tested on many, many browsers.
 *
 * This rater compared, has no cancel button. But i think we can live with that :)
 * To avoid conflicts i have changed the function name.
 * 
 * Licensed under The MIT License
 * 
 * @version     1.0
 * @since       03.01.2007
 * @author      Kjell Bublitz <m3nt0r.de@gmail.com
 * @link        http://www.m3nt0r.de/devel/raterDemo/ Demonstration and Documentation
 * @link        http://php.scripts.psu.edu/rja171/widgets/rating.php Based on Ritesh Agrawal Star Rating System
 * @link        http://komodomedia.com/blog/index.php/2007/01/20/css-star-rating-redux/ The Komodo Media CSS Rater Blogpost
 * @license     http://www.opensource.org/licenses/mit-license.php MIT 
 * @package     jQuery Plugins
 * @subpackage  Rater
 */ 

/**
 * Usage: $('#rating').rater('your_servlet', {style:'basic', maxvalue:5, curvalue:0});
 *  
 * @param url The address you want to post the result to. 
 * @param options The style and value attributes
 *
 * Valid options:
 * ---------------------------------------
 *       style:       'basic', 'inline' OR 'small'
 *       maxvalue:    the maximum value / number of stars
 *       curvalue:    the initial value / selected stars
 */ 
jQuery.fn.rater = function(url, options)
{
	if(url == null) return;
	var settings = {
		url       : url, // post changes to 
		maxvalue  : 5,   // max number of stars
		curvalue  : 0    // number of selected stars
	};
	
	if(options) { jQuery.extend(settings, options); };
	jQuery.extend(settings, {cancel: (settings.maxvalue > 1) ? true : false});
	
	var container = jQuery(this);
	jQuery.extend(container, { averageRating: settings.curvalue, url: settings.url });

	if(!settings.style || settings.style == null) {
		var raterwidth = settings.maxvalue * 18;
		var ratingparent = '<ul class="star-rating" style="width:'+raterwidth+'px">';
	}
	container.append(ratingparent);
	
	// create rater
	var starWidth, starIndex, listitems = '';
	var curvalueWidth = Math.floor(100 / settings.maxvalue * settings.curvalue);
	for(var i = 0; i <= settings.maxvalue ; i++) {
		if (i == 0) {
			listitems+='<li class="star-rating-current" style="width:'+curvalueWidth+'%;">'+settings.curvalue+'/'+settings.maxvalue+'</li>';
		} else {
			starWidth = Math.floor(100 / settings.maxvalue * i);
			starIndex = (settings.maxvalue - i) + 2;
			listitems+='<li class="star"><a href="#'+i+'" class="star-link" title="'+i+'/'+settings.maxvalue +'" style="width:'+starWidth+'%;z-index:'+starIndex+'">'+i+'</a></li>';
		}
	}
	container.find('.star-rating').append(listitems); // i am using find here, because the span wrapped in the small style would break children()

	if(settings.maxvalue > 1) // add a container for the ajax result
	{
		container.append('<span class="star-rating-result"></span>'); 
	}
	var stars = jQuery(container).find('.star-rating').children('.star');
	stars.click(function()
	{
		if(settings.maxvalue == 1) // on / off
		{
			settings.curvalue = (settings.curvalue == 0) ? 1 : 0;
			jQuery(container).find('.star-rating').children('.star-rating-current').css({width:(settings.curvalue*100)+'%'});
			jQuery.post(container.url, { "rating": settings.curvalue });
			return false;
		}
		else
		{
			
			settings.curvalue = stars.index(this) + 1;
			raterValue = jQuery(this).children('a')[0].href.split('#')[1];
			jQuery.post(container.url, { "rating": raterValue }, function(response){
				container.children('.star-rating-result').html(response)	
			});
			return false;
		}
		return true;
	});

	return this; // strict warning: anonymous function does not always return a value. fix?
}