var Site = {
  rootPath: '/',

  init: function() {
    Site.handleNewWinOpen();
    Site.handleFooter();
    Site.handleLightbox();
    var spinner = new Image();
    spinner.src = Site.rootPath +'client/css/images/spinner.gif';
  },
  
  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();})});
  },
  
  handleFooter: function() {
    var totalContentHeight = $('header').offsetHeight + $('content').offsetHeight + $('footer').offsetHeight;
    var visibleHeight = window.getHeight();
    if(totalContentHeight<visibleHeight) $('content').setStyle('height', (visibleHeight-$('header').offsetHeight-$('footer').offsetHeight-50) +'px');
    
    // *Debug*
    // alert('Visina browsera: '+ visibleHeight);
    // alert('Visina sadrzaja: '+ totalContentHeight);
  }, 
  
  handleLightbox: function() {
    var imgs = $ES('a img', 'main-content');
    if(imgs.length>0) imgs.each(function(img) {img.getParent().setProperty('rel', 'lightbox');});
    
    var tables = $ES('table', 'main-content');
    if(tables.length>0) tables.each(function(table, i) {
      var tablesImages = $ES('a img', $(table));
      if(tablesImages.length>0) tablesImages.each(function(img) {img.getParent().setProperty('rel', 'lightbox[project'+ i +']');});
    });
    // alert($('content').innerHTML);
  }, 
  
  handlePoll: function() {
    if($('frmPoll')) {
      $('frmPoll').addEvent('submit', function(e) {
        e = new Event(e).stop();
        var spinner = new Element('img').setProperties({id:'spinner', src:Site.rootPath +'client/css/images/spinner.gif', width:'16', height:'16', alt:'Učitavam...'});
        spinner.inject($('poll'));
        var ajaxOptions = {postBody:$('frmPoll'), update:'poll'};
        new Ajax(Site.rootPath +'homepage/poll/', ajaxOptions).request();
      });
    }
  }

}

// Otvara sliku u popup-u velicine slike
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>Preglednik slika</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="Klik za zatvaranje prozora"><img src="'+path+'" width="'+width+'" height="'+height+'" border="0" /></a>');
	}

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

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