$(document).ready(function()
{
	var shadowbox_options = ''; // Will be appended to rel attribute for images and videos
	
	//---------------------------------------------START CUSTOM JS//

	if($('input#end_date').length > 0)
	{
		$('input#end_date').parent().hide();
		$('label[for="start_date"]').html('Date <em>(Select first date if weekly event)</em>');
	}
	$('input#start_date').change(function(){
		$('input#end_date').val($('input#start_date').val())
	});

	if($('input#qr_code_image').length > 0)
	{
		$('input#qr_code_image').parent().parent().hide();
	}
	
	/* START POP UP ADS */
	var all_cookies = document.cookie.split(';');
	var no_more_ads = 'false';
	
	for(var i = 0; i < all_cookies.length; i++)
	{
		if(all_cookies[i] == 'ymt_ads=no_more' || all_cookies[i] == ' ymt_ads=no_more')
		{
			no_more_ads = 'true';
		}
	}
	
	if(no_more_ads == 'false')
	{
		if($('span.menu_download a').length > 0)
		{
			$('span.menu_download a').addClass('show_ad');
		}
		
		if($('div#vouchers a').length > 0)
		{
			$('div#vouchers a').addClass('show_ad');
		}
		
		$('a.show_ad').each(function()
		{
			$(this).attr('rel',$(this).attr('href')).attr('href','');
			
			$(this).unbind().bind('click', function()
			{
				var target = $(this).attr('rel');
				
				window.open(target);
				
				if(no_more_ads == 'false')
				{
					$.ajax({
							async			: false,
							url				: _root+'ads.php',
							type			: "POST",
							data			: ({
													action			: 'render',
													ajax			: 'true'
												}),
							success			: function(data)
							{
								if(data != 'false')
								{
									$('div#container').after(data);
									
									$('div.ad_container').height($('div#container').height());
									$('div.ad_container div').css('marginTop', $(window).scrollTop() + "px");
									
									$('div.ad_container a').unbind().bind('click',function()
									{
										if($(this).hasClass('close'))
										{
											$('div.ad_container').remove();
											
											return false;
										}
										else if($(this).hasClass('no_more'))
										{
											var date = new Date();
											
											date.setTime(date.getTime()+(12*60*60*1000));
											
											var expires = "; expires="+date.toGMTString();
											
											document.cookie = "ymt_ads=no_more"+expires+"; path=/;";
											
											no_more_ads = 'true';
											
											$('div.ad_container').remove();
											
											return false;
										}
										else
										{
											var target = $(this).attr('href');
											
											$.ajax({
													async			: false,
													url				: _root+'ads.php',
													type			: "POST",
													data			: ({
																			action			: 'click',
																			ad				: $(this).attr('id'),
																			ajax			: 'true'
																		}),
													success			: function(data)
													{
														$('div.ad_container').remove();
													}
											});
										}
									});
								}
							}
					});
				}
				
				return false;
			});
		});
	}
	/* END POP UP ADS */
	
	/* ***  area navigation menu *** */
	$('select#your_area').change(function() {
		var date = new Date();
		date.setTime(date.getTime()+(30*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();

		url_path = $('select#your_area').val();
		if(url_path != 'null')
		{
			//local
			/*
			if(url_path!='')	url_path += '/';
			parent.window.location = "http://192.168.1.10/YourMealTicket/www.yourmealticket.co.uk/public_html/"+url_path;
			document.cookie = "ymt_region"+"="+"http://192.168.1.10/YourMealTicket/www.yourmealticket.co.uk/public_html/"+url_path+expires+"; path=/;";
			*/
			//live
			if(url_path!='')	url_path += '.';
			parent.window.location = "http://www."+url_path+"yourmealticket.co.uk/";
			document.cookie = "ymt_region"+"="+"http://www."+url_path+"yourmealticket.co.uk/"+expires+"; path=/; domain=yourmealticket.co.uk";
			
		}
	});
	
	
	if($('body#home').length > 0)
	{
		var ca = document.cookie.split(';');
		for(var i=0;i < ca.length;i++)
		{
			var c = ca[i];
			
			while(c.charAt(0)==' ') c = c.substring(1,c.length);
			
			if(c.indexOf("ymt_region") == 0)
			{
				var redirect = c.substring(("ymt_region".length+1),c.length);
			}
		}
		
		if(redirect != undefined && parent.window.location != redirect)
		{
			parent.window.location = redirect;
		}
	}
	
	
	if($('body').attr('id') == 'preview' && $('div#title_panel').length > 0)
	{
		$('body').attr('id','members');
	}
	
	$.fn.limit = function(n)
	{
		var self = this;
		this.click(function()
		{
			return (self.filter(':checked').length <= n);
		});
	}
	
	if($('li.limit dl.inputgroup').length > 0)
	{
		$('li.limit dl.inputgroup input:checkbox').limit(4);
	}
	
	if($('table.categories-5').length > 0)
	{
		$('table.categories-5 th.pages').css('display','none');
		$('table.categories-5 td.pages').css('display','none');		
	}
	
	if($('li.contact_notes').length > 0)
	{
		$('li.contact_notes').css('display','none');
	}
	
	if($('p.venue_value').length > 0)
	{
		$('div.ratings_explained').hide();
		
		$('p.venue_value span').hover(function()
		{
			$('div.ratings_explained').fadeIn('fast');
		},
		function()
		{
			$('div.ratings_explained').fadeOut('fast');
		});
	}
	
	// ensures that the image/video box never overlaps the proceeding special offers area
	if($('div.video_box').length > 0)
	{
		if($('div#venue_description').height() < $('div#venue_photos').height())
		{
			$('div#venue_description').css('height',($('div#venue_photos').height()-180)+'px');
		}
	}
	
	
	/* slide in/out table and room booking forms on venue profiles */
	// hide by default, but not if the user has already posted the data (else they won't be able to see the message!)
	if($('#book_a_table_box li.warning').length == 0 && $('#book_a_table_box p.success').length == 0)
	{
		$('#book_a_table_box').hide();
	}
	if($('#book_a_room_box li.warning').length == 0 && $('#book_a_room_box p.success').length == 0)
	{	
		$('#book_a_room_box').hide();
	}
	$('a#toggle-table-booking').click(function() {
		$('#book_a_table_box').slideToggle(400);
		if($('#book_a_room_box').length > 0)
		{
			$('#book_a_room_box').hide();
		}
		return false;
	});
	$('a.table').click(function() {
		$('#book_a_table_box').slideToggle(400);
		if($('#book_a_room_box').length > 0)
		{
			$('#book_a_room_box').hide();
		}
		theAnchor('table');
		return false;
	});
	$('a#toggle-room-booking').click(function() {
		$('#book_a_room_box').slideToggle(400);
		if($('#book_a_table_box').length > 0)
		{
			$('#book_a_table_box').hide();
		}
		return false;
	});	
	$('a.room').click(function() {
		$('#book_a_room_box').slideToggle(400);
		if($('#book_a_table_box').length > 0)
		{
			$('#book_a_table_box').hide();
		}
		theAnchor('room');
		return false;
	});	
	function theAnchor(anchorName) {
	   var sPath = window.location.pathname;
	   var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
	   location.href = sPage+"#"+anchorName
	}
	
	
	// navigation side bar features	
	if($('div#side_nav').length > 0)
	{
		// hide bookmarks box by default
		$('div#mn_bookmarks_area').hide();
		// hide party booking form by default
		$('div#mn_party_area').hide();

		// don't hide calendar on what's on guide pages
		if($('body#events').length != 0 || $('body#members').length != 0)
		{
			if($('div.page h2.search').length != 0)
			{
				//$('a#mn_whatson').hide();
				//$('div#mn_whatson_area').hide();
				$('a#mn_party').hide();	
			}
			else 
			{
			
				$('div#mn_search_area').hide(); 		// but do hide search
				if($('form.party_contact').length != 0)	// if we're on party search page...
				{
					//$('a#mn_whatson').hide();
					//$('div#mn_whatson_area').hide();	// hide what's on calendar
					$('div#mn_party_area').show();		// and show party booking form in full
				}
				else {
					$('a#mn_party').hide();
				}
			}
		}
		/*
		else
		{
			$('div#mn_whatson_area').hide();		// but do hide the calendar everywhere else!
		}
		*/
		// hide sign-up if it's not being used!
		if($('div#mn_signup_area li.warning').length == 0 &&
			$('div#mn_signup_area p.success').length == 0)
		{
			$('div#mn_signup_area').hide();	
		}
		else // if there's an error in sign-up form, hide the intro paragraph instead
		{
			$('div#mn_signup_area p.intro').hide();	
		}
		
		// don't display search by default on contact page
		if($('body#contact-us').length != 0)
		{
			$('div#mn_search_area').hide();
		}
	}
	// toggle buttons for navigation form to show/hide elements
	$('a#mn_search').click(function() {
		$('#mn_search_area').slideToggle(200);
	});
	$('a#mn_whatson').click(function() {
		$('#mn_whatson_area').slideToggle(200);
	});
	$('a#mn_signup').click(function() {
		$('#mn_signup_area').slideToggle(200);
	});
	$('a#mn_party').click(function() {
		$('#mn_party_area').slideToggle(200);
	});
	$('a#mn_bookmarks').click(function() {
		$('#mn_bookmarks_area').slideToggle(200);
	});

	// always hide extra party booking options, unless on party booking pages
	if($('body#members').length == 0)
	{	
		$('form.party_search div#party_extras').hide();
	}
	
	// Make offer boxes on offer-type or detail pages equal height
	// but only bother looking for it on offer-type and members id pages
	if($('body').attr('id') == 'members')
	{
		var box_height = 0;
		$('div#vouchers div.item').each(function() // start by getting the height of the tallest box
		{
			if($(this).height() > box_height)
			{
				box_height = $(this).height();
			}
		});
		$('div#vouchers div.item').each(function() // apply this height to all other boxes
		{
			$(this).css('height',box_height+'px');
		});		
	}
	if($('body').attr('id') == 'offer-type')
	{
		var box_height = 0;
		$('body#offer-type div#content div.page div.item').each(function()
		{
			if($(this).height() > box_height)
			{
				box_height = $(this).height();
			}			
		});
		$('body#offer-type div#content div.page div.item').each(function()
		{
			$(this).css('height',box_height+'px');
		});
	}
	
	//----------------------rounded corner objects //
	if(!$.browser.msie)
	{
		if($('div#fb-wrapper').length > 0)
		{
			$('div#fb-wrapper').corner('7px');
		}
		if($('form').length > 0)
		{
			$('form').corner('7px');
		}
		if($('p.admin_note').length > 0)
		{
			$('p.admin_note').corner('5px');
		}
		// What's on?
		if($('div#mn_whatson_area').length > 0)
		{
			$('div#mn_whatson_area').corner('7px');
		}
		// "downloads" red box in side panel
		if($('div.red_box').length > 0)
		{
			$('div.red_box').corner('7px');
		}
		// faq boxes	
		if($('div#content div.page div.item dl').length > 0)
		{
			$('div#content div.page div.item dl').corner('7px');
		}
		// news snippet panels
		if($('div#content div.page div#news_area div.item').length > 0)
		{
			$('div#content div.page div#news_area div.item').corner('7px keep');
		}
		// featured restaurant panel
		if($('div.featured_venue').length > 0)
		{
			$('div.featured_venue').corner('7px keep');
		}
		// venue listing box
		if($('div#content div.page div.venue_item').length > 0)
		{
			$('div#content div.page div.venue_item').corner('7px');
		}
		// title panel in detail page
		if($('body#members div#content div.page div#title_panel').length > 0)
		{
			$('body#members div#content div.page div#title_panel').corner('7px');
		}
		// social bookmarks box-out 
		if($('div#content div.column div.bookmarks').length > 0)
		{
			$('div#content div.column div.bookmarks').corner('7px');
		}
		// opening times panel 
		if($('div.opening_times').length > 0)
		{
			$('div.opening_times').corner('7px keep');
		}
		// item box (for coupons, previews, snippets, etc.)
		if($('div.item').length > 0)
		{
			$('div.item').corner('7px');
		}
		if($('div.more_info').length > 0)
		{
			$('div.more_info').corner('7px');
		}
		if($('p.results_intro').length > 0)
		{
			$('p.results_intro').corner('7px');
		}
		if($('p.results_second').length > 0)
		{
			$('p.results_second').corner('7px');
		}		
		if($('div#content div.page form#venue_profile p.terms').length > 0)
		{
			$('div#content div.page form#venue_profile p.terms').corner('7px');
		}
		if($('div#content div.column div.contact').length > 0)
		{
			$('div#content div.column div.contact').corner('7px');
		}
		if($('form.party_contact ul.party_errors').length > 0)
		{
			$('form.party_contact ul.party_errors').corner('7px');
		}
		if($('p.party_success').length > 0)
		{
			$('p.party_success').corner('7px');
		}
		if($('body#foodie-links div.page ul').length > 0)
		{
			$('body#foodie-links div.page ul').corner('7px');
		}
		if($('div.highlights').length > 0)
		{
			$('div.highlights').corner('10px top');
		}
		if($('body#best-deal-in-town div.page h2').length > 0)
		{
			$('body#best-deal-in-town div.page h2').corner('7px');
		}
		if($('body#best-deal-in-town div.page a.page_download').length > 0)
		{
			$('body#best-deal-in-town div.page a.page_download').corner('7px');
		}
		if($('body#members div#content div.page div.my_account_listing div.item span.view_item_2 a').length > 0)
		{
			$('body#members div#content div.page div.my_account_listing div.item span.view_item_2 a').corner('3px');
		}
		if($('body#members div#content div.page div.my_account_listing div.item span.view_item_3 a').length > 0)
		{
			$('body#members div#content div.page div.my_account_listing div.item span.view_item_3 a').corner('3px');
		}
		if($('body#members div#content div.page div.my_account_listing div.item span.view_item_4 a').length > 0)
		{
			$('body#members div#content div.page div.my_account_listing div.item span.view_item_4 a').corner('3px');
		}
	}
	
	// Ajax to limit search options
	check_search();
	check_party_search();
	
	function check_search()
	{
	
		if($('form.search').length > 0)
		{
			$('form.search select').bind('change',function()
			{
				$.post(_root+'venues/',
				{
					search_changed	: $(this).attr('id'),
					offer_type		: $('select#offer_type').val(),
					venue_area		: $('select#venue_area').val(),
					cuisine_type	: $('select#cuisine_type').val(),
					ajax			: 'true'
				},
				function(data)
				{
					//$('form.search').html(data);
					$('form.search').remove();
					$('div.column h2.search').after(data);
					$('form.search').corner('7px');
					
					check_search();
				});
				
				return false;
			});
		}
	}
	
	/* party search form */
	function check_party_search()
	{
		if($('form.party_search').length > 0)
		{
			$('form.party_search select').bind('change',function()
			{
				if($(this).attr('name') == 'party_catering' || 
				   $(this).attr('name') == 'max_people' || 
				   $(this).attr('name') == 'max_price')
					return false;
				$.post(_root+'venues/',
				{
					party_search_changed	: $(this).attr('id'),
					party_type				: $('select#party_type').val(),
					party_location			: $('select#party_location').val(),
					party_cuisine			: $('select#party_cuisine').val(),
					ajax					: 'true'
				},
				function(data)
				{
					//$('form.search').html(data);
					$('form.party_search').remove();
					$('h2.party').after(data);
					
					$('form.party_search').corner('7px');
					if($('body#members').length == 0)
					{
						$('div#party_extras').hide();
						$('a#more_party_options').hide();
					}
					check_party_search();
				});
				
				return false;
			});
		}
	}
	
	$('a#more_party_options').click(function() {
		$('div#party_extras').slideToggle(200,function() {
			if($('div#party_extras').css('display') == 'none')
			{
				$('a#more_party_options').removeClass('fewer');
				$('a#more_party_options').addClass('more');
			}
			else
			{
				$('a#more_party_options').removeClass('more');
				$('a#more_party_options').addClass('fewer');
			
			}
		});
	});
	

	
	
	if($('p.results_intro').length != 0)
	{
		$('div.item').each(function() 
		{
			$(this).css('cursor','pointer');
		});
	
		$('textarea#party_comments').focus(function()
		{
			if($(this).val() == 'Use this space to tell us more about your party')
			{
				$(this).val('');
			}
		});
		$('textarea#party_comments').blur(function()
		{
			if($(this).val() == '')
			{
				$(this).val('Use this space to tell us more about your party');
			}		
		});
		/*
		$('div.item').click(function()
		{
			if($(this).css('background-color')=='rgb(239, 245, 239)')
			{
				$(this).css('background-color','rgb(241, 246, 220)');
				$(this).find('input.checkbox').attr('checked',true);
			}
			else
			{
				$(this).css('background-color','rgb(239, 245, 239)');
				$(this).find('input.checkbox').attr('checked',false);
			}
		});*/
	}

	$('form.party_search').mouseenter(function()
	{
		if($('select#party_type').val() == 'Select a party type')
		{
			$('div#party_error').fadeIn('fast');
			$('button.party_search').attr('disabled', 'disabled');
			$('div#party_error').attr('z-index','100');
			$('div#party_error').delay(3000).fadeOut('slow');
			//$('button.party_search').delay(3000).removeAttr('disabled');
		}
	});	
	

	
	if($('li.date input').length > 0)
	{
		$("li.date input").datePicker({startDate: new Date().asString()}).dpSetSelected(new Date().asString());
	}
	
	$("div#venue_photos a").mouseover(function()
	{
		$("a#enlarge img").attr("src", ($(this).attr('href')).replace(".thumb.", "."));
		$("a#enlarge img").attr("alt", ($(this).attr('alt')));
		$("a#enlarge").attr("href", ($(this).attr('href')));
		$("a#enlarge").attr("title", ($(this).attr('title')));
	});
	
	$('div#venue_photos a[rel="venue"]').click(function()
	{
		$("a#enlarge").click();
		
		return false;
	});
	
	//---------------------------------------------END CUSTOM JS//
	
	$("li.subnav").hover(function()
	{
		$(this).addClass("active");
		
		if($("a.nav_search").hasClass("active"))
		{
			$("a.nav_search").removeClass("active");
			$("form.search").animate({ opacity: 0 }, 250);
			
			search_height_timeout = setTimeout(function()
			{
				$("form.search").css("height","0");
			}, 1000);
		}
	},
	function()
	{
		$(this).removeClass("active");
	});
	
	if($("ul#subnav").length > 0)
	{
		if(!$.browser.msie || $.browser.version != 5.5)
		{
			$("ul#subnav").css("display","none");
		}
	}
	
	$('a[rel*="external"]').click(function()
	{
		open($(this).attr('href'));
		return false;
	});
	
	$('a[rel*="shadowbox"]').each(function()
	{
		if(!$(this).hasClass('page_video'))
		{
			var enlarge_img = $(this).attr('rel').split(' ');
			
			if(enlarge_img != 'undefined' && enlarge_img != '')
			{
				$(this).removeAttr('rel');
				
				if(enlarge_img[1] != undefined)
				{
					$(this).attr('rel', enlarge_img[1]+shadowbox_options);
				}
				else
				{
					$(this).attr('rel', enlarge_img[0]+shadowbox_options);
				}
			}
		}
		else
		{
			var video_rel = $(this).attr('rel');
			
			$(this).removeAttr('rel');
			
			$(this).attr('rel', video_rel+shadowbox_options);
		}
	});
	
	$("ul.thumbs li a img").each(function()
	{
		$.preloadImages.add($(this).attr("src").replace("thumb", "mid"));
	});
	
	$("ul.thumbs li a").mouseover(function()
	{
		$("img#full").attr("src", ($(this).attr('href')).replace("full", "mid"));
		$("img#full").attr("alt", ($(this).attr('alt')));
		$("a#enlarge").attr("href", ($(this).attr('href')).replace("thumb", "full"));
		$("a#enlarge").attr("title", ($(this).attr('title')));
	});
	
	$("ul.thumbs li:first a").click(function()
	{
		$("a#enlarge").click();
	});
	
	$("ul.thumbs li a").click(function()
	{
		$("img#full").attr("src", ($(this).attr('href')).replace("full", "mid"));
		$("img#full").attr("alt", ($(this).attr('alt')));
		$("a#enlarge").attr("href", ($(this).attr('href')).replace("thumb", "full"));
		$("a#enlarge").attr("title", ($(this).attr('title')));
		return false;
	});
	
	if($('a.page_video').length > 0)
	{
		var page_video = $('a.page_video').attr('rel').split(' ');
		
		if(page_video != 'undefined' && page_video != '')
		{
			$('a.page_video').removeAttr('rel');
			$('a.page_video').attr('rel', page_video[1]);
		}
	}
	
	if($("li.help a").length > 0)
	{
		$("li.help a").click(function()
		{
			return false;
		});
		
		var help_timeout = 'undefined';
		
		$("li.help a").bind("mouseenter",function()
		{
			if(help_timeout != 'undefined')
			{
				clearTimeout(help_timeout);
			}
			$("li.help div").fadeOut();
			$(this).next().addClass('active').fadeIn();
		}).bind("mouseleave",function()
		{
			help_timeout = setTimeout(function()
			{
				$('li.help div.active').removeClass('active').fadeOut();
			},2000);
		});
		
		$('input').focus(function()
		{
			$('li.help div.active').removeClass('active').fadeOut();
		});
		
		$('select').focus(function()
		{
			$('li.help div.active').removeClass('active').fadeOut();
		});
		
		$('textarea').focus(function()
		{
			$('li.help div.active').removeClass('active').fadeOut();
		});
	}
	
	if($('a[rel*="shadowbox"]').length > 0)
	{
		if($('a.page_video').length > 0)
		{
			Shadowbox.init({
								continuous		:	true,
								ext				: 	{
										                img		:	['png', 'jpg', 'jpeg', 'gif', 'bmp'],
										                swf		:	['swf'],
										                flv		:	['flv', 'm4v', 'mp4'],
										                qt		:	['dv', 'mov', 'moov', 'movie'],
										                wmp		:	['asf', 'wm', 'wmv'],
										                qtwmp	:	['avi', 'mpg', 'mpeg']
										            },
								players			: 	["img", "flv", "iframe"],
								useSizzle		: 	false
							});
		}
		else
		{
			Shadowbox.init({
								continuous		: 	true,
								players			: 	["img", "iframe"],
								useSizzle		: 	false
							});
		}
	}
});
