/*
 * Visual effects
 *
 * Copyright (c) 2011 Senscape s.r.l. (senscape.net)
 *
 */
 
// Ajax calls
 
$.ajax({
	url : "login.php",
	success : function(response) {
		$("#login_ajax").html(response);
	}
});
$.ajax({
	url : "posts.php",
	success : function(response) {
		$("#posts_ajax").html(response);
	},
	error : function(x, s, t) {
		$("#posts_ajax").html("<p>Unable to load data.</p>");
	}
});
$.ajax({
	url : "news.php",
	success : function(response) {
		$("#news_ajax").html(response);
	},
	error : function(x, s, t) {
		$("#news_ajax").html("<p>Unable to load data.</p>");
	}
});
$.ajax({
	url : "news_full.php",
	success : function(response) {
		$("#news_full_ajax").html(response);
	},
	error : function(x, s, t) {
		$("#news_full_ajax").html("Unable to load data.");
	}
});
		
// Site navigation

var tabs = ['home', 'projects', 'team_hype', 'news', 'contact', 'terms', 'policy', 'legal'];
var activeTab = '';

for (i = 0; i < tabs.length; i++) {
	$('#m_' + tabs[i]).click(function() {
		var id = this.id;
		id = id.slice(2);
		switchTab(id);
	});
	$('#m_' + tabs[i]).mouseover(function() { this.style.cursor='pointer'; });
}

function switchTab(tab, instant) {
	var fade_step = 1000;
	
	if (instant == true)
		fade_step = 1;
	
	if (tab != activeTab) {
		activeTab = tab;
		for (i = 0; i < tabs.length; i++) {
			$('#' + tabs[i] + '_container').fadeOut(fade_step);
		}
		$('#' + tab + '_container').fadeIn(fade_step);
	}
}

// Contact form operations

$('#submit').click(function() { sendContactForm(this.form); });
$('#submit').mouseover(function() { 
	this.style.cursor='pointer'; 
	document.getElementById('submit').setAttribute('src','images/btn_submit_lit.png');
});
$('#submit').mouseout(function() { document.getElementById('submit').setAttribute('src','images/btn_submit.png'); });
						 
function showContactForm() {
	document.getElementById('contact_form').style.display = "block";
	document.getElementById('contact_full_ajax').style.display = "none";
}

function sendContactForm() {
	$.post("email.php", {name: contactform.name.value, email: contactform.email.value,
									message: contactform.message.value },
	  function( data ) {
		  $( "#contact_full_ajax" ).empty().append( data );
	  }
	);
	document.getElementById('contact_form').style.display = "none";
	document.getElementById('contact_full_ajax').style.display = "block";
}

// Slideshow operations
var active = 0;
var s = null;
	
function slide() {
	if (active == 0) active = 1;
	else if (active == 1) {
		$('#slide_asylum').fadeOut(1000);
		$('#slide_beyond').fadeIn(1000);
		$('#slide_dagon').fadeOut(1000);

		document.getElementById('button1').setAttribute('src','images/button_normal.png');
		document.getElementById('button2').setAttribute('src','images/button_lit.png');
		document.getElementById('button3').setAttribute('src','images/button_normal.png');
		
		active = 2;
	}
	else if (active == 2) {
		$('#slide_asylum').fadeOut(1000);
		$('#slide_beyond').fadeOut(1000);
		$('#slide_dagon').fadeIn(1000);
		
		document.getElementById('button1').setAttribute('src','images/button_normal.png');
		document.getElementById('button2').setAttribute('src','images/button_normal.png');
		document.getElementById('button3').setAttribute('src','images/button_lit.png');
		
		active = 3;		
	}
	else if (active == 3) {
		$('#slide_asylum').fadeIn(1000);
		$('#slide_beyond').fadeOut(1000);
		$('#slide_dagon').fadeOut(1000);
		
		document.getElementById('button1').setAttribute('src','images/button_lit.png');
		document.getElementById('button2').setAttribute('src','images/button_normal.png');
		document.getElementById('button3').setAttribute('src','images/button_normal.png');
		
		active = 1;		
	}
					
	s = setTimeout("slide();", 5000);
}

slide();

$('#slide_asylum').click(function() {
		switchProjectInstant('asylum');
		switchTab('projects');
	});
$('#slide_asylum').mouseover(function() { this.style.cursor='pointer'; });

$('#slide_beyond').click(function() {
		switchProjectInstant('beyond');
		switchTab('projects');
	});
$('#slide_beyond').mouseover(function() { this.style.cursor='pointer'; });

$('#slide_dagon').click(function() {
		switchProjectInstant('dagon');
		switchTab('projects');
	});
$('#slide_dagon').mouseover(function() { this.style.cursor='pointer'; });


$('#button1').click(function() { active = 3; clearTimeout(s); slide(); });
$('#button1').mouseover(function() { this.style.cursor='pointer'; });

$('#button2').click(function() { active = 1; clearTimeout(s); slide(); });
$('#button2').mouseover(function() { this.style.cursor='pointer'; });

$('#button3').click(function() { active = 2; clearTimeout(s); slide(); });
$('#button3').mouseover(function() { this.style.cursor='pointer'; });


// Flicker effect
var t = null;

function flicker() {
	if (t) clearTimeout(t);
	var nextf= 50 + (Math.random() * 400);
		
	if (active == 1 || active == 2) {
		var opa= Math.random();
		if (document.all) {
			document.getElementById('flicker').style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=" + (opa * 100) + ")";
		} else {
			document.getElementById('flicker').style.opacity=opa;
		}
	}
	else document.getElementById('flicker').style.opacity = 0;
	
	t = setTimeout("flicker();", nextf);
}

flicker(); 

// Preload highlights
var i1= new Image(); i1.src="http://www.senscape.net/images/mnu_home_lit.png";
var i2= new Image(); i2.src="http://www.senscape.net/images/mnu_projects_lit.png";
var i3= new Image(); i3.src="http://www.senscape.net/images/mnu_community_lit.png";
var i4= new Image(); i4.src="http://www.senscape.net/images/mnu_news_lit.png";
var i5= new Image(); i5.src="http://www.senscape.net/images/mnu_company_lit.png";
var i6= new Image(); i6.src="http://www.senscape.net/images/mnu_contact_lit.png";

