var blank = {
  config:
  {
    wrapperId: '#wrapper'
  },
  init: function()
  {
    if(!$(this.config.wrapperId).length) return false;
  }
};


function log(e)
{
  //return false;
  if(typeof(console)=='object') console.log(e);
  //else alert(e);
};

/*
//find max value in array
var max = function(array)
{
  var mxm = array[0];
  for (i = 0; i < array.length; i++)
  {
    if (array[i] > mxm)
    {
      mxm = array[i];
    }
  }
  return mxm;
 };

//preloader of images
var preloader = {
  load: function(source)
  {
    var key = this.images.length;
    this.images[key] = new Image();
    this.images[key].src = source;
  },
  images: new Array()
};

//auto rollovers
var imageHover = {
  elementsFilter: '*[src*=but_]',
  replaceSrcNormal: '.png',
  replaceSrcHover:  '_hover.png',
  init: function()
  {
    if(!$(this.elementsFilter).length) return false;
    $(this.elementsFilter).each(function(key){
      imageHover.images[key] = new Image();
      imageHover.images[key].src = $(this).attr('src').replace(imageHover.replaceSrcNormal,imageHover.replaceSrcHover);
      if(imageHover.isLoaded(imageHover.images[key]))
      {
        $(this).hover(
          function(){ $(this).attr('src',$(this).attr('src').replace(imageHover.replaceSrcNormal,imageHover.replaceSrcHover)); },
          function(){ $(this).attr('src',$(this).attr('src').replace(imageHover.replaceSrcHover,imageHover.replaceSrcNormal)); }
        );
      }
    });
  },
  isLoaded: function(el)
  {
     if (!el.complete) {
        return false;
     }
     if (typeof el.naturalWidth != "undefined" && el.naturalWidth == 0) {
        return false;
     }
     return true;
  },
  images: new Array()
};

var autoTableHover = {
  config:
  {
    table: '.autoHover',
		hoverClass: 'hover'
  },
	settings:
	{
		rowClick: false
	},
  init: function(settings)
  {
    if(!$(this.config.table).length) return false;
		this.settings = settings;
		$(this.config.table+' tr').hover(function(){ $(this).addClass(autoTableHover.config.hoverClass); },function(){ $(this).removeClass(autoTableHover.config.hoverClass); })
		if(this.settings.rowClick)
			$(this.config.table+' tr').each(function(){ $(this).children().css('cursor','pointer').click(function(){ window.location = $(this).parent().find('a').get(0).href }) })
  }
};
*/
// 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;
}
/*
<a href="#" onclick="return AddFavorite(this,'http://<?=$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].($_GET['lang']?'?lang='.$_GET['lang']:'')?>','Energy 21 - <?=$title?>');">Přidat do oblíbených</a>
*/

var customLightbox = {
  config:
  {
    overlayBgColor:'#000',
    overlayOpacity:0.8,
    fixedNavigation:false,
    imageLoading:'/images/lightbox-ico-loading.gif',
    imageBtnPrev:'/images/lightbox-btn-prev.png',
    imageBtnNext:'/images/lightbox-btn-next.png',
    imageBtnClose:'/images/lightbox-btn-close.png',
    imageBlank:'/images/lightbox-blank.gif',
    containerResizeSpeed:150,
    txtImage:'Obrázek:<br />',
    txtOf:'z',
    keyToClose:'c',
    keyToPrev:'p',
    keyToNext:'n',
    imageArray:[],
    activeImage:0
  },
  init: function()
  {
    $('a[rel=lightbox]').lightBox(customLightbox.config); //only rel="lightbox"
    var lbRels = new Array();  var thisRel = ''; var trIsIn = false;
    $('a[rel*="lightbox["]').each(function(){ //each rel="lightbox[..."
        thisRel = $(this).attr('rel').replace('lightbox[','').replace(']',''); //curren rel
        for(i=0; i<lbRels.length; i++) { //find if is not yet in array of rels
          if(thisRel == lbRels[i]) trIsIn = true;
          else trIsIn = false;
        }
        if(!trIsIn) lbRels[lbRels.length] = $(this).attr('rel').replace('lightbox[','').replace(']',''); //not i array of rels => store rel
    });
    for(i=0; i<lbRels.length; i++) { $('a[rel="lightbox['+lbRels[i]+']"]').lightBox(customLightbox.config); } //each unique rel => apply lightbox
  }
};

partnersSlide = {
  config:
  {
    wrapperId: '#partnersSlide',
	  innerId: '#partnersSlideInner',
	  windowWidth: 250,
	  motionTimeNormal: 300,
	  motionTimeSkip: 200,
	  timer: 3000,
	  waitAfterClick: 6000
  },
  init: function()
  {
     if(!$(this.config.wrapperId).length) return false;

     this.partnersCount = $(this.config.innerId).children().length;

     this.active = 1;

     if( this.partnersCount )
      $(partnersSlide.config.wrapperId).addClass('active');

  	 $(this.config.innerId).after('<a href="#prev" id="partnerPrev"><!--  --></a><a href="#next" id="partnerNext"><!--  --></a>');
  	 $('#partnerPrev').hover(function(){partnersSlide.markPrev();},function(){partnersSlide.unmark();}).click(function(){partnersSlide.prev(true);return false;});
  	 $('#partnerNext').hover(function(){partnersSlide.markNext();},function(){partnersSlide.unmark();}).click(function(){partnersSlide.next(true);return false;});

     partnersSlide.interval = setInterval('partnersSlide.prev()', partnersSlide.config.timer);
  },
  markPrev: function()
  {
		  $(partnersSlide.config.wrapperId).addClass('prevActive');
  },
  markNext: function()
  {
		  $(partnersSlide.config.wrapperId).addClass('nextActive');
  },
  unmark: function()
  {
		  $(partnersSlide.config.wrapperId).removeClass('prevActive').removeClass('nextActive');
  },
  setHref: function()
  {
    var thref = $(partnersSlide.config.innerId).children(':eq('+(partnersSlide.active-1)+')').attr('href');
    if( typeof(thref)=='undefined' || !thref)
      thref = '#';
    $(partnersSlide.config.wrapperId).children('.overlay').attr('href', thref );
  },
  prev: function(delay)
  {
      if(delay)
      {
        clearTimeout(partnersSlide.interval);
        partnersSlide.interval = setInterval('partnersSlide.prev()', partnersSlide.config.waitAfterClick);
      }
      if( partnersSlide.active < partnersSlide.partnersCount )
      {
		    partnersSlide.active += 1;
        $(partnersSlide.config.innerId).animate({marginLeft:'-='+partnersSlide.config.windowWidth+'px'}, partnersSlide.config.motionTimeNormal);
      }
      else
      {
		    partnersSlide.active = 1;
        $(partnersSlide.config.innerId).animate({marginLeft: '0px'}, partnersSlide.config.motionTimeSkip);
      }
      partnersSlide.setHref();
  },
  next: function(delay)
  {
      if(delay)
      {
        clearTimeout(partnersSlide.interval);
        partnersSlide.interval = setInterval('partnersSlide.prev()', partnersSlide.config.waitAfterClick);
      }
      if( partnersSlide.active > 1 )
      {
		    partnersSlide.active -= 1;
		    $(partnersSlide.config.innerId).animate({marginLeft:'+='+partnersSlide.config.windowWidth+'px'}, partnersSlide.config.motionTimeNormal);
      }
      else
      {
		    partnersSlide.active = partnersSlide.partnersCount;
        $(partnersSlide.config.innerId).animate({marginLeft: '-'+(partnersSlide.config.windowWidth*(partnersSlide.partnersCount-1))+'px'}, partnersSlide.config.motionTimeSkip);
      }
      partnersSlide.setHref();
  }
};

















mainPartnersSlide = {
  config:
  {
    wrapperId: '#partners #mainPartners',
	  innerId: '#partners #mainPartners .slideIn',
	  windowHeight: 110,
	  motionTimeNormal: 300,
	  motionTimeSkip: 200,
	  timer: 5000,
	  waitAfterClick: 6000
  },
  init: function()
  {
     if(!$(this.config.wrapperId).length) return false;

     this.partnersCount = $(this.config.innerId).children().length;
     this.active = 1;

     if( this.partnersCount )
      $(this.config.wrapperId).addClass('active');

     this.interval = setInterval('mainPartnersSlide.next()', this.config.timer);
  },
  setHref: function()
  {
    var thref = $(this.config.innerId).children(':eq('+(this.active-1)+')').attr('href');
    if( typeof(thref)=='undefined' || !thref)
      thref = '#';
    $(this.config.wrapperId).children('.overlay').attr('href', thref );
  },
  next: function()
  {

    $(this.config.innerId).children(':last').after($(this.config.innerId).children(':first') );

//     $(mainPartnersSlide.config.innerId).children(':first').queue(function ()
//     {
//       $(this).animate({marginTop:'-='+mainPartnersSlide.config.windowHeight+'px'}, mainPartnersSlide.config.motionTimeNormal);
//       $(mainPartnersSlide.config.innerId).children(':last').after($(this));
//       $(this).dequeue();
//     });

  }
};

cooperationPartnersSlide = {
  config:
  {
    wrapperId: '#partners #cooperationPartners',
	  innerId: '#partners #cooperationPartners .slideIn',
	  windowHeight: 110,
	  motionTimeNormal: 300,
	  motionTimeSkip: 200,
	  timer: 5000,
	  waitAfterClick: 6000
  },
  init: function()
  {
     if(!$(this.config.wrapperId).length) return false;

     this.partnersCount = $(this.config.innerId).children().length;
     this.active = 1;

     if( this.partnersCount )
      $(this.config.wrapperId).addClass('active');

     this.interval = setInterval('cooperationPartnersSlide.next()', this.config.timer);
  },
  setHref: function()
  {
    var thref = $(this.config.innerId).children(':eq('+(this.active-1)+')').attr('href');
    if( typeof(thref)=='undefined' || !thref)
      thref = '#';
    $(this.config.wrapperId).children('.overlay').attr('href', thref );
  },
  next: function()
  {
    $(this.config.innerId).children(':last').after($(this.config.innerId).children(':first') );
  }
};

otherPartnersSlide = {
  config:
  {
    wrapperId: '#partners #otherPartners',
	  innerId: '#partners #otherPartners .slideIn',
	  windowHeight: 110,
	  motionTimeNormal: 300,
	  motionTimeSkip: 200,
	  timer: 5000,
	  waitAfterClick: 6000
  },
  init: function()
  {
     if(!$(this.config.wrapperId).length) return false;

     this.partnersCount = $(this.config.innerId).children().length;
     this.active = 1;

     if( this.partnersCount )
      $(this.config.wrapperId).addClass('active');

     this.interval = setInterval('otherPartnersSlide.next()', this.config.timer);
  },
  setHref: function()
  {
    var thref = $(this.config.innerId).children(':eq('+(this.active-1)+')').attr('href');
    if( typeof(thref)=='undefined' || !thref)
      thref = '#';
    $(this.config.wrapperId).children('.overlay').attr('href', thref );
  },
  next: function()
  {
    $(this.config.innerId).children(':last').after($(this.config.innerId).children(':first') );
  }
};

mediaPartnersSlide = {
  config:
  {
    wrapperId: '#partners #mediaPartners',
	  innerId: '#partners #mediaPartners .slideIn',
	  windowHeight: 110,
	  motionTimeNormal: 300,
	  motionTimeSkip: 200,
	  timer: 5000,
	  waitAfterClick: 6000
  },
  init: function()
  {
     if(!$(this.config.wrapperId).length) return false;

     this.partnersCount = $(this.config.innerId).children().length;
     this.active = 1;

     if( this.partnersCount )
      $(this.config.wrapperId).addClass('active');

     this.interval = setInterval('mediaPartnersSlide.next()', this.config.timer);
  },
  setHref: function()
  {
    var thref = $(this.config.innerId).children(':eq('+(this.active-1)+')').attr('href');
    if( typeof(thref)=='undefined' || !thref)
      thref = '#';
    $(this.config.wrapperId).children('.overlay').attr('href', thref );
  },
  next: function()
  {
    $(this.config.innerId).children(':last').after($(this.config.innerId).children(':first') );
  }
};


















gallerySlide = {
  config:
  {
    wrapperId: '#galleryOuter',
	  innerId: '#galleryInner',
	  windowWidth: 160,
	  windowsInline: 5,
	  motionTimeNormal: 300,
	  motionTimeSkip: 200
  },
  init: function()
  {
     if(!$(this.config.wrapperId).length) return false;

  	 this.imagesCount = $(this.config.innerId).children().length;
  	 if( !this.imagesCount ) return false;

  	 gallerySlide.firstActive = 1;

  	 $(this.config.wrapperId).after('<a href="#" id="galleryPrev">&laquo;<span class="overlay"><!--  --></span></a><a href="#" id="galleryNext">&raquo;<span class="overlay"><!--  --></span></a>');
  	 $('#galleryPrev').click(function(){gallerySlide.prev();return false;});
  	 $('#galleryNext').click(function(){gallerySlide.next();return false;});
  },
  prev: function()
  {
      if( gallerySlide.firstActive < gallerySlide.imagesCount - gallerySlide.config.windowsInline + 1 )
      {
		    $(gallerySlide.config.innerId).animate({marginLeft:'-='+gallerySlide.config.windowWidth+'px'}, partnersSlide.config.motionTimeNormal);
		    gallerySlide.firstActive += 1;
		  }
      else
      {
        $(gallerySlide.config.innerId).animate({marginLeft:'0px'}, partnersSlide.config.motionTimeSkip);
		    gallerySlide.firstActive = 1;
      }
  },
  next: function()
  {
      if( gallerySlide.firstActive > 1 )
      {
		    $(gallerySlide.config.innerId).animate({marginLeft:'+='+gallerySlide.config.windowWidth+'px'}, partnersSlide.config.motionTimeNormal);
        gallerySlide.firstActive -= 1;
      }
      else
      {
        $(gallerySlide.config.innerId).animate({marginLeft: '-'+(gallerySlide.config.windowWidth*gallerySlide.imagesCount - gallerySlide.config.windowWidth*gallerySlide.config.windowsInline)+'px'}, partnersSlide.config.motionTimeSkip);
        gallerySlide.firstActive = gallerySlide.imagesCount - gallerySlide.config.windowsInline + 1;
      }
  }

};

function balance_cols(left, right)
{
  if( !( $(left).length && $(right).length ) ) return false;

  var content_real_height = $(left).height() + parseInt($(left).css('padding-top')) + parseInt($(left).css('padding-bottom'));
  if(content_real_height)
  {
	  var sidebar_real_height = $(right).height() + parseInt($(right).parent().css('padding-top')) + parseInt($(right).parent().css('padding-bottom') );

	  /**/
	  if( content_real_height  > sidebar_real_height )
	  {
			$(right).height( content_real_height - parseInt($(right).parent().css('padding-top')) - parseInt($(right).parent().css('padding-bottom')) + 2 );
	  }
	  else
	  {
			$(left).height( sidebar_real_height - parseInt($(left).css('padding-top')) - parseInt($(left).css('padding-bottom')) - 2 );
	  }
	  /**/
  }
}

ajaxActionMidDetail = {
  init: function()
  {
    //if(typeof(console)!='object') return false;
    $('#boxActualList').find('a').click(function(){ ajaxActionMidDetail.load($(this)); return false; });
  },
  load: function(link)
  {
    var href = link.attr('href');
    href = href.split('?');
    href = 'http://www.missaerobik.cz/ajax-news-detail/?'+href[1];

    $.ajax({
	     type: "GET",
	     url: href,
	     success: function(data)
	     {
          $('#boxActualDetail').replaceWith(data);
		   },
			 error: function()
			 {
          window.location.href = href;
			 }
	   });
  }


};

calendarEvents = {
  init: function()
  {
    if(!$('.eventsCalendar').length) return false;
    $('#textBlockC').css('height',$('#textBlockC').height());
    $('.eventsCalendar .text:visible').slideUp();
    $('.eventsCalendar .title').css({textDecoration:'underline', cursor:'pointer'}).hover(
        function(){
          $(this).css('text-decoration','none') ;
        },
        function(){
          $(this).css('text-decoration','underline') ;
        }
      ).click(
      function(){
        //$('.eventsCalendar .text:visible').slideUp();
        $(this).next('.text').slideToggle();
      }
      );
  }

}

var selectReplace = {
  init: function(select, seo)
  {
    if(!$(select).length) return false;

    selectReplace.selectName = $(select).attr('name');

    selectReplace.list = '<div id="selectReplaced">';
    selectReplace.list += '<p id="current_selection">&nbsp;</p>';
    selectReplace.list += '<ul id="select_list">';
    $(select).children('option').each(function(){
        if($(this).attr('selected'))
        {
          selectReplace.selectAfterVal = $(this).attr('value');
          selectReplace.selectAfterName = $(this).text();
        }
        selectReplace.list += '<li><span onclick="selectReplace.select(\''+$(this).attr('value')+'\', \''+$(this).text()+'\')">'+$(this).text()+'</span></li>'
    });
    selectReplace.list += '</ul></div>';

    $(select).after('<input type="hidden" name="'+selectReplace.selectName+'" id="selected_stack" />');
    $(select).replaceWith(selectReplace.list);
    if(selectReplace.selectAfterVal)
    {
      selectReplace.select(selectReplace.selectAfterVal, selectReplace.selectAfterName);
    }
    $('#current_selection').click(function(){selectReplace.open()});
    $('#select_list').hide();

    if(seo)
    {
      $('#selectReplaced').parents('form').bind('submit', function(){
        var link = $('#selectReplaced').parents('form').serialize().split('=');
        link = link[1]+'/';
        var url = String(document.location).split('/');
        var urlComplete = '';
        for(i=0; i<4; i++)
        {
          urlComplete += url[i]+'/';
        }
        window.location.href = urlComplete+link+'#content';

        return false;
      });
    }
  },
  select: function(value, name)
  {
    $('#selected_stack').attr('value', value);
    $('#current_selection').text(name);
    selectReplace.close();
  },
  open: function()
  {
    $('#select_list').toggle();
  },
  close: function()
  {
    $('#select_list').hide();
  }
};





actionList = {
  init: function()
  {
    $('#boxActualList .vals a').click(
      function(){
        $('#boxActualList .vals').removeClass('act');
        $(this).parent('.vals').addClass('act');
      }
    );
    /**
     * PJA vypnul zvyrazneni prvniho odkazu dne 17.3.2010. Nejak to pozbyvalo vyznamu...
     */         
    //$('#boxActualList .vals:first').addClass('act');
  }
}


finalistsThumbs = {
  config:
  {
    elThumb:    '#finalistsThumbs .finalist a',
    elFinalist: '#finalistsInfo .finalist',
    // elClose:    '#finalistsInfo .finalist .close',
    // htmlClose:  '<a href="#" class="close"><!-- --></a>',
	  motionTimeNormal: 300,
	  timer: 3000//,
    // waitAfterClick: 6000
  },
  init: function()
  {
//     // APPEND CLOSE BUTTON
//     $(finalistsThumbs.config.elFinalist ).append(finalistsThumbs.config.htmlClose );
//     // FINALIST THUMBNAIL HOVER
//     $(finalistsThumbs.config.elThumb ).hover(
//       function()
//       {
//         $(finalistsThumbs.config.elFinalist ).hide();
//         $($(this).attr('href')).fadeIn(finalistsThumbs.config.motionTimeNormal );
//       },
//       function()
//       {
//       }
//     );
    // FINALIST THUMBNAIL ONCLICK
    $(finalistsThumbs.config.elThumb ).click(
      function()
      {
        clearTimeout(finalistsThumbs.interval);
        // finalistsThumbs.interval = setInterval('finalistsThumbs.next()', finalistsThumbs.config.waitAfterClick);
        // finalistsThumbs.active = parseInt($(this).attr('href').replace('#finalist-', '' ) );
        $(finalistsThumbs.config.elFinalist ).hide();
        $(finalistsThumbs.config.elThumb ).parent().removeClass('show');
        $(this).parent().addClass('show');
        $($(this).attr('href')).fadeIn(finalistsThumbs.config.motionTimeNormal );
        return false;
      }
    );
//     // CLOSE BUTTON ONCLICK
//     $(finalistsThumbs.config.elClose ).click(
//       function()
//       {
//         $(this).parent(finalistsThumbs.config.elFinalist+':visible' ).fadeOut(finalistsThumbs.config.motionTimeNormal );
//         return false;
//       }
//     );
    this.active = 1;
    finalistsThumbs.interval = setInterval('finalistsThumbs.next()', finalistsThumbs.config.timer);
  },
  next: function()
  {
    finalistsThumbs.showed = $(finalistsThumbs.config.elFinalist+':visible');

    $(finalistsThumbs.config.elThumb ).parent().removeClass('show');

    if ( finalistsThumbs.active < $(finalistsThumbs.config.elFinalist).length )
    {
      finalistsThumbs.active++;
      finalistsThumbs.showed.hide();
      $('#thumb-'+finalistsThumbs.active).addClass('show');
      finalistsThumbs.showed.next().fadeIn(finalistsThumbs.config.motionTimeNormal );
    }
    else
    {
      finalistsThumbs.active = 1;
      finalistsThumbs.showed.hide();
      $('#thumb-'+finalistsThumbs.active).addClass('show');
      $(finalistsThumbs.config.elFinalist+':first').fadeIn(finalistsThumbs.config.motionTimeNormal );
    }
  }
}

//--------------------------------------------------------------------------

$(document).ready(function() {

//   balance_cols('#galleryBlockC', '#texMenu');//'#galleryMenu');
//   balance_cols('#medBlockC', '#medMenu');
//   balance_cols('#textBlockC', '#textMenu');
//   balance_cols('#textBlockC', '#texMenu');

  Cufon.replace('.cufon' );//, { letterSpacing: '1px' });

  partnersSlide.init();

  mainPartnersSlide.init();
  otherPartnersSlide.init();
  cooperationPartnersSlide.init();
  mediaPartnersSlide.init();

  gallerySlide.init();

  actionList.init();

  customLightbox.init();

  //ajaxActionMidDetail.init();
  //calendarEvents.init();
  
  finalistsThumbs.init();
  

  selectReplace.init('#modelSelector select', true);

});
