jQuery(function(){
	var title1 ="";
	var title2 = "";
	var d = new Date();

	//Linkable Search Calendar
	var searchURL = '/Default.aspx?CCID=4945&FID=51168&ExcludeBoolFalse=True&ID=/recipes-tips/results';
	

	//search function
	function postSearch() {
		//jQuery('html, body').animate({scrollBot:0}, 100);
		jQuery('#results h1, #results h2').remove();
		//jQuery('#content').hide();
		jQuery('#results').hide();
		jQuery('#searchresults').prepend('<div class="load">Loading...</div>');
	}
	
	function search(){
		jQuery('.load').remove();
		jQuery('#results').prepend('<h2>'+title2+'</h2>');
		jQuery('#results').prepend('<h1 class='+title1+'>'+title1+'</h1>');
		//jQuery(".event-list .text p").truncate(120);
		jQuery(".event-list .text ul, .event-list .text ol").hide();
		jQuery('.pagination .pag-current:eq(1)').remove(); //removes duplicate current page bug
	}
	

	
	
	// Initialize history plugin.
	// The callback is called at once by present location.hash. 
	//$.historyInit(pageload);
	
	//Creates today/tomorrow search links
	today = d.getDate();
	tomorrow = d.getDate() + 1;
	if (today <= 9) {today = "0" + today;}
	if (tomorrow <= 9) {tomorrow = "0" + tomorrow;}
	jQuery('a#today').attr('href','#'+today);
	jQuery('a#tomorrow').attr('href','#'+tomorrow);
	
    jQuery('a#today, a#tomorrow, .calendar a').click(function(){
		//console.log(jQuery(this).attr('hash'));
		var query = jQuery(this).attr('hash').replace('#','');
		title1 = 'whats-on';
		title2 = query.replace(/^0/,'');
		title2 = 'October '+title2+'th';
		title2 = title2.replace(/1th$/,'1st').replace(/2th$/,'2nd').replace(/3th$/,'3rd');
		title2 = title2.replace(/11st$/,'11th').replace(/12nd$/,'12th').replace(/13rd$/,'13th');
		
		postSearch();
		jQuery('#searchresults').load(searchURL +" #results",{CAT_Custom_66898: query},search);
		return false;
    });
	
	//Pagination links
	jQuery('.pagination li a').live("click",function() {
		title1 = jQuery('#content-wrap h1').attr('class');
		title2 = jQuery('#content-wrap h2').text();
		var query = jQuery(this).attr('href');
		postSearch();
		jQuery('#searchresults').load(query +" #results",search);
		return false;
	});
	
	//Next week link
	jQuery('a#thisweek').click(function() {
		postSearch();
		var week=new Array();
		day = d.getDate();
		title1 = 'whats-on';
		title2 = 'This week';
		var query = "";
		for (i=0;i<6;i++) {
			if (day <= 9) {day = '0' + day;}
			week[i] = '&CAT_Custom_66898='+day;
			day++;
			query=query+week[i];
		}
		$.post(searchURL,query,weeksearch);
	  return false;
	});
	
	function weeksearch(data){
		jQuery('.load').remove();
		jQuery('#content').html(data);
		jQuery('#results').prepend('<h2>'+title2+'</h2>');
		jQuery('#results').prepend('<h1 class='+title1+'>'+title1+'</h1>');
		jQuery('#results').show();
		jQuery(".event-list .text p").truncate(120);
		jQuery('#content').fadeIn('1000');
		jQuery('#content title, #content link').remove();
		return false;
	}
	
	//advanced search
	jQuery('.advanceds select option:first-child').attr('value','');
	
	var course = jQuery('select[id="CAT_Custom_133865"]');
	var occasion = jQuery('select[id="CAT_Custom_133866"]');
	var cuisine = jQuery('select[id="CAT_Custom_72500"]');
	var date = jQuery('select[id="CAT_Custom_66898"]');
	var restaurant = jQuery('input[id="CAT_Custom_67203"]');
	var keyword = jQuery('input[id="CAT_txtKeywords"]');
	var vego = jQuery('input[id="CAT_Custom_69494"]');
	var vegoTrue = 0;
	var child = jQuery('input[id="CAT_Custom_69495"]');
	var childTrue = 0;
	var wheelchair = jQuery('input[id="CAT_Custom_69496"]');
	var wheelchairTrue = 0;
	var title = "Advanced Search";
	
	// Search Quick recipes on click
	$('#hurry-link').click( function() {
		postSearch();
		//title1 = 'advanced';
		title2 = 'Results';
		jQuery(window).scrollTop('0');
		if (restaurant.val() == 'Restaurant Name') {restaurant.val('');}
		if (keyword.val() == 'Keyword') {keyword.val('');}
		if (vego.is(':checked') == true) {vegoTrue = 1;}
		if (child.is(':checked') == true) {childTrue = 1;}
		if (wheelchair.is(':checked') == true) {wheelchairTrue = 1;}
		
		jQuery('#searchresults').load(searchURL +" #results",{
				CAT_Custom_133865: course.val(),
				CAT_Custom_133866: occasion.val(),
				CAT_Custom_72500: cuisine.val(),
				CAT_Custom_66898: date.val(),
				CAT_Custom_67203: restaurant.val(),
				CAT_txtKeywords: keyword.val(),
				CAT_Custom_69494: vegoTrue,
				CAT_Custom_69495: childTrue,
				CAT_Custom_69496: wheelchairTrue,
				CAT_Custom_133868: 'yes'
			},search);
		return false;
	});
	
	
	// Show all recipes on click
	$('#all-link').click( function() {
		postSearch();
		//title1 = 'advanced';
		title2 = 'Results';
		jQuery(window).scrollTop('0');
	
		
		jQuery('#searchresults').load(searchURL +" #results",{
		
			},search);
		return false;
	});
	
	
	//jQuery('.advanceds input[type="submit"]').click(function(){
	jQuery('.advanceds #myform').submit(function(){
		postSearch();
		//title1 = 'advanced';
		title2 = 'Results';
		jQuery(window).scrollTop('0');
		if (restaurant.val() == 'Restaurant Name') {restaurant.val('');}
		if (keyword.val() == 'Keyword') {keyword.val('');}
		if (vego.is(':checked') == true) {vegoTrue = 1;}
		if (child.is(':checked') == true) {childTrue = 1;}
		if (wheelchair.is(':checked') == true) {wheelchairTrue = 1;}

		jQuery('#searchresults').load(searchURL +" #results",{
				CAT_Custom_133865: course.val(),
				CAT_Custom_133866: occasion.val(),
				CAT_Custom_72500: cuisine.val(),
				CAT_Custom_66898: date.val(),
				CAT_Custom_67203: restaurant.val(),
				CAT_txtKeywords: keyword.val(),
				CAT_Custom_69494: vegoTrue,
				CAT_Custom_69495: childTrue,
				CAT_Custom_69496: wheelchairTrue
			},search);
		return false;
	});
	
	//Hover bubbles for key
	jQuery(".vego1, .vegoTrue, .child1, .childTrue, .wheelchair1, .wheelchairTrue").hover(function() {
		jQuery(this).children().stop(true, true).animate({opacity: "show", top: "-30"}, "slow");
	}, function() {
		jQuery(this).children().animate({opacity: "hide", top: "-40"}, "fast");
	});
});