$(document).ready(function(){	
adminGalleryTooltip();
setExpander();
basculeObserver();
zebraTable();
});

/* bascule list expander in project admin*/
function setExpander(){
	$(".expanCollapseBasculeSelector").click(
	function(){
	if($(this).attr("src") == "/images/admin/expandicon.gif")
	{	$(this).attr("src","/images/admin/collapseicon.gif");
		$(this).next('div').show();
		
	}
	else
	{	$(this).attr("src","/images/admin/expandicon.gif");
		$(this).next('div').hide();
	}
	}
	);
}

function SearchSubmit(frm) 
{	if(frm.elements['textfield'].value.length > 0) 
	{	frm.submit();
	}
}


/* selectall(), selectAllBascule, and bascule observer are used to ensure the working of the bascule list selectors*/
function selectall(control)
{	for(i=0;i<control.length;i++)
	{	control.options[i].selected = true;
	}
}
	
function selectAllBascule(e)
{	
	$(".basculeTarget").each(function(i)
	{	
		selectall(this);
	});
}

function basculeObserver()
{	$(".basculeTarget").eq(0).parents("form").submit(selectAllBascule);
}


function adminGalleryTooltip() {
	
	
	// tooltip menu nav
	$(".imgManagementExistingGallery ul li").hover(function(){
		var myImage= $(this).find("img.gallery_thumb");
		var bigLinkHref = myImage.attr("src").replace("_small","_big");
		var bigLink = $("#gallery_image_tool").find("#imgBigLink");
		var delLink = $("#gallery_image_tool").find("#imgDeleteLink");
		var setMainLink = $("#gallery_image_tool").find("#imgSetAsMain");
		var imageTitle = $("#gallery_image_tool").find("div#title");
		bigLink.attr("href",bigLinkHref);
		delLink.attr("href",delLink.attr("rel")+myImage.attr("id").replace("img",""));
		setMainLink.attr("href",setMainLink.attr("rel")+myImage.attr("id").replace("img",""));
		if(myImage.attr("alt") != "") 
		{	imageTitle.text(myImage.attr("alt"));
		}
		else
		{	imageTitle.text("No name set in current language");
		}
		myImage.after($("#gallery_image_tool"));
		$("#gallery_image_tool").show();
		},
		function(){
		$("#gallery_image_tool").hide();
		}
	);
}
/* ZEBRA TABLE*/
function zebraTable()
{	$(".zebra tr:not(tr tr):even").each(function(){$(this).attr("class", $(this).attr("class") + " even");});
}



/* tabs for language change in admin forms*/
function switchBox(boxToSwitch,language)
{	// Hide all boxes and show the good one
	eval("document.getElementById('block"+boxToSwitch+"EN').style.display = 'none'");
	eval("document.getElementById('block"+boxToSwitch+"NL').style.display = 'none'");
	eval("document.getElementById('block"+boxToSwitch+"FR').style.display = 'none'");
	eval("document.getElementById('block"+boxToSwitch+language+"').style.display = ''");
	
	//change language choice label appearance
	changeLblAppearance(boxToSwitch,language);
	 return false;
}

	function changeLblAppearance(boxToSwitch,language)
	{		
		eval("document.getElementById('lgChoice"+boxToSwitch+"EN').className = ''");
		eval("document.getElementById('lgChoice"+boxToSwitch+"NL').className = ''");
		eval("document.getElementById('lgChoice"+boxToSwitch+"FR').className = ''");
		eval("document.getElementById('lgChoice"+boxToSwitch+language+"').className = 'selected'");
		
	}
	
	function switchLang(boxToSwitch,language)
{	// Hide all boxes and show the good one
	eval("document.getElementById('block"+boxToSwitch+"EN').style.display = 'none'");
	eval("document.getElementById('block"+boxToSwitch+"NL').style.display = 'none'");
	eval("document.getElementById('block"+boxToSwitch+"FR').style.display = 'none'");
	eval("document.getElementById('block"+boxToSwitch+language+"').style.display = ''");
	
	//change language choice label appearance
	changeLblAppearance(boxToSwitch,language);
	 return false;
}

/*
Browser detection function taken from http://www.quirksmode.org/js/detect.html

Used because IE doesn't support DOM standards even though they offer better functionalities,
so we have to use old techniques with that dumb browser. 

*/
	
	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: "Chrome",
			identity: "Chrome"
		},
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari",
			versionSearch: "Version"
		},
		{
			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.userAgent,
			   subString: "iPhone",
			   identity: "iPhone/iPod"
	    },
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();
var isIE = false;
if(BrowserDetect.browser.toLowerCase()=="explorer") 
{	
	isIE = true;
}


/* END OF BROWSER DETECTION  */

var basculelist = {};
basculelist.switchbox =	function (action,box1,box2)	{		var switching = true;
			var i = 0;
			//Normal way is "full list" => "user list"
			source = document.getElementById(box1);
			destination = document.getElementById(box2);
			if(action =="2to1")
			{	tmp = source;
				source=destination;
				destination=tmp;
			}
			for (i=source.length-1;i>= 0;i--)
			{	if(source[i].selected)
				{	if(source[i].value != "")//Don't pass eventual empty values
					{
						var goodPosition = 	getPosition(destination,source[i].text.toLowerCase());
						if(goodPosition>-1)
						{	if(!isIE) // standards compliant => Very logically, doesn't work in IE
							{	destination.add(source[i],destination[goodPosition]);
							}
							else// For IE only
							{	var y=document.createElement('option');
								y.text=source[i].text;
								y.value=source[i].value;
	  							destination.add(y,goodPosition);
	  							source.remove(i);
	  						}
						}
						else
						{	
							if(!isIE)
							{	destination.add(source[i],null); // standards compliant => Very logically, doesn't work in IE
	  						}
	  						else // For IE only
	  						{   var y=document.createElement('option');
								y.text=source[i].text;
								y.value=source[i].value;
	  							destination.add(y); 
	  							source.remove(i);
	  						}
						}
					}
				}
			}
			
	};
	
	function getPosition(destList,optionText)
	{	
		var returnValue = -1;
		if(destList.length > 0)
		{	// to do only if the text inserted is the first item to be added, or should be added at the end of the list
			var tmpIndex = destList.length -1;
			
			while (tmpIndex >= 0)
			{	
				if(destList[tmpIndex].text.toLowerCase() > optionText)
				{	returnValue = tmpIndex;
				}
				else
				{	tmpIndex = 0;//don't finish the loop
				}
				tmpIndex--;
			}
		}
		return returnValue;
	}
