/**
 * aheadWorks Co.
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the EULA
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://ecommerce.aheadworks.com/LICENSE-M1.txt
 *
 * @category   AW
 * @package    AW_Maprice
 * @copyright  Copyright (c) 2003-2010 aheadWorks Co. (http://www.aheadworks.com)
 * @license    http://ecommerce.aheadworks.com/LICENSE-M1.txt
 */

var mapDetect = navigator.userAgent.toLowerCase();
var mapOS,mapBrowser,mapVersion,mapTotal,mapThestring;

function mapGetBrowserInfo() {
    if (mapCheckIt('konqueror')) {
        mapBrowser = "Konqueror";
        mapOS = "Linux";
    }
    else if (mapCheckIt('safari')) mapBrowser 	= "Safari"
    else if (mapCheckIt('omniweb')) mapBrowser 	= "OmniWeb"
    else if (mapCheckIt('opera')) mapBrowser 	= "Opera"
    else if (mapCheckIt('webtv')) mapBrowser 	= "WebTV";
    else if (mapCheckIt('icab')) mapBrowser 	= "iCab"
    else if (mapCheckIt('msie')) mapBrowser 	= "Internet Explorer"
    else if (!mapCheckIt('compatible')) {
        mapBrowser = "Netscape Navigator"
        mapVersion = mapDetect.charAt(8);
    }
    else mapBrowser = "An unknown browser";

    if (!mapVersion) mapVersion = mapDetect.charAt(place + mapThestring.length);

    if (!mapOS) {
        if (mapCheckIt('linux')) mapOS 		= "Linux";
        else if (mapCheckIt('x11')) mapOS 	= "Unix";
        else if (mapCheckIt('mac')) mapOS 	= "Mac"
        else if (mapCheckIt('win')) mapOS 	= "Windows"
        else mapOS 	= "an unknown operating system";
    }
};

function mapCheckIt(string) {
    place = mapDetect.indexOf(string) + 1;
    mapThestring = string;
    return place;
};

/*-----------------------------------------------------------------------------------------------*/

Event.observe(window, 'load', mapGetBrowserInfo, false);

var Maprice = Class.create();

Maprice.prototype = {
    yPos : 0,
    xPos : 0,
    isLoaded : false,

    initialize: function(url) {
        // this.content = url+productId+'/';
        this.content = url;
        this.activate();
    },

    activate: function(){
        if (mapBrowser == 'Internet Explorer'){
            this.getScroll();
        //      this.prepareIE('100%', 'hidden');
            this.setScroll(0,0);
            this.hideSelects('hidden');
        }
        this.displayMaprice("block");
    },

    prepareIE: function(height, overflow){
        bod = document.getElementsByTagName('body')[0];
        bod.style.height = height;
        bod.style.overflow = overflow;

        htm = document.getElementsByTagName('html')[0];
        htm.style.height = height;
        htm.style.overflow = overflow; 
    },

    hideSelects: function(visibility){
        selects = document.getElementsByTagName('select');
        for(i = 0; i < selects.length; i++) {
            selects[i].style.visibility = visibility;
        }
    },

    getScroll: function(){
        if (self.pageYOffset) {
            this.yPos = self.pageYOffset;
        } else if (document.documentElement && document.documentElement.scrollTop){
            this.yPos = document.documentElement.scrollTop; 
        } else if (document.body) {
            this.yPos = document.body.scrollTop;
        }
    },

    setScroll: function(x, y){
        window.scrollTo(x, y); 
    },

    displayMaprice: function(display){
        $('maprice-overlay').style.display = display;
        $('maprice').style.display = display;
        if(display != 'none') this.loadInfo();
    },

    loadInfo: function() {
        $('maprice').className = 'loading';
        var myAjax = new Ajax.Request(
            this.content,
            {method: 'post', parameters: '', onComplete: this.processInfo.bindAsEventListener(this)}
        );
        
    },

    processInfo: function(response){
        $('mapContent').update(response.responseText);
        $('maprice').className = 'done';
        this.isLoaded = true;
        this.actions();
    },

    actions: function(){
        mapActions = document.getElementsByClassName('mapAction');
    },

    deactivate: function(){
        if (mapBrowser == "Internet Explorer"){
            this.setScroll(0,this.yPos);
            this.prepareIE("auto", "auto");
            this.hideSelects("visible");
        }

        this.displayMaprice("none");
    }
};

// = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

arrayIntersect = function(a, b) {
    var res = [];

    for(var i=0; i<a.length; i++)
        for(var j=0; j<b.length; j++)
            if(a[i] == b[j]) {
                res.push(b[j]);
                break;
            }

    return res;
};
