Array.indexOf = function(arr,obj){for(var i=0,l=arr.length;i<l; i++){if(arr[i]==obj){return i;}}return -1;}

function parseCity(loc){
	var list = loc.split(/[ ]|,[ ]*/);
	var city = list[list.length-1];
	if(city && Array.indexOf(CityList,city)!=-1 && !readCookie("cityname")) 
		saveCookie("cityname",capitalize(city));
	return city;
}

function createCityMenu(cities){
	var city = readCookie("cityname");
	if(city) $('cityText').innerHTML = city;
	var citymenu = $("cityMenu");
	citymenu.options.length = 0;
	citymenu.options[0] = new Option("--Select City --",0);
	var l = cities.sort().length;
	var isCity = false;
	for (var i = 0; i < l; i++) {
		if(Browser.IE) isCity = (cities[i-1]==city);
		else isCity = (cities[i]==city);
		var opt = new Option(cities[i], cities[i],isCity,isCity);
		citymenu.options[i+1] = opt;
	}
	
	addEvent($('cityText'),"click",function(){
		hideNode("cityText");
		showNode("cityMenuContainer");
		$("cityMenu").focus();
	});
	
	addEvent($('cityMenu'),"change",function(){
		var citymenu = $("cityMenu");
		var index = citymenu.selectedIndex;
		if(index >0){
			var city = citymenu.options[index].value;
			saveCookie("cityname",city);
			$('cityText').innerHTML = city;
		}else{
			saveCookie("cityname","");
			$('cityText').innerHTML = "Select City";
		}
		citySelected();
	})
	
	addEvent($('cityMenu'),"blur",citySelected);
}
function citySelected(){
	hideNode("cityMenuContainer");
	showNode("cityText");	
	if($('chartbusters')){
		var loc = readCookie("cityname");
		if(loc){hideNode('chartbusters');downloadJS("/scripts/chartbusters/"+loc.toLowerCase()+".js?preventCache="+(new Date()).getTime());}
	}else window.location.reload()
}

function createChartBusters(movieList,theaterList){
	
	if(movieList.length <2 || theaterList.length <2) return;
	
	var moviesOL = $('moviesOL');
	var theatersOL = $('theatersOL');
	
	var moviesURL = "/movietiming?q=[TITLE]&cat=c0,moviename&clusOpt=1";
	var theaterURL = "/movietiming?q=[TITLE]&cat=c0,moviehallname&clusOpt=1";
	var hl = readCookie("hl");if(hl){moviesURL+="&hl="+hl;theaterURL+="&hl="+hl;}
	var loc = readCookie("cityname");if(loc){moviesURL+="&loc="+loc;theaterURL+="&loc="+loc;}
	
	if (moviesOL) {
		removeChildren(moviesOL);
		var l = (movieList.length<5)?movieList.length:5;
		for(var i=0;i<l;i++){
			var li = ce("li");var a = ce("a");
			a.setAttribute("href",moviesURL.replace("[TITLE]",movieList[i].replace(/[ ]+/g,"+")));
			a.appendChild(tn(movieList[i]));
			li.appendChild(a);
			moviesOL.appendChild(li);
		}
	}
	if (theatersOL) {
		removeChildren(theatersOL);
		var l = (theaterList.length<5)?theaterList.length:5;
		for(var i=0;i<l;i++){
			var li = ce("li");var a = ce("a");
			a.setAttribute("href",theaterURL.replace("[TITLE]",theaterList[i].replace(/[ ]+/g,"+")));
			a.appendChild(tn(theaterList[i]));
			li.appendChild(a);
			theatersOL.appendChild(li);
		}
	}
	showNode('chartbusters');
}

function preferedCity(){		
	if($('prefCity')){
		var cookieCity = capitalize(readCookie("cityname"));
		var resultCity = capitalize(parseCity($('prefCity').innerHTML));
		if($('whereBox')) $('whereBox').value = capitalize($('prefCity').innerHTML);
		if(Array.indexOf(CityList,resultCity)==-1) return;
		
		/* If Cookie Not Found */
		if(cookieCity == "" || !cookieCity){
			saveCookie("cityname",resultCity);
			return;
		}
		if (resultCity.toLowerCase() != cookieCity.toLowerCase()){
			var saveNode = ce("span");
			saveNode.style.marginLeft = "10px";
			saveNode.style.fontSize = "12px";
			var saveButton = ce("a");
			saveButton.href = "javascript:void(0)";
			saveButton.innerHTML = "Save <b> "+resultCity+"</b> as My City ";
			addEvent(saveButton,"click",function(){
				saveCookie("cityname",resultCity);
				saveButton.parentNode.removeChild(saveButton);
				saveMessage.innerHTML = "<b style='color:red;'>Saved</b>";
				setTimeout("var saveMessage = $('saveMessage');saveMessage.parentNode.removeChild(saveMessage);",2000);
			});
			
			var saveMessage = ce("small");
			saveMessage.setAttribute("id","saveMessage");
			saveMessage.innerHTML = "(<span style=\"font-size:10px;color:gray\">"+
									"Current city :</span> <i style=\"color:black;\"><b id=\"currentCity\">"+
									cookieCity+"</b></i> ) ";
			saveNode.appendChild(saveButton);
			saveNode.appendChild(saveMessage);
			$('prefCity').parentNode.appendChild(saveNode);
		}
	}		
}

addEvent(window,"load",function(){preferedCity();});