function shuffle(ary) {
    function randOrd(){
        return (Math.round(Math.random())-0.5);
    }
    ary.sort( randOrd );
}

var Banner = {
    item:new Array(),
    order:new Array(),
    counter: 0,
    load: function(id,name,type,href,onclick,width,height){
        if(name!="" && type != "") {
            this.item[id]=new Array();
            this.item[id]["name"]=name;
            this.item[id]["height"]=height;
            this.item[id]["width"]=width;
            this.item[id]["type"]=type;
            this.item[id]["href"]=href;
            this.item[id]["onclick"]=onclick;
            this.order[this.counter] =  id;
            this.counter++;
        }
    },

    popup: function(idPopup){
        /*private functions*/
        var showPopup = function(idPopup){
            $('#banner_' + idPopup).fadeIn("slow");
            setRachCookie(1);
        }

        var setRachCookie = function(value, cookieName){
            /*initialization*/
            cookieName = cookieName || 'rachPopupsStatus';
            /*end initialization*/
            $.setCookie(cookieName, value, {
                duration: 1, // In days
                secure: false
            });
        }

        var testCookie = function(cookieName){
            /*initialization*/
            cookieName = cookieName || 'rachPopupsStatus';
            /*end initialization*/

            var popupStatus = $.readCookie(cookieName)
            if (!popupStatus) {
                setRachCookie(0, cookieName);
                return 0;
            }
            else {
                return popupStatus;
            }
        }

        var popupStatus = testCookie();
        if (popupStatus == 0) {
            showPopup(idPopup);
        }

    },

    rotate: function(){
        var swapArrayElements = function(array,from,to){
            var tempTo = array[to];
            array[to] = array[from];
            array[from] = tempTo;
            return array;
        }
        var rtb = $.readCookie('rtb');
        var order = this.order;
        shuffle(order);
        if (rtb != null) {
            order = rtb.split(',');
        }

        this.order = swapArrayElements(order,0,1)
        this.order = swapArrayElements(order,1,this.counter-1)

        $.setCookie('rtb', this.order, {
            duration: 0.02, // In days
            secure: false
        });

        this.render();
    },

    render: function(){
        var container = $('#ban-container').addClass('banner-container').html('');
        for(var count in this.order){
            var id = this.order[count];
            var bannerItemContainer = $('<div/>').addClass('banner-item-container');
            if(this.item[id]["type"] == 'image'){
                bannerItemContainer.attr('id', id).append(
                    $('<a/>')
                    .attr('href', this.item[id]["href"])
                    .attr('onclick',this.item[id]["onclick"])
                    .append(
                        $('<img/>')
                        .attr('src', '/images/rach_com/'+this.item[id]["name"])
                        .attr('width',this.item[id]["width"])
                        .attr('height',this.item[id]["height"])
                        .attr('alt',this.item[id]["name"])
                        )
                    );
            }else if (this.item[id]["type"] == 'flash'){          	
            	var html = '<embed type="application/x-shockwave-flash" '+
            	'src="/images/rach_com/'+this.item[id]["name"]+'?clickTAG=http://rachunkowosc.com.pl'+
            	this.item[id]["href"]+
            	'" width="'+this.item[id]["width"]+
            	'" height="'+this.item[id]["height"]+
            	'" style="undefined" id="heder" name="heder" quality="high" wmode="transparent" flashvars="clickTAG=http://rachunkowosc.com.pl'+
            	this.item[id]["href"]+
            	'&clickURL=http://rachunkowosc.com.pl'+
            	this.item[id]["href"]+'">';
                 bannerItemContainer.attr('id', id).append(html); 	
            }
            
            container.append(bannerItemContainer);
        }
    }
};

