﻿/**
 * @author prcvictim
 * Import: zXml.js
 * Using: AdRotator.initialize();
 */
var AdRotator = {
	initialize: function () {
	    this.divEls = null;
	    this.ad = null;
	    this.isLoaded = false;
	    this.initAdvByWebServices();
	},
	initAdvByWebServices: function () {
	    new zXmlHttp.Request("Web_services/services.asmx/GetAdvertisementList", {onSuccess: function (transport) {
	        var content = transport.responseXML.documentElement.childNodes[0].nodeValue;
	        eval("AdRotator.ad = " + content);
	        AdRotator.fill();
	    }});
	},
	fill: function () {
	    this.divEls = {};
        var divs = document.getElementsByTagName("DIV");
        for (var i = 0; i < divs.length; i++) {
            if (divs[i].getAttribute("AdRotator") != null) {
                this.divEls[divs[i].getAttribute("AdRotator")] = divs[i];
            }
        }
        
	    var listType = this.ad.Types;
	    for (var i = 0; i < listType.length; i++) {
	        var divEl = this.divEls[listType[i]];
	        var listAd = this.ad.Advertisements[listType[i]];
	        if (divEl != null) {
	            this.fillAd(divEl, listAd);
	        }
	    }
	    AdRotator.isLoaded = true;
	},
	fillAd: function (divEl, listAd, index) {
	    if (index == null) {
	        index = 0;
	        var timeout = 1;
	    } else {
	        var timeout = listAd[index][0];
	    }
	    setTimeout(function () {
	        if (listAd[index][1].indexOf(".swf") == -1) {
	            divEl.innerHTML = "<img class='" + divEl.getAttribute("AdRotator") + "' src='" + listAd[index][1] + "' />";
	            
	        } else {
	            divEl.innerHTML = "<object class='" + divEl.getAttribute("AdRotator") + "' classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0'>" +
                    "   <param name='movie' value='" + listAd[index][1] + "' />" +
                    "   <param name='quality' value='high' />" +
                    "</object>";
	        }
	        divEl.onclick = function () { AdRotator.onHitAdvertisement(listAd[index][3], listAd[index][2]) };
	        if (listAd.length > 1) {
	            if (index == listAd.length - 1)
	                index = -1;
	            eval("AdRotator.fillAd(divEl, listAd, ++index)");
	        }
	    }, timeout);
	},
	onHitAdvertisement: function (id, url) {
		if (url.indexOf("http://") != -1)
		{
			window.open(url);
			new zXmlHttp.Request('Web_services/services.asmx/HitAdvertisement?Id=' + id, {});
	    }
	}
};

(function () {
    var code = setInterval(function () {
	    if (document.body == null) {
	        return;
        }
	    clearInterval(code);
        AdRotator.initialize();
    }, 10); 
})();
 
//        this.ad = {
//            "Types": [ "PAGE_TOP", "DETAILS_PAGE_RIGHT", "PAGE_CENTER" ],
//            "Advertisements": {
//                "PAGE_TOP": [ 
//                    [5000, "images/banklogo/bank_1.gif", "http://www.ueeeu.com/config" ],
//                    [6000, "images/banklogo/bank_2.gif", "http://www.ueeeu.com/config" ],
//                    [7000, "images/banklogo/bank_3.gif", "http://www.ueeeu.com/config" ]
//                ],
//                "DETAILS_PAGE_RIGHT": [ 
//                    [8000, "images/banklogo/bank_4.gif", "http://www.ueeeu.com/config" ]
//                ],
//                "PAGE_CENTER": [ 
//                    [11000, "images/banklogo/bank_7.gif", "http://www.ueeeu.com/config" ],
//                    [12000, "images/banklogo/bank_12.gif", "http://www.ueeeu.com/config" ],
//                    [13000, "images/banklogo/bank_09.gif", "http://www.ueeeu.com/config" ]
//                ]
//            }
//        }
//        this.fill();