﻿//file: ajaxJSON.js
//
//require ajaxutils.js
//require domutils.js

var oAsyncRotator = {
    mInit : function(){
        var oSubFullBlock = document.getElementById("content");
        if(oSubFullBlock == null){
            //alert("no sub_fullblock");
            return false;
        }
        var aRotators = oDOMUtils.mFindElements(oSubFullBlock, oAsyncRotator.mIsRotator, new Array());
        if(aRotators == null){
            //alert("no rotators");
            return false;
        }
        for(var i = 0; i < aRotators.length; i++){
            var oLink = aRotators[i].getElementsByTagName('a')[0];
            var oImageContainer = oDOMUtils.mFindElement(aRotators[i], oAsyncRotator.mIsImageContainer);
            if(oImageContainer == null)
                continue;
            
            oDOMUtils.mAddEvent(oImageContainer, "click", oAsyncRotator.mPop);
            
            var sSpotId = oImageContainer.id.match(/([1-9]\d*)$/)[0];
            var sUrl = document.location + "?spotid=" + sSpotId + "&imageid=0";
            oAsyncRotator.mShowImage(sUrl, oImageContainer);
            if(oLink != null){
                oDOMUtils.mAddEvent(oLink, "click", oAsyncRotator.mRotate);
            }
        }  
    },
    
    mIsRotator : function(oElm) {
        if(oElm.className && oElm.className.match(/\brotator\b/))
            return true;
        else
            return false
    },
    
    mIsImageContainer : function(oElm) {
        if(oElm.id && oElm.id.match(/\bthumb\d+/))
            return true;
        else
            return false
    },
    
    mShowImage : function( url , container){
        var oContainer;
        if(typeof container == "string")
            oContainer = document.getElementById(container);
        else if(typeof container == "object")
            oContainer = container;
        else {
            //alert("illegal type");
            return false;
        }
        var oRequest = typeof XMLHttpRequest == "undefined" ? new XMLHttpRequestIE() : new XMLHttpRequest(); //remember to include ajaxutils.js
        if(oRequest.overrideMimeType)
            oRequest.overrideMimeType('text/xml');
        
        if (!oRequest) {
            //alert('Giving up :( Cannot create an XMLHTTP instance');
            return false;
        }
        
        oRequest.onreadystatechange = function() {
            if (oRequest.readyState == 4) {        
                   if (oRequest.status == 200)
                        oContainer.innerHTML = oRequest.responseText;
                   else
                        oContainer.innerHTML = 'There was a problem with the request.';
            }
            else {
                oContainer.innerHTML = 'loading...';//'<img src="/ui/park/img/animated/spinning.gif" />';
            }
        }
        oRequest.open('GET', url, true);
        oRequest.send(null);
    },
    
    mPop : function(e) {
        oDOMUtils.sTarget = "div";
        var oBox = oDOMUtils.mFindTarget(e);
        var oImageContainer = oDOMUtils.mFindElement(oBox, oAsyncRotator.mIsImageContainer);
        var sSpotId = oImageContainer.id.match(/([1-9]\d*)$/)[0];
        var oImage = oImageContainer.getElementsByTagName("img")[0];
        var sUrl = document.location + "?spotid=" + sSpotId + "&imageid=" + oImage.id + "&large=true";
        window.open(sUrl)
    },
    
    mRotate : function(e) {
        oDOMUtils.sTarget = "div";
        var oBox = oDOMUtils.mFindTarget(e);
        var oLink = oBox.getElementsByTagName('a')[0];
        var oImageContainer = oDOMUtils.mFindElement(oBox, oAsyncRotator.mIsImageContainer);
        var sSpotId = oImageContainer.id.match(/([1-9]\d*)$/)[0];
        var oImage = oImageContainer.getElementsByTagName("img")[0];
        var sUrl = document.location + "?spotid=" + sSpotId + "&imageid=" + oImage.id;
        oAsyncRotator.mShowImage(sUrl, oImageContainer);
    }
}

oDOMUtils.mAddEvent(window, "load", oAsyncRotator.mInit);