// Modernizr.load loading the right scripts only if you need them
Modernizr.load([
	{
	    // Let's see if we need to load selectivizr
	    test : Modernizr.borderradius,
	    // Modernizr.load loads selectivizr for IE6-8
	    nope : ['library/js/libs/selectivizr-min.js']
	},
]);

String.prototype.startsWith = function (str){
    return this.slice(0, str.length) == str;
};

/* imgsizer (flexible images for fluid sites) */
var imgSizer={Config:{imgCache:[],spacer:"/path/to/your/spacer.gif"},collate:function(aScope){var isOldIE=(document.all&&!window.opera&&!window.XDomainRequest)?1:0;if(isOldIE&&document.getElementsByTagName){var c=imgSizer;var imgCache=c.Config.imgCache;var images=(aScope&&aScope.length)?aScope:document.getElementsByTagName("img");for(var i=0;i<images.length;i++){images[i].origWidth=images[i].offsetWidth;images[i].origHeight=images[i].offsetHeight;imgCache.push(images[i]);c.ieAlpha(images[i]);images[i].style.width="100%";}
if(imgCache.length){c.resize(function(){for(var i=0;i<imgCache.length;i++){var ratio=(imgCache[i].offsetWidth/imgCache[i].origWidth);imgCache[i].style.height=(imgCache[i].origHeight*ratio)+"px";}});}}},ieAlpha:function(img){var c=imgSizer;if(img.oldSrc){img.src=img.oldSrc;}
var src=img.src;img.style.width=img.offsetWidth+"px";img.style.height=img.offsetHeight+"px";img.style.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+src+"', sizingMethod='scale')"
img.oldSrc=src;img.src=c.Config.spacer;},resize:function(func){var oldonresize=window.onresize;if(typeof window.onresize!='function'){window.onresize=func;}else{window.onresize=function(){if(oldonresize){oldonresize();}
func();}}}}

//Dining Guide Defaults
var txt_search = '';
var cost = '4';
var alcohol = new Array();
alcohol['low'] = '1';
alcohol['high'] = '3';
var options = new Array();
options['delivery'] = 'False';
options['handicap'] = 'False';
options['valet'] = 'False';
options['rsvp'] = 'False';
options['patio'] = 'False';
var awards = new Array();
awards['best-of'] = 'False';
awards['bee'] = 'False';
awards['hall'] = 'False';
var dining_location = '';
var cuisine = '';

$(document).ready(function() {
	$('#restaurant_search_widget #search_box').keypress(function(e) {
		var keycode = (e.keyCode ? e.keyCode : e.which);
        if(keycode == 13) {
			txt = $(this).val();
			location.href = '/restaurants/#'+txt;
        }
    });
    	
	$('#restaurant_search_widget button').click(function(e) {
		e.preventDefault();
		txt = $('#restaurant_search_widget #search_box').val();
		location.href = '/restaurants/#'+txt;
	});
	$('.truncate').jTruncate({
        length: 300,
        minTrail: 0,
        moreText: "Read More",
        lessText: "Show Less",
        ellipsisText: " ...",
        moreAni: "fast",
        lessAni: 2000
    });
	$('.video_link').click(function(e){
		e.preventDefault();
		$('#youtube_iframe').attr('src',$(this).attr('href'));
	});
	$('article.guide').hover(
		function () {
			$(this).children("div.overlay").animate({top:'0'},1000);
		},
		function () {
			$(this).children("div.overlay").animate({top:'-224px'},1000);
		}
	);
    //DINING GUIDE
	$('input').defaultValue();
	$( "#cost" ).slider({
		range: "min",
		value: 4,
		min: 1,
		max: 4,
		step: 1,
		slide: function( event, ui ) {
			name = "four";
			key = ui.value
			if (key==1) {
				name = "one";
			} else if (ui.value==2) {
				name = "two";
			} else if (ui.value==3) {
				name = "three";
			} else  {
				name = "four";
			}
			$( "#amount li" ).removeClass('selected');
			$( "#amount li."+name ).addClass('selected');
			cost = key;
			filterResultsNow()
		}
	});
	$( "#alcohol" ).slider({
		range: true,
		values: [ 1, 3 ],
		//value:3,
		min: 1,
		max: 3,
		step: 1,
		slide: function( event, ui ) {
			alcohol['low'] = ui.values[0];
			alcohol['high'] = ui.values[1];
			
			if (alcohol['low'] > 1 ){
				$( "#alcohol_icons li#one" ).removeClass('selected');	
			} else {
				$( "#alcohol_icons li#one" ).addClass('selected');				
			}
			if (alcohol['low'] > 2 || alcohol['high'] < 2  ){
				$( "#alcohol_icons li#two" ).removeClass('selected');	
			} else {
				$( "#alcohol_icons li#two" ).addClass('selected');
			}
			if (alcohol['high'] < 3 ){
				$( "#alcohol_icons li#three" ).removeClass('selected');	
			} else {
				$( "#alcohol_icons li#three" ).addClass('selected');
			}
			filterResultsNow();	
		}
	});
	$('#options li').click(function(e){
		e.preventDefault();
		key = $(this).attr('id');
		if ($(this).hasClass('selected')) {
			$(this).removeClass('selected');
			options[key] = 'False';
		} else {
			$(this).addClass('selected');
			options[key] = 'True';
		}
		filterResultsNow();
	});
	$('#awards li').click(function(e){
		e.preventDefault();
		key = $(this).attr('id');
		if ($(this).hasClass('selected')) {
			$(this).removeClass('selected');
			awards[key] = 'False';
		} else {
			$(this).addClass('selected');
			awards[key] = 'True';
		}
		filterResultsNow();
	});
	$('#locations li a').click(function(e){
		e.preventDefault();
		key = $(this).attr('id');
		dining_location = key;
		$(this).parents('li').addClass('active').prepend('<a href="#" class="remove">[X]</a> ');
		$('#locations li a.remove').click(function(e){
			e.preventDefault();
			$(this).parents('li').removeClass('active');
			$('#locations li a.remove').remove();
			$('section.filterable article').fadeIn();
			$('#locations li').show();
		});
		$('#locations li').not('.active').hide();
		filterResultsNow()
	});
	$('#cuisine li a').click(function(e){
		e.preventDefault();
		key = $(this).attr('id');
		cuisine = key;
		$(this).parents('li').addClass('active').prepend('<a href="#" class="remove">[X]</a> ');
		$('#cuisine li a.remove').click(function(e){
			e.preventDefault();
			$(this).parents('li').removeClass('active');
			$('#cuisine li a.remove').remove();
			$('section.filterable article').fadeIn();
			$('#cuisine li').show();
		});		
		$('#cuisine li').not('.active').hide();
		filterResultsNow()
	});
	$('#restaurant-search .search_box button').click(function(e){
        e.preventDefault();
        txt_search = $('#restaurant-search #search').val();
        filterResultsNow();
    });
 //    if(window.location.hash) {
	//     hash = window.location.hash.substring(1);
	//     if (hash!='') {
	// 		txt_search = hash;
	// 		$('#restaurant-search #search').val(hash);
	// 		$('section.filterable article').each(function(index) {
	// 		    if (txt_search !='' && !checkName($(this).data('name'),txt_search,true)) {
	// 		        $(this).fadeOut();
	// 		    }
	// 		});
	// 	}
	// }

}); /* end of as page load scripts */ 


function filterResultsNow() {

    $('section.filterable article').each(function(index) {
        var status = true;
        var name = $(this).data('name');
        var cost_key = $(this).data('cost');
        var alcohol_key = $(this).data('alcohol');
        var location_key = $(this).data('location');
        var cuisine_key = $(this).data('cuisine');
        if (txt_search !='' && !checkName(name,txt_search,true)) {
            status = false;
        }
        if (cost_key !='' && cost<cost_key) {
            status = false;
        }
        if (alcohol_key !='') {
        	if (alcohol_key >= alcohol['low'] && alcohol_key <= alcohol['high']) {
            	status = true;
            } else {
            	status = false;
            }
        }
        if (options['patio']!='False' && $(this).data('patio')!=options['patio'] ) {
            status = false;
        }
        if (options['delivery']!='False' && $(this).data('delivery')!=options['delivery'] ) {
            status = false;
        }
        if (options['handicap']!='False' && $(this).data('handicap')!=options['handicap'] ) {
            status = false;
        }
        if (options['valet']!='False' && $(this).data('valet')!=options['valet'] ) {
            status = false;
        }
        if (options['rsvp']!='False' && $(this).data('rsvp')!=options['rsvp'] ) {
            status = false;
        }
        if (awards['best-of']!='False' && $(this).data('award-winner')!=awards['best-of'] ) {
            status = false;
        }
        if (awards['bee']!='False' && $(this).data('award-beehive')!=awards['bee'] ) {
            status = false;
        }
        if (awards['hall']!='False' && $(this).data('award-hall')!=awards['hall'] ) {
            status = false;
        }
        if (cuisine !='' && !checkName(cuisine_key,cuisine,false)) {
            status = false;
        }
        if (dining_location !='' && !checkName(location_key,dining_location,false)) {
            status = false;
        }              
        if (status) {
            $(this).fadeIn();
        } else {
            $(this).fadeOut(); 
        }
    });
    //sortResults();
}

function checkName(name,filter,does_contain) {
    name = name.toLowerCase();
    filter = filter.toLowerCase();
    if (filter != '') {
        if (does_contain) {
            if (name.indexOf(filter) != -1) {
                return true;
            } else {
                return false;
            }
        } else {
            if (name.startsWith(filter)) {
                return true;
            } else {
                return false;
            }
        }
    } else {
        return false;
    }
}
