/***************************/
//@Author: Adrian "yEnS" Mato Gondelle
//@website: www.yensdesign.com
//@email: yensamg@gmail.com
//@license: Feel free to use it, but keep this credits please!					
/***************************/

//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;
var popupStatus = 0;

//loading popup with jQuery magic!
function loadPopup(){
	//loads popup only if it is disabled
	if(popupStatus==0){
		$("#backgroundPopup").css({
			"opacity": "0.7"
		});
		$("#backgroundPopup").fadeIn("slow");
		$("#popupContact").fadeIn("slow");
		popupStatus = 1;
	}
}

//disabling popup with jQuery magic!
function disablePopup(){
	//disables popup only if it is enabled
	if(popupStatus==1){
		$("#backgroundPopup").fadeOut("slow");
		$("#popupContact").fadeOut("slow");
		popupStatus = 0;
	}
}

//centering popup
function centerPopup(){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#popupContact").height();
	var popupWidth = $("#popupContact").width();
	//centering
	$("#popupContact").css({
		"position": "absolute",
		"top": windowHeight/2-popupHeight/2,
		"left": windowWidth/2-popupWidth/2
	});
	//only need force for IE6
	
	$("#backgroundPopup").css({
		"height": windowHeight
	});
	
}




function geo() {
	if (!document.getElementsByTagName) // Check for browser support
			return false;
	if (rot13) // Initiate ROT13 only if needed
		var map = rot13init(); 
	var tooltip_js_on = '';
	var tooltip_js_off = '';
	var links = document.getElementsByTagName('a'); // Get all anchors
	function geo_decode(anchor) { // function to recompose the orginal address
		var href = anchor.getAttribute('href');
		if (href != null) {
			var address = href.replace(/.*contact\/([a-z0-9._%-]+)\+([a-z0-9._%-]+)\+([a-z.]+)/i, '$1' + '@' + '$2' + '.' + '$3');
			var linktext = anchor.innerHTML; // IE Fix
			if (href != address) {
				anchor.setAttribute('href','mailto:' + (rot13 ? str_rot13(address,map) : address)); // Add mailto link	
				anchor.innerHTML = linktext; // IE Fix
			}
		}
	}
	for (var l = 0 ; l < links.length ; l++) { // Loop through the anchors
		var anchor = links[l];
		var href = anchor.getAttribute('href');
		if (href != null) {
			var address = href.replace(/.*contact\/([a-z0-9._%-]+)\+([a-z0-9._%-]+)\+([a-z.]+)/i, '$1' + '@' + '$2' + '.' + '$3');
			var linktext = anchor.innerHTML; // IE Fix
			if (href != address) {
				anchor.setAttribute('href','mailto:' + (rot13 ? str_rot13(address,map) : address)); // Add mailto link	
				anchor.innerHTML = address; // IE Fix
			}	

			links[l].onclick = function() { // Encode links when clicked
				geo_decode(this);
			}
			links[l].onmouseover = function() { // Display tooltip when links are hovered
				if (this.getAttribute('title') == tooltip_js_off) { // Set custom tooltip if specified
					this.setAttribute('title',tooltip_js_on);
					geo_decode(this); // Encode links when hovered (so that the address appears correctly in the browser's status bar)
				}
			}
		}
	}
}

var rot13 = 0;

function rot13init() {
	var map = new Array();
	var s = "abcdefghijklmnopqrstuvwxyz";
	for (var i = 0 ; i < s.length ; i++)
		map[s.charAt(i)] = s.charAt((i+13)%26);
	for (var i = 0 ; i < s.length ; i++)
		map[s.charAt(i).toUpperCase()] = s.charAt((i+13)%26).toUpperCase();
	return map;
}

function str_rot13(a,map) {
	var s = "";
	for (var i = 0 ; i < a.length ; i++) {
		var b = a.charAt(i);
		s += (b>='A' && b<='Z' || b>='a' && b<='z' ? map[b] : b);
	}
	return s;
}


function controlValueOnFocus(control) {
	c = $(control);
	if (c.val() == c.defaultValue) {
		c.val() = '';
	}
}

function controlValueOnBlur(control) {
	if (control.value == '') {
		control.value = control.defaultValue;
	}
}

function submitSearch() {
	if ( (document.search.term.value != 'Rechercher...') && (document.search.term.value !='Search...') ) {
		document.search.submit();
	}
}

/*
window.addEvent('domready', function(){
	geo();

	$$('input.text').each(function(el){
		el.onfocus = function() {
			controlValueOnFocus(this);
		},
		el.onblur = function() {
			controlValueOnBlur(this);
		}
	});
});
*/

function submitForm( id ) {
	form = $('#' + id + ' form');
	if (form) {
		var options = { 
			target: '#' + id,
			beforeSubmit: function() {
				$('div.form-status').show();
			},
			success: function() {
				$('div.form-status').hide();
			}
		};
		form.ajaxSubmit(options);
	}
}

/* Video Functions */
function sendEvent(swf,typ,prm) { 
  thisMovie(swf).sendEvent(typ,prm); 
};
function getUpdate(typ,pr1,pr2,swf) {};

function thisMovie(swf) {
  if(navigator.appName.indexOf("Microsoft") != -1) {
    return window[swf];
  } else {
    return document[swf];
  }
};
function loadFile(swf,obj) { 
  thisMovie(swf).loadFile(obj); 
};
function getUpdate(typ,pr1,pr2,swf) { 
 	if (swf == 'jstest') {
 		if (typ == 'state') {
 			if (pr1 == 0) {
				$('#playpause img').attr('src', '/application/lib/images/video-play.png');
	 			$('p#status').html('Video arr&ecirc;t&eacute;e.');
 			}
 			if (pr1 == 2) {
 				$('#playpause img').attr('src', '/application/lib/images/video-stop.png');
 			}
 		}
 		if (typ == 'time') {
 			if (String(pr1).length < 2) { pr1 = '0' + String(pr1); }
 			if (String(pr2).length < 2) { pr2 = '0' + String(pr2); }
// 			$('p#status').html('00:' + pr1 + ' / 00:' + pr2);
 			$('p#status').html('');
 		}
 		if (typ == 'load') {
 			$('p#status').html('Video en chargement: ' + pr1 + '%');
 		}
 	}
};


function selectVideo(id) {
	loadFile('jstest', {
		file: '/public/flash/videos/0-' + id + '.flv',
		image: '/public/images/videos/0-' + id + '.jpg'
	});
	$('a#video-1 img').attr('src', '/application/lib/images/video-button-off.png');
	$('a#video-2 img').attr('src', '/application/lib/images/video-button-off.png');
	$('a#video-' + id + ' img').attr('src', '/application/lib/images/video-button-on.png');
	sendEvent('jstest','play');
}

function playPause() {
	sendEvent('jstest','playpause');
}

jQuery.fn.processImages = function() {
	return this.each(function(){
		el = $(this);
		text = el.attr('alt');
		if (text == 'undefined') {
			text = '';
		}
		width = el.attr('width');
		$("<div class='" + el.attr('class') + "'><img src='" + el.attr('src') + "' /><p style='width: 200px;'>" + text + "</p></div>").replaceAll(this);
	});
}

function setupVideo() {
		if ($('#videocontent').length != 0) {
			var so = new SWFObject('/lib/flash/mediaplayer2.swf','jstest','360','204','7');
  			so.addParam('allowfullscreen','false');
			so.addVariable('file','/public/flash/videos/01.flv');
			so.addVariable('image','/public/images/videos/01.jpg');
			so.addVariable('backcolor','0x000000');
			so.addVariable('autostart','true');
			so.addVariable('shownavigation','false');
			so.addVariable('javascriptid','jstest');
			so.addVariable('enablejs','true');
			so.write('videocontent');
		}
}

$(document).ready(function(){
	var COOKIE_NAME = 'popup_cookie';
    var options = { path: '/', expires: 1 };

    if (showpopup == 0) {
	   	setupVideo();
    }
    else {
	    if ($.cookie(COOKIE_NAME) != null) {
	    	setupVideo();
	    }
	    else {
	    	$.cookie(COOKIE_NAME, 'popup', options);
			centerPopup();
			loadPopup();
	    }
    }

                
	/* Top Fader */
	$('#contents .rotator').cycle('fade');
	
	$('div.selector a').bind('mouseenter',function() {
		el = $(this);
		id = el.attr('id');
		$('.images div').each(function(){
			cel = $(this);
			cid = cel.attr('id');
			cdisplay = cel.css('display');
			if ( (('img'+id) != cid) && (cdisplay != 'none') ) {
				cel.hide();
			}
			if (('img'+id) == cid) {
				cel.show();
			}
		});
	});

  	/* Handle Images */
  	$('img.right-image').processImages();
  	$('img.left-image').processImages();

  	/* Handle Images */
  	$('a.vid').media();

	/* Decrypt mails */
	geo();
	
	
	//LOADING POPUP

	//CLOSING POPUP
	//Click the x event!
	$("#popupContactClose").click(function(){
		disablePopup();
		setupVideo();
	});
	//Click out event!
	$("#backgroundPopup").click(function(){
		disablePopup();
		setupVideo();
	});
	//Press Escape event!
	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disablePopup();
			setupVideo();
		}
	});



});


