$.fn.equalHeights = function(px) {
	$(this).each(function(){
		var currentTallest = 0;
		$(this).children().each(function(i){
			if ($(this).height() > currentTallest) { currentTallest = $(this).height(); }
		});
//		if (!px || !Number.prototype.pxToEm) currentTallest = currentTallest.pxToEm(); //use ems unless px is specified
		// for ie6, set height since min-height isn't supported
		if ($.browser.msie && $.browser.version == 6.0) { $(this).children().css({'height': currentTallest}); }
		$(this).children().css({'min-height': currentTallest}); 
	});
	return this;
};


// paulirish.com/2009/log-a-lightweight-wrapper-for-consolelog/

window.log = function(){
  log.history = log.history || [];   // store logs to an array for reference
  log.history.push(arguments);
  if(this.console) {
    arguments.callee = arguments.callee.caller;
    var newarr = [].slice.call(arguments);
    (typeof console.log === 'object' ? log.apply.call(console.log, console, newarr) : console.log.apply(console, newarr));
  }
};

// make it safe to use console.log always

(function(b){function c(){}for(var d="assert,count,debug,dir,dirxml,error,exception,group,groupCollapsed,groupEnd,info,log,timeStamp,profile,profileEnd,time,timeEnd,trace,warn".split(","),a;a=d.pop();){b[a]=b[a]||c}})((function(){try
{console.log();return window.console;}catch(err){return window.console={};}})());




$(function() {

$('body').not('.page-1, .page-4, .page-3').find('#article-inner').equalHeights();

//setup google viewer pdf links on colorbox

   var pdfElelement = $('aside a[href$=".pdf"]');
   if(pdfElelement.length > 0) {
      var currentHref = pdfElelement.attr('href');
      var newHref = 'http://docs.google.com/viewer?url=' + encodeURI('http://' +window.location.hostname +currentHref) +'&embedded=true'
      pdfElelement.colorbox({           
          scrolling : false,
          href: newHref, 
          iframe:true, 
          width: '600px', 
          height: '780px'
       });
   }

   $('#news').hide();
 
    $('#tabs a').live('click', function(){
           var ele = $(this);
           var xid = ele.attr('href');
           ele.parents('li:first').addClass('active').siblings('li').removeClass('active');
           $(xid).show().siblings('div').not('.primary').hide();
           
           return false;
    });

  $('.public #homepage-header .secondary a').addClass('button white');


//Button html modifications

 //$('.public .button').prepend('<span class="l"></span>').append('<span class="r"></span>');


//timeline events 

var dayWidth = 499 / 365;

$('#timeline li').each(function(){
    var eventPos = ($(this).data('day') * dayWidth) +10;
    $(this).css({'left': eventPos});
});


//Move to event by clicking on them

$('#timeline li').live('click', function() {
    var header = $(this).data('header');
    var date = $(this).data('date');
    var url = $(this).data('url');
    $(this).attr('id', 'active-event').siblings('li').attr('id', null);
    $('#homepage-header-inner .primary').find('p.date').text(date).end()
       .find('a').attr('href', url).end()
       .find('.header').text(header);
});


var timelineFirst = $('#timeline ul li:first-child');
var timelineLast = $('#timeline ul li:last-child');

//Move to previous event

$('#prev-year-link').live('click', function() {
    var prevEvent = $('#active-event').next('li');
    if(prevEvent.length == 0) {
         prevEvent = timelineFirst;
    }
    prevEvent.trigger('click');
    return false;
});

//Move to next event

$('#next-year-link').live('click', function() {
    var nextEvent = $('#active-event').prev('li');
    if(nextEvent.length == 0) {
         nextEvent = timelineLast;
    }
     nextEvent.trigger('click');
    return false;
});


function doEventSwicth() {
    $('#prev-year-link').trigger('click');
}

  var preLoadTimer;

  $(this).mousemove(function(e){
    //clear prior timeout, if any
    window.clearTimeout(preLoadTimer);

    //create new timeout.
    preLoadTimer = window.setTimeout(doEventSwicth, 1000);
  });



//product image carousel

function onAfter(curr, next, opts) {
    var index = opts.currSlide;
    $('#prev-slide')[index == 0 ? 'hide' : 'show']();
    $('#next-slide')[index == opts.slideCount - 1 ? 'hide' : 'show']();
}

$('#homepage-slideshow').cycle({ 
    fx:     'scrollHorz', 
    prev:   '#prev-slide', 
    next:   '#next-slide', 
    timeout: 0 
});

    $('#media').equalHeights();

if($('body').hasClass('page-1') && window.location.hash == '#joulutervehdys') {
    $.colorbox({
            href:"/jouluvideo-2011",
           scrolling : false
    });
}



});
