﻿var _mainUrl = null;
var _isSafari = false;

function go(mainUrl)
{
    if(mainUrl != null && mainUrl != "null")
    {
		_mainUrl = mainUrl;
		if(_mainUrl == "")
		{
			document.getElementById("maincontent").innerHTML = "";
		}
		else
		{
			var extraFunc = "";
			if(mainUrl == "design_process.aspx")
			{
				extraFunc = "rotateText();";
			}
			getPage(_mainUrl, "loadDiv('maincontent', oXml); closeZoom();" + extraFunc);
		}
    }
}

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", "C_1"); 
	    fl.SetVariable("showDealerNav", newVal); 
	    fl.Play();
    }
}

function init()
{
    _isSafari = (navigator.userAgent.toLowerCase().indexOf("safari") > -1);        
	if(typeof(initPage) != "undefined" && initPage.length > 0)
	{
		try
		{
			go(initPage[0]);
		}
		catch(e) {}
	}
}

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(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");
}

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 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);
        }
    }
}

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;
}

var designText=new Array()
designText[0]="<img src='images/headers/distinctivecolors.gif' /><p style='margin-top:3px'><font size='2'>Mandala features a distinctive master color palette that is anchored by <br />" +
                "traditional color families, updated with new and contemporary nature-inspired <br />" +
                "hues. From rich neutrals like Carbon, Cocoa, Cayenne, and Crema to <br />" +
                "landscape shades of Caspian Blue and Tarragon Green, it is a “chameleon” <br />" +
                "palette that adapts well to any style, modern to traditional. Mandala’s colors <br />" +
                "are intuitive but not impulsive. They are based on extensive design and color <br />" +
                "research, offering you fresh alternatives that will last for many years.</font></p>"
designText[1]=""
designText[2]="<img src='images/headers/globaldesignnetwork.gif' /><p style='margin-top:3px'><font size='2'>Mandala’s designers are adventurers, driven to create new and innovative art <br />" +
                "tile concepts. Once a vision is established, we search the world for the very <br />" +
                "best suppliers to realize Mandala’s designs in the tile medium of choice. Our <br />" +
                "global design and manufacturing network allows us to bring you exceptional <br />" +
                "tile that appeals to the most discerning tastes.</font></p>"
designText[3]=""
designText[4]="<img src='images/headers/texturalinterest.gif' /><p style='margin-top:3px'><font size='2'>From the silky-smooth softness of Etrusco natural stone to the crystal clarity <br />" +
        "of Wavelength fused glass or the hand-rubbed modern antiquity of Labyrinth <br />" +
        "etched stone, Mandala is a gallery of functional art. Its lush finishes and visual <br />" +
        "textures breathe life into any environment.</font></p>"
designText[5]=""    
var current=0

var ns6=document.getElementById&&!document.all
function rotateText()
{
	var ele = document.getElementById("design_process_text");
	if(ele != null)
	{
		ele.innerHTML=designText[current]
		if (current==5) { current=0 }
		else { current++ }
		
		if (current==1)
		{
			setTimeout("rotateText()",20000)    
		}
		else if (current==3 || current==5)
		{
			setTimeout("rotateText()",15000)
		}
		else
		{
			setTimeout("rotateText()",1000)
		}
	}
	
}

function doImageRequestForm()
{
    window.open("PhotoGallery/ImageRequestForm.aspx", "", "width=400,scrollbars=yes,resizable=yes,toolbar=no,status=no,alwaysRaised=yes");
}

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) {}
    }
}