// JavaScript Document
	
	var currSection = "";
	var prevSection = "";
	var http_request = false;
	var minx = 325;
	
	var req_lastfm = null;
		
	var BrowserDetect = {
		init: function () {
			this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
			this.version = this.searchVersion(navigator.userAgent)
				|| this.searchVersion(navigator.appVersion)
				|| "an unknown version";
			this.OS = this.searchString(this.dataOS) || "an unknown OS";
		},
		searchString: function (data) {
			for (var i=0;i<data.length;i++)	{
				var dataString = data[i].string;
				var dataProp = data[i].prop;
				this.versionSearchString = data[i].versionSearch || data[i].identity;
				if (dataString) {
					if (dataString.indexOf(data[i].subString) != -1)
						return data[i].identity;
				}
				else if (dataProp)
					return data[i].identity;
			}
		},
		searchVersion: function (dataString) {
			var index = dataString.indexOf(this.versionSearchString);
			if (index == -1) return;
			return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
		},
		dataBrowser: [
			{ 	string: navigator.userAgent,
				subString: "OmniWeb",
				versionSearch: "OmniWeb/",
				identity: "OmniWeb"
			},
			{
				string: navigator.vendor,
				subString: "Apple",
				identity: "Safari"
			},
			{
				prop: window.opera,
				identity: "Opera"
			},
			{
				string: navigator.vendor,
				subString: "iCab",
				identity: "iCab"
			},
			{
				string: navigator.vendor,
				subString: "KDE",
				identity: "Konqueror"
			},
			{
				string: navigator.userAgent,
				subString: "Firefox",
				identity: "Firefox"
			},
			{
				string: navigator.vendor,
				subString: "Camino",
				identity: "Camino"
			},
			{		// for newer Netscapes (6+)
				string: navigator.userAgent,
				subString: "Netscape",
				identity: "Netscape"
			},
			{
				string: navigator.userAgent,
				subString: "MSIE",
				identity: "Explorer",
				versionSearch: "MSIE"
			},
			{
				string: navigator.userAgent,
				subString: "Gecko",
				identity: "Mozilla",
				versionSearch: "rv"
			},
			{ 		// for older Netscapes (4-)
				string: navigator.userAgent,
				subString: "Mozilla",
				identity: "Netscape",
				versionSearch: "Mozilla"
			}
		],
		dataOS : [
			{
				string: navigator.platform,
				subString: "Win",
				identity: "Windows"
			},
			{
				string: navigator.platform,
				subString: "Mac",
				identity: "Mac"
			},
			{
				string: navigator.platform,
				subString: "Linux",
				identity: "Linux"
			}
		]
	
	};
	BrowserDetect.init();
   
	function makePOSTRequest(url, parameters,optional) {
	   
	  http_request = false;
	  if (window.XMLHttpRequest) { // Mozilla, Safari,...
		 http_request = new XMLHttpRequest();
		 if (http_request.overrideMimeType) {
			// set type accordingly to anticipated content type
			//http_request.overrideMimeType('text/xml');
			http_request.overrideMimeType('text/html');
		 }
	  } else if (window.ActiveXObject) { // IE
		 try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		 } catch (e) {
			try {
			   http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		 }
	  }
	  if (!http_request) {
		 alert('Cannot create XMLHTTP instance');
		 return false;
	  }
	  
	  if(optional == null){ 
		
			http_request.onreadystatechange = alertContents;
			http_request.open('POST', url, true);
			http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			http_request.setRequestHeader("Content-length", parameters.length);
			http_request.setRequestHeader("Connection", "close");
			http_request.send(parameters);
	  
		  } else { 
					
					http_request.onreadystatechange = function(){
						
							//alert("yes, " + optional);
							alertContentsMultiple(optional);
						
						};
					http_request.open('POST', url, true);
					http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
					http_request.setRequestHeader("Content-length", parameters.length);
					http_request.setRequestHeader("Connection", "close");
					http_request.send(parameters);
					//alert(parameters);
			  
				}
	}

   function alertContents() {
	   
      if (http_request.readyState == 4) {
		  
         if (http_request.status == 200) {
            
			//	alert(http_request.responseText);
            result = http_request.responseText;
			raw = result.split("~");
			
			// MAKE DIV
			newDiv('CLR_' + raw[0] + '_' + raw[1],raw[0],raw[6],raw[2],raw[3],raw[4],raw[5],raw[1],raw[7]);

         } else {
			    
				alert('There was a problem with the request. : ' + http_request.status + "***");
         
		 }
      }
   }
   
   function alertContentsMultiple(val) {
	   
      if (http_request.readyState == 4) {
         if (http_request.status == 200) {
			 
			//alert(http_request.responseText);
            result = http_request.responseText;
        	// document.getElementById("output_" + val).innerHTML = "<em>item adjusted...</em>";


			 switch(val){
				 case "add":
				 	// IF SUCCES, CHANGE FORM
					document.getElementById("clr_mailing_output").innerHTML = result;
					setTimeout("document.getElementById('clr_mailing_output').innerHTML=''",3000);
				break;				 
				 default:
				 	// fait rien... :)
				 
				 }
			
         } else {
            alert('There was a problem with the request.' + http_request.status + " ***");
         }
      } 
   }
   
   // FUNCTIONS *****************************************************************************************************************************************************************
	
   function makeBlog(workId) {
		
		var poststr = "workId=" + encodeURI( workId );
		makePOSTRequest('pages/blog/_post_get blog.php', poststr, null);
	  
   }
   function makeWork(workId) {
		
		var poststr = "workId=" + encodeURI( workId );
		makePOSTRequest('pages/work/_post_get work.php', poststr, null);
	  
   }
   function makeContact(workId) {
		
		var poststr = "workId=" + encodeURI( workId );
		makePOSTRequest('pages/contact/_post_get contact.php', poststr, null);
	  
   }
   
   function initSection(val){
	   		
			// SET CURR
	   		currSection = val;
			
			// SET PATH
			if(currSection == "selected_work"){ path = "work"; } else { path = currSection; }
			$("clr_"+path).options.selectedIndex =0;
			
			// MENU STUFF
			$("menu_"+currSection).className="menuItemSelected";
			$("menu_"+currSection).innerHTML = val.toUpperCase();
			
			if(prevSection != ""){
						
					$("menu_"+prevSection).className="menuItem";
					$("menu_"+prevSection).innerHTML = '<a href="javascript:initSection(\'' + prevSection + '\');">' + prevSection.toUpperCase() + '</a>';
					
				}
			
			// LOAD CORRESPONDING MENU
			var xChange = new Fx.Style($(currSection), 'left', {duration:750});
			xChange.start((minx+100),minx);
			var alphaChange = new Fx.Style($(currSection), 'opacity', {duration:500});
			alphaChange.start(0,1);			
				
			// LOAD INIT CONTENT
			setTimeout("initContent(" + $("clr_"+path).options[0].value + ")",550);				
			
			// UNLOAD PREVIOUS MENU
			if(prevSection != ""){
				
					var xChange = new Fx.Style($(prevSection), 'left', {duration:750});
					xChange.start(parseInt($(prevSection).style.left),(parseInt($(prevSection).style.left)+100));
					var alphaChange = new Fx.Style($(prevSection), 'opacity', {duration:500});
					alphaChange.start(1,0);
					
				}			

			// PREV
			prevSection = val;
	   
	   }
	   
	function initContent(initid){
		
					var poststr = "workId=" + encodeURI( initid );
					if(currSection == "selected_work"){ path = "work"; } else { path = currSection; }
					makePOSTRequest('pages/' + path + '/_post_get ' + path + '.php', poststr, null);
		
		}   
		
	// ** MAILING ************************************************************************************************************************************************************************
   
   function addMailing() {
		
        document.getElementById("clr_mailing_output").innerHTML = '<span style="padding:3px; font-size:10px; font-style:italic; background-color:#ab8ed3; color:#3b3b3b;">processing...</span>';
		
		var poststr = "email=" + encodeURI( document.getElementById("clr_email").value );
					
		makePOSTRequest('scripts/_post_add item.php', poststr, "add");
	  
   }
		
	function checkMail(email)
	{
		var x = email;
		var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		if (filter.test(x)) return true;
		else return false;
	}
		
	function SetCookie(cookieName,cookieValue,nDays) {
			
			if(checkMail(cookieValue)){
				
				var today = new Date();
				var expire = new Date();
				if (nDays==null || nDays==0) nDays=1;
				expire.setTime(today.getTime() + 3600000*24*nDays);
				document.cookie = cookieName+"="+escape(cookieValue)+ ";expires="+expire.toGMTString();
				
			}
			
		}
		
	// ** API'S *********************************************************************************************************************************************************************
	
	function fetchLastFm(){
		
		var i1;
		var name1;
		var x;
		var lastfmOutput;
		
		x = (window.ActiveXObject) ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
		
		if (x)
		{
			x.onreadystatechange = function ()
			{
				if(x.readyState == 4 && x.status == 200)
				{
					
					var output = "";
					name1 = x.responseXML.getElementsByTagName("name")
					
					for (i1=0;i1<name1.length;i1++)
					{
						if(x.responseXML.getElementsByTagName("artist").length == "0")
						{
							artist = ""
						}
						else
						{
							artist = x.responseXML.getElementsByTagName("artist")[i1].firstChild.nodeValue
						}
						if(x.responseXML.getElementsByTagName("name").length == "0")
						{
							name = ""
						}
						else
						{
							name = x.responseXML.getElementsByTagName("name")[i1].firstChild.nodeValue
						}
						if(x.responseXML.getElementsByTagName("url").length == "0")
						{
							url = ""
						}
						else
						{
							url = x.responseXML.getElementsByTagName("url")[i1].firstChild.nodeValue
						}
						if(x.responseXML.getElementsByTagName("date").length == "0")
						{
							date = ""
						}
						else
						{
							date = x.responseXML.getElementsByTagName("date")[i1].firstChild.nodeValue
						}
						
						output += '<div class="clr_lastfm_line" title="played on: '+date+'">' + artist + ' &bull; ' + name + '</div>\r\n'
					
					} // END FOR
					lastfmOutput.innerHTML = output
					return false
						
				}
			}
			
			lastfmOutput = $('clr_lastfm_output');
			x.open("GET", "scripts/lastfm.php", true)
			x.send(null)		
		
		}
		
	}
	
	function fetchFlickr(){
		
		var i1;
		var name1;
		var x;
		var flickrOutput;
		
		x = (window.ActiveXObject) ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
		
		if (x)
		{
			x.onreadystatechange = function ()
			{
				if(x.readyState == 4 && x.status == 200)
				{
					
					var output = "";
					name1 = x.responseXML.getElementsByTagName("photo")
					
					for (var i=0;i<name1.length;i++)
					{
					
						
						id = x.responseXML.getElementsByTagName("photo")[i].getAttribute('id');
						owner = x.responseXML.getElementsByTagName("photo")[i].getAttribute('owner');
						secret = x.responseXML.getElementsByTagName("photo")[i].getAttribute('secret');
						server = x.responseXML.getElementsByTagName("photo")[i].getAttribute('server');
						farm = x.responseXML.getElementsByTagName("photo")[i].getAttribute('farm');
						title = x.responseXML.getElementsByTagName("photo")[i].getAttribute('title');
						
						output += '<a href="http://www.flickr.com/photos/'+owner+'/'+id+'" target="_blank"><img src="http://farm'+farm+'.static.flickr.com/'+server+'/'+id+'_'+secret+'_s.jpg" alt="" border="3" width="65"></a>\r\n';
					
					} // END FOR
					flickrOutput.innerHTML = output
					return false
						
				}
			}
			
			flickrOutput = $('clr_flickr_output');
			x.open("GET", "scripts/flickr.php", true)
			x.send(null)		
		
		}
		
	}