/******************
Continent Navigation
*******************/
var cN = {
	
	init: function() {
		var macIE = ((navigator.appVersion.indexOf("MSIE")>-1) && (navigator.appVersion.indexOf("Mac") >= 0));
		if (!document.getElementsByTagName || !document.getElementById || !document.createElement || !document.appendChild || macIE){
			return;		
		}
		if(!document.getElementById('continent_nav')){
			return;	
		}
		cN.getContinents();
		cN.addShowHide();
		cN.createDropDowns();
		//set the default selected;
		cN.updateNav();
	},
	
	getContinents: function(){
		var navUl = document.getElementById('continent_nav');
		var navLis = navUl.getElementsByTagName('li');
		for (var i = 0; i < navLis.length; i++) {
			var li = navLis[i];
			cN.continents[i] = li.id;
		}
	},
	
 	createElem: function(Elem, Type, Value, Name, Id, Class, Parent, Action, Method, Src, Alt){
		if(!Elem || !Parent) return;
		var temp = document.createElement(Elem);
		if(Type) temp.setAttribute('type', Type);
		if(Name) temp.name = Name;
		if(Value) temp.value = Value;
		if(Id) temp.id = Id;
		if(Class) temp.className = Class;
		if(Action) temp.action = Action;
		if(Method) temp.method = Method;
		if(Src) temp.src = Src;
		if(Alt) temp.alt = Alt;
		Parent.appendChild(temp);
		return temp;
	},
	
	
	createDropDowns: function(){
		for (var i = 0; i < cN.continents.length; i++) {
			/*create the form*/
			var navForm = cN.createElem("form",null,null,cN.continents[i]+"Form",cN.continents[i]+"Form",null,document.getElementById(cN.continents[i]),"/gallery/","get");
			util.addEvent(navForm, 'submit', util.formatSubmit, false);
			var continent = cN.createElem("input","hidden",i+1,"continent",null,null,navForm);
			var countrySelect = cN.createElem("select",null,null,"country",cN.continents[i] + "CountrySel",null,navForm);
			var ie5Dummy = cN.createElem("option",null,null,null,null,null,countrySelect);
			var citySelect = cN.createElem("select",null,null,"city",cN.continents[i] + "CitySel",null,navForm);
			ie5Dummy = cN.createElem("option",null,null,null,null,null,citySelect);
			var navSubmit = cN.createElem("input","image","submit","submit",null,"submit",navForm,null,null,"/i/nav/search.gif","search");
			countrySelect.options[0] = new Option("all countries", "0");
			countrySelect.onchange = cN.updateSelect;
			citySelect.options[0]=new Option("all locations", "0");
			citySelect.disabled = true;
			for (j=0; j<eval(cN.continents[i]).length; j++){
					countrySelect.options[j+1]=new Option(eval(cN.continents[i])[j][0].text,eval(cN.continents[i])[j][0].value);
			}
			if(countrySelect.options.length == 2) {
				countrySelect.selectedIndex = 1;
				countrySelect.onchange();
			}
		}
	},
	
	updateSelect: function(){
		var continent = this.id.substring(0, this.id.indexOf("CountrySel"));
		var selectedCountry  = ((this.selectedIndex - 1) >= 0)?this.selectedIndex - 1: "0";
		var citySelect = document.getElementById(continent + "CitySel");
		for (m=citySelect.options.length-1;m>0;m--){
			citySelect.options[m]=null;
		}
		switch (selectedCountry){
			case "0":
				citySelect.options[0]=new Option("all locations", "0");
				citySelect.disabled = true;
			break;
			default : 
				for (i=0;i<eval(continent)[selectedCountry][1].length;i++){
					citySelect.options[i+1]=new Option(eval(continent)[selectedCountry][1][i].text,eval(continent)[selectedCountry][1][i].value);
				}
				if(citySelect.options.length == 2) citySelect.selectedIndex = 1;
				citySelect.disabled = false;
		}
		
	},
	updateNav: function(){
		if(!this.currentContinent && !this.currentCountry && !this.currentCity){
			return;
		}
		
		if(document.getElementById(this.currentContinent + "CountrySel") != "null"){
			var myCountrySelect = document.getElementById(this.currentContinent + "CountrySel");
			for(i=0; i<myCountrySelect.options.length; i++){
				if(myCountrySelect.options[i].value==this.currentCountry){
					myCountrySelect.selectedIndex = i;
				}
			}
			myCountrySelect.onchange();
			if(document.getElementById(this.currentContinent + "CitySel") != "null"){
				var myCitySelect = document.getElementById(this.currentContinent + "CitySel");
				for(i=0; i<myCitySelect.options.length; i++){
					if(myCitySelect.options[i].value==this.currentCity){
						myCitySelect.selectedIndex = i;
					}
				}
			}
		}
	},
	//this is called from the continent_nav.cfm
	setCurrentNav: function(cont, countID, cityID){
		this.currentContinent = cont;
		this.currentCountry = countID;
		this.currentCity = cityID;
	},
	addShowHide: function(){
		for (var i = 0; i < cN.continents.length; i++) {
			var li = document.getElementById(cN.continents[i]);
			var a = li.getElementsByTagName('a')[0];
			a.onclick = cN.mClick;
			if ((' ' + li.className + ' ').indexOf(' current ') != -1) {cN.currentMenu = li.id;}
		}
	},
	
	mClick: function(e) {
		var target = util.getTarget(e, "li");
		if (!target) return;
		target.getElementsByTagName('a')[0].blur();
		//we're cliking on an already open link
		if(target.className.indexOf('current') > 0){
			document.getElementById(cN.currentMenu).className = document.getElementById(cN.currentMenu).className.replace(/\b ?current\b/, '');
			//keep current in memory
			cN.currentMenu = '';
		}else if(cN.currentMenu != target.id){
			//remove the current class from the current Menu
			if(document.getElementById(cN.currentMenu)){
				document.getElementById(cN.currentMenu).className = document.getElementById(cN.currentMenu).className.replace(/\b ?current\b/, '');
			}
			//add it to the new one
			target.className += ' current';
			//keep current in memory
			cN.currentMenu = target.id;
		}
		util.stopProp(e);
		//safari needs this in the click function with propaganda
		util.cancelClick();
	},
	
	cleanUp: function() {
		cN = null;
	},
	currentMenu: 'null',
	continents: []
};
/******************
Top Search
*******************/
var searchForm = {
	init: function(){
		if (document.getElementById && document.getElementById("searchForm")){
			util.addEvent(document.getElementById("searchForm"), 'submit', util.formatSubmit, false);
		}
	}
};
/******************
Tabs
*******************/
tabDisplay = {
	handleLink: function(e){
		var el;
		if (window.event && window.event.srcElement)
			el = window.event.srcElement;
		if (e && e.target)
			el = e.target;
		if (!el)
			return;
		while (el.nodeName.toLowerCase() != 'li' &&	el.nodeName.toLowerCase() != 'body'){
			el = el.parentNode;
		}
		if (el.nodeName.toLowerCase() == 'body')
			return;
			
		//get the class to ad to the parent .
		var classNum = el.className.match(/\b ?tab\w\b/).toString();
		classNum = classNum.match(/[0-9]/);
		
		while (el.nodeName.toLowerCase() != 'div' &&	el.nodeName.toLowerCase() != 'body'){
			el = el.parentNode;
		}
		if (el.nodeName.toLowerCase() == 'body')
			return;
			
		var parentClass = el.className.replace(/\btabbedContent\w\b/, ' tabbedContent' + classNum);
		el.className = parentClass;
		
		if (window.event) {
			window.event.cancelBubble = true;
			window.event.returnValue = false;
		}
		if (e && e.stopPropagation && e.preventDefault) {
			e.stopPropagation();
			e.preventDefault();
		}
	},
	
	createTabs: function(tabbedContent){
		var tabs = util.getElementsByClassName(tabbedContent , "ul", "tabs");
		if (!tabs) return;
		var tab = tabs[0].getElementsByTagName('li');
		for(var i=0; i<tab.length; i++){
			tabLink = tab[i].getElementsByTagName('a')[0];
			util.addEvent(tabLink, 'click', this.handleLink, false);
			tabLink.onclick = util.cancelClick;
		}
	},
	
	init: function() {
		var macIE = ((navigator.appVersion.indexOf("MSIE")>-1) && (navigator.appVersion.indexOf("Mac") >= 0));
	 	if (!document.getElementById || macIE || !util.getElementsByClassName || !util.getElementsByClassName(document , "div", "tabbedContent")){
			 return;
		}
		var tabbedContent = util.getElementsByClassName(document , "div", "tabbedContent");
		for (i=0; i<tabbedContent.length; i++){
			tabDisplay.createTabs(tabbedContent[i]);
		}
	}
};
/******************
Utilty function
*******************/
var util = {
	addEvent: function(elm, evType, fn, useCapture) {
		// addEvent cross-browser event handling for IE5+, NS6 and Mozilla By Scott Andrew
		if (elm.addEventListener) {
			elm.addEventListener(evType, fn, useCapture); 
			return true;
		} else if (elm.attachEvent) {
			var r = elm.attachEvent('on' + evType, fn);
			return r;
		} else {
			elm['on' + evType] = fn;
		}
	},
	
	stopProp: function(e){
		if (window.event) {
			window.event.cancelBubble = true;
			window.event.returnValue = false;
		}
		if (e && e.stopPropagation && e.preventDefault) {
	  		e.stopPropagation();
	  		e.preventDefault();
		}
	},
	
	cancelClick: function() {
   		return false;
  	},
	
	getTarget: function(e, elem){
		var target = window.event ? window.event.srcElement : e ? e.target : null;
		if (!target) return;
		while (target != document.body && target.nodeName.toLowerCase() != elem){
			target = target.parentNode;
		}
		return target;
	},
	
	formatSubmit: function cancel(e){
		var target = util.getTarget(e, "form");
		if (!target) return;
		var queryString = "";
		
		if(target.id=="searchForm"){
			queryString = "keyword/" + target.keyword.value;
			//window.location.href = "/gallery/index.cfm/" + queryString + "/";
		}else{
			if (target.getElementsByTagName("select")[1].value != 0){
				queryString = "city/" + target.getElementsByTagName("select")[1].value;
			} else if(target.getElementsByTagName("select")[0].value != 0){
				queryString = "country/" + target.getElementsByTagName("select")[0].value;
			} else if(target.getElementsByTagName("input")[0].value != 0){
				queryString = "continent/" + target.getElementsByTagName("input")[0].value;
			}
		}
		if(queryString != ""){
			window.location.href = "/gallery/index.cfm/" + queryString + "/";
			if (e && e.preventDefault){
				e.preventDefault(); // DOM style*/
			}
  			return false;// IE style
		}
	},
	
	getElementsByClassName: function(oElm, strTagName, oClassNames){
		//    getElementsByClassName(document, "a", "info-links");
    	//		getElementsByClassName(document.getElementById("container"), "div", "col");
    	//		getElementsByClassName(document, "*", "click-me");
		 var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
		 var arrReturnElements = new Array();
		 var arrRegExpClassNames = new Array();
		 if(typeof oClassNames == "object"){
			  for(var i=0; i<oClassNames.length; i++){
					arrRegExpClassNames.push(new RegExp("(^|\\s)" + oClassNames[i].replace(/\-/g, "\\-") + "(\\s|$)"));
			  }
		 }
		 else{
			  arrRegExpClassNames.push(new RegExp("(^|\\s)" + oClassNames.replace(/\-/g, "\\-") + "(\\s|$)"));
		 }
		 var oElement;
		 var bMatchesAll;
		 for(var j=0; j<arrElements.length; j++){
			  oElement = arrElements[j];
			  bMatchesAll = true;
			  for(var k=0; k<arrRegExpClassNames.length; k++){
					if(!arrRegExpClassNames[k].test(oElement.className)){
						 bMatchesAll = false;
						 break;                      
					}
			  }
			  if(bMatchesAll){
					arrReturnElements.push(oElement);
			  }
		 }
		 return (arrReturnElements)
	}	
};
util.addEvent(window, 'load', cN.init, false);
util.addEvent(window, 'load', searchForm.init, false);
util.addEvent(window, 'load', tabDisplay.init, false);
util.addEvent(window, 'unload', cN.cleanUp, false);


