var SearchParts = {};
dojo.require("dojo.cookie");
function LocationSearch(){
    //console.log("in LocationSearch");
    var rawArray = window.location.search.substr(1).split("&");
    if (rawArray.length != 1 && rawArray[0] != "") {
        for (var i in rawArray) {
            var parts = rawArray[i].split("=");
            SearchParts[parts[0]] = parts[1];
        }
    }
    SearchParts.gcurpage = SearchParts.gcurpage || 1;
    SearchParts.categoryid = SearchParts.categoryid || -1;
    SearchParts.galleryid = SearchParts.galleryid || selectedGalleryid || 0;
    SearchParts.photoid = SearchParts.photoid || dojo.query("#dl1").query("div")[0].id.replace("photo", "");
}


function updateLinks(){
	//update tab Cookie
	dojo.cookie("tab",dojo.cookie("tab")||"1");
	
    //update search parts
    LocationSearch();
    
    //current photo id should be the id from query String(window.location) or the first photo in #wrapper
    var currentPhotoId = SearchParts.photoid || dojo.query("#dl1").query("div")[0].id.replace("photo", "");
    //console.log("currentPhotoId = " + currentPhotoId);
    var currentIndex = 0;
    //show the "current photo and hide others" (seems no need to worry about this?)
    dojo.query(".dl").forEach(function(item, n){
        var id = dojo.query("div", item)[0].id.replace("photo", "");
        dojo.style(item, "display", id == currentPhotoId ? "block" : "none");
        currentIndex = (id == currentPhotoId ? n : currentIndex);
    });
    
    //console.log("currentIndex = ",currentIndex);
    
    var nextPhotoId = photoIds[(currentIndex + 1) % dls];
    //console.log("nextPhotoId=" + nextPhotoId);
    dojo.query("#next")[0].href = "?categoryid=" + categoryid + "&gcurpage=" + SearchParts.gcurpage +  "&galleryid=" + SearchParts.galleryid + "&photoid=" + nextPhotoId;
    var prevPhotoId = photoIds[(currentIndex + dls - 1) % dls];
    //console.log("prevPhotoId=" + prevPhotoId);
    dojo.query("#prev")[0].href = "?categoryid=" + categoryid + "&gcurpage=" + SearchParts.gcurpage +  "&galleryid=" + SearchParts.galleryid + "&photoid=" + prevPhotoId;
	
	
	//update "selected gallery"
	dojo.addClass(dojo.query("#gallery"+SearchParts.photoid+" a")[0],"current");
	
	/*dojo.query("#thu img").forEach(function(item){
		item.src= item.src.replace(/[0-9]+x[0-9]+.jpg$/,"80x60.jpg");
	});*/
}

dojo.addOnLoad(updateLinks);
