//sliding fotos
var fslider = {
  fwidth: 190, //image width 
  fheight: 143, //image height
  fmarginleft: 13, //image margin left
  inrow: 3, //images in row
  fadeLength: 300, //length of a effect
  wrapper: '#projectfotos', //wrapper
  ctrlStopClass: 'deny', //stop class of a control link
  prevLinkId: '#slide_prev', //prev control link
  nextLinkId: '#slide_next', //next control link
  
  first: 0, //current first image
  init: function()
    {
      //stop if wrapper isnt found
      if(!$(this.wrapper).length) return false;
      
      $(this.wrapper+' a:gt(2)').hide();
			$(this.wrapper+' a:first').addClass('first');
      
      //set events to control links
      $(this.prevLinkId).click(function(){ fslider.prev(); return false; });
      $(this.nextLinkId).click(function(){ fslider.next(); return false; });
      
      //first is 0
      if( this.first == 0 )
        $(this.prevLinkId).addClass(this.ctrlStopClass);
      //not enough images  
      if( this.inrow >= $(this.wrapper+' a').length )
        $(this.nextLinkId).addClass(this.ctrlStopClass);

    },
  prev: function()
    {
       // one step before stop left cond 
       if( this.first == 1 )
       {
          $(this.prevLinkId).addClass(this.ctrlStopClass);
       }
       //stop left cond
       if( this.first > 0 )
       {
         if( this.first < $(this.wrapper+' a').length )
         {
           $(this.wrapper+' a:eq('+(this.first+this.inrow-1)+')').hide(this.fadeLength);
           $(this.wrapper+' a:eq('+(this.first-1)+')').css('margin-left',0);
           $(this.wrapper+' a:eq('+(this.first)+')').css('margin-left',this.fmarginleft+'px');
           $(this.wrapper+' a:eq('+(this.first-1)+')').show(this.fadeLength);
           $(this.nextLinkId).removeClass(this.ctrlStopClass);
           this.first--;
         }
       }  
   
    },
  next: function(ctrl)
    {
         // one step before stop right cond 
         if( this.first >= ( $(this.wrapper+' a').length - this.inrow - 1 ) )
         {
            $(this.nextLinkId).addClass(this.ctrlStopClass);
         }
         //stop right cond
         if( this.first < ( $(this.wrapper+' a').length - this.inrow ) )
         {
           if( this.first < $(this.wrapper+' a').length )
           {
             $(this.wrapper+' a:eq('+this.first+')').hide(this.fadeLength);
             $(this.wrapper+' a:eq('+(this.first+1)+')').css('margin-left',0);
             $(this.wrapper+' a:eq('+(this.first+this.inrow)+')').css('margin-left',this.fmarginleft+'px');
             $(this.wrapper+' a:eq('+(this.first+this.inrow)+')').show(this.fadeLength);
             $(this.prevLinkId).removeClass(this.ctrlStopClass);
             this.first++;
           }
         }
    }
}

//sliding fotos
var photoGallery = {
  config: 
  {
    imageWidth: 190, //image width 
    imageHeight: 143, //image height
    marginLeft: '9px', //image margin left
    imagesInRow: 3, //images in row
    fadeLength: 300, //length of a effect
    wrapper: '.projectfotos', //wrapper
    ctrlStopClass: 'deny', //stop class of a control link
    prevLinkClass: '.slide_prev', //prev control link
    nextLinkClass: '.slide_next', //next control link
    ctrlWrapperClass: '.projectfotos_ctrl'
  },
  galleries: new Array(),
  first: 0, //current first image
  
  init: function()
    {
      //stop if wrapper isnt found
      if(!$(this.config.wrapper).length) return false;
      
      //load all photogalleries to array of objects
      $(this.config.wrapper).each(function(key){
        photoGallery.galleries[key] = {
          index: key,
          wrapperEl: $(this).get(0),
          ctrlWrapper: $(this).next(photoGallery.config.ctrlWrapperClass).get(0),
          prevLink: $(this).next(photoGallery.config.ctrlWrapperClass).find('a'+photoGallery.config.prevLinkClass).get(0),
          nextLink: $(this).next(photoGallery.config.ctrlWrapperClass).find('a'+photoGallery.config.nextLinkClass).get(0),
          firstPlace: 0,
          imagesCount: $(this).children().length
        };
        //set css
        $(photoGallery.galleries[key].wrapperEl).css({ height: $(photoGallery.galleries[key].wrapperEl).find('img').height()+4, overflow: 'hidden' })
          .children('a').css({ marginLeft: photoGallery.config.marginLeft, display: 'block', float: 'left' })
          ;//.children('img').css({ width: photoGallery.config.imageWidth, height: photoGallery.config.imageHeight });
        //hide unvisible images
        $(photoGallery.galleries[key].wrapperEl).children('a:gt('+(photoGallery.config.imagesInRow-1)+')').hide();
        //set class first (IE6)
			  $(photoGallery.galleries[key].wrapperEl).children('a:first').addClass('first').css('margin-left', 0);
        //bind click events to conrol links
        $(photoGallery.galleries[key].prevLink).click(function(){ photoGallery.prev(key); return false; });
        $(photoGallery.galleries[key].nextLink).click(function(){ photoGallery.next(key); return false; });
        //check count of images, set classes to control links
        if( photoGallery.galleries[key].firstPlace == 0 )
          $(photoGallery.galleries[key].prevLink).addClass(photoGallery.config.ctrlStopClass);
        if( photoGallery.config.imagesInRow >= photoGallery.galleries[key].imagesCount )
          $(photoGallery.galleries[key].nextLink).addClass(photoGallery.config.ctrlStopClass);
      });
 
    },
  prev: function(key)
    {
       // one step before stop left cond 
       if( photoGallery.galleries[key].firstPlace == 1 )
       {
          $(photoGallery.galleries[key].prevLink).addClass(photoGallery.config.ctrlStopClass);
       }
       //stop left cond
       if( photoGallery.galleries[key].firstPlace > 0 && photoGallery.galleries[key].firstPlace < photoGallery.galleries[key].imagesCount )
       {
         $(photoGallery.galleries[key].wrapperEl).children('a:eq('+(photoGallery.galleries[key].firstPlace+photoGallery.config.imagesInRow-1)+')').hide(photoGallery.config.fadeLength);
         if(photoGallery.config.marginLeft)
         {         
           $(photoGallery.galleries[key].wrapperEl).children('a:eq('+(photoGallery.galleries[key].firstPlace-1)+')').css('margin-left',0);
           $(photoGallery.galleries[key].wrapperEl).children('a:eq('+(photoGallery.galleries[key].firstPlace)+')').css('margin-left',photoGallery.config.marginLeft);
         }
         $(photoGallery.galleries[key].wrapperEl).children('a:eq('+(photoGallery.galleries[key].firstPlace-1)+')').show(photoGallery.config.fadeLength);
         $(photoGallery.galleries[key].nextLink).removeClass(photoGallery.config.ctrlStopClass);
         photoGallery.galleries[key].firstPlace--;
       }  
   
    },
  next: function(key)
    {
         // one step before stop right cond 
         if( photoGallery.galleries[key].firstPlace >= ( photoGallery.galleries[key].imagesCount - photoGallery.config.imagesInRow - 1 ) )
         {
            $(photoGallery.galleries[key].nextLink).addClass(photoGallery.config.ctrlStopClass);
         }
         //stop right cond
         if( photoGallery.galleries[key].firstPlace < ( photoGallery.galleries[key].imagesCount - photoGallery.config.imagesInRow ) )
         {
           $(photoGallery.galleries[key].wrapperEl).children('a:eq('+(photoGallery.galleries[key].firstPlace+photoGallery.config.imagesInRow)+')').show(photoGallery.config.fadeLength);
           $(photoGallery.galleries[key].wrapperEl).children('a:eq('+photoGallery.galleries[key].firstPlace+')').hide(photoGallery.config.fadeLength).queue(function(){
             if(photoGallery.config.marginLeft)
             {
              $(photoGallery.galleries[key].wrapperEl).children('a:eq('+(photoGallery.galleries[key].firstPlace)+')').css('margin-left',0);
              $(photoGallery.galleries[key].wrapperEl).children('a:eq('+(photoGallery.galleries[key].firstPlace+photoGallery.config.imagesInRow)+')').css('margin-left',photoGallery.config.marginLeft);
             }
             $(this).dequeue()
           });
           /*if(photoGallery.config.marginLeft)
           {
             $(photoGallery.galleries[key].wrapperEl).children('a:eq('+(photoGallery.galleries[key].firstPlace+1)+')').css('margin-left',0);
             $(photoGallery.galleries[key].wrapperEl).children('a:eq('+(photoGallery.galleries[key].firstPlace+photoGallery.config.imagesInRow)+')').css('margin-left',photoGallery.config.marginLeft);
           }*/
           $(photoGallery.galleries[key].prevLink).removeClass(photoGallery.config.ctrlStopClass);
           photoGallery.galleries[key].firstPlace++;
         }
    }
}

// pridat stranku do zaloziek
function AddFavorite(linkObj,addUrl3,addTitle3)
{
     if (document.all && !window.opera){
         window.external.AddFavorite(addUrl3,addTitle3);
         return false;
     }
     else if (window.opera && window.print)     {
         linkObj.title = addTitle3;
         return true;
     }
     else if ((typeof window.sidebar == 'object') && (typeof window.sidebar.addPanel == 'function')) {
         window.sidebar.addPanel(addTitle3,addUrl3,'');
         return false;
     }
     window.alert('Po potvrzení stiskněte CTRL-D,\nstránka bude přidána k Vašim oblíbeným odkazům.');
     return false;
}


var dcalendar = {
  wrapper: 'div#calendar', //wrapper,
  eventHeaderElement: 'h3.leve',
  eventDateElement: 'p.leve', 
  highlightClass: 'highlight',
  init: function()
  {
      //stop if wrapper isnt found
      if(!$(this.wrapper).length) return false; 
      
      //$('p.leveAnnot').hide();
      
      $(this.wrapper+' td.event').css('cursor','crosshair').mouseover(function(){ dcalendar.markDay(this) });
      $(this.wrapper+' td.event').mouseout(function(){ dcalendar.unmarkAll() });
      
      $(this.eventDateElement).mouseover(function(){ dcalendar.markDay(this) });  
      $(this.eventDateElement).prev().mouseover(function(){ dcalendar.markDay(this) });//.click(function(){ dcalendar.showText(this); return false; }); 
      $(this.eventDateElement).next('p').mouseover(function(){ dcalendar.markDay($(this).prev()) }); 
      
      $(this.eventDateElement).mouseout(function(){ dcalendar.unmarkAll() });  
      $(this.eventDateElement).prev().mouseout(function(){ dcalendar.unmarkAll() });
      $(this.eventDateElement).next('p').mouseout(function(){ dcalendar.unmarkAll() });
  },
  unmarkAll: function()
  {
    $(this.eventHeaderElement+','+this.eventDateElement).removeClass(this.highlightClass);
    $(this.wrapper+' td.event').removeClass(this.highlightClass);
  },
  markDay: function(e)
  {
    var direction;
    //get date depending by element name, get direction of highliting
    if($(e).get(0).tagName=='P')
    {
      this.date = $(e).html();
      direction = 'toCalendar';
      $(e).prev(this.eventHeaderElement).addClass(this.highlightClass);
    }
    if($(e).get(0).tagName=='H3')
    {
      this.date = $(e).next('p').html();
      direction = 'toCalendar';
      $(e).addClass(this.highlightClass);
    }
    if($(e).get(0).tagName=='TD')
    {
      this.date = $(e).attr('title');
      direction = 'toList';
    }
    
    //mark day in list  
    if(direction=='toList')
    {
      $(this.eventDateElement).filter(function(i){ return $(this).text() == dcalendar.date; }).addClass(dcalendar.highlightClass).prev(this.eventHeaderElement).addClass(dcalendar.highlightClass);
    }
    //mark day in calendar
    else
    {  
      $(this.wrapper+' td.event').filter(function(){ return $(this).attr('title')==dcalendar.date; }).addClass(dcalendar.highlightClass);
    }
  },
  hideAllText: function()
  {
    $('p.leveAnnot:visible').slideUp(200);
  },
  showText: function(el)
  {
    dcalendar.hideAllText();
    $(el).next().next('p.leveAnnot:hidden').slideDown(200);
  }
}


$(function() {
	$('a[@rel*=lightbox]').lightBox(); // Select all links that contains lightbox in the attribute rel
});

//current power graph
function showGraph(url,el)
{
	$.get(url,function(data){
		$('div#currentPowerGraph').html(data);
		$('p#currentPowerTabs a').removeClass('act');
		$(el).addClass('act');
	})
	return false;
}

var milestones = {
  effectDuration: 200,
  ulId: 'ul#milestonesLast',
  init: function()
  {
    if(!$(this.ulId).length) return false;
    //hide details
    $('div.milestoneDetail').hide();
    //for each strong - title of milestone
    $(this.ulId+' li strong').each(function(){
      //if has detail
      if($(this).parent().next('div').length)
      {
        //add class to title
        $(this).addClass('likeA');
        //set hover classes
        $(this).mouseover(function(){ $(this).addClass('hover') });
        $(this).mouseout(function(){ $(this).removeClass('hover') });
        //ad event onclick
        $(this).click( function(){ 
          //hide all details
          $(milestones.ulId+' div.milestoneDetail').slideUp(milestones.effectDuration).parent().removeClass('opened');
          if(!$(this).parent().next('div:visible').length)
          {
            $(this).parent().parent().toggleClass('opened');
            $(this).parent().next('div').slideToggle(milestones.effectDuration);
          } 
          return false; 
        });
      }
    });
     //first and last li
    $(this.ulId+' li:first').addClass('first-child');
    $(this.ulId+' li:last').addClass('last-child');
    //hover li, except first and last li 
    $(this.ulId+' li:not(:first):not(:last)').mouseover(function(){$(this).addClass('hover')});
    $(this.ulId+' li:not(:first):not(:last)').mouseout(function(){$(this).removeClass('hover')});
    //hover first li
    $(this.ulId+' li:first').mouseover(function(){$(this).addClass('first-child_hover')});
    $(this.ulId+' li:first').mouseout(function(){$(this).removeClass('first-child_hover')});
    //hover last li
    $(this.ulId+' li:last').mouseover(function(){$(this).addClass('last-child_hover')});
    $(this.ulId+' li:last').mouseout(function(){$(this).removeClass('last-child_hover')});
  }
}
//same as milestones, but for second ul
var milestones2 = {
  ulId: 'ul#milestonesFuture',
  init: function()
  {
    if(!$(this.ulId).length) return false;
    //hide details
    $('div.milestoneDetail').hide();
    //for each strong - title of milestone
    $(this.ulId+' li strong').each(function(){
      //if has detail
      if($(this).parent().next('div').length)
      {
        //add class to title
        $(this).addClass('likeA');
        //set hover classes
        $(this).mouseover(function(){ $(this).addClass('hover') });
        $(this).mouseout(function(){ $(this).removeClass('hover') });
        //ad event onclick
        $(this).click( function(){ 
          //hide all details
          $(milestones2.ulId+' div.milestoneDetail').slideUp(milestones.effectDuration).parent().removeClass('opened');
          //if hided detail is current opened
          if(!$(this).parent().next('div:visible').length)
          {
            $(this).parent().parent().toggleClass('opened');
            $(this).parent().next('div').slideToggle(milestones.effectDuration);
          } 
          return false; 
        });
      }
    });
     //first and last li
    $(this.ulId+' li:first').addClass('first-child');
    $(this.ulId+' li:last').addClass('last-child');
    //hover li, except first and last li 
    $(this.ulId+' li:not(:first):not(:last)').mouseover(function(){$(this).addClass('hover')});
    $(this.ulId+' li:not(:first):not(:last)').mouseout(function(){$(this).removeClass('hover')});
    //hover first li
    $(this.ulId+' li:first').mouseover(function(){$(this).addClass('first-child_hover')});
    $(this.ulId+' li:first').mouseout(function(){$(this).removeClass('first-child_hover')});
    //hover last li
    $(this.ulId+' li:last').mouseover(function(){$(this).addClass('last-child_hover')});
    $(this.ulId+' li:last').mouseout(function(){$(this).removeClass('last-child_hover')});
  }
}


//company structure
var cstructure = {
  init: function()
  {
    if(!$('ul#companyStructure').length) return false;
    $('div.companyInfo:not(#default):not(.visible)').hide();
    $('ul#companyStructure a').each(function(){
      $(this).click(function(){ cstructure.show($(this),$(this).attr('href')); return false; })
    });
  },
  show: function(el,url)
  {
    $('div.companyInfo').hide();
    $('div.companyInfo'+url).show();
    $('ul#companyStructure a').removeClass('act');
    $(el).addClass('act');
  }
} 

//sliding projects in sidebar
var projectSlider = {
  effLength: 250,
  init: function()
  {
    //if place to insert conroll links wasnt found, end function
    if(!$('p.lbox_links.projects').length) return false;
    //insert controll links
    //$('p.lbox_links.projects').prepend('<a id="lblPrev" href="#" title="Zobrazit následující projekt"><span>Předchozí</span></a><a id="lblNext" href="#" title="Zobrazit předchozí projekt"><span>Následující</span></a><span id="lblSep"></span>')
    //set events to controll links
    $('a#lblPrev').click(function(){ projectSlider.prev(); return false; })
    $('a#lblNext').click(function(){ projectSlider.next(); return false; })
    //remove class hidden from paragraphs, hide them, hide also images in paragraphs 
    $('p.lproject:not(:first)').removeClass('hidden').hide().addClass('white').find('img').hide();
  },
  prev: function()
  {
    //if paragraph is not first
    if( $('p.lproject:visible').prev('p').length )
    {
      //hide previous image
      $('p.lproject:visible').prev('p').find('img').hide();
      //fade out this image, wait for finish effect
      $('p.lproject:visible').addClass('white').find('img').fadeOut(projectSlider.effLength).queue(function(){
        //hide this par. 
        $(this).parent().parent('p').hide().addClass('white');
        //show previous par.
        $(this).parent().parent('p').prev('p').show();
        //fade in image in previous par. 
        $(this).parent().parent('p').prev('p').find('img').fadeIn(projectSlider.effLength).queue(function(){
          $(this).parent().parent().removeClass('white');
          $(this).dequeue();
        });
        //remove queue from element
        $(this).dequeue();
      });
    }
    //paragraph is first
    else
    {
      $('p.lproject:last').find('img').hide()
      $('p.lproject:first').addClass('white').find('img').fadeOut(projectSlider.effLength).queue(function(){ 
        $(this).parent().parent('p').hide().addClass('white');
        $('p.lproject:last').show().removeClass('white'); 
        $('p.lproject:last').show().find('img').fadeIn(projectSlider.effLength).queue(function(){
          $(this).parent().parent().removeClass('white');
          $(this).dequeue();
        });
        $(this).dequeue();
      });

    }
  },
  next: function()
  {
    if( $('p.lproject:visible').next('p').length )
    {
      $('p.lproject:visible').next('p').find('img').hide();
      $('p.lproject:visible').addClass('white').find('img').fadeOut(projectSlider.effLength).queue(function(){ 
        $(this).parent().parent('p').hide().addClass('white');
        $(this).parent().parent('p').next('p').show().removeClass('white'); 
        $(this).parent().parent('p').next('p').find('img').fadeIn(projectSlider.effLength).queue(function(){
          $(this).parent().parent().removeClass('white');
          $(this).dequeue();
        });
        $(this).dequeue();
      });
    }
    else
    {
      $('p.lproject:first').find('img').hide()
      $('p.lproject:last').addClass('white').find('img').fadeOut(projectSlider.effLength).queue(function(){ 
        $(this).parent().parent('p').hide().addClass('white');
        $('p.lproject:first').show().removeClass('white'); 
        $('p.lproject:first').show().find('img').fadeIn(projectSlider.effLength).queue(function(){
          $(this).parent().parent().removeClass('white');
          $(this).dequeue();
        });
        $(this).dequeue();
      });

    }
  }
} 

var powerSlider = {
  
  init: function()
  {
  
    $('#currentPowerOuter').after('<a href="#" id="powerSliderPrev"><span class="forBlind">&lt;</span></a><a href="#" id="powerSliderNext"><span class="forBlind">&gt;</span></a>');
    $('#powerSliderPrev').click(function(){ powerSlider.prev($(this)); return false; });
    $('#powerSliderNext').click(function(){ powerSlider.next($(this)); return false; });
    

    
  },
  
  displayed: 0,
  
  prev: function(e)
  {
      if(typeof(p[powerSlider.displayed+1])=='object')
      {
        powerSlider.displayed++;
        graphDisplay(powerSlider.displayed);
        powerSlider.refreshTitle(powerSlider.displayed);
      }
      else
      {
        powerSlider.displayed = 0;
        graphDisplay(powerSlider.displayed);
        powerSlider.refreshTitle(powerSlider.displayed);
      }
  },
  
  next: function()
  {
      if(typeof(p[powerSlider.displayed-1])=='object')
      {
        powerSlider.displayed--;
        graphDisplay(powerSlider.displayed);
        powerSlider.refreshTitle(powerSlider.displayed);
      }
      else
      {
        powerSlider.displayed = p.length - 1;
        graphDisplay(powerSlider.displayed);
        powerSlider.refreshTitle(powerSlider.displayed);
      }
  },
  
  refreshTitle: function(id)
  {
    var title = $('#currentPowerOuter h5 .title');
    var height = $('#currentPowerOuter h5 .title').height();
    if(typeof(p[id].name)=='string')
    {
      var text = powerTitle + ' <b>' + p[id].name + '</b>';
    }
    else
    {
      var text = powerTitleTotal;
    }
    
    title.height(height+'px');
    title.html(text).removeClass('sIFR-replaced');
    
    // gray normal
    color = '#a8b1b7';
    colorh = color;

    // gray uppercase
    sIFR.replace(ffont, {
    	selector: '#currentPower h5 span'
    	,css:
    		[
    		'.sIFR-root { font-weight: bold; letter-spacing: -1; color: '+color+'; text-transform: uppercase; } b { color: #92ca43; }',
    		,'a { color: '+colorh+'; text-decoration: none; cursor: pointer; }'
    		,'a:link { color: '+color+'; }'
    		,'a:hover { color: '+colorh+'; }'
    		]
    	,wmode: 'transparent'
    	,fitExactly: true
    });
    
  },
  
  slide:
  {  
    left: function()
    {
      $('#currentPowerInner').animate({ marginLeft: '-569px' });
    },  
    right: function()
    {
      $('#currentPowerInner').animate({ marginLeft: '569px' });
    },
    reset: function()
    {
      $('#currentPowerInner').css({ marginLeft: 0 });
    }
  },
  
  title:
  {
    hide: function()
    {
      $('#currentPowerOuter .title').hide();
    },
    show: function()
    {
      $('#currentPowerOuter .title').show();
    }
  
  }

}

    

$(document).ready(function() {     

  powerSlider.init();

  fslider.init();
  
  photoGallery.init();
  
  dcalendar.init();
  milestones.init();
  milestones2.init();
  cstructure.init();
  projectSlider.init();
  
});

