/*
* wdscript2 v2
* http://www.stanusch.com/
*
* Stanusch Technologies S.A.
*
* Date: 05-12-2011
* Author: Kamil Muichalik
*
* Wymagane skrypty: 1) SWFObject v2.2 - http://code.google.com/p/swfobject 
*                   2) jQuery 1.3.x lub wyższy http://jquery.com/ - gdy isMovable == true
*/

var stwd_adviser = {
    loader: "http://wenus.stanuschtechnologies.pl/players/player3/cugino/loader.swf",
    expressInstall: "http://wenus.stanuschtechnologies.pl/players/player3/Common/expressInstall.swf",
    skin: "http://wenus.stanuschtechnologies.pl/players/player3/cugino/avatar.swf",
    configFile: "http://wenus.stanuschtechnologies.pl/players/player3/cugino/avatar.xml",
    engine: "http://wenus.stanuschtechnologies.pl/players/player3/Common/engine.swf",
	bcgImage: "url('http://wenus.stanuschtechnologies.pl/players/player3/cugino/bcg.png')",
	buttonOnImage: "http://wenus.stanuschtechnologies.pl/players/player3/cugino/btnOpen.png",
    buttonOffImage: "http://wenus.stanuschtechnologies.pl/players/player3/cugino/btnClose.png",
    actionId: "",
    currentPage: "",
    playerId: "",
    partitionName: "",
    cookieName: "swtd_cookie_12342",
    wmode: "transparent",
    width: "299",
    height: "540",
    conatiner: "wdContentOnSite",
    uid: "stwduid",
    isMovable: true, //wymagany jQuery 1.3.X
    isOpen: true,
    top: "5%",
    dynamicContainerId: "stwd_dynamic_container_12342",
    wdContainerFormMoving: "wdst",
    wdWrapperForMoving: "wdwrapper",
    wdHeaderForMoving: "wdheader",
    buttonWidth: "31px",
    buttonHeight: "278px",
    float: "left",
    bcgColor: "transparent",
    saveState: true,
    borderColor: "#dddddd",
    borderStyle: "solid",
    borderWidth: 0,
	buttonMarginTop: "1px",
    noFlashInfo: "<p>Wymagany Flash Player 9 lub nowszy: <a href='http://get.adobe.com/flashplayer/?promoid=DAFYL'>http://get.adobe.com</a></p>",
      embed: function (actionId) {
        if (actionId) this.actionId = actionId;
        this.flashvars = {
            skin: this.skin,
            configfile: this.configFile,
            engine: this.engine,
            startQuestion: this.actionId,
            currentpage: this.currentPage,
            playerId: this.playerId,
            partitionName: this.partitionName
        };
        this.params = {
            menu: "false",
            wmode: this.wmode,//this.isFfGe4() ? "window":this.wmode, //fla FF4+ zawsze window
            allowScriptAccess: "always"
        };
        this.attributes = {
            id: this.uid,
            name: this.uid
        };

        if (this.isMovable) {

            //wymagane jQuery, sprawdx i poinformuj, ze nie ma jQuery

            if (this.float == 'left')
                jQuery('body').append("<div id='" + stwd_adviser.wdContainerFormMoving + "'><div id='" + this.wdWrapperForMoving + "'></div><div id='" + stwd_adviser.wdHeaderForMoving + "'></div></div>"); //tylko raz!!uwaga funckaja rmove()
            else
                jQuery('body').append("<div id='" + stwd_adviser.wdContainerFormMoving + "'><div id='" + stwd_adviser.wdHeaderForMoving + "'></div><div id='" + this.wdWrapperForMoving + "'></div></div>"); //tylko raz!!uwaga funckaja rmove()
            document.getElementById(this.wdWrapperForMoving).innerHTML = "<div id='" + this.dynamicContainerId + "'>" + stwd_adviser.noFlashInfo + "</div>";

            //state saved in cookie
            if (this.saveState) {
				if(this.cookie() == '1')
				{
					this.isOpen = true;
				}
				else if(this.cookie() == '0')
				{
					this.isOpen = false;
				}
            }

            var bcgStr = this.isOpen ? "url(\'" + this.buttonOnImage + "\')" : "url(\'" + this.buttonOffImage + "\')";
            jQuery('#' + stwd_adviser.wdHeaderForMoving).css('background-image', bcgStr);
            jQuery('#' + stwd_adviser.wdHeaderForMoving).css('background-repeat', "no-repeat");
            jQuery('#' + stwd_adviser.wdHeaderForMoving).css('background-color', this.bcgColor);
			jQuery('#' + stwd_adviser.wdHeaderForMoving).css('margin-top', this.buttonMarginTop);
            jQuery('#' + stwd_adviser.wdWrapperForMoving).css('background-color', this.bcgColor);
			jQuery('#' + stwd_adviser.wdWrapperForMoving).css('background-image', this.bcgImage);
			jQuery('#' + stwd_adviser.wdHeaderForMoving).css('background-repeat', "no-repeat");
			
            jQuery('#' + stwd_adviser.wdWrapperForMoving).css('border', this.borderWidth + "px  " + this.borderStyle + " " + this.borderColor);
            jQuery('#' + stwd_adviser.wdHeaderForMoving).css('border', this.borderWidth + "px  " + this.borderStyle + " " + this.borderColor);
            
            jQuery('#' + stwd_adviser.wdHeaderForMoving).css('cursor', "pointer");
            jQuery('#' + stwd_adviser.wdHeaderForMoving).css('width', this.buttonWidth);
            jQuery('#' + stwd_adviser.wdHeaderForMoving).css('height', this.buttonHeight);
            jQuery('#' + this.wdContainerFormMoving).css("width", "auto");
            jQuery('#' + this.wdContainerFormMoving).css("top", this.top);

            //zeby animate dobrze działało
            jQuery('#' + this.wdWrapperForMoving).css("width", this.width + "px");
            jQuery('#' + this.wdWrapperForMoving).css("height", this.height + "px");

            var tmpWidth = parseInt(this.width) + parseInt(this.borderWidth) * 2;
            if (this.float == 'left') {
                jQuery('#' + stwd_adviser.wdHeaderForMoving).css('border-left-style', "none");
                if (!this.isOpen) {
                    jQuery('#' + this.wdContainerFormMoving).css("left", "-" + tmpWidth + "px");
                }
                else
                    jQuery('#' + this.wdContainerFormMoving).css("left", "0");
                jQuery('#' + this.wdHeaderForMoving).css("float", "left");
                jQuery('#' + this.wdWrapperForMoving).css("float", "left");

            }
            else {
                jQuery('#' + stwd_adviser.wdHeaderForMoving).css('border-right-style', "none");
                if (!this.isOpen)
                    jQuery('#' + this.wdContainerFormMoving).css("right", "-" + tmpWidth + "px");
                else
                    jQuery('#' + this.wdContainerFormMoving).css("right", "0");
                jQuery('#' + this.wdWrapperForMoving).css("float", "left");
                jQuery('#' + this.wdHeaderForMoving).css("float", "left");
            }

            if (!this.isOpen) {
                this.cookie('0', null);
            }
            else {
                this.cookie('1', null);
                this.show();
            }

            if (document.all && !window.opera && !window.XMLHttpRequest) {
                jQuery('#' + this.wdContainerFormMoving).css("position", "absolute");
            }
            else {

                jQuery('#' + this.wdContainerFormMoving).css("position", "fixed");
            }

			
            //swfobject.embedSWF(this.loader, this.dynamicContainerId, this.width, this.height, "9", this.expressInstall, this.flashvars, this.params, this.attributes);

            jQuery("#wdheader").live("click", function () {
                stwd_adviser.toggle();
            });
        }
        else {
            document.getElementById(this.conatiner).innerHTML = "<div id='" + this.dynamicContainerId + "'>" + this.noFlashInfo + "</div>";
            swfobject.embedSWF(this.loader, this.dynamicContainerId, this.width, this.height, "9", this.expressInstall, this.flashvars, this.params, this.attributes);
        }
    },
    isFfGe4 :function() {
        if (/Firefox[\/\s](\d+\.\d+)/.test(navigator.userAgent)) { //test for Firefox/x.x or Firefox x.x (ignoring remaining digits);
            var ffversion = new Number(RegExp.$1) // capture x.x portion and store as a number
            if (ffversion >= 4)
                return true
        }
        return false;
    },
    hide: function () {
        this.isOpen = false;
        jQuery('#' + this.wdHeaderForMoving).css('background-image', "url(\'" + this.buttonOffImage + "\')");
        jQuery('#' + this.wdHeaderForMoving).attr("title", "Otwórz");
        jQuery('#' + this.wdWrapperForMoving).css("width", this.width + "px");
        jQuery('#' + this.wdWrapperForMoving).css("height", this.height + "px");
        var tmpWidth = parseInt(this.width) + parseInt(this.borderWidth) * 2;

        if (this.float == 'left') {
            jQuery("#" + this.wdWrapperForMoving).parent().stop().animate({ left: '-' + tmpWidth + 'px' }, function () {
                stwd_adviser.remove();
            });
        }
        else {
            jQuery("#" + this.wdWrapperForMoving).parent().stop().animate({ right: '-' + tmpWidth + 'px' }, function () {
                stwd_adviser.remove();
            });
        }

        //set cookie
        this.cookie('0', null);
    },
    show: function () {
        this.isOpen = true;
        document.getElementById(this.wdWrapperForMoving).innerHTML = "<div id='" + this.dynamicContainerId + "'></div>";

        jQuery('#' + this.wdHeaderForMoving).css('background-image', "url(\'" + this.buttonOnImage + "\')");

        jQuery('#' + stwd_adviser.wdHeaderForMoving).css('background-color', this.bcgColor);
		jQuery('#' + stwd_adviser.wdHeaderForMoving).css('margin-top', this.buttonMarginTop);
        jQuery('#' + stwd_adviser.wdWrapperForMoving).css('background-color', this.bcgColor);
		jQuery('#' + stwd_adviser.wdWrapperForMoving).css('background-image', this.bcgImage);
		jQuery('#' + stwd_adviser.wdHeaderForMoving).css('background-repeat', "no-repeat");
			
        jQuery('#' + this.wdHeaderForMoving).attr("title", "Zamknij");
        jQuery('#' + this.wdWrapperForMoving).css("width", this.width + "px");
        jQuery('#' + this.wdWrapperForMoving).css("height", this.height + "px");

        if (this.float == 'left') {
            jQuery("#" + this.wdWrapperForMoving).parent().stop().animate({ left: '0' }, 'slow', function () {
                stwd_adviser.open();
            });
        }
        else {
            jQuery("#" + this.wdWrapperForMoving).parent().stop().animate({ right: '0' }, 'slow', function () {
                stwd_adviser.open();
            });
        }

        //set cookie
        this.cookie('1', null);
    },
    toggle: function () {
        if (this.isOpen) {
            this.hide();
        }
        else {
            this.show();
        }
    },
    close: function () {
    },
    open: function () {
        swfobject.embedSWF(this.loader, this.dynamicContainerId, this.width, this.height, "9", this.expressInstall, this.flashvars, this.params, this.attributes);
    },
    answerQuestion: function (actionId) {
        try { document.getElementById(this.uid).answerQuestion(actionId); }
        catch (e) { }
    },
    remove: function () {
        swfobject.removeSWF(this.uid);
    },
    openData: function (data) {
        window.open(data, "_self");
    },
    cookie: function (value, options) {
        var key = this.cookieName;
        // key and value given, set cookie...
        if (arguments.length > 1 && (value === null || typeof value !== "object")) {
            options = jQuery.extend({}, options);

            if (value === null) {
                options.expires = -1;
            }

            if (typeof options.expires === 'number') {
                var days = options.expires, t = options.expires = new Date();
                t.setDate(t.getDate() + days);
            }

            return (document.cookie = [
                encodeURIComponent(key), '=',
                options.raw ? String(value) : encodeURIComponent(String(value)),
                options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
                options.path ? '; path=' + options.path : '',
                options.domain ? '; domain=' + options.domain : '',
                options.secure ? '; secure' : ''
            ].join(''));
        }

        // key and possibly options given, get cookie...
        options = value || {};
        var result, decode = options.raw ? function (s) { return s; } : decodeURIComponent;
        return (result = new RegExp('(?:^|; )' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie)) ? decode(result[1]) : null;
    }
}



