/*-----------------------------------------------------------------------------
 JS for Clients.fi
 version:    1.0
 date:       ----------
 author:     Fusion
 email:      florian.plank@fusion.fi, jani.komulainen@fusion.fi
 website:    http://www.clients.fi
 -----------------------------------------------------------------------------*/


/*
CSS Browser Selector v0.2.7
Rafael Lima (http://rafael.adm.br)
http://rafael.adm.br/css_browser_selector
License: http://creativecommons.org/licenses/by/2.5/
Contributors: http://rafael.adm.br/css_browser_selector#contributors
*/

var isIE6 = (jQuery.browser.msie && Number(jQuery.browser.version) <= 6.0);

var css_browser_selector = function() {var ua=navigator.userAgent.toLowerCase(),is=function(t){return ua.indexOf(t) != -1;},h=document.getElementsByTagName('html')[0],b=(!(/opera|webtv/i.test(ua))&&/msie (\d)/.test(ua))?('ie ie'+RegExp.$1):is('firefox/2')?'gecko ff2':is('firefox/3')?'gecko ff3':is('gecko/')?'gecko':is('opera/9')?'opera opera9':/opera (\d)/.test(ua)?'opera opera'+RegExp.$1:is('konqueror')?'konqueror':is('applewebkit/')?'webkit safari':is('mozilla/')?'gecko':'',os=(is('x11')||is('linux'))?' linux':is('mac')?' mac':is('win')?' win':'';var c=b+os+' js'; h.className += h.className?' '+c:c;}();


function initAccordionMenus() 
{
	$('ul.accordion ul').hide();
	$.each($('ul.accordion'), function()
	{
		//$('#' + this.id + '.expandfirst ul:first').show();
		//$('#' + this.id + '.expandfirst li:first').addClass('open');
	});
	$('ul.accordion li a').click(
		function() 
		{
			var checkElement = $(this).next();
			var parent = this.parentNode.parentNode.id;
			$('.open').removeClass('open');	
			
			if($('#' + parent).hasClass('noaccordion')) 
			{
				$(this).next().slideToggle('normal');
				return false;
			}
			if((checkElement.is('ul')) && (checkElement.is(':visible'))) 
			{
				if($('#' + parent).hasClass('collapsible')) 
				{
					$('#' + parent + ' ul:visible').slideUp('normal');
				}
				
				return false;
			}
			if((checkElement.is('ul')) && (!checkElement.is(':visible'))) 
			{
				$('#' + parent + ' ul:visible').slideUp('normal');
				checkElement.slideDown('normal');
				$(this).parent().addClass('open');
				return false;
			};
		}
	);
}

$(document).ready(function()
{
	
	// PNG FIX
	if( isIE6 )
	{
		DD_belatedPNG.fix('#header h1 a, .col-box-head, #sidebar .box-blue .box-colored, #sidebar .col-box-head .blue, .col-box-pad, .col-box-pad3, ol.commentlist li');
	}
	
	$('.hide').hide();
	
	$("#blog-tabs").tabs();
	
	// toggler
	$.fn.toggler = function(div) {
		return this.click(function () {
			$(div).slideToggle('normal');
			return false;
		});
	};
	$("#toggle-tags").toggler("#all-tags");
	$("#toggle-authorposts").toggler("#author-archive");
	
	// this is a bit of a hack: set link in main navigation as active depending on category ID
	
	
	if ($("#active-category-id").length > 0 ) 
	{
		var curCat = $("#active-category-id").val();
		switch(curCat)
		{
			case '29':
				$("#nav-uutiset").addClass("active");
			break;
			
			case '25':
				$("#nav-yritys").addClass("active");
			break;
			
			default:
				$("#nav-blogi").addClass("active");
		}
		/*
			if ($("#active-category-id").val() == 29) 
			{
				$("#nav-uutiset").addClass("active");
			} 
			else 
			{
				$("#nav-blogi").addClass("active");
			}
		*/
	}
	
	/*
	$('.caption img, .size-thumbnail, .size-medium, .size-large').captify({
		// all of these options are... optional
		// ---
		// speed of the mouseover effect
		speedOver: 'fast',
		// speed of the mouseout effect
		speedOut: 'normal',
		// how long to delay the hiding of the caption after mouseout (ms)
		hideDelay: 500,	
		// 'fade', 'slide', 'always-on'
		animation: 'slide',		
		// text/html to be placed at the beginning of every caption
		prefix: '',		
		// opacity of the caption on mouse over
		opacity: '0.7',					
		// the name of the CSS class to apply to the caption box
		className: 'caption-bottom',	
		// position of the caption (top or bottom)
		position: 'bottom',
		// caption span % of the image
		spanWidth: '100%'
	});
		
	$('div.caption-wrapper img').removeAttr('title');
	$('div.caption-wrapper').css("margin","0 10px 10px 0");
	*/
	$('img.alignnone').parent().css('clear','left');

    // replace email address
    // anti-spam measure
    $('span.email-address').each(function()
    {
        var obfAddress = $(this).text();
        var realAddress = obfAddress.replace(/\[at\]/g, '@').replace(/\[dot\]/g, '.');
        var emailLink = document.createElement('a');
        $(emailLink).attr(
        {
            'href': 'mailto:' + realAddress,
            'title': 'Email: ' + realAddress,
            'class': 'email-address'
        }).text(realAddress);
        $(this).replaceWith(emailLink);
    });
    
    // function to clear searchfield default text on focus
	$.fn.search = function() 
	{
		return this.focus(function() 
		{
			if( this.value == this.defaultValue ) 
			{
				this.value = "";
				$('#header .search').addClass('search-focus');
			}
		}).blur(function() {
			if( !this.value.length ) 
			{
				this.value = this.defaultValue;
				$('#header .search').removeClass('search-focus');
			}
		});
	};
	$("#search").search();
	
	initAccordionMenus();
	
	$(function()
	{
		$('.form').jqTransform({imgPath:'js/plugins/jqtransformplugin/img/'});
	});
	
	// jQuery SmoothScroll
	// from http://blog.medianotions.de/en/articles/2009/smoothscroll-for-jquery
	$('a.top[href*=#]').click(function() 
	{
	
	   // make sure it's the same location      
	   if(location.pathname.replace(/^\//,'')==this.pathname.replace(/^\//,'') && location.hostname==this.hostname && this.hash.replace(/#/,''))
	   {
	      // get parameters
	      var hash=this.hash;
	      var target=$(hash).offset().top-80; //need -80px because of the fixed #header
	      // animate to target and set the hash to the window.location after the animation
	      $("html:not(:animated),body:not(:animated)").animate({ scrollTop: target }, 700, function() { /*location.hash=hash;*/ });
	      // cancel default click action
	      return false;
	   }
	
	});
	
});

Cufon.replace('h2', {
	hover: true
})('h3', {
	hover: true
})('h4.head', {
	hover: true
});
	


