﻿// ------------------------------------ //
// ---- Global Functions -------------- //
// ------------------------------------ //

if(document.referrer.toLowerCase().indexOf("chernobylee") != -1)
{
document.location.href = "http://www.darkoptics.net/project_chernobyl/home.aspx"
}



var isAdmin = "no"

    function checkEmailForm(form)
    {
        if(form.elements[4].value.toLowerCase() == "camera")
        {
        return true;
        }
        else
        {
            alert("please enter the word in the image");
            return false;
        }
    }

function ScrollPastNav(to, type) {
    window.scrollTo(0, to)
    if (type == 1)
        document.getElementById("head_goUp").firstChild.style.visibility = "hidden";
    else
        document.getElementById("head_goUp").firstChild.style.visibility = "visible";
}
function goLocation(sel) {
    if (sel.value != "")
        document.location.href = sel.value
}
function transformDate(dte) {
    // var dte = "16:42 - 29/08/2010"

    // 2010:05:29 15:17:33
    alert("start "+dte)
    //finalDateStr = ""
    if (dte != "") {
        dtetime = dte.split("-")
        dtetime[0].trim()
        dtetime[1].trim()
        newDate = dtetime[1].trim().split("/")
        newDateStr = newDate[2] + ":" + newDate[1] + ":" + newDate[0] + " "
        finalDateStr = newDateStr + dtetime[0].trim() + ":00"
    }
    alert("end " + finalDateStr)
    return finalDateStr
}

function addNewLocation(ele, catId, parentCatId, eleId, db) {
    // http://www.darkoptics.net/ajax/admin/location.php?attr=addLocation&title=test&categoryId=75&parentCategoryId=10&db=undefined
    http: //www.darkoptics.net/ajax/admin/admin_add_location.php?attr=addLocation&title=test&categoryId=10&parentCategoryId=0&db=dark
    alert(document.getElementById(ele).value)
    attr = "addLocation&title=" + document.getElementById(ele).value + "&categoryId=" + catId + "&parentCategoryId=" + parentCatId
    getAjaxResponse1(attr, eleId, 'admin_add_location', db)
}
  
function showLocationIdEdit(eleId) {
    document.getElementById(eleId).style.display = "block";
}

function updateElementWithId(source, target) {

    if (source.options[source.selectedIndex].value != "select")
        document.getElementById(target).value = source.options[source.selectedIndex].value
}

function addTag(target, tag, max) {
    tagValLengthArray = 0
    var tagVal = document.getElementById(target).value
    var tagValLength = tagVal.length;
    if (tagValLength > 0) {
        // there is a value
        var tagValLengthArray = tagVal.split(" ");
    }
    // alert("(parseInt("+max+") > 0)");
    // alert("("+tagValLengthArray.length+" >= "+max+")");
    
    
    if ((parseInt(max) > 0) && (tagValLengthArray.length >= max)) {
        alert("maximum tags already chosen");
    }
    else {
        var tagStr = "";
        if (tagVal.indexOf(tag) == -1) {
            if (tagVal.length > 0) {
                tagStr += " " + tag
            }
            else {
                tagStr = tag;
            }
            document.getElementById(target).value += tagStr
        }
        else {
            alert("tag already added")
        }
    }
}

function closeAdminPanel(ele) {
    document.getElementById(ele).style.display = "none";
}
function hover(ele) {

}

function showHide(show, hide) {
    if (hide == "") {
        if (document.getElementById(show).style.display == "none")
            document.getElementById(show).style.display = "block";
        else if (document.getElementById(show).style.display == "block")
            document.getElementById(show).style.display = "none";
    }
    else {
        document.getElementById(show).style.display = "block";
        var hideEle = hide.split("-");
        for (f = 0; f < hideEle.length; f++) {
            if(document.getElementById(hideEle[f]))
                document.getElementById(hideEle[f]).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("/");

    if ((catArray[1] == "")||(catArray[1] == "0")) {
        _pCat = "";
        _cCat = catArray[0]
    }
    else {
        _pCat = catArray[1];
        _cCat = catArray[0]
    }
    
    document.getElementById("parentCatId").value = _pCat;
    document.getElementById("catId").value = _cCat;
}


function getBrowserWidth() {
    var browserWidth = 0, browserHeight = 0;
    //For checking non-IE browsers Mozilla, Safari, Opera, Chrome.  
    if (typeof (window.innerWidth) == 'number') {
        browserWidth = window.innerWidth;
        browserHeight = window.innerHeight;
    }
    //All IE except version 4  
    else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
        browserWidth = document.documentElement.clientWidth;
        browserHeight = document.documentElement.clientHeight;
    }
    //IE 4  
    else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
        browserWidth = document.body.clientWidth;
        browserHeight = document.body.clientHeight;
    }
    return browserWidth
}

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 showThumbnail(type) {
    document.getElementById("img" + type).style.display = "block";
}
function hideThumbnail(type) {
    document.getElementById("img" + type).style.display = "none";
}

function getStats(fName) {
    fullName = fName;
    shortName = fullName.match(/[^\/\\]+$/);
    document.getElementById("imageName").value = shortName
}

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;
}
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()
}


function dbObj(_eleId, _eleType, _dbId) {
    this.elementId = _eleId;
    this.elementValue = ""
    try {
        if (_eleType == "chk") {
            if (document.getElementById(_eleId).checked)
                this.elementValue = "1"
        } else if (_eleType == "txt") {
            this.elementValue = document.getElementById(_eleId).value
        }
        this.dbId = _dbId
    }
    catch (e) {
        alert("Error with element " + _eleId)
    }
}
dbObj.prototype.attr = function() {
    var str = ""
    if (this.elementValue != "") {
        str = "&" + this.dbId + "=" + this.elementValue;
    }

    return str
}

BitlyCB = new Object();
function bookmark(appName, pType, pTitle, _isAdmin) {
    app = appName;
    isAdmin = _isAdmin
    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":
            if(isAdmin == "no")
                window.open("http://twitter.com/home/?status=" + escape("I found this picture called " + pageTitle + " that I want to share with you ") + result['shortUrl']);
            else
                window.open("http://twitter.com/home/?status=" + escape("#photo \"" + pageTitle + "\" on tripleoptics.net #foto #photo #photography #art ") + 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;
    }
}

