var basedir = '';





getPath = function() {

    if(document.URL.lastIndexOf('www.rzgw.szczecin.pl')>-1) 

    var url = basedir;

    else if(document.URL.lastIndexOf('www')>-1)

    var url = document.URL.substr(0,document.URL.lastIndexOf('www')) + 'www';

    return url;

};

var tellAFriend = function(id) {

    var link = id.getElement('a');

    var sendform = function(windowDiv,myForm) {

        if(myForm) myForm.addEvent('submit', function(e){

            var e = new Event(e);

            e.stop();

            var save = new Element('input',{

                'type':'hidden',

                'value':1,

                'name':'save'

            }).inject(myForm);

            myForm.set('send', {

                onRequest: function() {

                    windowDiv.getElement('div.cont').empty();

                    windowDiv.addClass('loading');

                },

                onComplete: function(response) {

                    windowDiv.getElement('div.cont').set('html',response);

                    windowDiv.removeClass('loading');

                    if ($('save')) {

                        $('save').set('id','save').inject($('save').getParent(), 'after');

                        $('save').getPrevious('div').destroy();

                    }

					if (!windowDiv.getElement('div.cont').getElement('form') && $('taf-req')) {

                        $('taf-req').destroy();

                    }

                    if ($('taf-msg')) {

                    $('taf-msg').getElement('em').inject($('comment').getParent().getPrevious().getElement('label'));

                        $('taf-msg').destroy();

                    }

                    windowDiv.getElements('label').each(function(el){el.set('html',el.get('html').replace('*','<span class="req">*</span>'))});

                    sendform(windowDiv,windowDiv.getElement('form'));

                }

            });

            myForm.send();

        });

    };

    var insert = function() {

        var windowDiv = new Element('div',{

            'id': 'windowTellAFriend',

            'class':'loading',

            'html':'<div class="window"><a href="#" class="close">' + lang_close + '</a><h3>' + lang_tellAFriend + '</h3><div class="cont"></div></div>'

        }).inject(document.body);

        windowDiv.setStyle('left',window.getSize().x / 2 - 350 + 'px');

        if(Browser.Engine.trident4) windowDiv.setStyle('top',parseInt(window.getScroll().y + window.getSize().y / 10) + 'px');

        var myDragInstance = new Drag(windowDiv,{

            handle: windowDiv.getElement('h3')

        });

        windowDiv.getElement('h3').setStyle('cursor','move');

        var req = new Request({

            url: link.get('href'),

            onComplete: function(response) {

                windowDiv.getElement('div.cont').set('html',response);

                windowDiv.removeClass('loading');

                if ($('save')) {

                    $('save').inject($('save').getParent(), 'after');

                    $('save').getPrevious('div').destroy();

                }

                if (!windowDiv.getElement('div.cont').getElement('form') && $('taf-msg')) {

                        if($('comment')) $('taf-msg').getElement('em').inject($('comment').getParent().getPrevious().getElement('label'));

                        $('taf-msg').destroy();

                    }

                if (!windowDiv.getElement('div.cont').getElement('form') && $('taf-req')) {

					alert(2);

                    $('taf-req').destroy();

                }

                if (!windowDiv.getElement('div.cont').getElement('form')) {

                    windowDiv.getElement('div.cont').setStyle('display','none');

                    var x = new Element('div',{

                       'class': 'cont'

                    }).inject(windowDiv.getElement('div.cont'),'after');

                    windowDiv.getElement('div.cont').getElement('div').inject(x);

                }

                windowDiv.getElements('label').each(function(el){el.set('html',el.get('html').replace('*','<span class="req">*</span>'))});

                sendform(windowDiv,windowDiv.getElement('form'));

            }

        }).send();

        windowDiv.getElement('a').addEvent('click',function(d){

            var d = new Event(d);

            d.preventDefault();

            windowDiv.destroy();

        });

    };

    link.addEvent('click',function(e){

        var e = new Event(e);

        e.preventDefault();

        if(!$('windowTellAFriend')) {

            insert();

        }

    });

};



document.addEvent('domready',function(){

	

	var slides = $$('#blinds img');

	if ($('blinds')) {

	$('blinds').floom(slides, {

		slidesBase:basedir + 'static/img/banner/',

		sliceFxIn: {

			top: 20

		}

	});

	}

	

	if($('tools2') && !$('printTools')) {

		new FontSize({content:'font',path:getPath()+'/static/js/fontsize/',text:[lang_text+':',lang_small,lang_normal,lang_big], id: 'fontsize2', selector: 'tools2', where: 'top'});

        //new Bookmarks({text:lang_share,textMore:'&raquo;',path:getPath()+'/static/js/bookmarks/',target:'_blank', selector: 'tools2', id: 'bookmarks2',main: ['wykop','blip','facebook'],more: ['wykop','blip','facebook','separator','twitter','live','friendfeed','delicious','digg','google','technorati','gwar','bebopl','linkologia','zakladka']});   

    }

	if($('tellAFriend')) tellAFriend($('tellAFriend'));

	if($('tellAFriend2')) tellAFriend($('tellAFriend2'));

	if($('printTools'))

		{ if(Browser.Engine.trident4)  $('printTools').removeClass('fixed').inject(document.body,'top');

		if(Browser.Engine.gecko || Browser.Engine.webkit)  $('printTools').setStyle('background-color','rgba(245,241,184,0.95)');

		new  FontSize({selector:'printTools',path:getPath()+'static/js/fontsize/',text:[lang_text_size+':',lang_small,lang_normal,lang_big]});

		if(window.print)  $('printTools').getElement('input').addEvent('click',function(){window.print();});

		else  $('printTools').getElement('input').destroy();    }

	/*

	 * jesli na stronie jest cos co wymaga imagezooma/lightboxa to ladujemy zoomboxa - css i js :)

	 */

	if($$('a[rel^=zoombox]').length) {

		new Asset.css(basedir + '/static/js/zoombox/zoombox.css');

		new Asset.javascript(basedir + '/static/js/zoombox/zoombox.js', {

            onload: function(){

				new ZoomBox({

		            back:1

		        });

            }

        });

	}
	$$('.tab-group').each(function(group){
        var id    = group.get('id');
        var tabs  = group.getElements('[id^=' + id + '-]');
        var boxes = $$('[id^=' + id + '-][id$=-box]');
        
        tabs.getElements('a').each(function(a){
            a.addEvent('click', function(e){
                var e = new Event(e);
                e.preventDefault();
                
                toggleBox(this.getParent().get('id'));
                tabs.removeClass('active');
                this.getParent().addClass('active');
            });
        });
        
        var toggleBox = function(tabId){
            boxes.setStyle('display', 'none');
            if ( tabId && $(tabId + '-box') ) {
                $(tabId + '-box').setStyle('display', '');
            }
        };
        
        var activeTab   = tabs.getParent().getElement('.active');
        var activeTabId = activeTab ? activeTab.get('id') : null;
        toggleBox(activeTabId);
    });

	/*

     * jesli na stronie jest cos co do szarowania linka to ladujemy bookmarks

     * - czyli sprawdzamy czy jest zmienna bookmarks (tablica z idami elementow do ktorych

     * wrzucamy widgeta)

     */

	if(typeof bookmarks != 'undefined') {

        new Asset.css(basedir + '/static/js/bookmarks/bookmarks.css');

        new Asset.javascript(basedir + '/static/js/bookmarks/bookmarks.js', {

            onload: function(){

				bookmarks.each(function(element,index){

					new Bookmarks({

	                    text:lang_share,

	                    textMore:'&raquo;',

	                    path:basedir+'/static/js/bookmarks/',

	                    target:'_blank',

	                    selector: element,

	                    id: 'bookmarks'+index

	                });

				});

            }

        });

    }

	/*

     * jesli na stronie jest cos co chce zmienic fonty to ladujemy fontsize

     * - wlasciwie wszystko jak wyzej

     */

	if(typeof fontsize != 'undefined' && typeof fontsizetarget != 'undefined') {

        new Asset.css(basedir + '/static/js/fontsize/fontsize.css');

        new Asset.javascript(basedir + '/static/js/fontsize/fontsize.js', {

            onload: function(){

				fontsize.each(function(element,index){

	                new FontSize({

						content:fontsizetarget,

						path:basedir+'/static/js/fontsize/',

						text:[lang_text+':',lang_small,lang_normal,lang_big],

						selector: element,

						id:'fontsize'+index

					});
				});

            }

        });

    }

	

    

	if ($('hotnews')) {

    	var parent = $('hotnews');

    	var act    = 'act';

    	var lis    = $('hotnews').getElements('li');

    	var lia    = parent.getElement('li.' + act);

    	var time   = 5000;

    	var periodical;



    	

    lis.getElements('a').each(function(a){

	      

		    a.removeEvents('click');

            a.addEvent('mouseover', function(e){

                var e = new Event(e);

                e.preventDefault();

                var lio = lia;

                lia = this.getParent('li');                

				

				lis.removeClass('act');

				lia.addClass('act');

				

    			$(lio.get('id') + '-box').fade(0);

    			$(lia.get('id') + '-box').setStyle('display', '').setStyle('opacity', '0').fade(1);

    			

    			$clear(periodical);

    			periodical = setNextAct.periodical(time);

            });



        });

    	

    	var setNextAct = function() {

    		var lio = lia;

			lia = lia.getNext('li') ? lia.getNext('li') : lis[0];

			

			$(lio.get('id') + '-box').fade(0);

			$(lia.get('id') + '-box').setStyle('display', '').setStyle('opacity', '0').fade(1);

			

    		lia.addClass(act);

    		lio.removeClass(act);

    	}

    	

    	periodical = setNextAct.periodical(time);

    }

	

	$$('.tab-group').each(function(group){

        var activeClass = 'act';

        var id    = group.get('id');

        var tabs  = group.getElements('[id^=' + id + '-]');

        var boxes = $$('[id^=' + id + '-][id$=-box]');

        

        tabs.getElements('a').each(function(a){

            a.addEvent('click', function(e){

                var e = new Event(e);

                e.preventDefault();

                

                toggleBox(this.getParent('*[id^=' + id + ']').get('id'));

                tabs.removeClass(activeClass);

                this.getParent('li').addClass(activeClass);

            });

        });

        

        var toggleBox = function(tabId){

            boxes.setStyle('display', 'none');

            if ( tabId && $(tabId + '-box') ) {

                $(tabId + '-box').setStyle('display', '');

            }

        };

        

        var activeTab   = tabs.getParent().getElement('.' + activeClass);

        var activeTabId = activeTab ? activeTab.get('id') : null;

        toggleBox(activeTabId);

    });

	//news background

	$$('.news').each(function(news){

		var color = news.getStyle('backgroundColor');

		news.addEvents({

		    mouseenter: function(){

		        this.setStyle('cursor','pointer');

		        this.morph({

			        'background-color': '#ffffff'

		        });

			},

		    mouseleave: function(){

		        this.morph({

		        	'background-color': color

		        });

		    }

		});

	});



		var link = $$('.news a');

		link.each(function(element) {

		    element.getParent().addEvent('click', function(){

		        window.location = element.get('href');

		        // on click, background color and border will turn to a different color

		        this.morph({

		            //'background-image': 'none',

		            //'background-color': '#fff'

		        });

		    });

		});

});
