function initHTTPObject(){
	var xHRObjectSp = false;
	if (window.XMLHttpRequest) {  // Mozilla
		xHRObjectSp = new XMLHttpRequest();
		} 
	else if (window.ActiveXObject) {  // IE
		try {
		xHRObjectSp = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
			try {
				xHRObjectSp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
	if (!xHRObjectSp) {
		alert('Your web browser is not compatible');
		return false;
	}		
	return xHRObjectSp;		
}

function urlCheck() {
	var regmail = /^(http):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?$/;
	if(regmail.test(document.getElementById("url").value)==false){
		errorMessage(eval("linkMessage"),'urlerror');
		return false;
	}else {
		errorMessage("",'urlerror');
		return true;
	}
}

//Initialisation of the jquery plugin compomemts
$(document).ready(function(){
    	//dialog		
    try{
		$('#link_dialog').dialog({
			autoOpen: false,
			resizable:false,
			width: 400,
			buttons: {
				"Ok": function() { 
					var url = urlCheck();
					if (url) {
						postlink();
						$(this).dialog("close");
						document.getElementById("url").value = "";
					}
				}
			}
		})
		//postingdialog	
		$('#postingdialog').dialog({
			autoOpen: false,
			resizable:false,
			width: 400
		});
		//deleting dialog	
		$('#deletedialog').dialog({
			autoOpen: false,
			resizable:false,
			width: 400
		});
		$('#postlink').click(function(){
			$('#link_dialog').dialog('open');
			return false;
		})
	}catch(e){
		alert(e);
	}
	if(document.location.hash=='#share'){
			$('#link_dialog').dialog('open');
			return false;
	};
})

function postlink(){
	var postdiv='';
	var xhr=initHTTPObject();
	xhr.onreadystatechange = function() {
		if(xhr.readyState==0)$('#postingdialog').dialog('open');
		if(xhr.readyState==1)$('#postingdialog').dialog('open');
		if(xhr.readyState==2)$('#postingdialog').dialog('open');
		if(xhr.readyState==3)$('#postingdialog').dialog('open');
		if(xhr.readyState==4 && xhr.status==200){
			$('#postingdialog').dialog('close');
			
		try{
			//Comun XML tags : for video links and other links
			var docXML=xhr.responseXML;
			var postvideourl=docXML.getElementsByTagName("videourl");
			var posturl=docXML.getElementsByTagName("posturl");
			var postid=docXML.getElementsByTagName("postid");
			var postdate=docXML.getElementsByTagName("postdate");
			var postdescription=docXML.getElementsByTagName("description");
			
			
			
			//getting the data into variables : Commun Data for videos and none video links
			var postvideourl=postvideourl.item(0).firstChild.data;
			var posturl=posturl.item(0).firstChild.data;
			var postid=postid.item(0).firstChild.data;
			var postdate=postdate.item(0).firstChild.data;
			var postdescription=postdescription.item(0).firstChild.data;
			

	
			var lastheigh=0;
			var lastwidth=0;
			
			if(postvideourl==1){//if the posted link is a youtube videos
				//Videos XML tags : for video links only
				var postimgurl=docXML.getElementsByTagName("imgurl");
				var postwidth=docXML.getElementsByTagName("width");
				var postheight=docXML.getElementsByTagName("height");
				var postvideoscript=docXML.getElementsByTagName("postvideoscript");
				
				//getting the data into variables : Data for videos only
				var postimgurl=postimgurl.item(0).firstChild.data;
				var postwidth=postwidth.item(0).firstChild.data;
				var postheight=postheight.item(0).firstChild.data;
				var postvideoscript=postvideoscript.item(0).firstChild.data;
				
				if(navigator.appName=='Netscape'){//in case the browser is Firefox, Safari or Chrome
					lastheight=parseInt(postheight)+10;
					lastwidth=parseInt(postwidth);
				}else{
					lastheight=parseInt(postheight);
					lastwidth=parseInt(postwidth);
				}
				
					postdiv+='<div class="linkheader">';
						postdiv+='<span class="postdate">'+postdate+'</span>';	
						postdiv+='<a rel="Delete" class="deletepost" href="#" onclick="deletepost('+postid+');"></a>'
					postdiv+='</div>';
					postdiv+='<div class="linktext"><a target="_blank" href="'+posturl+'">'+posturl+'</a></div>';
					
					postdiv+='<div class="linktext">';
						postdiv+='<table>';
							postdiv+='<tr>';
								postdiv+='<td>';
									postdiv+='<a class="thickbox" href="#TB_inline?height='+lastheight+'\&width='+lastwidth+'\&inlineId='+postid+'&modal=true">';
										postdiv+='<img class="youtubethumb" src="'+decodeURIComponent(postimgurl)+'">';
									postdiv+='</a>';
								postdiv+='</td>';
								postdiv+='<td>'+postdescription+'</td>';
							postdiv+='</tr>';
						postdiv+='</table>';
					postdiv+='</div>';
					
					postdiv+='<div class="videoscript">';
						postdiv+='<div id="'+postid+'" class="youtubevideo">';
							postdiv+='<div style="width:'+lastwidth+'px" class="videoheader">';
								postdiv+='<a class="closevideo" rel="Delete" href="#" onclick="tb_remove();"></a>';
							postdiv+='</div>';
							postdiv+=postvideoscript;
						postdiv+='</div>';
					postdiv+='</div>';
				var alllinksdiv=document.getElementById('alllinks');//Retrieving the alllinksdiv 
				var myElement = document.createElement('div');//Creating the new link post div to hold the new link
				$(myElement).attr('class','linkpost');//Assigning to the div the class attribute
				$(myElement).attr('id','postid'+postid);//Assigning to the div the id attribute
				myElement.innerHTML=postdiv;//Adding the content of postdiv to the linkpost div
				alllinksdiv.insertBefore(myElement,alllinksdiv.firstChild);//Adding the whole post to the alllinksdiv
			}else{
				postdiv+='<div class="linkheader">';
					postdiv+='<span class="postdate">'+postdate+'</span>';	
						postdiv+='<a rel="Delete" class="deletepost" href="#" onclick="deletepost('+postid+');"></a>'
				postdiv+='</div>';
				postdiv+='<div class="linktext"><a target="_blank" href="'+posturl+'">'+posturl+'</a></div>';
					
					//----------------------------
					postdiv+='<div class="linktext">';
								postdiv+=postdescription;
					postdiv+='</div>';
					//----------------------------
		
				var alllinksdiv=document.getElementById('alllinks');//Retrieving the alllinksdiv 
				var myElement = document.createElement('div');//Creating the new link post div to hold the new link
				$(myElement).attr('class','linkpost');//Assigning to the div the class attribute
				$(myElement).attr('id','postid'+postid);//Assigning to the div the id attribute
				myElement.innerHTML=postdiv;//Adding the content of postdiv to the linkpost div
				alllinksdiv.insertBefore(myElement,alllinksdiv.firstChild);//Adding the whole post to the alllinksdiv
				
			}
		}catch(e){//in case the post is disabled by youtube or dailymotion
			alert('the link '+postid+' could not be posted, this post will be deleted');
			alert(e.message);
			deletefromserverwithoutpost(postid);
		}
		initthickbox();
		} 
	}	
	xhr.open("POST","/links/postlink",true);
	xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xhr.send("url="+encodeURIComponent($('#url').val())+"&description="+$('#description').val());
}

function deletepost(postid){
//try{
	var confirmdelet;
	confirmdelet=confirm($('#confirmmsg').html());
	if(confirmdelet){
		deletefromserver(postid);
		return false;
	}
}


function deletefromserver(postid){
	//var content = tinyMCE.getInstanceById("commentarea").getBody().innerHTML;
	var xhr=initHTTPObject();
	xhr.onreadystatechange = function() {
		if(xhr.readyState==0)$('#deletedialog').dialog('open');
		if(xhr.readyState==1)$('#deletedialog').dialog('open');
		if(xhr.readyState==2)$('#deletedialog').dialog('open');
		if(xhr.readyState==3)$('#deletedialog').dialog('open');
		if(xhr.readyState==4 && xhr.status==200){
			try{
				$('#deletedialog').dialog('close');
				var id='postid'+postid;
				var p2 = document.getElementById(id);
				$(p2).hide();
			}catch(e){
				alert(e);
			}
		} 
	}	
	xhr.open("POST","/links/deletelink",true);
	xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xhr.send("postid="+postid);

}


//Deleteing a post without even displying it
function deletefromserverwithoutpost(postid){
	var xhr=initHTTPObject();
	xhr.onreadystatechange = function() {
		if(xhr.readyState==0)$('#deletedialog').dialog('open');
		if(xhr.readyState==1)$('#deletedialog').dialog('open');
		if(xhr.readyState==2)$('#deletedialog').dialog('open');
		if(xhr.readyState==3)$('#deletedialog').dialog('open');
		if(xhr.readyState==4 && xhr.status==200){
			try{
				$('#deletedialog').dialog('close');
			}catch(e){
				alert(e);
			}
		} 
	}	
	xhr.open("POST","/links/deletelink",true);
	xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xhr.send("postid="+postid);

}

function errorMessage(msg,msgType){
	document.getElementById(msgType).innerHTML="<span style=\"font-family:Arial;color:red;font-size:11px\"><b>"+msg+"</b></span>";
}

function savelinkview(linkid) {
	var xhr=initHTTPObject();
	xhr.onreadystatechange = function() {}	
	xhr.open("GET","/publiclinks/savelinkview?linkid="+linkid,true);
    xhr.send(null);
}












 