﻿// JScript File
var pageTitle;
var app;


function closeAdminPanel() {
    document.getElementById("admin_panel_Admin_Panel").style.display = "none";
}
function showHide(show,hide) {
    document.getElementById(show).style.display = "block";
    document.getElementById(hide).style.display = "none";
}



function setName(ele) {
    var w = ele.selectedIndex;
    var selected_text = ele.options[w].text;

    document.getElementById("directory").value = selected_text;

    var catValue = document.getElementById("directoryId").options[document.getElementById("directoryId").selectedIndex].value
    var catArray = catValue.split("/");
    document.getElementById("parentCatId").value = catArray[0];
    document.getElementById("catId").value = catArray[1];    
}














function goLocation(sel) {
    if (sel.value != "")
        document.location.href = sel.value
}


    function showHideSpecs()
    {
        var spec = document.getElementById("imageSpecifications");
        var specText = document.getElementById("imageSpecificationsSwitchText");
        if(spec.style.display == "none")
        {
            spec.style.display = "block";
            specText.innerHTML = "Hide Image Specifications"
        }
        else
        {           
             spec.style.display = "none";
            specText.innerHTML = "Show Image Specifications"
        }
    }

    function showSwatch()
    {
        var sw = document.getElementById("swatch");
        if(sw.src == "http://www.tripleoptics.net/vid/grey_calibration_top.jpg")
            sw.src = "http://www.tripleoptics.net/vid/grey_calibration.jpg";
        else
            sw.src = "http://www.tripleoptics.net/vid/grey_calibration_top.jpg";
    }
	function showPic()
	{
		window.scrollTo(0,150)
	}
	
   function bookmark(appName,pType,pTitle)
   {
        app = appName;
        pageTitle = pTitle
        switch(pType)
            {
            case "pic":
               BitlyClient.shorten(document.location, 'BitlyCB.myShortenCallback_pic');
            break;
            case "cat":
               BitlyClient.shorten(document.location, 'BitlyCB.myShortenCallback_cat');
            break; 
            case "page":
               BitlyClient.shorten(document.location, 'BitlyCB.myShortenCallback_page');
            break;                               
        }
    }
    BitlyCB.myShortenCallback_pic = function(data) {
		var result;
		for (var r in data.results) {
			result = data.results[r];
			result['longUrl'] = r;
			break;
		}
		
		switch(app)
            {
            case "twitter":
                window.open("http://twitter.com/home/?status="+ escape("I found this picture called "+ pageTitle +" that I want to share with you ") + result['shortUrl']);
            break;
            case "delicious":
               window.open("http://del.icio.us/post?url="+result['shortUrl']+"&amp;title="+escape("tripleoptics.net Picture titled: "+ pageTitle +" "));
            break; 
            case "digg":
               window.open("http://digg.com/submit?url="+ result['shortUrl'] +"&amp;title="+escape("tripleoptics.net Picture titled:  "+ pageTitle +" "));
            break;        
            case "reddit":
               window.open("http://reddit.com/submit?url="+ result['shortUrl'] +"&amp;title="+escape("tripleoptics.net Picture titled: "+ pageTitle +" "));
            break;     
            case "facebook":
               window.open("http://www.facebook.com/sharer.php?u="+result['shortUrl']);
            break;  
            case "stumbleupon":
               window.open("http://www.stumbleupon.com/submit?url="+ document.location.href +"&amp;title="+escape("I found this picture called "+ pageTitle +" that I want to share with you "));
            break;                                                           
        }
	}
	
    BitlyCB.myShortenCallback_cat = function(data) {
		var result;
		for (var r in data.results) {
			result = data.results[r];
			result['longUrl'] = r;
			break;
		}
		switch(app)
            {
            case "twitter":
                window.open("http://twitter.com/home/?status="+ escape("I found these picture called "+ pageTitle +" that I want to share with you ") + result['shortUrl']);
            break;
            case "delicious":
               window.open("http://del.icio.us/post?url="+result['shortUrl']+"&amp;title="+escape("tripleoptics photography by dn  "+ pageTitle +" pictures"));
            break; 
            case "digg":
               window.open("http://digg.com/submit?url="+ result['shortUrl'] +"&amp;title="+escape("tripleoptics photography by dn  "+ pageTitle +" pictures"));
            break;        
            case "reddit":
               window.open("http://reddit.com/submit?url="+ result['shortUrl'] +"&amp;title="+escape("tripleoptics photography by dn  "+ pageTitle +" pictures"));
            break;     
            case "facebook":
               window.open("http://www.facebook.com/sharer.php?u="+result['shortUrl']);
            break;  
            case "stumbleupon":
               window.open("http://www.stumbleupon.com/submit?url="+ document.location.href +"&amp;title="+escape("tripleoptics photography by dn "+ pageTitle +" pictures "));
            break;                                                           
        }
	}
    BitlyCB.myShortenCallback_page = function(data) {
		var result;
		for (var r in data.results) {
			result = data.results[r];
			result['longUrl'] = r;
			break;
		}
        	switch(app)
            {
            case "twitter":
                window.open("http://twitter.com/home/?status="+ escape("I found this link ["+ result['shortUrl'] +"] that I want to share with you"));
            break;
            case "delicious":
               window.open("http://del.icio.us/post?url="+result['shortUrl']+"&amp;title="+escape("link : photography tripleoptics.net"));
            break; 
            case "digg":
               window.open("http://digg.com/submit?url="+ result['shortUrl'] +"&amp;title="+escape("link : photography tripleoptics.net"));
            break;        
            case "reddit":
               window.open("http://reddit.com/submit?url="+ result['shortUrl'] +"&amp;title="+escape("link : photography tripleoptics.net"));
            break;     
            case "facebook":
               window.open("http://www.facebook.com/sharer.php?u="+result['shortUrl']);
            break;  
            case "stumbleupon":
               window.open("http://www.stumbleupon.com/submit?url="+ document.location.href +"&amp;title="+escape("link : photography tripleoptics.net"));
            break;                                                           
        }
     }


    var domIdText
    function getCategoryInformation(catId) {
        var catHolder = document.getElementById("EDITtxt_CategoryParamsHolder");
        catHolder.style.position = "absolute";
        catHolder.style.width = "400px";
        catHolder.style.height = "540px"; 
        
        var catIframe = document.createElement("iframe");
        catIframe.id = "catIframe";
        catIframe.style.width = "400px";
        catIframe.style.height = "500px";
        catIframe.style.backgroundColor = "black";
        catIframe.src = "http://www.tripleoptics.net/php/admin/editSmallCategoryDetails.php?dbtype=dark&catId=" + catId

        closeDiv = document.createElement("a");
        closeDiv.style.float = "left"
        closeDiv.href = "javascript:closeEdit('catIframe','EDITtxt_CategoryParamsHolder')"
        closeDiv.innerHTML = "<span style='font-size:11px'>close</span>";
         
        catHolder.appendChild(catIframe)
        catHolder.appendChild(closeDiv)

    }

    function addCategoryInformation(catId) {
        var catHolder = document.getElementById("EDITtxt_CategoryParamsHolder");
        catHolder.style.position = "absolute";
        catHolder.style.width = "400px";
        catHolder.style.height = "540px";

        var catIframe = document.createElement("iframe");
        catIframe.id = "catIframe";
        catIframe.style.width = "400px";
        catIframe.style.height = "500px";
        catIframe.style.backgroundColor = "black";
        catIframe.src = "http://www.tripleoptics.net/php/admin/addSmallCategoryDetails.php?dbtype=dark&catId=" + catId

        closeDiv = document.createElement("a");
        closeDiv.style.float = "left"
        closeDiv.href = "javascript:closeEdit('catIframe','EDITtxt_CategoryParamsHolder')"
        closeDiv.innerHTML = "<span style='font-size:11px'>close</span>";

        catHolder.appendChild(catIframe)
        catHolder.appendChild(closeDiv)

    }
    
    function edit(dbattribute, domId,target, id, attributes) {
       
        domIdText = document.getElementById(domId).innerHTML;
       
        
        var editPane = document.createElement("div")
        editPane.setAttribute("style", "z-index:200;position:relative");
        editPane.setAttribute("id","edit_pane_"+domId);
        if (dbattribute == "attribute_lens") {
            var lensArray = new Array('Sigma 10-20mm', 'Sigma 105mm macro', 'Canon 17-55mm f2.8', 'Canon 16-35mm f2.8L', 'Canon 17-40mm f4L', 'Canon 24-105mm f4L')
            var editSelection = document.createElement("select");
            editSelection.setAttribute("id", "edit__" + domId);
            for(i=0;i<lensArray.length;i++)
            {
                var lensOpt = document.createElement("option");
                lensOpt.setAttribute("value",lensArray[i])
                lensOpt.innerHTML = lensArray[i];
                editSelection.appendChild(lensOpt);
            }
        }
        else if (dbattribute == "imageFilter") {

            var imageFilterArray = new Array('isIr', 'isMono', 'isColour', 'isHdr', 'isFeatured')
            var editSelection = document.createElement("div");
            editSelection.setAttribute("style", "background-color:black;float:left;width:100px");
            for (f = 0; f < imageFilterArray.length; f++) {
                
                var imageFilterOptionDiv = document.createElement("div")
                imageFilterOptionDiv.setAttribute("style", "float:left;clear:left");
               
                imageFilterOptionDiv.innerHTML = imageFilterArray[f];
                var imageFilterOption = document.createElement("input");
                imageFilterOption.type = "checkbox";
                if (attributes.charAt(f) == "1") {
                    imageFilterOption.checked = true;
                    imageFilterOption.checked;
                }
                imageFilterOption.setAttribute("id", imageFilterArray[f]);
                imageFilterOptionDiv.appendChild(imageFilterOption)
                editSelection.appendChild(imageFilterOptionDiv);
            }
 
        }
        else {
            var editSelection = document.createElement("textarea");
            editSelection.setAttribute("id", "edit__" + domId);
            
            editSelection.value = domIdText;
        }
        var editPaneButton = document.createElement("input"); 
        editPaneButton.setAttribute("type","button");
        editPaneButton.setAttribute("value", "edit");
        editPaneButton.setAttribute("onclick", "submitEdit('" + dbattribute + "','edit__" + domId + "'," + id + ",'edit_pane_" + domId + "','" + target + "')");                
        
        var editPaneCloseButton = document.createElement("input");
        editPaneCloseButton.setAttribute("type", "button");  
        editPaneCloseButton.setAttribute("value","close");
        editPaneCloseButton.setAttribute("onclick", "closeEdit('edit_pane_" + domId + "','" + target + "')");                
              

        editPane.appendChild(editSelection);
        editPane.appendChild(editPaneButton);
        editPane.appendChild(editPaneCloseButton)
        //alert(editPane.innerHTML)
        document.getElementById(target).appendChild(editPane)
        editSelection.focus();
    }

    function commit(id, ele, container) {

        var catName = document.getElementById(ele).options[document.getElementById(ele).selectedIndex].text;     
        var cat = document.getElementById(ele).options[document.getElementById(ele).selectedIndex].value;
        if (cat != "") {
            commitCategory(cat, id, ele, catName)
        }
    }

    function commitCategory(category, id, select, catName) {
        var returnText = "f";
        xmlhttp = GetXmlHttpObject();
        var url = "/ajax/updateCategory.php";
        url = url + "?cat=" + category + "&id=" + id + "&catName=" + catName;
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4) {
                document.getElementById(select).selectedIndex = 0
            }
        }
        xmlhttp.open("GET", url, true);
        xmlhttp.send(null);
    }    

    function closeCategorySelect(container, editPane) {
        document.getElementById(container).parentNode.removeChild(document.getElementById(editPane));
    }

    function editCategory(dbattribute, domId, category, id) {

        var editPane = document.createElement("div")
        editPane.setAttribute("style", "z-index:200;position:relative");
        editPane.setAttribute("id", "edit_pane_" + domId);
        
            var lensArray = new Array('Sigma 10-20mm', 'Sigma 105mm macro', 'Canon 17-55mm f2.8', 'Canon 16-35mm f2.8L', 'Canon 17-40mm f4L', 'Canon 24-105mm f4L')
            var editSelection = document.createElement("select");
            editSelection.setAttribute("id", "edit__" + domId);
            for (i = 0; i < lensArray.length; i++) {
                var lensOpt = document.createElement("option");
                lensOpt.setAttribute("value", lensArray[i])
                lensOpt.innerHTML = lensArray[i];
                editSelection.appendChild(lensOpt);
            }
       
        var editPaneCloseButton = document.createElement("input");
        editPaneCloseButton.setAttribute("type", "button");
        editPaneCloseButton.setAttribute("value", "close");
        editPaneCloseButton.setAttribute("onclick", "closeCategorySelect('" + domId + "','edit_pane_" + domId + "')");

        var editCommitButton = document.createElement("input");
        editCommitButton.setAttribute("type", "button");
        editCommitButton.setAttribute("value", "commit");
        editCommitButton.setAttribute("onclick", "commit('"+ id +"','edit__" + domId+"')");

        editPane.appendChild(editSelection);
        editPane.appendChild(editCommitButton);
        editPane.appendChild(editPaneCloseButton);
        // alert(editPane.innerHTML)
        document.getElementById(domId).parentNode.appendChild(editPane)
        

        getCategories(category, "edit__" + domId);
    }
    
    
    function submitEdit(dbattribute, newval, id, editPane ,mainContainer)
    {
        if (dbattribute == "imageFilter") {
            var ele = document.getElementById(mainContainer)
            var options = ele.childNodes[1].childNodes[0].childNodes
            for (v = 0; v < options.length; v++) {
                var option = options[v].childNodes[1].id
                var optionVal = options[v].childNodes[1].checked
                // alert(option + " -- " + optionVal)
                //alert("updateAttribute(" + option + ", " + optionVal + ", " + id + ")")
                updateAttribute(option, optionVal, id)
            }
            //var ele2= ele.getElementByTagName("input")
            //alert(ele2.length)
        }
        else {
            // alert(document.getElementById(newval).value+" != "+domIdText)
            if (document.getElementById(newval).value != domIdText) {
                alert("updateAttribute(" + dbattribute + ", " + document.getElementById(newval).value + ", " + id + ")")
                updateAttribute(dbattribute, document.getElementById(newval).value, id)
            }

        }
        closeEdit(editPane, mainContainer)
        }    
    
    function closeEdit(editPane, mainContainer)
    {
        document.getElementById(mainContainer).removeChild(document.getElementById(editPane));
    }


     

     function updateAttribute(attribute, value, id) {
         var returnText = "f";
         xmlhttp = GetXmlHttpObject();
         if (xmlhttp == null) {
            document.getElementById("EDITtxt_"+attribute).innerHTML = "[f]";
         }
         var url = "/ajax/updateAttribute.php";
         url = url + "?a=" + attribute + "&v=" + value + "&i=" + id;
         xmlhttp.onreadystatechange = function() {
             if (xmlhttp.readyState == 4) {
                document.getElementById("EDITtxt_"+attribute).innerHTML = "[u]";
             }
         }
         xmlhttp.open("GET", url, true);
         xmlhttp.send(null);
     }

     function getCategories(category, sectionId) {
         var returnText = "";
         xmlhttp = GetXmlHttpObject();
         if (xmlhttp == null) {
             alert("Your browser does not support XMLHTTP!");
             return;
         }
         var url = "/ajax/category.php";
         url = url + "?category=" + category;
         xmlhttp.onreadystatechange = function() {
             if (xmlhttp.readyState == 4) {
                 document.getElementById(sectionId).innerHTML = xmlhttp.responseText
             }
         }
         xmlhttp.open("GET", url, true);
         xmlhttp.send(null);
     }       

     function update(imageId, func) {
         var returnText = "";
         xmlhttp = GetXmlHttpObject();
         if (xmlhttp == null) {
             alert("Your browser does not support XMLHTTP!");
             return;
         }
         var url = "/ajax/update.php";
         url = url + "?id=" + imageId + "&f=" + func;
         xmlhttp.onreadystatechange = function() {
             if (xmlhttp.readyState == 4) {
                 document.getElementById("updateResponse").innerHTML = xmlhttp.responseText
             }
         }
         xmlhttp.open("GET", url, true);
         xmlhttp.send(null);
     }

    function getImages(type, tagtype, cat, img)
    {
        try{
         var returnText = "";
         xmlhttp = GetXmlHttpObject();
         if (xmlhttp == null) {
             alert("Your browser does not support XMLHTTP!");
             return;
         }
         var url = "/ajax/getNextImage.php";
         url = url + "?type=" + type + "&tagtype=" + tagtype + "&id=" + img + "&cat=" + cat;
         xmlhttp.onreadystatechange = function() {
             if (xmlhttp.readyState == 4) {
                 var btnHolder = document.getElementById("nextPrevImageLinks")
				 var imgArray = xmlhttp.responseText.split("|")
				 // alert(imgArray[0].length)
				 if(imgArray[1].length <= 1)
				 {
				 	// do nothing - start of category
				 }
				 else
				 {	// previous image
				    // alert("previous")
				 	var image1Array = 	imgArray[1].split(":")
					var image1Id = 		image1Array[0]
					var image1cat = 	image1Array[1]					
					var image1Url = 	image1Array[2]
					var image1Title = 	image1Array[3]	
					btnHolder.appendChild(buildButton('prev',type,cat,image1Id,image1cat,image1Url,image1Title));			
					//alert("previous Image : "+image1Array[2])
            	}
				 if(imgArray[2].length <= 1)
				 {
				 	// do nothing - end of category
				 }	
				 else { 	// next image
				    // alert("next")
				 	var image2Array = 	imgArray[2].split(":")
					var image2Id = 		image2Array[0]
					var image2cat = 	image2Array[1]					
					var image2Url = 	image2Array[2]
					var image2Title = 	image2Array[3]
					btnHolder.appendChild(buildButton('next',type,cat,image2Id,image2cat,image2Url,image2Title));							
					//alert("next Image : "+image2Array[2])					
				 }			 
				 	///alert(imgArray[0])
					//alert(imgArray[1])				 
				 	//document.getElementById("updateResponse").innerHTML = 
             }
         }
         xmlhttp.open("GET", url, true);
         xmlhttp.send(null);
         }
         catch(e)
         {
            // no image can be displayed
         }
    }



     function showThumbnail(type) {
         document.getElementById("img" + type).style.display = "block";
     }
	 function hideThumbnail(type) {
	     document.getElementById("img" + type).style.display = "none";
	 }
	 
	 function buildButton(type,linktype,tagcat,id,cat,url,title)
	 {
	     var btn = document.createElement("div")
	     btn.className = "prevNextLink";
	     var tDiv = document.createElement("div")
	     tDiv.setAttribute("id", "img" + type);
	     //tDiv.setAttribute("style", "display:none");
	     tDiv.className = "imgHolder";
	     var tImg = document.createElement("img")


	     var btnLink = document.createElement("a")
	     var imgLink = document.createElement("a")
	     var finalCat = "";
	     if(linktype == "cat")
		    finalCat = id+"_"+ lowerise(addDashes(title)) +".aspx";
		 else if (linktype == "albumtags")
		    finalCat =  id + "_" + lowerise(addDashes(title)) + ".aspx";        
         else
             finalCat =  id + "_" + lowerise(addDashes(title)) + ".aspx";

         btnLink.setAttribute('href', finalCat);
         btnLink.setAttribute('title', "view the photo called " + title);
         imgLink.setAttribute('href', finalCat);
         imgLink.setAttribute('title', "view the photo called " + title);
         tImg.setAttribute("src", url);
         tImg.setAttribute("border", "0");
         tImg.setAttribute("width", "70");
         tImg.setAttribute("height", "98");         
         tImg.setAttribute("style", "width:70px;border:0px");
         tImg.setAttribute("alt", title);
         imgLink.appendChild(tImg);
         tDiv.appendChild(imgLink);
         
		// btnLink.setAttribute('onmouseover', "showThumbnail('"+type+"')");
		// btnLink.setAttribute('onmouseout', "hideThumbnail('"+type+"')");

		if (type == "prev") { type = "previous" };
		btnLink.innerHTML = type;
		btn.appendChild(btnLink)
		btn.appendChild(tDiv);
		return btn;
	 }
	// http://www.tripleoptics.net/objects-abstracts/machinery/images/788_watchmen.aspx
	// http://www.tripleoptics.net/objects-abstracts/786_glowing-budda.aspx
	function formatCat(str)
	{
		if(str.indexOf("ObjectsAbstracts") != -1)
		{
			return str.replace(/ObjectsAbstracts/gi,"objects-abstracts");
		}
		else
		{
			return str;
		}
	}
	function addDashes(str){
		return str.replace(/ /gi,"-")
	}
	function lowerise(str){
		return str.toLowerCase()
	}	

