var cities = new Array();
var img = "BLA BLA BLA";

function initCities(){
	var modalCitySelect = document.getElementById("modalCitySelect");
		
	var cityName;
	var totalPhotos;
	var city;

	var i;
	for (i=0;i<modalCitySelect.options.length;i++){
		if(modalCitySelect.options[i].text){
			cityName = modalCitySelect.options[i].text;
			city = cityName.toLowerCase().replace(/ /g,"-");
			totalPhotos = modalCitySelect.options[i].value;
			cities[i] = new Array(cityName,city,totalPhotos); // 0=Full City name, 1=city with no spaces and lowercase, 2=number of photos		
			
			// document.getElementById("cityList").innerHTML += '<a id="' + i + '" class="cityLink" href="images/photos/' + city + '-1-l.jpg" onclick="return false">' + cityName + '</a>'; // Use this to generate city links
		}
	}
}

function swapPhotoMain(imgName){
	if(this.id)
		var imgName = this.id;
	var mainPhoto = document.getElementById("photoMain");
	var wallpaperLink1 = document.getElementById("wallpaperLink1");
	var wallpaperLink2 = document.getElementById("wallpaperLink2");
	
	mainPhoto.src = "images/photos/" + imgName + "-m.jpg";
	wallpaperLink1.href = "images/photos/" + imgName + "-l.jpg";
	wallpaperLink2.href = "images/photos/" + imgName + "-l.jpg";
	
	$E.addListener(wallpaperLink1, "click", downloadThis);
	$E.addListener(wallpaperLink2, "click", downloadThis);
}

function downloadThis(){
	var href = this.href;
	
	if(this.target=="_blank"){
		pageTracker._trackPageview(href);
	}else{
		var downloadWindow = window.open("download.html?img="+href,"download","resizable=yes, scrollbars=yes, width=750, height=550");
		downloadWindow.focus();
	}
	//if (document.all){
		//var downloadWindow = window.open(href,"download","resizable=yes, scrollbars=yes, width=750, height=550");
		//downloadWindow.focus();
		//downloadWindow.document.execCommand('SaveAs');
	//}else{
		//var downloadWindow = window.open("download.html?img="+href,"download","resizable=yes, scrollbars=yes, width=750, height=550");
		//downloadWindow.focus();
		//downloadWindow = "";
	//}
}

function fillModal(cityId,photoId){
	if ((this.className=="cityLink")||(this.className=="subCol")){ // If this is a city link
		cityId = this.id;
		photoId = "1";
	}
	
	//if (document.all)
		//document.getElementById("downloadInst").innerHTML = "(Click to download)";
		
	var modalThums = document.getElementById("modalThumbs");
	modalThums.innerHTML = "";
	var cityName = cities[cityId][0];
	var city = cities[cityId][1];
	var totalPhotos = cities[cityId][2];
	var photoDiv = document.getElementById("photoDiv"); // Grabs template photo HTML code at the bottom of index.html
	var templatePhoto = document.getElementById("templatePhoto"); //  Grabs wrapper template photo HTML code at the bottom of index.html
		
	var photoDivHTML;
	var modalThums = document.getElementById("modalThumbs");
	var modalCitySelect = document.getElementById("modalCitySelect");
	
	modalCitySelect.options[cityId].selected=true;
	
	if(photoId)
		swapPhotoMain(city + '-' + photoId);
	
	if (totalPhotos>1){
		var i=0;
		for (i=1;i<=totalPhotos;i++){		
			photoDiv.id = city + '-' + i;
			templatePhoto.src = "images/photos/" + photoDiv.id + "-s.jpg";
			
			photoDivHTML = document.getElementById("templatePhotoDiv").innerHTML;
			//alert(photoDivHTML);
			
			modalThums.innerHTML += photoDivHTML;
		}
		photoDiv.id = "photoDiv";
		document.getElementById("additionalImages").className="box last";
		document.getElementById("wallpaperLinkInst").className="box first";
	}else{
		document.getElementById("additionalImages").className="box last off";
		document.getElementById("wallpaperLinkInst").className="box last";
	}
	
	var photos = $D.getElementsByClassName("photo","div","modalThumbs");
	$E.addListener(photos, "click", swapPhotoMain);
}