window.addEvent('domready', function() {        //basic show / hide
    Element.implement({
        show: function() {
            this.setStyle('display','block');
        },
        hide: function() {
            this.setStyle('display','none');
        },
        toggle: function() {
            switch( this.getStyle('display') ){
                case 'none':  this.show(); break;
                case 'block': this.hide(); break;
            }
        }
    });
    
    if(document.getElementById('flash')){
        var currentDate = new Date();
        swfobject.embedSWF(SITE_URL+'/assets/flash/flashWodociagi2.swf', "flash", "670", "250", "8.0.0", "expressInstall.swf",
            /* FLASHVARS    */    { plikXML: SITE_URL+'/calosc.xml?v='+currentDate.getTime() },
            /* PARAMS         */    { swliveconnect: true, wmode: "transparent", scale: "noScale" },
            /* ATTRIBUTES    */    { id: "flash", name: "flash" }
        );
    }


    MPWiK.start();
});

/**********************************************************************************************************************/
$$('.hint').each(function(el){
    var hintText = el.getProperty('alt') || '';
    el.value = hintText;
    
    var form = el.getParent('form');

    form.addEvent('submit',function(form){
    	$$('.hint').each(function(elem){
            if (elem.value == hintText){
            	elem.value = '';
            }
    	});
    });
    
    el.addEvent('focus', function(event) {
        el.removeClass('hint');
        if (el.value == hintText){
            el.value = '';
        }
    })
    
    el.addEvent('blur', function(event) {
        el.addClass('hint');
        if (el.value == ''){
            el.value = hintText;
        }
    });  
}); 


var MPWiK = {
    start: function(){
        this.cutting();
        this.closers();
        this.contact();
        this.newsTabs();
        this.bookmarksite();
        this.externalLinks();
        if($('news-year')){
            $('news-year').addEvent('change', function(){
                if( this.value != 1 )
                {
                    window.location = $(this).get('title').replace(':rok',this.value);
                }
            });
        }
    },
    
    cutting: function(){
        if( $('toggler') && $('podklad') )
        {
            $('toggler').addEvent('click', function(){
                $('podklad').toggle();
                return false;
            });
            if( $('podklad') ){
                $('podklad').hide();
            }
        }
    },
    
    closers: function(){
        $(document.body).getElements('.js_closer').each(function( closer ){
            closer.addEvent('click',function(){
                fadeOut('js_msg_container');
                return false;
            });
        });
        this._autoHide('js_msg_container',3000);
    },
    
    contact: function(){
        $(document.body).getElements('.reset').each(function( closer ){
            closer.addEvent('click',function(){
                this.getParent('form').reset();
                return false;
            });
        });
    },
    
    newsTabs: function(){
        var tabs = $(document.body).getElements('.tabs h5');
        var wrappers = $(document.body).getElements('.small-news .wrapper');
        var counter = 1;
        tabs.each(function( current ){
            if (current.id == '') {
                current.id = 'auto_tab_id_' + counter;
                counter++;
            }
            current.addEvent('click',function(){
                tabs.each(function( tab ){
                    tab.removeClass('active');
                    wrappers.each(function(w){ w.removeClass('active'); })
                });
                this.addClass('active');
                $(document.getElementById(this.id+'-content')).addClass('active');
                Cufon.replace('#'+this.id, { fontFamily: 'aller' });
                Cufon.refresh();
                return false;
            });
        });
    },
    
    externalLinks : function(){
    	$$('a').each(function(link){
    		if(!link.href.match(/^mailto\:/) && (link.hostname != location.hostname)){
    			link.target = '_blank';
    		}
    	})
    },
    
    bookmarksite : function(){
        // <a id="bookmarksite" href="BASEURL" rel="sidebar" title="TYTUL">dodaj do ulubionych</a>
        // add a "rel" attrib if Opera 7+
        // don't overwrite the rel attrib if already set
        if(window.opera) if ($("bookmarksite").attr("rel") != "") $("bookmarksite").attr("rel","sidebar");
        
        $("bookmarksite").addEvent('click',function(event){
            event.preventDefault(); // prevent the anchor tag from sending the user off to the link
            var url = location.href;
            var title = this.title;
            
            if (window.sidebar) { // Mozilla Firefox Bookmark
                window.sidebar.addPanel(title, url,"");
            } else if( window.external ) { // IE Favorite
                window.external.AddFavorite( url, title);
            } else if(window.opera) { // Opera 7+
                return false; // do nothing - the rel="sidebar" should do the trick
            } else { // for Safari, Konq etc - browsers who do not support bookmarking scripts (that i could find anyway)
                return false;
            }
        });
    },

    
    
    
    
    
    
    
    _autoHide: function(id,miliseconds){
        if( $(id) )
        {
            this._setTimeout(fadeOut, miliseconds, id);
        }
    },
    
    _setTimeout: function(fnPointer, ms){
        var args = arguments;
        function proxy(){
            var params = new Array(),
                i = null;
            for (i = 2; i < args.length; i++){ params.push(args[i]); }
            fnPointer.apply(this, params);
        }
        return window.setTimeout(proxy, ms);
    }
}

//////////////////////////////////////////
function setOpacity(eID, opacityLevel) {
    var eStyle = $(eID).style;
    eStyle.opacity = opacityLevel / 100;
    eStyle.filter = 'alpha(opacity='+opacityLevel+')';
}
function fade(eID, startOpacity, stopOpacity, duration) {
    var speed = Math.round(duration / 100);
    var timer = 0;
    if (startOpacity < stopOpacity){
        for (var i=startOpacity; i<=stopOpacity; i++) {
            setTimeout("setOpacity('"+eID+"',"+i+")", timer * speed);
            timer++;
        } return;
    }
    for (var i=startOpacity; i>=stopOpacity; i--) {
        setTimeout("setOpacity('"+eID+"',"+i+")", timer * speed);
        timer++;
    }
}
function fadeIn(eID) {
    setOpacity(eID, 0); $(eID).show(); var timer = 0;
    for (var i=1; i<=100; i++) {
        setTimeout("setOpacity('"+eID+"',"+i+")", timer * 5);
        timer++;
    }
}
function fadeOut(eID) {
    var timer = 0;
    for (var i=100; i>=1; i--) {
        setTimeout("setOpacity('"+eID+"',"+i+")", timer * 3);
        timer++;
    }
    setTimeout("$('"+eID+"').hide()", 310);
}

