(function($){
	$( document ).ready( function(){
		
		$('#navigation ul li .sub-menu').each(function(){
			$(this).wrap('<div class="dropdown" />');
		})
		
		if ($.browser.msie && $.browser.version.substr(0,1)<7) {
			DD_belatedPNG.fix('h1#logo a, .contact-info, #navigation ul li a span.btn-l, #navigation ul li a span.btn-r, #navigation ul li a span.cnt, #small-cnt-top, #small-cnt-bottom, #content-bg ');
		};
		
		$('#navigation ul li').hover(function () {
			$(this).find('.dropdown').show();
		}, function () {
			$(this).find('.dropdown').hide();
		});
		
		$('a.sizeChanger').click(function(){
			var size = $(this).attr('rel');
			if ( size != '' ) {
				$('body').removeClass('s1 s2').addClass(size);
				if ( size == 's1' ) {
					$(this).attr('rel', 's2');
					$(this).text('Increase font size');
				} else {
					$(this).attr('rel', 's1');
					$(this).text('Decrease font size');
				};
			};
			
			return false;
		})
		
		$('#slider').jcarousel({
			scroll: 1,
			auto: 8,
			wrap: 'circular',
			itemVisibleInCallback: {
				onBeforeAnimation: function(carousel, item){
					$('#slider-row ul li h2').hide();
				}, onAfterAnimation: function(carousel, item){
					setTimeout(function(){
						$(item).find('h2').fadeIn(700);
					}, 400);
				}
			}
		});
		
		/*$('#sidebar ul li.side-nav > ul > li > a.subNav').live('click', function () {
			$('#sidebar ul li.location-map').remove();
			$('#sidebar ul li.side-nav ul li a').removeClass('active');
			$('#sidebar ul li.side-nav ul li > ul').slideUp();
			$(this).addClass('active');
			var rel = $(this).attr('rel');
			if (rel != '') {
				$('#sidebar ul:eq(0)').prepend('<li class="location-map"><img src="' + rel + '" alt="" /></li>');
			};
			$(this).parent().find('ul').slideDown();	
			return false;
		}); */
		
		check_side_nav();
		
	});
	
	$(window).load(function() {
		sameHeights();
	});
	
	function check_side_nav() {
		if ( $('#sidebar ul li.side-nav ul > li').hasClass('current_page_item') || $('#sidebar ul li.side-nav ul > li').hasClass('current_page_ancestor') ) {
			$('#sidebar ul li.side-nav ul li.current_page_item, #sidebar ul li.side-nav ul li.current_page_ancestor').find('ul.children').show();
			var activeRel = $('#sidebar ul li.side-nav ul li.current_page_item a').attr('rel');
			activeRel = ( activeRel != '' )? activeRel : $('#sidebar ul li.side-nav ul li.current_page_ancestor a').attr('rel');
			if (activeRel != '') {
				$('#sidebar li.theme-widget-navigation > ul').prepend('<li class="location-map"><img src="' + activeRel + '" alt="" /></li>');
				$('#sidebar ul .location-map img').show();
			} else { 
				$('#sidebar ul li.location-map').remove();
			};
		};
	}
	
	function sameHeights() {
		var sidePadding = $('#sidebar #sidebar-bottom').outerHeight() - $('#sidebar #sidebar-bottom').height();
		var contentPadding = $('#content #content-bg').outerHeight() - $('#content #content-bg').height();
		contentPadding = contentPadding + $('#content #small-cnt-top').height() + $('#content #small-cnt-bottom').height();
		if ($('#sidebar').height() > $('#content').height()) {
			var sheight = $('#sidebar').height() - contentPadding;
			$('#content #content-bg').css('height', sheight);
		} else {
			var cheight =  $('#content').height() - sidePadding
			$('#sidebar #sidebar-bottom').css('height', cheight);
		};
	}
	
})(jQuery)
