// ============================================== //

		// EASING FUNCTIONS //

// ============================================== //

jQuery.easing['jswing'] = jQuery.easing['swing'];

jQuery.extend( jQuery.easing,
{
	def: 'easeOutQuad',
	swing: function (x, t, b, c, d) {
		//alert(jQuery.easing.default);
		return jQuery.easing[jQuery.easing.def](x, t, b, c, d);
	},
	easeInQuad: function (x, t, b, c, d) {
		return c*(t/=d)*t + b;
	},
	easeOutQuad: function (x, t, b, c, d) {
		return -c *(t/=d)*(t-2) + b;
	},
	easeInOutQuad: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t + b;
		return -c/2 * ((--t)*(t-2) - 1) + b;
	},
	easeInOutExpo: function (x, t, b, c, d) {
		if (t==0) return b;
		if (t==d) return b+c;
		if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b;
		return c/2 * (-Math.pow(2, -10 * --t) + 2) + b;
	}
});


// ============================================== //

		// GALLERY FUNCTIONS //

// ============================================== //

jQuery.fn.setGallery = function(options) {
	// set defaults
	var defaults = {
		pictWidth: 700,
		pictHeight: 438,
		animSpeed: 850
	};
	// Extend our default options with those provided.
	var opts = $.extend(defaults, options);
	var j=0;
	//
	return this.each(function(){
		var container = $(this);
		// number of li's in list
		var pictEls = container.find('li').size();
		
		// full width of ul
		var stripViewerWidth = opts.pictWidth*pictEls;
		// Set width of UL
		container.find('ul').css('width' , stripViewerWidth);
		if(pictEls>1){
			var galleryLinks = '<a href="" class="prev">PREVIOUS IMAGE<\/a><p>&nbsp;<\/p><a href="" class="next">NEXT IMAGE<\/a>'
		}else{
			var galleryLinks = '<p>&nbsp;<\/p>'
		};
		//
		$(this).append(galleryLinks);
		// Set buttons
		$(this).find('a.prev').bind('click', function(){
			changeImage(-1);
			return false
		}).parent().find('a.next').bind('click', function(){
			changeImage(1);
			return false
		});
		// Set data
		$(this).find('ul').data('currentState', 0)
		container.find('p').html(container.find('li').eq(0).find('img').attr('alt'));
		if(pictEls<=1){
			container.find('p').css('width', 700);
		}
		// Button function
		function changeImage(incr){
			var cnt = container.find('ul').data('currentState')+incr;
			if(cnt>(pictEls-1)){
				cnt=0;
			};
			if(cnt<0){
				cnt=(pictEls-1);
			};
			//
			var dist = - (opts.pictWidth*cnt);
			container.find('ul').animate({left: dist}, opts.animSpeed, 'easeInOutExpo').data('currentState', cnt);
			//
			container.find('p').html(container.find('li').eq(cnt).find('img').attr('alt'));
		};		
								
	});		
	
};

// ============================================== //

		// PAGE LOAD FUNCTIONS //

// ============================================== //

var flashMovie;
var query_string;
		
function initFlash() {
	if (document.getElementById) {
		flashMovie = document.getElementById('navigation_flash');
	};
};

function flashLoaded(){
	flashMovie.sendVarFromHtml('stop');
	//$("#remap_nav_id").css("width", 230);
	/*
	if(query_string[1].length<3){
		//flashMovie.sendVarFromHtml(query_string[1]);
		flashMovie.sendVarFromHtml("url");
	}else{
		flashMovie.sendVarFromHtml("stop");
	}*/
};

function rnd(){ return String((new Date()).getTime()).replace(/\D/gi,'') }

function loadContent(elementSelector, sourceURL, firstPage) {
	
	var htmlString = '<p class="content_loading">Page content loading. Please wait.<\/p>';
	$('#page_content_holder').html(htmlString);
	//
	if(firstPage){
		$('#master').css('background-color', '#fff');
		$("#navigation_flash").css("width", '230px');
		$("#page_content_holder").css("display", 'block');
	} 
	//
	$(''+elementSelector+'').load(''+sourceURL+'?'+rnd()+' #page_content', null, pageLoaded);
	//alert(elementSelector);
	//alert(sourceURL);
};

function pageLoaded(responseText, textStatus, XMLHttpRequest) {
 	if(textStatus){
		$("div #gallery").setGallery();
		$("div #navigation ul").initToggle();
		initContact();
		setSifr();
 	}
 // alert(this +"  "+responseText +"  "+textStatus +"  "+XMLHttpRequest) // dom element
}


function loadMapBack(){
	$('#page_content_holder').css('display', 'none');
	$('#master').css('background-color', 'transparent');
	$("#navigation_flash").css("width", '950px');
	//
	flashMovie.sendResetFromHtml();
}

// ============================================== //

		// CALENDAR LOAD FUNCTIONS //

// ============================================== //

function loadCalendarPage(targetURL, targetItem){
	//alert(targetURL);
	//alert(targetItem);
	
	$("#page_content a.calendar_active").removeClass("calendar_active");
	targetItem.addClass("calendar_active");
	//
	var htmlString = '<p class="content_loading_calendar">Page content loading. Please wait.<\/p>';
	$('#calender_load_content').html(htmlString);
	//
	$('#calender_load_content').load(''+targetURL+' #calender_load_content', null);
	
}

// ============================================== //

		// EMBED FLASH //

// ============================================== //

function embedFlash(){
	$('#master').css('background-color', 'transparent');
	//
	var flashvars = {
	  xmlNavPath: 'http://www.remapkm.com/includes/xml/remap_navigation_01.xml',
	  whichPage: 'home'
	};
	//
	var params = {
		wmode: 'transparent',
		allowScriptAccess: 'always'
	};
	//
	var attributes = {
	  id: 'navigation_flash'
	};
	//$('#navigation').css('width', '950px').css('background-color', '#333333');
	//$('#navigation_flash').css('width', '900px');
	$('#page_content_holder').css('display', 'none');
	swfobject.embedSWF('http://www.remapkm.com/includes/flash/remap_nav.swf', 'navigation', '950', '1140', '8.0.0','', flashvars, params, attributes);
};

// ============================================== //

		// SET SIFR //

// ============================================== //

function setSifr(){
	/*
	if(typeof sIFR == "function"){
		// This is the preferred "named argument" syntax
		sIFR.replaceElement(named({sSelector:"body h3", sFlashSrc:"http://www.remapkm.com/includes/flash/sifr_cfdin.swf", sColor:"#000000", sLinkColor:"#000000", sBgColor:"#FFFFFF", sHoverColor:"#CCCCCC", sCase: "upper", nPaddingTop:0, nPaddingBottom:20, sFlashVars:"textalign=center&offsetTop=-2"}));	
	};
	*/
};


// ============================================== //

		// SET SIFR //

// ============================================== //

function formSuccess(){
	alert("Thank you, your details have been submitted.");	
};

// ============================================== //

		// NAV TOGGLE //

// ============================================== //

function toggleItem(target){
	var blockTarget = $(target).parent('li:first').children('ul');
	if($(blockTarget).hasClass('entry_open')) {
		$(blockTarget).parent('li').parent('ul').css('border-bottom','#ccc 1px solid');
		$(blockTarget).removeClass('entry_open').hide();
	}else{
		$('div #navigation ul.entry_open').parent('li').parent('ul').css('border-bottom','#ccc 1px solid');
		$('div #navigation ul.entry_open').removeClass('entry_open').hide();
		$(blockTarget).parent('li').parent('ul').css('border-bottom','none');
		$(blockTarget).addClass('entry_open').show();
	}
};

jQuery.fn.initToggle = function(options) {
	//
	return this.each(function(){
		if($(this).attr('id')=='nav_gallery' || $(this).attr('id')=='nav_projects'){
			$(this).css('border-bottom','#ccc 1px solid');
			$(this).children('li').children('ul').hide();
		};
		$(this).children('li').children('a.toggle_nav').bind('click', function(){
			toggleItem($(this));
			return false;
		});				
	});		
};

// ============================================== //

		// CONTACT FORM //

// ============================================== //

function echeck(str) {
	var at="@";
	var dot=".";
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);
	if (str.indexOf(at)==-1){
		return false;
	};
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		return false;
	};
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		return false;
	};
	if (str.indexOf(at,(lat+1))!=-1){
		return false;
	};
	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		return false;
	};
	if (str.indexOf(dot,(lat+2))==-1){
		return false;
	};
	if (str.indexOf(" ")!=-1){
		return false;
	};
	return true;				
};

function initContact() {
	
	if ($('#contactme').length) {  
		
		$('#contactme .form_error').hide();
	 
		$("#contactme .submit").click(function() {  
			
			//alert("form submit");
			
			$('#contactme .form_error').hide();
			// validate and process form here  
			
			var name = $("#contactme #name").val();
			if (name == "") {  
				$("#contactme p#error_name").show();  
				$("input#name").focus();   
			}; 
			
			var surname = $("#contactme #surname").val();
			if (surname == "") {  
				$("#contactme p#error_surname").show();  
				$("input#surname").focus();   
			}; 
			
			var email = $("#contactme #email").val();
			if (email == "") {  
				$("#contactme p#error_email").html("<strong>Please enter an email address.<\/strong>").show();  
				$("input#email").focus();    
			} ;  
			
			if (name == "" || email == "") { 
				return false;
			};
			
			if(echeck(email)==false){
				$("#contactme p#error_email").html("<strong>This address is invalid.<\/strong>").show();
				$("input#email").focus();
				return false;
			};
			
			var dataString = '&name='+ name + '&surname=' + surname + '&email=' + email; 
			
			$('#contactme').html("<p><strong>Thanks for registering, we'll be in touch soon.<\/strong><\/p>");
			
			$.ajax({  
				type: "POST",
				url: "http://www.remapkm.com/includes/php/process.php",
				data: dataString,
				success: function() {
					$('#contactme').html("<p><strong>Thanks for registering, we'll be in touch soon.<\/strong><\/p>");  
				}
			});
			
			/*
			
			$.ajax({  
				type: "POST",  
			 	url: " http://www.remapkm.com/includes/php/process.php",  
				data: dataString,  
				success: function() {  
					$('#contactme').html("<strong>Thanks for registering, we'll be in touch soon.<\/strong>");  
				};  
			});  
			 */
			return false;
		
		});
	}
};

// ============================================== //

		// INITIALISE //

// ============================================== //

$(document).ready(function(){
	//alert(rnd());
	//embedFlash();
	//loadContent('#page_content_holder','gallery_01_gallery.html');
	
	/*
	if(document.location.hash){		
		query_string = document.location.hash
		//alert(query_string);
		//
		query_string = query_string.split('#');	
		//	
		if(query_string[1].length>3){
			//loadContent('#page_content_holder', query_string[1]);
		};
	};
	*/
});
//
$(window).load(function () {
	initFlash();
});