/*
--------------------------------------- 

Minisom - Javascript

--------------------------------------- 
*/


var site = {}
var emailPattern = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;

$(document).ready(function(){ site.init();
	$("#gbTopPicture").nivoSlider({
		effect:'fade',
		slices:7,
		animSpeed:1500,
		pauseTime:6000,
		directionNav:true, //Next & Prev
		directionNavHide:false, //Only show on hover
		controlNav:false, //1,2,3...
		pauseOnHover:false, //Stop animation while hovering
		manualAdvance:false, //Force manual transitions
		beforeChange: function(){},
		afterChange: function(){}
	});

	
	Cufon('#header h1, #header .subhead'); 
	Cufon('blockquote div');
	Cufon('.itsoShaded h2, .itsoShaded p,.itsoBlack h2,.right_column h1,.right_column h2 ');
})

/**
 * Init function
 */
site.init = function () {
	for (prop in this) {
		if (typeof this[prop].init == 'function') {
			this[prop].init();
		}
	}
}


/**
 * Clean form fields on focus and set the value
 * again if on blur it's empty
 */
site.cleanFormFields = {
	init: function () {
		$(':input').not("select")
			.bind('focus', function() { if ($(this).val() == $(this).attr('title')) $(this).val(''); })
			.bind('blur', function() { if ($(this).val() == '') $(this).val($(this).attr('title')); });
	}
}


site.textSize = {
	init: function() {
		
		$(".letra .small").bind('click',function(){
			$(".quotebox").animate({lineHeight:"16px"},500);
			$("#left_column,#right_column, #centre_column,#full_column").stop().animate({fontSize:"1em", lineHeight:"1.45em"},500);	
		})
		
		$(".letra .big").bind('click',function(){
			$(".quotebox").animate({lineHeight:"1.8em"},1500);
			$("#left_column,#right_column, #centre_column,#full_column").stop().animate({fontSize:"1.5em", lineHeight:"2em"},1500);	
		})
	}
}



site.validateContact = {
	init: function(){
		$("#cformsub").bind('click', function(event){
			site.validate.check("#contactme_form");
		});
	}
}


site.validateConsult = {
	init: function(){
		$("#booktest").bind('click', function(event){
			site.validate.check("#btf");
		});
	}
}

/**
 * Quick search 
 */
site.searchSectionOptions = {
	settings: {
		displaySelected: '#section_selected',
		valueSelected: '#section_selected_value',
		optionsList: '#section_options',
		showOptionsTrigger: '#section_show_options',
		bodyID: 'body'
	},
	init: function () {
		$(this.settings.showOptionsTrigger).bind('click', this.optionsList.toggle);
		$(this.settings.bodyID).bind('click', this.optionsList.hide);
		$(this.settings.displaySelected).bind('click', this.optionsList.toggle);
		$(this.settings.optionsList).find('a').bind('click', this.optionClick);
		if($('#gallery').length)
		    $('#gallery a').lightBox();
	},
	
	optionsList: {
		show: function(element) {
			$(site.searchSectionOptions.settings.optionsList).slideDown('fast');
		},
		hide: function() {
			$(site.searchSectionOptions.settings.optionsList).slideUp('fast');
		},
		toggle: function() { 
			var element = $(site.searchSectionOptions.settings.optionsList);
			if (element.is(':visible')) {
				site.searchSectionOptions.optionsList.hide();
			} else {
				site.searchSectionOptions.optionsList.show();
			}
			return false;
		}
	},	
	optionClick: function () {
		var display = $(site.searchSectionOptions.settings.displaySelected),
			value = $(site.searchSectionOptions.settings.valueSelected),
			option = $(this);
		display.html(option.html());
		value.val(option.attr('rel'));
		site.searchSectionOptions.optionsList.hide();
		
		return true;
	}
}

site.ctabs = {
	settings: {
		
	},
	init: function(){
		$(".typeswitch[rel]").bind("click",function(){
			
			var toShow = $(this).attr("rel");
			
	
			$(".reimburse[rel]").each(function(){
				if ($(this).attr("rel")==toShow) {
					$(".typeswitch[rel]").removeClass("current");
					$(".typeswitch[rel="+toShow+"]").addClass("current");
					$(this).show();
				}
				else{
					$(this).hide();
				};
			});
		});
	}
}

site.validate = {
	settings: {
		errors: false,
		elements: '#contactme_form'
	},
	
	check: function(elems) {
		this.settings.elements = elems;
		
		$(this.settings.elements + " :input[valid]").each(
			function(){
				
			var cval = $(this).val();
			var ctitle = $(this).attr("title");
	
			switch($(this).attr("valid")){
				case "required":
					if (cval.length >1 && cval != ctitle) {
						$(this).removeClass("error");
					}
					else {
						$(this).addClass("error");
						
					}
					break;
				
			
					
				case "email":
					if (emailPattern.test(cval)  && cval != ctitle) {
						$(this).removeClass("error");
					}
					else {
						$(this).addClass("error");
						
					}
					break;
				
			}
		})		
		if ($(this.settings.elements + " .error").length > 0) {
			this.settings.errors = true;
			return false;
		}
		else {
			this.settings.errors = false;
			$(this.settings.elements).submit();
		}
	}
}
