var runonce = false;
docReady = function(){
	$(".predef_link a").unbind();
	$(".move_link a").unbind();
	$(".fav_link a").unbind();
	$(".saved_link a").unbind();
	$("#hide_predef").unbind();
	$("#favourites_edit").unbind();
	$("#favourites_edit_cancel").unbind(); 
	$("#favourites_edit_cancel").unbind();
	$("#view_public_close").unbind();
	$("#view_public_all").unbind();
	$(".delete_prompt").unbind();
	$('.acc_search_clear').unbind();
	$(".paginate a").unbind();
	$(".loeriespaginate a").unbind();
	
	
	$(".paginate a").click(function() { 	
			var link = $(this).attr("href");
			$brokenstring=link.split("/");
			var page = "/accommodation/results/" + $brokenstring[$brokenstring.length-2] + "/";
			$.ajax({
				type: "GET",  
				url: page,
				cache: false,
				dataType: "html",
				success: function(data, textStatus){ 		
					$("#accommodation_result").replaceWith(data);				
					docReady();
				}
			});		
			return false;
	});
	
	$(".loeriespaginate a").click(function() { 	
			var link = $(this).attr("href");
			$brokenstring=link.split("/");
			var page = "/2010/loeries-accommodation/results/" + $brokenstring[$brokenstring.length-2] + "/";
			// var page = "/loeries-accommodation/results/" + $brokenstring[$brokenstring.length-2] + "/";
			$.ajax({
				type: "GET",  
				url: page,
				cache: false,
				dataType: "html",
				success: function(data, textStatus){ 		
					$("#accommodation_result").replaceWith(data);				
					docReady();
				}
			});		
			return false;
	});
	
	$(".delete_prompt").click(function() {
		if( confirm("Are you sure you want to delete this list?")) {		
			$.ajax({
				type: "GET",  
				url: $(this).attr("href") ,
				cache: false,
				dataType: "html",
				success: function(data, textStatus){ 		
					docReady();
				}
			});
				if($("#itinerary_full").length) {
					$.ajax({
						type: "GET",  
						url: "/itinerary/favourites_full/",
						cache: false,
						dataType: "html",
						success: function(data, textStatus){	
							$("#itinerary_full").replaceWith(data);
							docReady();						
						}
					});
				}
				$.ajax({
					type: "GET",  
					url: "/itinerary/saved_list/",
					cache: false,
					dataType: "html",
					success: function(data, textStatus){	
						$("#itinerary_saved").replaceWith(data);
						docReady();						
					}
				});
		}
		return false;
	});

	$('#itinerary_send_email_form').validate({
		rules: {
			email: {
				required: true,
				email: true
			}
		},
		messages: {
			email: "<br />Please enter a valid email address"
		}
	});
	

	var options = { 
        beforeSubmit:  showRequest,  // pre-submit callback 
        success:       showResponse  // post-submit callback 
    }; 
 
 
	$('#itinerary_send_email_form').ajaxForm(options);
	
	// post-submit callback 
	function showResponse(responseText, statusText)  { 
	    //alert(responseText);
		if(responseText == "success")
			alert('Email sent successfully');
		else			
			alert('Error sending email');
	}
	
	// pre-submit callback 
	function showRequest(formData, jqForm, options) { 
		formData[formData.length ] = {name: 'AJAX', value: '1' } ;
	     var queryString = $.param(formData); 
		// alert('About to submit: \n\n' + queryString); 

	    return true; 
	} 

	fav_link_click = function () { 
		$.ajax({
			type: "GET",  
			url: $(this).attr("href") + "&AJAX=2",
			cache: false,
			dataType: "html",
			success: function(data, textStatus){
				$.ajax({
					type: "GET",  
					url: "/itinerary/favourites/",
					cache: false,
					dataType: "html",
					success: function(data, textStatus){	
						$("#itinerary").html(data);		
						
						$.ajax({
                                                    type:   'GET',
                                                    url:    '/itinerary/count/',
                                                    cache: false,
                                                    dataType: "html",
                                                    success: function(data_count){
                                                        $('#itinerary-count').html(data_count);
                                                    }
                                                });
										
						docReady();
					}
				});
				if($("#itinerary_full").length) {
					$.ajax({
						type: "GET",  
						url: "/itinerary/favourites_full/",
						cache: false,
						dataType: "html",
						success: function(data, textStatus){	
							$("#itinerary_full").replaceWith(data);
							docReady();						
						}
					});
				}
				if($("#itinerary_small").length) {
					$.ajax({
						type: "GET",  
						url: "/itinerary/favourites_header/",
						cache: false,
						dataType: "html",
						success: function(data, textStatus){	
							$("#itinerary_small").html(data);						
							docReady();
						}
					});
				}
			}
		});
		var mylink =this;
		url = $(mylink).attr("href");
		var text = "entry_id=";
		start = url.search (text) + text.length;
		temp = url.substring(start);
		finish = temp.search("&");
		entry = temp.substring(0, finish);
		
		$.ajax({
			type: "GET",  
			url: $(this).attr("href") + "&AJAX=1",
			cache: false,
			dataType: "html",
			success: function(data, textStatus){ 		
				$(".entry" + entry).replaceWith(data);
				docReady();
			}
		});
      	
		return false;
	};
	
	fav_link_small_click = function () { 
		$.ajax({
			type: "GET",  
			url: $(this).attr("href") + "&AJAX=2",
			cache: false,
			dataType: "html",
			success: function(data, textStatus){
				$.ajax({
					type: "GET",  
					url: "/itinerary/favourites_header/",
					cache: false,
					dataType: "html",
					success: function(data, textStatus){	
						$("#itinerary_small").html(data);						
						docReady();
					}
				});
			}
		});
		var mylink =this;
		url = $(mylink).attr("href");
		var text = "entry_id=";
		start = url.search (text) + text.length;
		temp = url.substring(start);
		finish = temp.search("&");
		entry = temp.substring(0, finish);
		
		$.ajax({
			type: "GET",  
			url: $(this).attr("href") + "&AJAX=1",
			cache: false,
			dataType: "html",
			success: function(data, textStatus){ 		
				$(".entry" + entry).replaceWith(data);
				docReady();
			}
		});
				
		return false;
	};
	
	list_view_click = function () { 
		$.ajax({
			type: "GET",  
			url: $(this).attr("href") + "&AJAX=2",
			cache: false,
			dataType: "html",
			success: function(data, textStatus){
				$.ajax({
					type: "GET",  
					url: "/itinerary/favourites_full/",
					cache: false,
					dataType: "html",
					success: function(data, textStatus){	
						$("#itinerary_full").replaceWith(data);
						docReady();						
					}
				});
				$.ajax({
					type: "GET",  
					url: "/itinerary/saved_list/",
					cache: false,
					dataType: "html",
					success: function(data, textStatus){	
						$("#itinerary_saved").replaceWith(data);
						docReady();						
					}
				});
				if($("#itinerary_small").length) {
					$.ajax({
						type: "GET",  
						url: "/itinerary/favourites_header/",
						cache: false,
						dataType: "html",
						success: function(data, textStatus){	
							$("#itinerary_small").html(data);						
							docReady();
						}
					});
				}
			}
		});
				
		return false;
	};
	move_link_click = function () { 
		$.ajax({
			type: "GET",  
			url: $(this).attr("href") + "&AJAX=2",
			cache: false,
			dataType: "html",
			success: function(data, textStatus){
				$.ajax({
					type: "GET",  
					url: "/itinerary/favourites_full/",
					cache: false,
					dataType: "html",
					success: function(data, textStatus){	
						$("#itinerary_full").replaceWith(data);
						docReady();						
					}
				});
			}
		});
				
		return false;
	};
	
	
	$(".fav_link a").click(fav_link_click);
	$(".saved_link a").click(list_view_click);
	$(".move_link a").click(move_link_click);
	
	// if($("#itinerary").length) {
		// $(".fav_link a").click(fav_link_click);
	// }
	// else
	// {		
		// $(".fav_link a").click(list_view_click);
	// }
	add_item_form = function() { 
				$.ajax({
					type: "GET",  
					url: "/itinerary/favourites_full/",
					cache: false,
					dataType: "html",
					success: function(data, textStatus){	
						$("#itinerary_full").replaceWith(data);
						docReady();						
					}
				});
            }
	$('#itinerary_add_items_form').ajaxForm(add_item_form); 
			
	predef_link =	function() {
				var mylink =this;
				var urls = $(mylink).attr("href");
				var start = urls.lastIndexOf("/") + 1;
				var value = urls.substring(start);
				$.ajax({
					type: "GET",  
					url: "/itinerary/favourites_predefined/" + value + "/",
					cache: false,
					dataType: "html",
					success: function(data, textStatus){
						$("#itinerary_predefined_full").replaceWith(data);
						docReady();
					}
				});
				$.ajax({
					type: "GET",  
					url: "/itinerary/predefined_list/",
					cache: false,
					dataType: "html",
					success: function(data, textStatus){	
						$("#itinerary_predefined").replaceWith(data);
						docReady();						
					}
				});
				$.ajax({
					type: "GET",  
					url: "/itinerary/public_list/",
					cache: false,
					dataType: "html",
					success: function(data, textStatus){	
						$("#itinerary_public").replaceWith(data);
						docReady();						
					}
				});
				return false;
	}
	$(".predef_link a").click(predef_link );
	
	$("#hide_predef").click(function() {
		$("#itinerary_predefined_full").hide();
		return false;
	});
	
	save_form = function() { 
		$.ajax({
			type: "GET",  
			url: "/itinerary/favourites_full/",
			cache: false,
			dataType: "html",
			success: function(data, textStatus){	
				$("#itinerary_full").replaceWith(data);
				docReady();						
			}
		});
		$.ajax({
			type: "GET",  
			url: "/itinerary/saved_list/",
			cache: false,
			dataType: "html",
			success: function(data, textStatus){	
				$("#itinerary_saved").replaceWith(data);
				docReady();						
			}
		});
		$.ajax({
			type: "GET",  
			url: "/itinerary/predefined_list/",
			cache: false,
			dataType: "html",
			success: function(data, textStatus){	
				$("#itinerary_predefined").replaceWith(data);
				docReady();						
			}
		});
		
		$.ajax({
			type: "GET",  
			url: "/itinerary/public_list/",
			cache: false,
			dataType: "html",
			success: function(data, textStatus){	
				$("#itinerary_public").replaceWith(data);
				docReady();						
			}
		});
		
	}
	
	
	var saveformOptions = { 
        success:       save_form  // post-submit callback 
    }; 
 
	
	var searchformOptions = { 
        target:        '#itinerary_public',   // target element(s) to be updated with server response 
        beforeSubmit:  searchformRequest,  // pre-submit callback 
        success:       searchformResponse,  // post-submit callback 
		type: 'post',
		url: '/itinerary/public_list/'
    }; 
	
	$("#itinerary_save_form_div").hide();
	$("#favourites_edit").click(function () {
		$("#itinerary_save_form_div").show();
		$("#favourites_header").hide();
                $("#save_favourites_name").focus();
                $("#save_favourites_name").select();
	});
	
	$("#favourites_close").click(function () {
		$('#itinerary_save_form').ajaxSubmit(saveformOptions); 
		$("#favourites_header").show()
		$("#itinerary_save_form_div").hide();
	});	
	
	$(".favourites_public").click(function () {
		$('#itinerary_save_form').ajaxSubmit(saveformOptions); 
	});
	
	$(".favourites_predefined").click(function () {
		$('#itinerary_save_form').ajaxSubmit(saveformOptions); 
	});
	
	$('#itinerary_save_form').ajaxForm(saveformOptions); 
	


	
	$("#contentLoading").hide();  
	$("#i_overlay").hide();  
	
	$().ajaxSend(function(r,s){ 
		
		//$("#i_overlay").show();
		//$("#i_overlay").css("opacity",0.75);
		$("#contentLoading").show();  
		
	});   
	  
	$().ajaxStop(function(r,s){   
	
		//$("#i_overlay").fadeOut("normal");
		$("#contentLoading").fadeOut("normal");  
	});   


 
	// pre-submit callback 
	function searchformRequest(formData, jqForm, options) { 
	    // var queryString = $.param(formData); 
	    // alert('About to submit: \n\n' + queryString); 
	    return true; 
	} 
	 
	// post-submit callback 
	function searchformResponse(responseText, statusText)  { 
	    // alert('status: ' + statusText + '\n\nresponseText: \n' + responseText + 
	    // '\n\nThe output div should have already been updated with the responseText.'); 
	} 

	
	$("#view_public_close").click(function() {
			$.ajax({
				type: "GET",  
				url: "/itinerary/public_list/" ,
				cache: false,
				dataType: "html",
				success: function(data, textStatus){
					$("#itinerary_public").replaceWith(data);
					docReady();
				}
			});
			return false;});
			
	$("#view_public_all").click(function() {
			$.ajax({
				type: "GET",  
				url: "/itinerary/public_list/all/" ,
				cache: false,
				dataType: "html",
				success: function(data, textStatus){
					$("#itinerary_public").replaceWith(data);
					docReady();
				}
			});
			return false;});

	//Accommodation JavaScript
	var clearAccSearch = false;
	acc_search_form = function() { 
				$.ajax({
					type: "GET",  
					url: "/accommodation/results/",
					cache: false,
					dataType: "html",
					success: function(data, textStatus){	
						$("#accommodation_result").replaceWith(data);
						if(clearAccSearch) { 
							$.ajax({
								type: "GET",  
								url: "/accommodation/search/",
								cache: false,
								dataType: "html",
								success: function(data, textStatus){	
									$("#accommodation_search_form").replaceWith(data);
									docReady();	
								}
							});
							clearAccSearch = false;
						}
						else { 
							docReady();	
						}
					}
				});
            };
			
	$('#Accommodation_search_form').ajaxForm(acc_search_form); 
	
	//Loeries Accommodation JavaScript
	var loeriesClearAccSearch = false;
	loeries_search_form = function() { 
				$.ajax({
					type: "GET",  
					url: "/accommodation/results/",
					cache: false,
					dataType: "html",
					success: function(data, textStatus){	
						$("#accommodation_result").replaceWith(data);
						if(loeriesClearAccSearch) { 
							$.ajax({
								type: "GET",  
								url: "/accommodation/search/",
								cache: false,
								dataType: "html",
								success: function(data, textStatus){	
									$("#accommodation_search_form").replaceWith(data);
									docReady();	
								}
							});
							loeriesClearAccSearch = false;
						}
						else { 
							docReady();	
						}
					}
				});
				$("div.inner").removeClass('inner');
            };
			 
	$('#loeries-accommodation_form').ajaxForm(loeries_search_form); 
	$('#accommodation_form').ajaxForm(loeries_search_form);  // actually is loeries

	if(!runonce) {
		$('.acc_search_clear').click( function () { 
			clearAccSearch = true;});
			
			
		$('.loeries_acc_search_clear').click( function () { 
			clearAccSearch = true;})
			
		$('.subregionlist').hide();
		$('.regiontoggle').click(function() {
			href = $(this).attr('href');
			if($(this).html() == "[open]") {
				$(href).show();

				$(this).html("[close]");
			}
			else {
				$(this).html("[open]");
				$(href).hide();
			}
			return false;
		});
		
		if($("#startDate,#endDate").length) {		
			$("#startDate,#endDate").datepicker({ 
				dateFormat : "dd/mm/yy", 
			    beforeShow: customRange, 
			    showOn: "both", 
			    buttonImage: "/visitors/theme/images/calendar.gif", 
			    buttonImageOnly: true 
			});
		}
	}
};

$(document).ready(docReady);
