// Namespace function
function namespace(ns) {
    ns = ns.split('.');
    var cur = window, i;
    while ( i = ns.shift() ) {
        if ( !cur[i] ) cur[i] = {};
        cur = cur[i];
    }
}
// Put all tictoc functions into the tictoc object like:
// quartz.test = function() { alert("Test"); }
namespace("tictoc");


// Setup JS events when the DOM is ready
$(document).ready(function(){
	// Content area images
	$(".pagebody img[@align='left']").addClass("left");
	$(".pagebody img[@align='right']").addClass("right");
	
	// Popup links
	$("a.popup").each(tictoc.website.popup);
	
	// Admin links
	$("a.adminedit").click(tictoc.admin.edit);
	
	if ($("#search_form").length > 0) {
		// Remove submit button from form
		$("#search_form #search_field_submit").hide();
	}
	
	// Gmaps embed code
	tictoc.website.google_maps();
	
	// Registration form locations tidy up
	
	$('span.loc_break').hide();
	
	rest_italy = $('<label class="on_right" for="locations_rest_italy"><input type="checkbox" id="rest_italy" class="grouper" /> Rest of Italy<span class="rest_arrow"></span></label>');
	rest_italy.insertAfter($('label').filter(function(){return $(this).attr('for')=="locations_piedmont"}));
	italy_group = $('#locations_italian_riviera, #locations_venice, #locations_rome, #locations_sicily, #locations_sardinia, #locations_umbria, #locations_piedmont');
	italy_group.each(function(){
		$(this).add($(this).parent()).hide();
	});
	$('#rest_italy').click(function(){
		$(this).add($(this).parent()).hide();
		italy_group.each(function(){
			$(this).add($(this).parent()).show();
		});
	});
	
	spain_group = $('#locations_balearics, #locations_catalonia,#locations_canaries');
	spain_group.each(function(){
		$(this).add($(this).parent()).hide();
	});
	rest_spain = $('<label class="on_right" for="locations_rest_spain"><input type="checkbox" id="rest_spain" class="grouper" /> Rest of Spain<span class="rest_arrow"></span></label>');
	rest_spain.insertAfter($('label').filter(function(){return $(this).attr('for')=="locations_andalusia"}));
	$('#rest_spain').click(function(){
		$(this).add($(this).parent()).add($(this).parent().next('span')).hide();
		spain_group.each(function(){
			$(this).add($(this).parent()).show();
		});
	});
	
	euro_group = $('#locations_algarve,#locations_prague,#locations_switzerland,#locations_morocco,#locations_malta,#locations_cyprus,#locations_santorini,#locations_winery,#locations_winter,#locations_skiing');
	euro_group.each(function(){
		$(this).add($(this).parent()).hide();
	});
	rest_europe = $('<label class="on_right" for="locations_rest_europe"><input type="checkbox" id="rest_europe" class="grouper" /> Rest of Europe<span class="rest_arrow"></span></label>');
	rest_europe.insertAfter($('label').filter(function(){return $(this).attr('for')=="locations_home_counties"}));
	$('#rest_europe').click(function(){
		$(this).add($(this).parent()).add($(this).parent().next('span')).hide();
		euro_group.each(function(){
			$(this).add($(this).parent()).show();
		});
	});
	
	france_group = $('#locations_paris, #locations_burgundy, #locations_aquitaine, #locations_loire');
	france_group.each(function(){
		$(this).add($(this).parent()).hide();
	});
	rest_france = $('<label class="on_right" for="locations_rest_france"><input type="checkbox" id="rest_france" class="grouper" /> Rest of France<span class="rest_arrow"></span></label>');
	rest_france.insertAfter($('label').filter(function(){return $(this).attr('for')=="locations_provence"}));
	$('#rest_france').click(function(){
		$(this).add($(this).parent()).add($(this).parent().next('span')).hide();
		france_group.each(function(){
			$(this).add($(this).parent()).show();
		});
	});
	
	$('<span class="loc_break">&nbsp;</span>').insertAfter(rest_italy).show();
	$('<span class="loc_break">&nbsp;</span>').insertBefore($('#locations_london').parent()).show();
	$('<span class="loc_break">&nbsp;</span>').insertBefore($('#locations_andalusia').parent()).show();
	$('<span class="loc_break">&nbsp;</span>').insertBefore(rest_europe).show();
	
	
	// Hide Labels from particular fieldsets
	$("form.hide_labels").each(tictoc.website.hide_labels);
		
	// TODO: Refactor this into one general method if time
	tictoc.website.search_questions();
	tictoc.website.religious_questions();
	tictoc.website.budget_information();
	
	$("#client_delete_section").click(tictoc.website.client_delete_section);
	$("#client_delete_image input").click(tictoc.website.client_delete_image);
});

$.fn.hoverClass = function(c) {
	return this.each(function(){
		$(this).hover( 
			function() { $(this).addClass(c);  },
			function() { $(this).removeClass(c); }
		);
	});
};


// General website functions
tictoc.website = {
	original_submit_text: "",
  
	google_maps: function() {
		if (($google_map = $("#map")) && $google_map.length > 0) {
			$google_map.googleMap(
				map.lat, 
				map.lng, 
				map.zoom, 
				{ 
					controls: ["GSmallMapControl"], 
					markers: [{lat: map.lat, lng: map.lng, txt: map.txt}]
				}
			);
		}
	},

	client_delete_image: function() {
		if (tictoc.website.original_submit_text == "") {
			tictoc.website.original_submit_text = $("#client_update_button").val();
		}
		
		var any_checked = false;
		// Loop over all checkboxes
		var checkboxes = $(this).parent().parent().parent().find('input');		
		for (var ii=0; ii<checkboxes.length; ii++) {
			any_checked = checkboxes[ii].checked ? true : any_checked;				
		}
		
		$("#client_update_button").val(any_checked ? "Save changes (and permanently delete checked images)" : tictoc.website.original_submit_text); 
	},

	client_delete_section: function() {
		if (tictoc.website.original_submit_text == "") {
			tictoc.website.original_submit_text = $("#client_update_button").val();
		}
		
		$("#client_update_button").val($(this)[0].checked ? "Delete this page permanently" : tictoc.website.original_submit_text); 
	},

	budget_information: function() {
		$("#registration_budget_range").change(function() {
			budget_index = $("#registration_budget_range")[0].selectedIndex;
			(budget_index != 0 && budget_index > 6) ? $("#budget_information").show() : $("#budget_information").hide();
		});
		
		$("#budget_information").hide();
	},

	search_questions: function() {
		$(".referral_source").val() == "Search engine" ? $("#referral_information .if_search_engine").show() : $("#referral_information .if_search_engine").hide();
		
		$(".referral_source").change(function() {			
			if ($(this).val() == "Search engine") {
				$("#referral_information .if_search_engine").slideDown("slow").addClass("slidded");
			} else {
				$("#referral_information .slidded").slideUp("slow").removeClass("slidded");
			}
		});
	},

	religious_questions: function() {	
		$("#wedding_information .religious_list input").each(function() {
			$(this).click(tictoc.website.religious_click);
			
			if ($(this)[0].checked == true && $(this).val() == "Civil") {
				$("#wedding_information .if_religious").hide();
			}
		});
	},
	
	religious_click: function() {
		if ($(this)[0].checked == true && $(this).val() == "Religious") {
			$("#wedding_information .if_religious").slideDown("slow").addClass("slidded");
		} else {
			$("#wedding_information .slidded").slideUp("slow").removeClass("slidded");
		}
	},

	// Hide labels, set text to targets value
	hide_labels: function() {
		$(this).find("label").each(function() {
			$('#' + this.htmlFor).val(this.innerHTML);
			$(this).hide();
			$('#' + this.htmlFor).click(tictoc.website.clearbox);
		})
	},

    // Clear default text in an input box
    clearbox: function() {
        if (!this.default_value) this.default_value = this.value;
    
        if (this.value == '') {
            this.value = this.default_value;
			this.select();
        } else if (this.value == this.default_value) {
            this.value = '';
        }
    },
    
    // Jump to URL
    jump_to_url: function(url) {
        if (url == "") return false;
	    location.href = "/" + url;
    },
    
    // Popup link
    popup: function() {
        this.target = "_blank";
        this.title =  this.title ? this.title += ". " : "";
        this.title += "Link opens in a new window."
    }

};


// Admin functions
tictoc.admin = {
    popup_width: 675,
    popup_height: 650,
    
    edit: function() {
        var win = window.open(this.href, "_adminedit","height=" + tictoc.admin.popup_height + ",width=" + tictoc.admin.popup_width + ",resizable=yes,dependent,scrollbars=yes");
	    win.focus();
	    return false;
    }
};