/*
--------------------------------------- 

Minisom - Javascript

--------------------------------------- 
*/


var site = {}
var emailPattern = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;




if ($.flashPlayerVersion.length > 0) {
	var hasFlash = true;
} else {
	var hasFlash = false;
}

$(document).ready(function(){ 
	$("#gbTopPicture").nivoSlider({
		effect:'fade',
		slices:2,
		animSpeed:1500,
		pauseTime:6000,
		directionNav:false, //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(){}
	});

	$(".highlight").each(function(){
		$(this).hover(function(){
			$(this)
				.find(".tinfo")
				.stop()
				.animate({"backgroundColor":"#C5DFE3"},900);
		},function(){
			$(this).find(".tinfo").stop().animate({"backgroundColor":"#EAECE5"},320);
		})
	});

	$(".hphighlight ").each(function(){
		
		var original;
		
		$(this).hover(function(){
			var that = $(this);
			original = (that.css("backgroundColor"));
			$(this).stop()
					.animate({"backgroundColor":"#828884"},900);
			$(this).find("a").stop().animate({"color":"#fff"},600);
		},function(){
			$(this).stop().animate({"backgroundColor":original},320);
			$(this).find("a").stop().animate({"color":"#5395A2"},320);
		})
	});
	

	site.init(); 
	
})

/**
 * 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.validateOffer = {
	init: function(){
		$("#offers_send").bind('click', function(event){
			site.validate.check("#offers_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.dialogcontrol = {
	
	init: function(){
		$("#overlay,#dformsub").bind("click", function(){ site.dialogcontrol.close()});
		
	},
	
	open: function(options){
		
		var title = options.title;
		var content = options.type;
		
		
		//console.log(options);
		
		switch (content){
			
			case 'video':
			
				var elem = $("#dialog");
				var file = options.file;
				var image = options.thumb;
			
				$("#dialog h2:first").html(title);
				
				if (hasFlash) {
					//elem.addClass("blocked");
					$("#dialog").css({"top":"-200px"})
					$("#dialogVideo").show();
					
					$("#dialogVideo").flash({
						swf: '/flash/jwplayer.swf',
						wmode: 'transparent',
						
						width: site.loadVideos.settings.width,
						height: site.loadVideos.settings.height,
						params: {
							allowFullscreen : 'true',
							allowscriptaccess: 'true',	
							flashvars: {
								
								file: '/images/highlights/'+file,
								image: '/images/highlights/'+image
							}	
						}
						
					})	
				}
				else {
					$(this).html("<a href='#' class='blocked'><img src='/images/highlights/"+image+"'></a>");
				}
				
				break;			
		}
		
		$("#overlay").show();
		$("#dialog").show();
		
		
	},
	close: function(){
		$("#overlay").hide();
		$("#dialog").hide();
	}
}

site.loadVideos = {
	settings: {
		width: '630px',
		height: '350px'
	},
	init: function(){
		
		$(".vlink").each(function(){
			
			elem = $(this);
			
			var topass = new Object();
			
			topass
			
			
			
			topass.title = elem.find("h2:first").html();
			topass.type = "video";
			topass.file = elem.attr("file");
			topass.image = elem.attr("thumb");
			
			
			
			elem.bind("click",function(){
				site.dialogcontrol.open(topass);
			});
					
		})
	}
} 

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").not("#emsg").length > 0) {
			this.settings.errors = true;
			
			$("#emsg").addClass("error").show();
			
			return false;
		}
		else {
			this.settings.errors = false;
			var action = $(this.settings.elements).attr("fact");
			$("#emsg").hide();
			$(this.settings.elements).attr("action",action);
			
			$(this.settings.elements).submit();
		}
	}
}

