var Site = {
  init: function() {
		// Site.fixHomepageBoxes();
    Site.handleFormSubmit();
    Site.handleNewWinOpen();
    Site.handlePanels();
  },
  
	fixHomepageBoxes: function() {
		var arrNewsLi = $$('ul.fresh-news li');
		if(arrNewsLi.length>0) {
			var heightSize = 0;
			arrNewsLi.each(function(link) {
				if(link.offsetHeight>heightSize) heightSize = link.offsetHeight;
			});
			arrNewsLi.each(function(link) {
				link.style.height = heightSize +'px';
			});
		}
	},
  
  handleFormSubmit: function() {
    var confirmText = 'Da li ste sigurni da želite spremiti promjene?';
    var confirmDeleteText = 'Da li ste sigurni da želite brisati?\nOvo se ne moze povratiti!';
    if($('frmEdit')) {
      $('frmEdit').addEvent('submit', function(e) {if(!confirm(confirmText)) e = new Event(e).stop();});
    }
    var saveButton = $E('a.save', 'toolbar');
    if($(saveButton) && $(saveButton).getProperty('rel')!='') saveButton.addEvent('click', function(e) {if(confirm(confirmText)) {$(saveButton.getProperty('rel')).submit();}});
    var resetButton = $E('a.reset', 'toolbar');
    if($(resetButton) && $(resetButton).getProperty('rel')!='') resetButton.addEvent('click', function(e) {$(resetButton.getProperty('rel')).reset();return false;});
    var deleteButtons = $$('a.delete');
    if(deleteButtons.length>0) {
      if(deleteButtons.length>0) deleteButtons.each(function(button) {button.addEvent('click', function(e) {if(!confirm(confirmDeleteText)) {e = new Event(e).stop();}})});
    }
  }, 
  
  handleNewWinOpen: function() {
    var links = $$('a.newWin');
    if(links.length>0) links.each(function(link) {link.addEvent('click', function(e) {window.open(this, '');e = new Event(e).stop();})});
  }, 
  
  handlePanels: function() {
    var tabsContainers = $$('ul.tabs-container');
		var panelsContainers = $$('div.panels-container');
    
    if( tabsContainers.length>0 && panelsContainers.length>0 && tabsContainers.length==panelsContainers.length ) {
      
      tabsContainers.each(function(tabsContainer, i_container) {
      var tabs = tabsContainer.getElements('li');
      if(tabs.length>0) { 
        
        // *Debug*
        // alert(tabs.length);
        tabs.each(function(tab, i_tab) {
          tab.addEvent('click', function(e) {
            tabs.each(function(reset_tab) {reset_tab.removeClass('current');});
            tab.addClass('current');
            var panels = $ES('div.panel', panelsContainers[i_container]);
            panels.each(function(reset_panel) {reset_panel.removeClass('current');});
            panels[i_tab].addClass('current');
            
            // *Debug*
            // alert('Kliknut je tab broj: '+ i_tab +', a container je: '+ i_container);
            e = new Event(e).stop();
          });
        });
      }
        
      });
    }
  },
  
  //***** Realtime clock
  liveClock: function(godina, mjesec, datum, sati, minute, sekunde){
    
    var objDatum, arrDani, arrMjeseci, ispis, delayInterval;
    var objClock = $('clock');
    
    // *Napomena*: 'Month' parametar ide [0-11] zato radim korekciju sa '-1'
    objDatum = new Date(godina, mjesec-1, datum, sati, minute, sekunde);
    arrDani = new Array('Nedjelja', 'Ponedjeljak', 'Utorak', 'Srijeda', 'Četvrtak', 'Petak', 'Subota');
    arrMjeseci = new Array('Siječanj', 'Veljača', 'Ožujak', 'Travanj', 'Svibanj', 'Lipanj', 'Srpanj', 'Kolovoz', 'Rujan', 'Listopad', 'Studeni', 'Prosinac');
    
    // Formatiranje ispisa
    ispis = arrDani[objDatum.getDay()] + ', ';
    ispis += datum + '. ' + arrMjeseci[mjesec-1] + ' ' + godina;
    ispis += ' ';
    ispis += ((sati<10)?'0':'') + sati + ':' + ((minute<10)?'0':'') + minute;
    
    // Ispis na stranici
    objClock.innerHTML = ispis;
  
    //*Napomena*: 'Month' parametar ide [0-11] zato radim korekciju sa '+1'
    objDatum.setMinutes(objDatum.getMinutes()+1);
    godina = objDatum.getFullYear();
    mjesec = objDatum.getMonth()+1;
    datum = objDatum.getDate();
    sati = objDatum.getHours();
    minute = objDatum.getMinutes();
    sekunde = objDatum.getSeconds();
  
    delayInterval = (60 - sekunde) * 1000;
      
    setTimeout("Site.liveClock("+ godina +", "+ mjesec +", "+ datum +", "+ sati +", "+ minute +", 0)", delayInterval);
  
  }

}

// Open picture in popup in picture size
function popup(path, width, height) {
	var win = window.open('', 'popup_win', 'width='+width+',height='+height+',top='+(screen.height/2-(height/2))+',left='+(screen.width/2-(width/2)));
	win.document.write('<html>\n<head>\n<title>Image viewer</title>\n</head>\n');
	win.document.write('<body marginwidth=0 marginheight=0 topmargin=0 leftmargin=0 onblur="window.close()">\n');

	if( path.substring( (path.length-3),path.length )=='swf' ) {
		win.document.write('<OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=5,0,0,0" WIDTH='+width+' HEIGHT='+height+'><PARAM NAME=movie VALUE="'+path+'"> <PARAM NAME=quality VALUE=high> <EMBED src="'+path+'" quality=high WIDTH='+width+' HEIGHT='+height+' TYPE="application/x-shockwave-flash" PLUGINSPAGE="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=Shockwav eFlash"></EMBED> </OBJECT>');
	} else {
		win.document.write('<a href="javascript:void(0);" onclick="window.close()" title="Click to close window"><img src="'+path+'" width="'+width+'" height="'+height+'" border="0" alt="" /></a>');
	}

	win.document.write('</body>\n');
	win.document.write('</html>\n');
	win.document.close();
}

function desc(num) {
	var route = $ES('.route-'+ num, 'main-content');
	if(route) {
		for(i=1; i<=6; i++) {
			if(i==num) {
				route.setStyle('display', 'block');
			} else {
				var hide_route = $ES('.route-'+ i, 'main-content');
				hide_route.setStyle('display', 'none');
			}
		}
		// alert('Ko je kliknuo na rutu broj: '+ num);
	}
}

window.addEvent('domready', Site.init);
