/**
 * The very beginnings of js functionality..,
 */
jQuery(document).ready(function() {
    jQuery('#user-prefs-nav').bind('click', function(){
        jQuery('ul#user-prefs').toggle(200);
        return false;
    });
    
    //Show hide the payment method
    if (jQuery('select#payment-method option:selected').attr('id')) {
        var selId = jQuery('select#payment-method option:selected').attr('id');
        jQuery('div#pay-' + selId).show();        
    }
    
    jQuery('select#payment-method').bind('change', function(){
        jQuery('div.payment-box').hide();
        var selId = jQuery('select#payment-method option:selected').attr('id');
        jQuery('div#pay-'+selId).show();
    });
    
    //Show hide the billing container
    if(jQuery('input.billing-radio:checked').val()==='no') jQuery('div#billing-address-container').show();
    jQuery('input.billing-radio').bind('click', function(){
        if(jQuery(this).val()==='no'){
            jQuery('div#billing-address-container').show(100);
        } else {
            jQuery('div#billing-address-container').hide(100);
        }
    });
    
    //Float the your order while adding it.
    if(jQuery('#sidebar-order').length){
        //jQuery(window).scrollTop(0);
		var orderTop = jQuery('#create-order').position().top;
		
        jQuery(window).scroll(function () { 
            if(orderTop < $(this).scrollTop()) {
               var offset = $(this).scrollTop() - orderTop;
               			   
			   if(jQuery('#sidebar-order').height() + offset <= jQuery('#create-order').height()) {
                   jQuery("#sidebar-order, .quick-links-box").animate({top: offset+"px"}, {duration: 300,queue: false});				   
               }
			   
            } else {
				jQuery("#sidebar-order, .quick-links-box").animate({top: 0}, {duration: 400,queue: false});
			}
        });
    }
    
    //Mouseover for the cities.
    jQuery('a.star, a.star-lrg').bind('mouseover', function(){
    	jQuery('div.city').hide();
    	$(this).attr('title',''); //remove the title attribute
    	var cityName = $(this).html();
    	var cityNameId = cityName.replace( /\s/g, ''); //removes spaces in the city names
    	var cityPos = $(this).position();
    	//if it's a little star adjust by slightly less.
    	var cityLeft, cityTop;
    	if($(this).hasClass('star')){
    		cityLeft = cityPos.left+19;
    		cityTop = cityPos.top-10;
    	} else {
    		cityLeft = cityPos.left+23;
    		cityTop = cityPos.top-8;
    	}
    	if(jQuery('div#'+cityNameId).length){
    		//jQuery('div#'+cityName).fadeIn('medium'); .....IE has a bug rendering the png fading.
    		jQuery('div#'+cityNameId).show();
    	} else {
    		jQuery('<div class=\"city\" id=\"'+cityNameId+'\"><h4>'+cityName+'</h4></div>').appendTo('div#find-restaurant-map');
    		jQuery('div#'+cityNameId).css({'left' : cityLeft, 'top' : cityTop}).show();    		
    	}
    });
    
    jQuery.fn.mailme = function() {
        var at = / at /;
        var dot = / dot /g;
        this.each( function() {
            var addr = jQuery(this).text().replace(at,"@").replace(dot,".");
            var title = jQuery(this).attr('title')
            $(this)
                .after('<a href="mailto:'+addr+'" title="'+title+'">'+ addr +'</a>')
                .remove();
        });
    };
    
    //Mouseout for the cities
    jQuery('a.star, a.star-lrg').bind('mouseout', function(){
    	jQuery('div.city').hide();    	
    });
    
    //z-index madness in IE.
    if(jQuery.browser.msie) jQuery('td ul.star-rating').eq(0).css('zIndex','-1');
});
