$(document).ready(function() {
	disableSelection(document.body);
	
	Cufon.replace('#pagenav a', {
		textShadow: '1px 1px #666666'
	});
	Cufon.replace('.widget h2');
	Cufon.replace('#contact_form h1');
	Cufon.replace('#footer h2');
	
	$("ul.sf-menu").superfish();
	
	equalHeight($(".miniblogpostgroup h3"));
	
	// Sidebar icons
	$('#fbIcon').hover(
		function() {
			var baloon = $('#fbMsg');
			baloon.css({left:$(this).position().left - baloon.width()/2 + $(this).width()/2 - 5, display: 'block'}).animate({opacity: 1, top: '28px'}, 400);
		},
		function() {
			$('#fbMsg').css({display: 'none', opacity: 0, top: '23px'});
	});
	$('#mailIcon').hover(
		function() {
			var baloon = $('#mailMsg');
			baloon.css({left:$(this).position().left - baloon.width()/2 + $(this).width()/2 - 5, display: 'block'}).animate({opacity: 1, top: '28px'}, 400);
		},
		function() {
			$('#mailMsg').css({display: 'none', opacity: 0, top: '23px'});
	});
	$('#feedIcon').hover(
		function() {
			var baloon = $('#feedMsg');
			baloon.css({left:$(this).position().left - baloon.width()/2 + $(this).width()/2 - 5, display: 'block'}).animate({opacity: 1, top: '28px'}, 400);
		},
		function() {
			$('#feedMsg').css({display: 'none', opacity: 0, top: '23px'});
	});
	$("#mailLink").colorbox({width: '500px', inline:true, href:"#contact_form", opacity: 0.5}); 
	
	// =========== Menu links =========== //
	// Remove the links from nodes with children and mark them with white arrow (not on 1st level)
	$('#nav li:has(ul)').children('a[title!=Novice]').attr('href', '#');
	$('#nav li ul li:has(ul)').children('a[title!=Novice]').addClass('nodeHasChildren');
	
	// For each first level node set hover function
	$('#nav').children('li').hover(function() {
		$('#slide').stop().animate({left: $(this).position().left, width: $(this).width()});
	});
	
	$('#nav').mouseleave(function() {
		markSelectedNode(true);
	});
	
	// =========== Sponzorji =========== //
	$(document).ready(function() {
		$('#sponzorji').cycle({
			fx:     'fade',
			random: 'true',
			before: centerImage
		});
	});
	
	// =========== Search form =========== //
	$('#keyword').change(function() {
		if ($(this).val() == '')
			$(this).css('text-align', 'center').attr({value: 'Išči'});
	}).focus(function() {
		if ($(this).val() == 'Išči')
			$(this).css('text-align', 'left').attr({value: ''});
	});
	
	$(document).bind('cbox_closed', function(){
        $("#sendEmail #contact_loading").hide();
		$("#contact_form form").show();
		$("#contact_form div").last().remove();
		$("#contact_form #submit_btn").show();
		$("input #emailFrom").val("");
		$("input #subject").val("");
		$("input #message").val("");
	});
	
	Cufon.now();	// IE fix
	
	// =========== Menu slider =========== //
	// First mark the currently selected node
	markSelectedNode(false);
});

function disableSelection(target){
if (typeof target.onselectstart!="undefined") //IE route
	target.onselectstart=function(){return false}
else if (typeof target.style.MozUserSelect!="undefined") //Firefox route
	target.style.MozUserSelect="none"
else //All other route (ie: Opera)
	target.onmousedown=function(){return false}
target.style.cursor = "default"
}

function equalHeight(group) {
	tallest = 0;
	group.each(function() {
		thisHeight = $(this).height();
		if(thisHeight > tallest) {
			tallest = thisHeight;
		}
	});
	group.height(tallest);
}

function initAccordion() {
	$('.accordion div').hide();
	$('.accordion div:first').show();
	
	$('.accordion h3').click( function() {
		var clickedElement = $(this).next();
		if (clickedElement.is('div') && clickedElement.is(':visible')) {
			return false;
		}
		else {
			$('.accordion div:visible').slideUp('normal');
			clickedElement.slideDown('normal');
		}
	});
}

// before the picture is shown it is centered and the parent div is enlarged if height
// of the picture is larger then the height of the div
function centerImage(curr,next,opts) {
		var $slide = $(next);
		var $parent = $slide.parent();
		var w = $slide.outerWidth();
		var h = $slide.outerHeight();
			
		$parent.height(h+10);
		$slide.css({
			marginTop: ($parent.outerHeight() - h) / 2,
			marginLeft: ($parent.outerWidth() - w - 10) / 2
		});
};

function markSelectedNode(animation) {
	var selectedNode = $('#nav li.current_page_parent,li.current_page_item,li.current_page_ancestor');
	if (animation)
		$('#slide').stop().animate({left: selectedNode.position().left, width: selectedNode.width()});
	else
		$('#slide').css({left: selectedNode.position().left, width: selectedNode.width()});
}