/* -----------------------------------------------------------------------
 
   Centralni javascript
   Projekt: www.filmjukebox.cz
   Autor: Martin Michalek, Studio Shortcat, michalek@shortcat.cz

----------------------------------------------------------------------- */


// ===== jQuery: Po nacteni dokumentu pridame cekani na akce uzivatele =====
$(document).ready(function() { 
   
  handle_poll_table();
  handle_content_layers();
  handle_message_fadeout();

	if ($('#cinemas li').length) {
		$('#cinemas li').each(function() { handle_clickable($(this)); });
	}
	
	// Superfish drop-down menu jako navigace mezi kiny v jejich detailu
	if ($('#cinema_navigation').length) {
		jQuery('#cinema_navigation ul.sf-menu').superfish({
			firstOnClick: true
		});
	}
	
});


// ===== Helpery =====
function getVar(name) {
// Vraci promennou z URL
  get_string = document.location.search;         
  return_value = '';  
  do { //This loop is made to catch all instances of any get variable.
    name_index = get_string.indexOf(name + '=');    
    if (name_index != -1) {
      get_string = get_string.substr(name_index + name.length + 1, get_string.length - name_index);      
      end_of_value = get_string.indexOf('&');
      if (end_of_value != -1)                
        value = get_string.substr(0, end_of_value);                
      else                
        value = get_string;                
        
      if (return_value == '' || value == '')
         return_value += value;
      else
         return_value += ', ' + value;
      }
    } while (name_index != -1)    
  //Restores all the blank spaces.
  space = return_value.indexOf('+');
  while (space != -1) { 
      return_value = return_value.substr(0, space) + ' ' + 
      return_value.substr(space + 1, return_value.length);  		 
      space = return_value.indexOf('+');
  }  
  return (return_value);        
}


// ===== Handlery pro osetreni chovani jednotlivych prvku =====

function handle_poll_table() {
// === Osetruje mouseovery u tabulky s hlasovanim ===
  
  // Mouseover nad sloupcovym grafem
  $('.poll table .bar a').hover(function () {
    var original_content = $(this).text();
    $(this).text($(this).attr('title'));
    $(this).attr('title',original_content);
  },function () {
    var original_content = $(this).attr('title');
    $(this).attr('title',$(this).text());
    $(this).text(original_content);
  });

  // Mouseover na radek taulky
  $('.poll table tr').hover(function () {
     $(this).addClass('hover'); 
  },function () {
     $(this).removeClass('hover'); 
  }); 
}


function handle_layer_close() {
// === Ceka na zavreni vrstvy  === 

  // Zavirame na ESC
  $(document).keyup(function(event){
    if (event.keyCode == 27) {
        $('.layer').css('visibility', 'hidden');
        $('#navigation a').removeClass('opened');          
    }
  });
  // Zavirame na kliknuti kamkoliv mimo vrstvu
  $('.layer .close').click(function(){
        $('.layer').css('visibility', 'hidden'); 
        $('#navigation a').removeClass('opened');
  });    
}


function handle_content_layers() {
// === Otevira a zavira obsahove vrstvy - diskuze, o projektu, prihlaseni filmu === 
  
  function handle_navigation_click() {
  // Ovladac kliknuti na polozku v navigaci
     $('#navigation a').click(function() { 
    	 if ($(this).hasClass('opened')) {
    	   $(this).removeClass('opened');
    	   $('.layer').css('visibility', 'visible');
    	   return false;
    	 } else {
    	   var related_layer = $(this).attr('href');
    	   $('.layer').css('visibility', 'hidden');
    	   $('#navigation a').removeClass('opened');
    	   $(this).addClass('opened');
    	   $(related_layer).css('visibility', 'visible');
    	   handle_layer_close();
    	   return false;		 
    	 }
     });   
  }  

  function handle_nomination_input_click() {  
  // Po kliknuti na textove pole pro nominaci filmu prestane zobrazovat napovedu
    $('#nomination #text_input').focus(function() {
  	  this.value='';
    });   
  }

  function handle_newsletter_input_click() {
  // Po kliknuti na textove pole pro newsletter prestane zobrazovat napovedu
    $('#newsletter #text_input').focus(function() {
  	  this.value='';
    });
  }
  
  handle_navigation_click();
  handle_nomination_input_click();
  handle_newsletter_input_click();
  handle_layer_close();
}

function handle_message_fadeout() {
// === Fadeout chybove nebo informativni hlasky ===
  
  function blind_up(element) {
    $(element).animate({ 
      top: '-50px'
    }, 200 );
  }
  
  if ($('.message')) {
    setTimeout(blind_up, 3000,'.message');
  }
}

// Po najeti mysi na element prida tridu .hover
function handle_hover(obj) {
	obj.hover(function () {
     $(this).addClass('hover'); 
  },function () {
     $(this).removeClass('hover'); 
  }); 
}

// Po kliknuti na element nacte URL definovanou v prvnim odkazu
function handle_clickable(obj) {
	obj.click(function () {
		location.href = obj.children("a:first").attr("href");
		// alert(obj.children("a:first").attr("href"));
	});
}
