/**
 * Master js file
 *
 * @author	Kristián Feldsam
 * @charset	utf-8
 *
 */

Cufon.replace('#content h1', {fontFamily: 'EtelkaBold'});
Cufon.replace('#content h1 span', {fontFamily: 'Etelka'});
Cufon.replace('#content h2', {fontFamily: 'EtelkaBold'});
Cufon.replace('#content h3', {fontFamily: 'EtelkaBold'});
Cufon.replace('#content p.strong', {fontFamily: 'Arial'});
Cufon.replace('#why_us h2', {fontFamily: 'EtelkaBold'});
Cufon.replace('.list .heading', {fontFamily: 'EtelkaBold'});
Cufon.replace('#news h2', {fontFamily: 'EtelkaBold'});
Cufon.replace('h1.sub', {fontFamily: 'Etelka'});
Cufon.replace('#slider p .heading', {fontFamily: 'Etelka'});
 
(function($) {
  var cache = [];
  // Arguments are image paths relative to the current page.
  $.preLoadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
  }
})(jQuery)
 
$(document).ready(function(){

	// open A tags with class external in new windows - replace unsemantic target="_blank"
	$('a.external').click(function() { window.open($(this).attr('href')); return false; });
	
	// slider items
	
	// global vars
	pos = new Array();
	index = new Array();
	count = 0;
	stopSlide = 0;
	
	$('ul.items li').each(function(i){
		var id = $(this).attr('id');
		pos[id] = 45 * i;
		index[i] = id;
		if($(this).hasClass('selected')){
			$(this).css({backgroundPosition: '-640px -'+pos[id]+'px'});
			$('#slider').find('.'+id).show();
		}else{
			$(this).css({backgroundPosition: '0 -'+pos[id]+'px'});
		}
	});
	$('ul.items li').hover(function(){
		if(!$(this).hasClass('selected')){
			var id = $(this).attr('id');
			$(this).css({backgroundPosition: '-320px -'+pos[id]+'px'});
		}
	},function(){
		if(!$(this).hasClass('selected')){
			var id = $(this).attr('id');
			$(this).css({backgroundPosition: '0 -'+pos[id]+'px'});
		}
	});
	
	$('ul.items li').click(function(){
		stopSlide = 1;
		sliderChange($(this));
	});
	
	count = index.length;
	slide(0);
	function slide(i){
		if(i >= count)i = 0;
		setTimeout(function(){
	       if(stopSlide == 0){
	       	sliderChange($('#'+index[i]));
	       	i++;
	       	slide(i);
	       }
		}, 4000);
		return;
	}
	
	// slider change
	function sliderChange(obj){
		var sid = $('ul.items li.selected').attr('id');
		$('ul.items li.selected').removeClass('selected').css({backgroundPosition: '0 -'+pos[sid]+'px'});
		var id = obj.attr('id');
		obj.addClass('selected').css({backgroundPosition: '-640px -'+pos[id]+'px'});
		$('#slider p.'+sid).hide();
		$('#slider p.'+id).show();
	}
	
	// set news date format
	$('#news .date').each(function(){
		var date = Date.parse($(this).text());
		var day = date.toString('d.');
		var month = date.toString('MMM');
		$(this).html('<span>'+day+'</span> <span>'+month+'</span>');
	});
	
	// vertical center sub menu
	$('#sidebar menu li a').each(function(i){
		if(!i)$(this).addClass('first');
		var height = $(this).height();
		if(height > 15){
			var diff = (height - 15)/2;
			var newPad = 10 - diff;
			$(this).css({paddingTop: Math.round(newPad)+'px', paddingBottom: Math.round(newPad)-1+'px'});
		}
	});

});


// Returns the version of Internet Explorer or a -1
// (indicating the use of another browser).
function getInternetExplorerVersion()
{
  var rv = -1; // Return value assumes failure.
  if (navigator.appName == "Microsoft Internet Explorer")
  {
    var ua = navigator.userAgent;
    var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
    if (re.exec(ua) != null)
      rv = parseFloat( RegExp.$1 );
  }
  return rv;
}
