var animation_time = 300; // for 1000 px

function isValidEmailAddress(emailAddress) {
	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	return pattern.test(emailAddress);
}

function open_what(type){
	var ww = Math.floor($(window).width());
	var wh = Math.floor($(window).height());

	var width = Math.floor($('#what_holder').width());
	var height = Math.floor($('#what_holder').height());

	var offset_left = 0;

	var pos_top = 0;
	var pos_left = 0;
	
	if (wh<height){
		$('#wrap').css({'min-height':height+'px'});
	}
	
	if (ww>width){ // horizontal alignment
			$('#what').css({'width':ww+'px'});
			var pages_width = $('#what').outerWidth(true)+$('#about').outerWidth(true)+$('#media').outerWidth(true)+$('#seminars').outerWidth(true)+$('#testimonials').outerWidth(true)+$('#contact').outerWidth(true);
			$('#holder').css({'width':pages_width+'px'});
			$('#wrap').css({'min-width':ww+'px'});
	}
	else{
		$('#wrap').css({'min-width':width+'px'});
		pos_left = Math.floor(-offset_left);
	}
	var current_pos_top = parseInt($('#holder').css('top').replace('px',''));
	var current_pos_left = parseInt($('#holder').css('left').replace('px',''));
	if (current_pos_top!=pos_top || current_pos_left!=pos_left){	
		if (type==1){
			var a_speed = Math.abs(Math.round(((current_pos_left-pos_left)*animation_time)/1000));
			$('html').animate({'opacity':0.5},{queue:false});
			$('#holder').animate({'top':pos_top+'px','left':pos_left+'px'},a_speed,function(){
				$('html').animate({'opacity':1});
				window.location.hash = '#what';
			});
		
		}
		else{
			$('#holder').css({'top':pos_top+'px','left':pos_left+'px'});	
			window.location.hash = '#what';	
		}
	}
}

function open_about(type){
	var ww = Math.floor($(window).width());
	var wh = Math.floor($(window).height());

	var width = Math.floor($('#about_holder').width());
	var height = Math.floor($('#about_holder').height());

	var offset_left = $('#what').outerWidth(true);

	var pos_top = 0;
	var pos_left = 0;

	if (ww>width){ // horizontal alignment
			$('#about').css({'width':ww+'px'});
			var pages_width = $('#what').outerWidth(true)+$('#about').outerWidth(true)+$('#media').outerWidth(true)+$('#seminars').outerWidth(true)+$('#testimonials').outerWidth(true)+$('#contact').outerWidth(true);
			$('#holder').css({'width':pages_width+'px'});
			$('#wrap').css({'min-width':ww+'px'});
			pos_left = Math.floor(-offset_left);
	}
	else{
		$('#wrap').css({'min-width':width+'px'});
		pos_left = Math.floor(-offset_left);
	}
	var current_pos_top = parseInt($('#holder').css('top').replace('px',''));
	var current_pos_left = parseInt($('#holder').css('left').replace('px',''));
	if (current_pos_top!=pos_top || current_pos_left!=pos_left){	
		if (type==1){
			var a_speed = Math.abs(Math.round(((current_pos_left-pos_left)*animation_time)/1000));
			$('html').animate({'opacity':0.5},{queue:false});
			$('#holder').animate({'top':pos_top+'px','left':pos_left+'px'},a_speed,function(){
				$('html').animate({'opacity':1});
				window.location.hash = '#about';
			});
		
		}
		else{
			$('#holder').css({'top':pos_top+'px','left':pos_left+'px'});	
			window.location.hash = '#about';
		}
	}
}

function open_media(type){
	var ww = Math.floor($(window).width());
	var wh = Math.floor($(window).height());

	var width = Math.floor($('#media_holder').width());
	var height = Math.floor($('#media_holder').height());

	var offset_left = $('#what').outerWidth(true)+$('#about').outerWidth(true);

	var pos_top = 0;
	var pos_left = 0;

	if (ww>width){ // horizontal alignment
			$('#media').css({'width':ww+'px'});
			var pages_width = $('#what').outerWidth(true)+$('#about').outerWidth(true)+$('#media').outerWidth(true)+$('#seminars').outerWidth(true)+$('#testimonials').outerWidth(true)+$('#contact').outerWidth(true);
			$('#holder').css({'width':pages_width+'px'});
			$('#wrap').css({'min-width':ww+'px'});
			pos_left = Math.floor(-offset_left);
	}
	else{
		$('#wrap').css({'min-width':width+'px'});
		pos_left = Math.floor(-offset_left);
	}
	var current_pos_top = parseInt($('#holder').css('top').replace('px',''));
	var current_pos_left = parseInt($('#holder').css('left').replace('px',''));
	if (current_pos_top!=pos_top || current_pos_left!=pos_left){	
		if (type==1){
			var a_speed = Math.abs(Math.round(((current_pos_left-pos_left)*animation_time)/1000));
			$('html').animate({'opacity':0.5},{queue:false});
			$('#holder').animate({'top':pos_top+'px','left':pos_left+'px'},a_speed,function(){
				$('html').animate({'opacity':1});
				window.location.hash = '#media';
			});
		
		}
		else{
			$('#holder').css({'top':pos_top+'px','left':pos_left+'px'});	
			window.location.hash = '#media';
		}
	}
}

function open_contact(type){
	var ww = Math.floor($(window).width());
	var wh = Math.floor($(window).height());

	var width = Math.floor($('#contact_holder').width());
	var height = Math.floor($('#contact_holder').height());

	var offset_left = $('#what').outerWidth(true)+$('#about').outerWidth(true)+$('#media').outerWidth(true)+$('#seminars').outerWidth(true)+$('#testimonials').outerWidth(true);

	var pos_top = 0;
	var pos_left = 0;

	if (ww>width){ // horizontal alignment
			$('#contact').css({'width':ww+'px'});
			var pages_width = $('#what').outerWidth(true)+$('#about').outerWidth(true)+$('#media').outerWidth(true)+$('#seminars').outerWidth(true)+$('#testimonials').outerWidth(true)+$('#contact').outerWidth(true);
			$('#holder').css({'width':pages_width+'px'});
			$('#wrap').css({'min-width':ww+'px'});
			pos_left = Math.floor(-offset_left);
	}
	else{
		$('#wrap').css({'min-width':width+'px'});
		pos_left = Math.floor(-offset_left);
	}
	var current_pos_top = parseInt($('#holder').css('top').replace('px',''));
	var current_pos_left = parseInt($('#holder').css('left').replace('px',''));
	if (current_pos_top!=pos_top || current_pos_left!=pos_left){	
		if (type==1){
			var a_speed = Math.abs(Math.round(((current_pos_left-pos_left)*animation_time)/1000));
			$('html').animate({'opacity':0.5},{queue:false});
			$('#holder').animate({'top':pos_top+'px','left':pos_left+'px'},a_speed,function(){
				$('html').animate({'opacity':1});
				window.location.hash = '#contact';
			});
		
		}
		else{
			$('#holder').css({'top':pos_top+'px','left':pos_left+'px'});	
			window.location.hash = '#contact';
		}
	}
}

function open_testimonials(type){
	var ww = Math.floor($(window).width());
	var wh = Math.floor($(window).height());

	var width = Math.floor($('#testimonials_holder').width());
	var height = Math.floor($('#testimonials_holder').height());

	var offset_left = $('#what').outerWidth(true)+$('#about').outerWidth(true)+$('#media').outerWidth(true)+$('#seminars').outerWidth(true);

	var pos_top = 0;
	var pos_left = 0;

	if (ww>width){ // horizontal alignment
			$('#testimonials').css({'width':ww+'px'});
			var pages_width = $('#what').outerWidth(true)+$('#about').outerWidth(true)+$('#media').outerWidth(true)+$('#seminars').outerWidth(true)+$('#testimonials').outerWidth(true)+$('#contact').outerWidth(true);
			$('#holder').css({'width':pages_width+'px'});
			$('#wrap').css({'min-width':ww+'px'});
			pos_left = Math.floor(-offset_left);
	}
	else{
		$('#wrap').css({'min-width':width+'px'});
		pos_left = Math.floor(-offset_left);
	}
	var current_pos_top = parseInt($('#holder').css('top').replace('px',''));
	var current_pos_left = parseInt($('#holder').css('left').replace('px',''));
	if (current_pos_top!=pos_top || current_pos_left!=pos_left){	
		if (type==1){
			var a_speed = Math.abs(Math.round(((current_pos_left-pos_left)*animation_time)/1000));
			$('html').animate({'opacity':0.5},{queue:false});
			$('#holder').animate({'top':pos_top+'px','left':pos_left+'px'},a_speed,function(){
				$('html').animate({'opacity':1},{queue:false});
				window.location.hash = '#testimonials';
			});
		
		}
		else{
			$('#holder').css({'top':pos_top+'px','left':pos_left+'px'});	
			window.location.hash = '#testimonials';
		}
	}
}

function open_seminars(type){
	var ww = Math.floor($(window).width());
	var wh = Math.floor($(window).height());

	var width = Math.floor($('#seminars_holder').width());
	var height = Math.floor($('#seminars_holder').height());

	var offset_left = $('#what').outerWidth(true)+$('#about').outerWidth(true)+$('#media').outerWidth(true);

	var pos_top = 0;
	var pos_left = 0;

	if (ww>width){ // horizontal alignment
			$('#seminars').css({'width':ww+'px'});
			var pages_width = $('#what').outerWidth(true)+$('#about').outerWidth(true)+$('#media').outerWidth(true)+$('#seminars').outerWidth(true)+$('#testimonials').outerWidth(true)+$('#contact').outerWidth(true);
			$('#holder').css({'width':pages_width+'px'});
			$('#wrap').css({'min-width':ww+'px'});
			pos_left = Math.floor(-offset_left);
	}
	else{
		$('#wrap').css({'min-width':width+'px'});
		pos_left = Math.floor(-offset_left);
	}
	var current_pos_top = parseInt($('#holder').css('top').replace('px',''));
	var current_pos_left = parseInt($('#holder').css('left').replace('px',''));
	if (current_pos_top!=pos_top || current_pos_left!=pos_left){	
		if (type==1){
			var a_speed = Math.abs(Math.round(((current_pos_left-pos_left)*animation_time)/1000));
			$('html').animate({'opacity':0.5},{queue:false});
			$('#holder').animate({'top':pos_top+'px','left':pos_left+'px'},a_speed,function(){
				$('html').animate({'opacity':1},{queue:false});
				window.location.hash = '#seminars';
			});
		
		}
		else{
			$('#holder').css({'top':pos_top+'px','left':pos_left+'px'});	
			window.location.hash = '#seminars';
		}
	}
}

var sel_media = 'photos';
var sel_pcateg = 'gym_fit';
var sel_photo = 0;

var act_categ = 0;
var act_photo = 0;

var tn_pages = 0;
var tn_sel_page = 1;

$(document).ready(function(){
	
	$('#photo-box1').html('<img src="images/media/'+photos[sel_pcateg][sel_photo]+'.jpg" alt="" />');
	
	$('#wrap').css({'overflow':'hidden'});
	var ww = Math.floor($(window).width());
	var width = Math.floor($('#what_holder').width());

	if (ww>width) $('#what').css({'width':ww+'px'});
	width = Math.floor($('#about_holder').width());
	if (ww>width) $('#about').css({'width':ww+'px'});
	width = Math.floor($('#media_holder').width());
	if (ww>width) $('#media').css({'width':ww+'px'});
	width = Math.floor($('#contact_holder').width());
	if (ww>width) $('#contact').css({'width':ww+'px'});
	width = Math.floor($('#testimonials_holder').width());
	if (ww>width) $('#testimonials').css({'width':ww+'px'});
	width = Math.floor($('#seminars_holder').width());
	if (ww>width) $('#seminars').css({'width':ww+'px'});

	var pages_width = $('#what').outerWidth(true)+$('#about').outerWidth(true)+$('#media').outerWidth(true)+$('#seminars').outerWidth(true)+$('#testimonials').outerWidth(true)+$('#contact').outerWidth(true);
	$('#holder').css({'width':pages_width+'px'});

	var myFile = document.location.toString();
	if (myFile.match('#')) { 
		var anchor = myFile.split('#')[1];
		switch (anchor){
			case "what": {
				open_what(0);
				break;
			}
			case "about": {
				open_about(0);
				break;
			}
			case "media": {
				open_media(0);
				break;
			}
			case "contact": {
				open_contact(0);
				break;
			}
			case "testimonials": {
				open_testimonials(0);
				break;
			}
			case "seminars": {
				open_seminars(0);
				break;
			}
			default: {
				open_what(0);
				break;
			}
		}
	} 
	else {
		open_what(0);
	}	

	$('li.mm1 a').live('click',function(){
		open_what(1);
		return false;
	});
	$('li.mm2 a').live('click',function(){
		open_about(1);
		return false;
	});
	$('li.mm3 a').live('click',function(){
		open_media(1);
		return false;
	});
	$('li.mm4 a').live('click',function(){
		open_contact(1);
		return false;
	});
	$('li.mm5 a').live('click',function(){
		open_testimonials(1);
		return false;
	});
	$('li.mm6 a').live('click',function(){
		open_seminars(1);
		return false;
	});
	
	$('#form_contact').submit(function(){
		var my_form = document.form_contact;
		var error = 0;
		$('#fmsg').html('');
		if ($('input#contact_name').parent().hasClass('isel')) $('input#contact_name').parent().removeClass('isel');
		if ($('input#contact_email').parent().hasClass('isel')) $('input#contact_email').parent().removeClass('isel');
		if ($('textarea#contact_message').parent().hasClass('isel')) $('textarea#contact_message').parent().removeClass('isel');
		if ($('input#contact_name').val()==''){
			$('input#contact_name').parent().addClass('isel')
			error = 1;
		}
		if (!isValidEmailAddress($('input#contact_email').val())){
			$('input#contact_email').parent().addClass('isel')
			error = 1;
		}
		if ($('textarea#contact_message').val()==''){
			$('textarea#contact_message').parent().addClass('isel')
			error = 1;
		}
		if (error == 1){
			$('#fmsg').html('<em>Please fill in the highlighted fields.</em>');
			return false;
		}
		else{
			var inputs = [];
			$(':input', my_form).each(function() {
				inputs.push(this.name + '=' + encodeURIComponent(this.value));
			})  
			jQuery.ajax({
				data: inputs.join('&'),
				url: this.action,
				timeout: 5000,
				error: function() {
					$('#fmsg').html('<em>Your request could not be sent.<br />Please try again later.</em>');
				},
				success: function(r) { 
					switch(r){
						case "1":
							$('input#contact_name').val('');	
							$('input#contact_email').val('');	
							$('input#contact_phone').val('');	
							$('textarea#contact_message').val('');
							$('#fmsg').html('Your message has been sent.<br />Thank you very much!');
							break;
						default:
							$('input#contact_name').val('');	
							$('input#contact_email').val('');	
							$('input#contact_phone').val('');	
							$('textarea#contact_message').val('');
							$('#fmsg').html('<em>Your request could not be sent.<br />Please try again later.</em>');
							break;
					}
				}
			});
		}
		return false;
	});
	
	$('#what_content li a').live('click',function(){
		var content = $(this).attr('href').replace('#','');
		$('#what_image2').html('<img src="images/'+content+'.jpg" alt="" />');
		$('#what_box_icontent').load('content/'+content+'.html',function(){
			$('#what_image_holder').animate({'left':'-589px'},{'queue':true});
			$('#what_box_holder').animate({'left':'-589px'},function(){
				$('#what_box_icontent').jScrollPane({
					scrollbarWidth:4,
					bottomCapHeight:50,
					dragMaxHeight: 100
				});
			});
		});
		return false;
	});
	
	$('#what_back').live('click',function(){
		$('#what_image_holder').animate({'left':'0px'},{'queue':true});
		$('#what_box_holder').animate({'left':'0px'},function(){
		
		});
		return false;
	});
	
	$('#about_right_content').jScrollPane({
		bottomCapHeight:140,
		scrollbarWidth:4,
		dragMaxHeight: 250
	});

	$('#seminars_icontent').jScrollPane({
		topCapHeight:10,
		bottomCapHeight:300,
		scrollbarWidth:4,
		dragMaxHeight: 130
	});

	$('#testimonials_icontent').jScrollPane({
		topCapHeight:10,
		bottomCapHeight:300,
		scrollbarWidth:4,
		dragMaxHeight: 130
	});

	$('#photo_nleft').css('display','none');
	$('#photo_nright').css('display','none');
	$('#photo_aleft').mouseenter(function(){
		$('#photo_nleft').css('display','block');
	});
	$('#photo_aleft').mouseleave(function(){
		$('#photo_nleft').css('display','none');
	});
	$('#photo_aright').mouseenter(function(){
		$('#photo_nright').css('display','block');
	});
	$('#photo_aright').mouseleave(function(){
		$('#photo_nright').css('display','none');
	});
	
	$('#mcateg li a').live('click',function(){
		var my_media = $(this).attr('href').replace('#','');
		if (my_media != sel_media && act_categ == 0){
			act_categ = 1
			$('#mcateg li a').removeClass('sel');
			$(this).addClass('sel');
			sel_media = my_media;
			var pos_left = '0px';	
			if (sel_media=='videos'){
				pos_left = '-=225px';
			}
			$('#media_menu_holder').animate({'left':pos_left},function(){
				act_categ = 0;
			});
		}
		return false;
	});

	$('#photo_nleft').live('click',function(){
		if (act_photo == 0){
			act_photo = 1;
			var photo_count = photos[sel_pcateg].length;
			var prev_photo = (sel_photo-1>=0)?sel_photo-1:photo_count-1;
			$('#photo_holder').css({'left':'-437px'});
			$('#photo-box2').remove();
			$('#photo-box1').attr('id','photo-box2');
			$('#photo_holder').prepend('<div id="photo-box1" class="photo_box">&nbsp;</div>');				
			$('#photo-box1').html('<img src="images/media/'+photos[sel_pcateg][prev_photo]+'.jpg" alt="" />');
			$('#photo_holder').animate({'left':'0px'},function(){
				sel_photo = prev_photo;
				act_photo = 0;
			});
		}
		return false;
	});

	$('#photo_nright').live('click',function(){
		if (act_photo == 0){
			act_photo = 1;
			var photo_count = photos[sel_pcateg].length;
			var next_photo = (sel_photo+1<=(photo_count-1))?sel_photo+1:0;
			$('#photo-box2').html('<img src="images/media/'+photos[sel_pcateg][next_photo]+'.jpg" alt="" />');
			$('#photo_holder').animate({'left':'-=437px'},function(){
				sel_photo = next_photo;
				$('#photo_holder').css({'left':'0px'});
				$('#photo-box1').remove();
				$('#photo-box2').attr('id','photo-box1');
				$('#photo_holder').append('<div id="photo-box2" class="photo_box">&nbsp;</div>');				
				act_photo = 0;			
			});
		}
		return false;
	});
	
	$('#tn_nav_prev').live('click',function(){
		if ($('#tn_iholder').children('ul').hasClass('tn_videos')){
			tn_pages = $('#tn_iholder').children('ul').size();
		
		}
		var ppage = (tn_sel_page-1>=1)?tn_sel_page-1:0;
		if (ppage!=0 && act_categ==0){
			act_categ = 1;
			$('#tn_iholder').animate({'left':'+=215px'},function(){
				tn_sel_page = ppage;
				act_categ = 0;
			});		
		}
		return false;
	});

	$('#tn_nav_next').live('click',function(){
		if ($('#tn_iholder').children('ul').hasClass('tn_videos')){
			tn_pages = $('#tn_iholder').children('ul').size();
		
		}
		var npage = (tn_sel_page+1<=tn_pages)?tn_sel_page+1:0;
		if (npage!=0 && act_categ==0){
			act_categ = 1;
			$('#tn_iholder').animate({'left':'-=215px'},function(){
				tn_sel_page = npage;
				act_categ = 0;
			});		
		}
		return false;
	});

	$('ul.tn_photos li a').live('click',function(){
		$('#photo_aleft').css({'display':'block'});
		$('#photo_aright').css({'display':'block'});
		var my_id = $(this).attr('class').replace('tn','');
		if (act_photo == 0){
			act_photo = 1;
			$('#photo-box2').html('<img src="images/media/'+photos[sel_pcateg][my_id]+'.jpg" alt="" />');
			$('#photo_holder').animate({'left':'-=437px'},function(){
				$('#photo_holder').css({'left':'0px'});
				$('#photo-box1').remove();
				$('#photo-box2').attr('id','photo-box1');
				$('#photo_holder').append('<div id="photo-box2" class="photo_box">&nbsp;</div>');				
				sel_photo = parseInt(my_id);
				act_photo = 0;			
			});
		}
		return false;
	});

	$('ul.tn_videos li a').live('click',function(){
		$('#photo_aleft').css({'display':'none'});
		$('#photo_aright').css({'display':'none'});
		var video_file = $(this).attr('href').replace('#','');
		if (act_photo == 0){
			act_photo = 1;
			$('#photo-box2').html('<div id="video">&nbsp;</div>');
			$('#photo_holder').animate({'left':'-=437px'},function(){
				$('#photo_holder').css({'left':'0px'});
				$('#photo-box1').remove();
				$('#photo-box2').attr('id','photo-box1');
				$('#photo_holder').append('<div id="photo-box2" class="photo_box">&nbsp;</div>');				
				act_photo = 0;			
				var flashvars = {};
				var flashvars = {dflv:video_file,dtype:"FLVL",dprev:""};
				var params = {};
				params.wmode = "transparent";
				params.allowfullscreen = "true";
				var attributes = {};
				attributes.id = "video";
				swfobject.embedSWF("video.swf", "video", "437", "328", "9.0.0", false, flashvars, params, attributes);
			});
		}
		return false;
	});


	$('#media_menu_holder a').live('click',function(){
		if (act_categ==0){
			act_categ = 1;
			var categ = $(this).parent().parent().attr('id').replace('_categ','');
			var content = $(this).attr('href').replace('#','');
			$('#media_menu_holder ul li').removeClass('sel');
			$(this).parent().addClass('sel');
			if (categ=='photos'){
				$('#tn_nav').css({'display':'block'});
				$('#tn_holder').html('');
				var c = '<div id="tn_iholder" style="width:'+(Math.ceil(photos[content].length/9)*225)+'px">';
				var page_count = 1;
				c += '<ul class="thumbnails tn_photos" id="tnp_'+page_count+'">';
				for (i=0;i<photos[content].length;i++){
					if (i%9==0 && i!=0){
						page_count++;
						c += '</ul><ul class="thumbnails tn_photos" id="tnp_'+page_count+'">';	
					}
					c += '<li><a href="#" class="tn'+i+'"><img src="images/media/'+photos[content][i]+'_tn.jpg" alt="" /></a></li>';
				}
				c += '</ul></div>';
				$('#tn_holder').html(c);
				if (photos[content].length>9){
					$('#tn_nav').css({'display':'block'});
				}
				else{
					$('#tn_nav').css({'display':'none'});
				}
				$('#media_icateg_holder').animate({'left':'-=225px'},function(){
					tn_pages = page_count;
					tn_sel_page = 1;
					act_categ = 0;
					sel_pcateg = content;
				});
			}
			else{
				$('#tn_nav').css({'display':'none'});
				$('#tn_holder').load('content/'+categ+'/'+content+'.html',function(){		
					if ($('#tn_iholder').children('ul').size()>1){
						$('#tn_nav').css({'display':'block'});				
					}
					$('#media_icateg_holder').animate({'left':'-=225px'},function(){
						act_categ = 0;
						tn_sel_page = 1;
					});
				});
			}
		}
		return false;
	});
	
	$('#categ_back').live('click',function(){
		if (act_categ==0){
			act_categ = 1;
			$('#media_icateg_holder').animate({'left':'0px'},function(){
				act_categ = 0;
			});
		}
		return false;
	});
});

$(window).load(function () {
  $('#page_loader').fadeOut(function(){
  	$(this).remove();
  });
});

