var agt=navigator.userAgent.toLowerCase();
var is_major = parseInt(navigator.appVersion);
var is_minor = parseFloat(navigator.appVersion);
var is_nav  = ((agt.indexOf('mozilla')!=-1) && (agt.indexOf('spoofer')==-1)
               && (agt.indexOf('compatible') == -1) && (agt.indexOf('opera')==-1)
               && (agt.indexOf('webtv')==-1) && (agt.indexOf('hotjava')==-1));
var is_nav4 = (is_nav && (is_major == 4));
var is_nav6 = (is_nav && (is_major == 5));
var is_nav6up = (is_nav && (is_major >= 5));
var is_ie     = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));

var mouseXStartPos =0;
var mouseYStartPos =0;
var offsetX=0;
var offsetY=0;
var toolTipSTYLE;
isIE=document.all;
var e = new Object();
var popup_mouseX;
var popup_mouseY;
var url = window.location.href;
var hostPort = url.split('index.php')[0]
var image_loading = '<img src='+hostPort+'images/common/loading2.gif>';

var is_tag_process = false;

function GetXmlHttpObject(){
	var xmlHttp=null;
	try
	{
 		// Firefox, Opera 8.0+, Safari
 		xmlHttp=new XMLHttpRequest();
 	}
	catch (e){
	 	//Internet Explorer
	 	try{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	  	}
	 	catch (e){
	  		xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
	  	}
 	}
	return xmlHttp;
}




//////////////////popup//////////////////
function getCursor(event,module,data){


	var ie = navigator.appName == "Microsoft Internet Explorer";
	var	check_tag	   = document.getElementById('tag_friend').value;

  	if(check_tag == "tag_friend"){

  		is_tag_process = true;
		mouseXStartPos = event.offsetX?(event.offsetX):event.pageX-document.getElementById("image").offsetLeft;
		mouseYStartPos = event.offsetY?(event.offsetY):event.pageY-document.getElementById("image").offsetTop;

		var toolTipSTYLE = document.getElementById("pop_tag").style;

		 var content = '<div id="testDiv" style="border:4px solid #B1C3D9;position:relative;left:'+ (mouseXStartPos-35) +';top:'+(mouseYStartPos-35)+';visibility:visible;width:69px;"><table width="69" height="69" border="1"> <tr> <td>&nbsp;</td></tr></table></div>'+
		 '<div id="poppup" style="z-index:10;border:0px solid red;left:'+ (mouseXStartPos-35) +';top:'+(mouseYStartPos-35)+';position:relative;visibility:visible;">'+
			'<form action="javascript:addTag('+"'"+module+"'"+');" name="tag_pic" method="post">'+
			'<div id="tag">'+
	       	' type any name or tag:'+
			'<input type="text" id="inputString" class="text" onkeyup="auto_tag_pic(this.value,'+"'"+data+"'"+');" autocomplete="off" /><br>'+
			'<div id="Layer1" style="position:absolute;  z-index:0 ;width:170px" >'+
			'<div class="suggestionsBox" id="suggestions" style="display:none;float:left">'+
			'<div class="suggestionList" id="autoSuggestionsList"></div>'+
			'</div>'+
			'</div>'+
			'<input style="cursor:pointer" type="image" src="themes/french_tuesdays/gfx/buttons/btn_tag.gif" value="Tag" name="tag" />'+
			'<a style="cursor:pointer"id="popup_exit1" onClick="hidePopup();"><img src="themes/french_tuesdays/gfx/buttons/btn_cancel.gif"></a>'+
			'</div>'+
			'</form>'+
			'</div>';

	      document.getElementById("pop_tag").innerHTML = content;

	}

}

function hidePopup(){
	var testdiv = document.getElementById('testDiv') ;
	var poppup = document.getElementById('poppup') ;
	if(testdiv) testdiv.style.visibility = 'hidden';
	if(poppup) poppup.style.visibility = 'hidden';
	is_tag_process = false;
}

function focus_cursor(){
	check_tag	   = document.getElementById('tag_friend').value;
  	if(check_tag =="tag_friend"){
		var focus_input  = document.getElementById('inputString');
	       focus_input.focus();
       }
}


function showForm(id){

	element      = document.getElementById(id);
	element.style.position   = "absolute";
	element.style.visibility = "visible";
	element.style.display    = '';
	element.style.left = '350px';
    element.style.top  = '250px';
}
function exitForm(id,FormHandler){
	element      = document.getElementById(id);
	element.style.display    = 'none';
	FormHandler.comment.value = '';
    FormHandler.email_addresses.value = '';
}

function send_email(FormHandler,emails,comment){

    var xmlHttpReq = false;
    var self = this;

    if(window.XMLHttpRequest){//Mozilla/Safari
        self.xmlHttpReq = new XMLHttpRequest();
    }
    else if(window.ActiveXObject) {//IE
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('POST', "index.php", true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

    self.xmlHttpReq.send("handler=events&action=send_mail&email="+emails+"&comment="+comment);

}


//////////////////TooTip//////////////////
function toolTip(msg,id){
	if(id!=null){
		
		document.getElementById(id).style.display="";
		document.getElementById(id).innerHTML=msg;
		document.getElementById(id+"id").style.width="auto";

	}
}

function hide_toolTip(id){
	toolTipSTYLE = document.getElementById(id).style;
    toolTipSTYLE.display = "none";
    document.getElementById(id+"id").style.width="69px";

}



//////////////////Remove_Tag/////////////////
function remove_Tag(tag_id,photo_id,style) {

	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null) 	{
		 alert ("Browser does not support HTTP Request")
 		return
 	}
 	if(style == 'galleries'){
 		var url="?page=events_galleries_view_picture&action=remove_tag_galleries&status=remove"
		url=url+"&tag_id="+tag_id+"&photo_id="+photo_id+"&t="+new Date().getTime();
	}
	if(style == 'members'){
		var url="?page=events_member_view&action=remove_tag_members&style=tag_members&status=remove"
		url=url+"&tag_id="+tag_id+"&photo_id="+photo_id+"&t="+new Date().getTime();
	}

	if(style == 'album'){
		var url="?page=photos&section=album&action=remove_tag_album&status=remove"
		url=url+"&tag_id="+tag_id+"&photo_id="+photo_id+"&t="+new Date().getTime();
	}

	xmlHttp.onreadystatechange=function(){
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
			document.getElementById('mem_id_friend').value = '';
			document.getElementById("text_tag_pic").innerHTML=xmlHttp.responseText;
			show_position_tag(style);
 		}
	}
	xmlHttp.open("GET",url,true)
	xmlHttp.send("")
}



//////////////////Add_Tag//////////////////
function addTag(style) {

	var string = encodeURI(document.getElementById('inputString').value);
	var mem_id_friend = document.getElementById('mem_id_friend').value;

	if(string.length > 0 ){

	var x= mouseXStartPos-35;
	var y= mouseYStartPos-35;

	xmlHttp=GetXmlHttpObject()

	if (xmlHttp==null) 	{
		 alert ("Browser does not support HTTP Request")
 		return
 	}
 	var photo_id = document.getElementById('photo_id').value;

 	if(style == 'members'){

	 		var url="?page=events_member_view&action=add_tag_members&style=tag_members&status=insert"
				url=url+"&string="+string+"&x="+x+"&y="+y+"&photo_id="+photo_id+"&mem_id_friend="+mem_id_friend+"&event_pic="+photo_id+"&t=" + new Date().getTime();
	}
	if (style == 'galleries'){

			var url="?page=events_galleries_view_picture&action=add_tag_galleries&status=insert"
				url=url+"&string="+string+"&x="+x+"&y="+y+"&photo_id="+photo_id+"&mem_id_friend="+mem_id_friend+"&t="+new Date().getTime();
	}
	if (style == 'album'){

			var url="?page=photos&section=album&action=save_tag_album&status=insert";
				url=url+"&string="+string+"&x="+x+"&y="+y+"&photo_id="+photo_id+"&mem_id_friend="+mem_id_friend+"&t="+new Date().getTime();
	}

	hidePopup();
	xmlHttp.onreadystatechange=function(){
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
			document.getElementById('mem_id_friend').value = '';
			document.getElementById("inputString").value='';
			
			document.getElementById("text_tag_pic").innerHTML=xmlHttp.responseText;
			show_position_tag(style);
 		}
	}
	xmlHttp.open("GET",url,true)
	xmlHttp.send("")
	}
	else{
		alert("Enter tag Friend");
	}
}

function tag_friend(){
	document.getElementById("tag_friend").value= 'tag_friend';
	is_tag_process = true;
	change_cursor();
}


/*
TODO:Change picture of cursor
Create by:Quoc Bao
*/
function change_cursor(){
	var url = window.location.href;
	var hostPort = url.split('index.php')[0];

	if(is_ie){
		document.body.style.cursor="url("+hostPort+"themes/french_tuesdays/gfx/icons/lcross.cur), crosshair";
	}
	else{
		document.body.style.cursor="crosshair";
	}
	
}

function release_cursor(){
	document.body.style.cursor="default";	
}

function process_cursor(show_cursor){
	if(is_tag_process == true && show_cursor == true){
		change_cursor();
	}
	
	if(show_cursor == false){
		release_cursor();
	}
}

/////////////////////////TAG EVENT PICTURE///////////////////////////////////////

/*
TODO:function show position of tag
Create by:Quoc Bao
*/
function show_position_tag(module,photo_tag_id){


	xmlHttp=GetXmlHttpObject()

	if (xmlHttp==null) 	{
		 alert ("Browser does not support HTTP Request")
 		return
 	}

 	var photo_id = document.getElementById('photo_id').value;

 	if(module=='members'){

 		var url="?page=events_member_view&action=get_position_tag"
			url=url+"&photo_id="+photo_id+"&module="+module+"&t="+new Date().getTime();

 	}
 	if(module=='galleries'){
		if(photo_tag_id != null){
			photo_id = photo_tag_id;			
		}
		
		var url="?page=events_galleries_view_picture&action=get_position_tag"
			url=url+"&photo_id="+photo_id+"&module="+module+"&t="+new Date().getTime();
	}

	if(module=='album'){

		var url="?page=photos&section=album&action=get_position_tag"
			url=url+"&photo_id="+photo_id+"&module="+module+"&t="+new Date().getTime();
	}
	xmlHttp.onreadystatechange=function(){
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
			
			document.getElementById("position_tag").innerHTML=xmlHttp.responseText;
			load_text_tag(photo_id,module);
 	}
}

	xmlHttp.open("GET",url,true)
	xmlHttp.send("")
}

/*
TODO:function load text of tag
Create by:Quoc Bao
*/
function load_text_tag(pho_id,module){

	memID = document.getElementById("memberID").value;
	//alert(memID);
	if(memID > 0)
	{

	xmlHttp=GetXmlHttpObject();
	document.getElementById("text_tag_pic").innerHTML = image_loading;;
	if(xmlHttp==null){
		alert("Browser does not support HTTP Request");
		return
	}
	if(module == "galleries"){
		var url="index.php?page=events_galleries_view_picture&action=load_text_tag"
		url=url+"&pho_id="+pho_id+"&t="+new Date().getTime();
	}
	if(module == "members"){
		var url="index.php?page=events_member_view&action=load_text_tag_members"
		url=url+"&pho_id="+pho_id+"&t="+new Date().getTime();
	}
	if(module == "album"){
		var url="index.php?page=photos&section=album&action=load_text_tag"
		url=url+"&pho_id="+pho_id+"&t="+new Date().getTime();
	}

	xmlHttp.onreadystatechange=function(){
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
			document.getElementById("text_tag_pic").innerHTML=xmlHttp.responseText;
			if(module == "members"){
				infor_pic_contest(pho_id);
			}
	 	}
	}
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}
}

/*
TODO:function load infor of pictures contest
Create by:Quoc Bao
*/

function infor_pic_contest(photo_id){

	var event_id =document.getElementById("event_id").value;
	document.getElementById("vote_pic_contest").innerHTML = image_loading;
	document.getElementById("date_venu_name").innerHTML = image_loading;

	xmlHttp=GetXmlHttpObject()
	if(xmlHttp==null){
		alert("Browser does not support HTTP Request")
		return
 	}

	var url="index.php?page=events_member_view"
	url=url+"&event_id="+event_id+"&photo_id="+photo_id+"&action=view_infor_pic&t="+new Date().getTime();
	xmlHttp.onreadystatechange=function(){
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
			var xmlDoc = xmlHttp.responseXML;
			document.getElementById("date_venu_name").innerHTML = xmlDoc.getElementsByTagName("date_name")[0].childNodes[0].nodeValue;
			var page = xmlDoc.getElementsByTagName("page")[0].childNodes[0].nodeValue;
			var mem_id = xmlDoc.getElementsByTagName("mem_id")[0].childNodes[0].nodeValue;
			var mem_name = xmlDoc.getElementsByTagName("mem_name")[0].childNodes[0].nodeValue;
			var link = '<a href=?page='+page+'&mem_id='+mem_id+'>'+mem_name+'</a>';
			document.getElementById("create_by").innerHTML = link;
			document.getElementById("vote_pic_contest").innerHTML=load_vote_picture(photo_id);
			if(load_vote_picture(photo_id)=="<br>"){
				document.getElementById("vote_pic").style.display='none';
			}
			else{
				document.getElementById("vote_pic").style.display='';
			}
			load_comments_pic();
	  	}
	  	}
	xmlHttp.open("GET",url,true);
	xmlHttp.send(url);

 	}

/*
TODO:function load vote pictures contest
Create by:Quoc Bao
*/
function load_vote_picture(photo_id){

	var restext;
	xmlHttp=GetXmlHttpObject()
	if(xmlHttp==null){
		alert("Browser does not support HTTP Request")
		return
 	}

	var url="index.php?page=events_member_view"
	url=url+"&photo_id="+photo_id+"&action=load_vote_picture&t="+new Date().getTime();

	xmlHttp.open("GET",url,false);
	xmlHttp.send(url);
	return xmlHttp.responseText;
}

var eventPicture = 0;
function vote(ncount,evn_mem_pic,mem_id,action){

	document.getElementById("vote_pic_contest").innerHTML = image_loading;

	xmlHttp=GetXmlHttpObject()
	if(xmlHttp==null){
		alert("Browser does not support HTTP Request")
		return
	}
	var url="index.php?page=events_member_view&action=vote_pic"
	url=url+"&count="+ncount+"&evn_pic="+evn_mem_pic+"&mem_id="+mem_id+"&style="+action+"&t="+new Date().getTime();
	xmlHttp.onreadystatechange=vote_picture;
	eventPicture = evn_mem_pic;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(url);
}
function vote_picture(){
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
		document.getElementById("vote_pic_contest").innerHTML=xmlHttp.responseText;

		// Google Analytics tracking
		if(pageTracker){
			strTrackURL = MainURL+"index.php?page=events_member_view&event_id="+event_id+"&evn_pic="+eventPicture;
			pageTracker._trackPageview(strTrackURL);
			// debug
			// alert("URL tracked: "+strTrackURL);
		}

		/*
		 * Here's the a trick for AJAX-based call of Quantcast
		 */
		var objXmlHttp_qoptions = null;
		try{objXmlHttp_qoptions = new XMLHttpRequest();}
		catch(e){
			try{objXmlHttp_qoptions = new ActiveXObject("Msxml2.XMLHTTP");}
			catch(e){objXmlHttp_qoptions = new ActiveXObject("Microsoft.XMLHTTP");}
		}
		objXmlHttp_qoptions.onreadystatechange = function() {
			if(objXmlHttp_qoptions.readyState == 4){
				strResultValue = '' + objXmlHttp_qoptions.responseText;
				// Execute
				eval(strResultValue);
				if(_qoptions){
					var objXmlHttp_quantcast = null;
					try{objXmlHttp_quantcast = new XMLHttpRequest();}
					catch(e){
						try{objXmlHttp_quantcast = new ActiveXObject("Msxml2.XMLHTTP");}
						catch(e){objXmlHttp_quantcast = new ActiveXObject("Microsoft.XMLHTTP");}
					}
					objXmlHttp_quantcast.onreadystatechange = function() {
						if(objXmlHttp_quantcast.readyState == 4){
							strResultValue = '' + objXmlHttp_quantcast.responseText;
							// Execute
							eval(strResultValue);
						}
					}
					objXmlHttp_quantcast.open("GET","./jscript/quantcast/quant.js",true);
					objXmlHttp_quantcast.send("");
				}
			}
		}
		objXmlHttp_qoptions.open("GET","./jscript/quantcast/quant_qoptions.js",true);
		objXmlHttp_qoptions.send("");

 	}
}


/*
TODO:Load comments of picture contest
Create by:Quoc Bao
*/
function load_comments_pic(){

	var evn_mem_pic = document.getElementById("photo_id").value;
	var event_id = document.getElementById("event_id").value;

	xmlHttp=GetXmlHttpObject();
	if(xmlHttp==null){
		alert("Browser does not support HTTP Request");
		return
}
	var url="index.php?page=events_member_view&action=load_comments_pic"
	url=url+"&evn_mem_pic="+evn_mem_pic+"&event_id_pic="+event_id+"&t="+new Date().getTime();

	xmlHttp.onreadystatechange=function(){
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
			document.getElementById("load_com_pic").innerHTML=xmlHttp.responseText;
 		}
	}
	xmlHttp.open("GET",url,true);
	xmlHttp.send(url);
}

