var SITEURL = "http://"+document.domain+"/";

this.tooltip = function(){	
	xOffset = 0;
	yOffset = 32;		
	$("div.tooltip").hover(function(e){											  
		this.t = this.title;
		this.title = "";									  
		$("body").append('<div id="tooltip"><div class="title">'+ this.t +'</div></div>');
		var w = $('#tooltip').width();
		xOffset = -(w/2);
		$("#tooltip")
			.css("top",(e.pageY - yOffset) + "px")
			.css("left",(e.pageX + xOffset) + "px")
			.fadeIn("fast");		
    },
	function(){
		this.title = this.t;		
		$("#tooltip").remove();
    });	
	$("div.tooltip").mousemove(function(e){
		$("#tooltip")
			.css("top",(e.pageY - yOffset) + "px")
			.css("left",(e.pageX + xOffset) + "px");
	});			
};

function setImgRollover () {
	$("img.rollover").hover(
		function() { $(this).attr("src", $(this).attr("src").split("_static.").join("_over.")); },
		function() { $(this).attr("src", $(this).attr("src").split("_over.").join("_static.")); }
	);
}

function showQlink () {
	$('#quicklinks').show();
}


/* about us */
function getTeamMemberDescription (id) {
	$.get( SITEURL+"ajax_call.php", { op:"get-team-member-description", id:id }, function(data) {
		$("#left").html(data);
	});
	setImgRollover();
	$("img.active").each(function(){
		$(this).attr("src", $(this).attr("src").split("_over.").join("_static."));
		$(this).removeClass("active");
	});
	$("#team-member-"+id+" img").addClass("active");
	$("#team-member-"+id+" img").attr("src", $("#team-member-"+id+" img").attr("src").split("_static.").join("_over."));
	$("#team-member-"+id+" img").unbind();
}

function manageMemberHistory() {
	/* * jQuery history * */
	/* The following script is added to the document onready queue */
	$(function() {
		/* The "historyadd" event is triggered whenever navigation occurs. */
		$('#left').historyadd(function(e, currentHash, previousHash) {
			getTeamMemberDescription(currentHash);
		});
		/* The "history" event is triggered whenever the user presses the back button or jumps backwards or forwards in their browser's history list. */
		$('#left').history(function(e, currentHash, previousHash) {
			if(currentHash != '') {
				getTeamMemberDescription(currentHash);
			}
		});
		/* Use getCurrent() function to get the hash for the initial load or if the user refreshes the page */
		var initialHash = $.history.getCurrent();
		if(initialHash != '') {
			getTeamMemberDescription(initialHash);
		}
	});
}


/* stir-lecture */
function getLectureDetail (id, display, cat) {
	$("#lectures").html('<div class="purple">Loading...</div>');
	$.get( SITEURL+"ajax_call.php", { op:"get-lecture-detail", id:id, display:display, cat:cat }, function(data) {
		$("#lectures").html(data);
	});
	$("#left li a.active").each(function() {
		$(this).removeClass('active');
	});
	$("#stir-"+id+" a").addClass('active');
}

function manageLecturesHistory(category) {
	/* * jQuery history * */
	/* The following script is added to the document onready queue */
	$(function() {
		var temp = new Array();
		/* The "historyadd" event is triggered whenever navigation occurs. */
		$('#lectures').historyadd(function(e, currentHash, previousHash) {
			temp = currentHash.split('-');
			getLectureDetail(temp[0],temp[1],category);
		});
		/* The "history" event is triggered whenever the user presses the back button or jumps backwards or forwards in their browser's history list. */
		$('#lectures').history(function(e, currentHash, previousHash) {
			if(currentHash == '') {
				currentHash = latestId+'-summary';
			}
			temp = currentHash.split('-');
			getLectureDetail(temp[0],temp[1],category);
		});
		/* Use getCurrent() function to get the hash for the initial load or if the user refreshes the page */
		var initialHash = $.history.getCurrent();
		if(initialHash == '') {
			initialHash = latestId+'-summary';
		}
		temp = initialHash.split('-');
		getLectureDetail(temp[0],temp[1],category);
	});
}

/*
function activeAjaxNav (place) {
	$(place).bind("click", function(){
		$(place+".active").each(function() {
			$(this).removeClass('active');
		});
		$(this).addClass('active');
	});
}
*/


/* news events */
function loadNewsEventsPage (tag, category, page) {
	$("#newsevents").html('<div class="purple">Loading...</div>');
	$.get( SITEURL+"ajax_call.php", { op:"load-news-events-page", tag:tag, category:category, page:page}, function(data) {
		$("#newsevents").html(data);
	});
	$("#left li a.active").each(function() {
		$(this).removeClass('active');
	});
	$("#newsevents-"+id+" a").addClass('active');
}


function getNewsEventsDetail (id) {
	$("#newsevents_detail").html('<div class="purple">Loading...</div>');
	$.get( SITEURL+"ajax_call.php", { op:"get-news-events-detail", id:id }, function(data) {
		$("#newsevents_detail").html(data);
	});
	$("#left li a.active").each(function() {
		$(this).removeClass('active');
	});
	$("#newsevents-"+id+" a").addClass('active');
}

function manageNewsEventsHistory() {
	/* * jQuery history * */
	/* The following script is added to the document onready queue */
	$(function() {
		var temp = new Array();
		/* The "historyadd" event is triggered whenever navigation occurs. */
		$('#newsevents').historyadd(function(e, currentHash, previousHash) {
			temp = currentHash.split('-');
			getNewsEventsDetail(temp[0]);
		});
		/* The "history" event is triggered whenever the user presses the back button or jumps backwards or forwards in their browser's history list. */
		$('#newsevents').history(function(e, currentHash, previousHash) {
			if(currentHash == '') {
				currentHash = latestId+'-newsevents';
			}
			temp = currentHash.split('-');
			getNewsEventsDetail(temp[0]);
		});
		/* Use getCurrent() function to get the hash for the initial load or if the user refreshes the page */
		var initialHash = $.history.getCurrent();
		if(initialHash == '') {
			initialHash = latestId+'-newsevents';
		}
		temp = initialHash.split('-');
		getNewsEventsDetail(temp[0]);
	});
}


/* Load videos */
function loadVideos (slug) {
	$("#vidoe-container").html('<div class="purple">Loading...</div>');
	$.get( SITEURL+"template/education/student-experience-video.php", { op:"load-videos", slug:slug}, function(data) {
		$("#vidoe-container").html(data);
	});
	$("#left li a.active").each(function() {
		$(this).removeClass('active');
	});
	$("#video-"+slug+" a").addClass('active');
}

function manageVideoHistory() {
	/* * jQuery history * */
	/* The following script is added to the document onready queue */
	$(function() {
		/* The "historyadd" event is triggered whenever navigation occurs. */
		$('#videonav').historyadd(function(e, currentHash, previousHash) {
			loadVideos(currentHash);
		});
		/* The "history" event is triggered whenever the user presses the back button or jumps backwards or forwards in their browser's history list. */
		$('#videonav').history(function(e, currentHash, previousHash) {
			if(currentHash == '') {
				currentHash = 'showcase_08_what_I_got_out_of_it';
			}
			loadVideos(currentHash);
		});
		/* Use getCurrent() function to get the hash for the initial load or if the user refreshes the page */
		var initialHash = $.history.getCurrent();
		if(initialHash == '') {
			initialHash = 'showcase_08_what_I_got_out_of_it';
		}
		loadVideos(initialHash);
	});
}






function formSubmit (form, url) {
	$.post(url, $("#"+form).serialize(), function() {
		if(form == "stir_form") {
			$('#'+form).html('<p>Thank you for reserving your seats!</p><p>Click <a href="'+SITEURL+'contact-us/join-our-mailing-list/">here</a> to sign up to our newsletter.</p>');
		} else if(form == "join_form") {
			$('#'+form).html('<h1>&nbsp;</h1><p>Thank you for registering your interest in Design London. We are delighted to welcome you to the Design London community. We will send you priority information and booking for upcoming events, the STIR lectures and other programmes as well as a regular newsletter.</p><p>Kind regards,<br />The Design London Team<br /><a href="mailto:team@designlondon.net">team@designlondon.net</a></p>');
		} else if(form == "competition_form") {
			$('#'+form).html('<h1>&nbsp;</h1><p>Thank you for completing the competition form, your submission has been received.</p><p>Kind regards,<br />The Design London Team<br /><a href="mailto:team@designlondon.net">team@designlondon.net</a></p>');
		}

	});
	return false;
}

function checkEmail(emailField, errorContainer) {
	if (emailField.length == 0) {
		$(errorContainer).html("This field is required.");
		return false;
	}
	if (emailField.indexOf(" ") > -1) {
		$(errorContainer).html("E-mail address has invalid space");
		return false;
	}
	if (emailField.indexOf("/") > -1) {
		$(errorContainer).html("E-mail address has invalid character: /");
		return false;
	}
	if (emailField.indexOf(":") > -1) {
		$(errorContainer).html("E-mail address has invalid character: :");
		return false;
	}
	if (emailField.indexOf(",") > -1) {
		$(errorContainer).html("E-mail address has invalid character: ,");
		return false;
	}
	if (emailField.indexOf(";") > -1) {
		$(errorContainer).html("E-mail address has invalid character: ;");
		return false;
	}
	if (emailField.indexOf("@") < 0) {
		$(errorContainer).html("E-mail address is missing @");
		return false;
	}
	if (emailField.indexOf("\.") < 0) {
		$(errorContainer).html("E-mail address is missing .");
		return false;
	}
	return true;
}



function scrollToTop (speed) {
	if (!speed) speed = "fast";
	$("body").animate({scrollTop:"0px"},speed);
}


$(document).ready(function() {
	tooltip();
	setImgRollover();
	
	$('#homelogo img').hover(
		function() { $('#logo .hidden').show(); },
		function() { $('#logo .hidden').hide(); }
	);
	$('#qlink a').hover(
		function() { $('#qlink img.arrow-header').attr({src:SITEURL+'images/arrow_header_black.gif'}); },
		function() { $('#qlink img.arrow-header').attr({src:SITEURL+'images/arrow_header_white.gif'}); }
	);
	$('#quicklinks').hover(
		function() { $(this).show(); },
		function() { $(this).hide(); }
	);
});
