
$(document).ready(function($){ 
	/* Layout preparation ********************/
	
	// Mask the top login form
	$("#login-form").hide();
	
	// Creates the accordion menu on the left
	$('#left-menu').accordion({ 
		autoHeight: false, 
		navigation: true,
		collapsible: true, 
		active: true,
		header: '.cat-link',
		speed: 'normal'
	});
	
	/* Mouse events **************************/
	// If mouse go on the top line, show the login form
	$("#line-top").mouseover(function(event) {
		$("#login-form").slideDown("normal");
	});
	
	//If mouse go on the page, hide the login form
	$("#page").mouseover(function(event) {
		$("#login-form").slideUp("normal");
	});
	
	//short description on thumbnails
	$(".short-descr").hide();
	$(".img").mouseover(function(event){
		$(this).next().slideDown("normal");
	});
	$(".img").mouseout(function(event){
		$(this).next().slideUp("normal");
	});
	
	//Twitter
	$("#tweet").tweet({
		join_text: "auto",
		username: "edustries",
		avatar_size: null,
		count: 1,
		auto_join_text_default: "", 
		auto_join_text_ed: "",
		auto_join_text_ing: "",
		auto_join_text_reply: "",
		auto_join_text_url: "",
		loading_text: "loading the last tweet..."
	});
	
	//homepage slider
	$('#next').click(homeNext);
	
	$('#previous').click(homePrevious);
	
	function homePrevious() {
		if (counter > 0) {
			counter--;
		}
		//alert(img[counter] - pos[counter] - 10);
		if(pos[counter]>0) {
			$('#slider').animate({
				left: '-' + pos[counter]
				}, 500, function() {
				// Animation complete.
				$('#next').removeClass("grey");
			});
		} else {
			$('#slider').animate({
				left: '0'
				}, 500, function() {
				// Animation complete.
				$('#next').removeClass("grey");
				$('#previous').addClass("grey");
			});
		}
	}
	
	function homeNext() {
		if (counter < (pos.length-1) && (size - pos[counter] >= zone)) {
			counter++;
		}
		//alert(size - pos[counter]);
		if(size - pos[counter] > zone) {
			$('#slider').animate({
				left: '-' + pos[counter]
				}, 500, function() {
				// Animation complete.
				$('#previous').removeClass("grey");
			});
		} else {
			$('#slider').animate({
				left: '-' + (size - zone)
				}, 500, function() {
				// Animation complete.
				$('#previous').removeClass("grey");						
				$('#next').addClass("grey");
			});
		}
	}
	
	shortcut.add("Left",function() {
		homePrevious();
	});
	
	shortcut.add("Right",function() {
		homeNext();
	});
	
	//news ajax
	
	/*$("a.ajax.news").click(function() {
		var pos = $(this).attr('href').lastIndexOf("/");
		function loadContent() {
			$("#news").load(toLoad, function(){
				initializeLoadedContent();
			})
		}
		return false;
	});*/

	
	
	function initializeLoadedContent () {
	
		// Load the content in the div on click on a .ajax link
		$('a.ajax.news').click(function(){
			var pos = $(this).attr('href').lastIndexOf("/");
			pos = pos-2;
			var toLoad = 'index.php?p=' + $(this).attr('href').substr(2,pos) + " #news";
			$('#news').fadeTo(500, 0, loadContent);
			
			window.location.hash = $(this).attr('href').substr(0,$(this).attr('href').length);
			function loadContent() {
				$("#news").load(toLoad, function(){
					initializeLoadedContent();
				})
			}
			return false;
		});
		$('#news').fadeTo(500,1);
		
		hrefUpdate();
	}
	initializeLoadedContent();
	
	
	// Change news links by anchor links
	function hrefUpdate(){
		$(".ajax").each(function()
		{
			this.href = this.href.replace(/^http:\/\/dev\.edustries\.com\/v5\/\?p\=/, "#/") + "/";
		});
		/*$(".ajax").each(function()
		{
			this.href = this.href.replace(/^http:\/\/dev\.edustries\.com\/v5\//, "#/");
		});*/
	}
	//hrefUpdate();
	
	//moving div with scrolling
	var header = 120;
	
	$().scroll(function() {
		var scrollPos = $().scrollTop();
		if(scrollPos > header) {
			$('#moving').css({"position":"relative","top":(scrollPos-header)+"px"});
		} else {
			$('#moving').css({"position":"relative","top":"0px"});
		}
	});

}); 
