﻿var _mainUrl = null;
var _isSafari = false;

function go(oldUrl, mainUrl)
{
    if((mainUrl == null || mainUrl == "null") && oldUrl != null && oldUrl != "null")
    {
        mainUrl = oldUrl;
    }

    if(mainUrl != null && mainUrl != "null")
    {
        if(checkUrlRules(mainUrl))
        {
            _mainUrl = mainUrl;
            if(_mainUrl == "")
            {
                document.getElementById("divMain").innerHTML = "";
            }
            else
            {
                getPage(_mainUrl, "loadDiv('divMain', oXml); initImageRollovers(); closeZoom();");
                urchinTracker(_mainUrl); // google analytics
            }
        }
    }
    
//    try
//    {
//    
//        var baseUrl = new String(location.href);
//        if(baseUrl.indexOf("#") > -1)
//        {
//            baseUrl = baseUrl.substr(0, baseUrl.indexOf("#"));
//        }
       
    //    if(!document.all)
    //    {
    //        location.href = baseUrl + "#" + encodeURIComponent(_midUrl) + "," + encodeURIComponent(_mainUrl);
    //    }
    
//    }
//    catch (ex) {}
    
}

function checkUrlRules(url)
{
    var lUrl = url.toLowerCase();
//    if(lUrl.indexOf("selectedimages.aspx") > -1)
//    {
//        alert("Image request feature coming soon...");
//        return false;
//    }
//    else
//    {
        return true;
//    }
}

function navigation_DoFSCommand(command, args)
{
    alert(args);
}

function loadDiv(divID, oXml)
{
    var o = document.getElementById(divID);
    if(o != null)
    {
        var html = oXml.responseText;
        o.innerHTML = html;
    }
}

function showDealerLinks(vl)
{
    var trigVal = "yes";
    if(vl)
    {
        trigVal = "no";
    }
    
    var fl = document.getElementById("navigation");

    if(fl.GetVariable("showDealerNav") == trigVal)
    {

        var newVal = "no";
        if(vl)
        {
            newVal = "yes";
        }

	    fl.SetVariable("autoOpenNode", "G_1"); 
	    fl.SetVariable("showDealerNav", newVal); 
	    fl.Play();
    }
}

function init()
{
    _isSafari = (navigator.userAgent.toLowerCase().indexOf("safari") > -1);


    var url = location.href;
    var poundIndex = url.indexOf("#");
    
    if(typeof(initPage) != "undefined" && initPage.length > 0)
    {
        try
        {
            go(initPage[0]);
        }
        catch(e) {}
    }
    else if(poundIndex > -1 && url.length > poundIndex + 1)
    {
        url = url.substr(poundIndex + 1);
        try
        {
            go(url);
        }
        catch(e) {}
    }
    else
    {
        go("side_intro.html", "tessera_intro.html");
    }

    //document.title = "test";
    //alert(document.title);
}

function ajaxSubmit(frmID, action, sender, changeText)
{
    var frm = document.getElementById(frmID);
    var inputs = frm.getElementsByTagName("input");
    var selects = frm.getElementsByTagName("select");
    
    var qs = "";
    
    qs = addToQs(qs, inputs);
    qs = addToQs(qs, selects);
    
    var url = action + "?" + qs;
    
    sender.disabled = true;
    if(changeText != null)
    {
        sender.value = changeText;
    }
    
    go(null, url);
}

function addToQs(qs, elements)
{
    for (var i = 0; i < elements.length; i++)
    {
        if(qs != "")
        {
            qs += "&";
        }
        qs += encodeURIComponent(elements[i].name) + "=" + encodeURIComponent(elements[i].value);
    }
    
    return qs;
}

function showFullSize(productID)
{
    window.open("FullSizePopUp.aspx?id=" + productID, "", "height=300,width=300,scrollbars=no,resizable=yes,toolbar=no,status=no,alwaysRaised=yes");
}

function popUpPic(picUrl)
{    
    window.open("popUpPic.aspx?src=" + picUrl,"","width=665,height=800,scrollbars=no,resizable=yes,toolbar=no,status=no,alwaysRaised=yes");    
}

function doImageRequestForm()
{
    window.open("PhotoGallery/ImageRequestForm.aspx", "", "width=400,scrollbars=yes,resizable=yes,toolbar=no,status=no,alwaysRaised=yes");
}

function showJobShot(url)
{
    getPage(url, "document.getElementById(\"divJobShotLarge\").innerHTML = oXml.responseText");
}

function highlightJobShot(sender, showIt)
{
    if(showIt)
    {
        sender.style.border = "2px solid #5582ab";
    }
    else
    {
        sender.style.border = "2px solid white";
    }
}

var _zoomWindow = null;
var _zoomUrl = null;
var _cache = new cache();

function openZoomURL(sender, e, url)
{
    getZoom();
    repos(_zoomWindow, e);

    if(_zoomUrl != url)
    {
        var cached = _cache.Get(url);
        if(cached != null) // in cache
        {
            _zoomWindow.innerHTML = cached;
        }
        else
        {
            _zoomWindow.innerHTML = "Loading Details...";
            getPage(url, "_cache.Add(\"" + url + "\", oXml.responseText); _zoomWindow.innerHTML = oXml.responseText;");
        }
        _zoomUrl = url;
    }
    _zoomWindow.style.visibility = 'visible';
}

function cache()
{
    var _keys = new Array();
    var _values = new Array();
    var _hits = 0;

    this.Add =
        function(key, value)
        {
            _keys.push(key);
            _values.push(value);
        }
        
    this.Get =
        function(key)
        {
            var found = false;

            if(_keys.length > 0)
            {
                var i;
                for(i = 0; i < _keys.length; i++)
                {
                    if(_keys[i] == key)
                    {
                        found = true;
                        break;
                    }
                }
            }


            if(found)
            {
                _hits++;
                
                if(_hits > 1000) // empty cache
                {
                    this.Clear();
                }
                
                return _values[i];
            }
            else
            {
                return null;
            }
        }

    this.Clear =
        function()
        {
            _hits = 0;
            _keys.splice(0, _keys.length);
            _values.splice(0, _values.length);
        }        
}

function openZoom(sender, e, imageUrl)
{
    getZoom();
    repos(_zoomWindow, e);

    if(_zoomUrl != imageUrl)
    {
        _zoomWindow.innerHTML = "<img src=\"" + imageUrl + "\" />";
        _zoomUrl = imageUrl;
    }

    _zoomWindow.style.visibility = "visible";
}

function getZoom()
{
    if(_zoomWindow == null)
    {
        _zoomWindow = document.createElement("div");
        _zoomWindow.style.position = "absolute";
        _zoomWindow.style.padding = "3px";
        _zoomWindow.style.border = "1px solid #000000";
        _zoomWindow.style.backgroundColor = "#ffffff";
        document.body.appendChild(_zoomWindow);
    }
}

function repos(obj, e)
{

    var scrollOffset = document.body.scrollTop > 0 ? document.body.scrollTop : document.documentElement.scrollTop;
    var windowWidth = document.documentElement.offsetWidth;
    var windowHeight = isNaN(window.innerHeight) ? document.documentElement.offsetHeight : window.innerHeight;

    var left = e.clientX + 15;
    var top;
    if(_isSafari)
    {
        top = e.clientY + 15;
    }
    else
    {
        top = e.clientY + 15 + scrollOffset;
    }
     
    var width = obj.offsetWidth;
    if(left + width > windowWidth)
    {
        left = e.clientX - width - 15;
    }
    
    var height = obj.offsetHeight;
    if(top + height > windowHeight + scrollOffset)
    {
        if(_isSafari)
        {
            top = e.clientY - height - 15;
        }
        else
        {
            top = e.clientY + scrollOffset - height - 15;
        }
        
    }
    
    if(top < scrollOffset)
    {
        top = windowHeight + scrollOffset - height;
    }

    obj.style.top = new String(top) + "px";
    obj.style.left = new String(left) + "px";

}

function closeZoom()
{
    if(_zoomWindow != null)
    {
        _zoomWindow.style.visibility = "hidden";
    }
}

function clearAllImages()
{
    if(confirm("Are you sure you want to remove all selected images?"))
    {
        var url = "PhotoGallery/GalleryFunctions.aspx?func=clearall";
        getPage(url, "go(\"PhotoGallery/SelectedImages.aspx\");");
    }
}

function checkImage(sender, imageID, updateParent)
{

    var url = "/PhotoGallery/GalleryFunctions.aspx?func=";
    if(sender.checked)
    {
        url += "addimage";
    }
    else
    {
        url += "removeimage";
    }
    url += "&id=" + imageID;

    getPage(url);
    
    if(updateParent)
    {
        try
        {
            opener.document.getElementById(sender.id).checked = sender.checked;
        }
        catch(ex) {}
    }
}

function toggleJob(jobID)
{
    var divShort = document.getElementById("jobShort" + jobID);
    var divFull = document.getElementById("jobFull" + jobID);
    
    var dispShort = divShort.style.display;
    var dispFull = divFull.style.display;
    
    if(dispFull == "none")
    {
        dispFull = "";
        dispShort = "none";
    }
    else
    {
        dispFull = "none";
        dispShort = "";
    }
    
    divShort.style.display = dispShort;
    divFull.style.display = dispFull;
}

function toggleMedia(MediaID)
{
    var divFull = document.getElementById("Text" + MediaID);
    
    var dispFull = divFull.style.display;
    
    if(dispFull == "none")
    {
        dispFull = "";        
    }
    else
    {
        dispFull = "none";        
    }
        
    divFull.style.display = dispFull;
}

function togglePress(PressID)
{
    var divShort = document.getElementById("pressShort" + PressID);
    var divFull = document.getElementById("pressFull" + PressID);
    
    var dispShort = divShort.style.display;
    var dispFull = divFull.style.display;
    
    if(dispFull == "none")
    {
        dispFull = "";
        dispShort = "none";
    }
    else
    {
        dispFull = "none";
        dispShort = "";
    }
    
    divShort.style.display = dispShort;
    divFull.style.display = dispFull;
}

function galleryDetail(imageID)
{
    window.open("/PhotoGallery/ImageDetailPopup.aspx?id=" + imageID, "", "height=400,width=500,scrollbars=no,resizable=yes,toolbar=no,status=no,alwaysRaised=yes");
}

var debug = false;
function getPage(url, retFunc, post, errFunc)
{
    var oXml;

    try
    {
        oXml = new XMLHttpRequest();
    }
    catch(e)
    {
        try
        {
              oXml = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch(e)
        {
              oXml = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }

    if(oXml == null)
    {
        return;
    }

    oXml.onreadystatechange = 
        function()
        {
              if(oXml.readyState == 4)
              {
                    if(oXml.status == 200)
                    {
                          eval(retFunc);
                    }
                    else
                    {
                          if(debug)
                          {
                                var newWin = window.open();
                                newWin.document.write(oXml.responseText);
                          }
                          if(errFunc)
                          {
                                eval(errFunc);
                          }
                    }
              }
        }

    var verb = "GET";
    if(post && post != "")
    {
        verb = "POST";
    }
    else
    {
        post = null;
    }

    oXml.open(verb, url, true);
    try
    {
        oXml.send(post);
    }
    catch(e)
    {
        if(errFunc)
        {
              eval(errFunc);
        }
    }
}