$nym=jQuery.noConflict();



$nym(document).ready(function(){

	// MENU
	var timeout    = 200;
	var closetimer = 0;
	var ddmenuitem = 0;

	function jsddm_open()
	{  jsddm_canceltimer();
	   jsddm_close();
		$nym(this).addClass('selected');
	   ddmenuitem = $nym(this).find('ul').css('visibility', 'visible');}

	function jsddm_close()
	{  if(ddmenuitem) { $nym('#menu li').removeClass('selected'); ddmenuitem.css('visibility', 'hidden'); } }

	function jsddm_timer()
	{  closetimer = window.setTimeout(jsddm_close, timeout);}

	function jsddm_canceltimer()
	{  if(closetimer)
	   {  window.clearTimeout(closetimer);
	      closetimer = null;}}

	$nym(document).ready(function()
	{  $nym('#menu > li').bind('mouseover', jsddm_open)
	   $nym('#menu > li').bind('mouseout',  jsddm_timer)});

	document.onclick = jsddm_close;

	// POSTS OPENER-CLOSER
	$nym('.closed .hidden').css({'display':'none'});

	$nym('.opener-closer, .title a, .comments-link').click(function(){
		var link_node = $nym(this);
		var post_node = $nym(this).parent().parent();
		var hidden_node = post_node.find('.hidden');
		if(post_node.hasClass('closed')){

			$nym('.post.opened').each(function(){
				if($nym(this).find('.hidden').length){
					$nym(this).find('.hidden').hide();
				}
				$nym(this).addClass('closed');
				$nym(this).removeClass('opened');
			});

			if(!link_node.hasClass('comments-link')){
				$nym(window).scrollTo({ top:parseInt(post_node.find('.title').offset().top), left:'0px'}, 500, {axis:'xy'});
			}

			if(hidden_node.length){
				hidden_node.animate({'height':'show'},500,function(){
					if(link_node.hasClass('comments-link')){
						$nym(window).scrollTo({ top:parseInt(post_node.find('.comments-handler').offset().top), left:'0px'}, 500, {axis:'xy'});
					}
					post_node.addClass('opened');
					post_node.removeClass('closed');
					post_node.find('.comments-scroll').jScrollPane({showArrows:false});
				});
			}else{
					post_node.find('.comments-scroll').jScrollPane({showArrows:false});
					post_node.addClass('opened');
					post_node.removeClass('closed');
			}

		}else{
			if(hidden_node.length){
				hidden_node.animate({'height':'hide'},500,function(){
					post_node.removeClass('opened');
					post_node.addClass('closed');
				});
			}
			$nym(window).scrollTo({ top:parseInt(post_node.find('.title').offset().top), left:'0px'}, 500, {axis:'xy'});
		}
		return false;
	});

	// COMMENTS SCROLLBAR INITIALISATION
	if($nym('.opened .comments-scroll').length){
		$nym('.opened .comments-scroll').jScrollPane({showArrows:false});
	}

	// GALLERY JCAROUSEL INIT
	function mycarousel_initCallback(carousel)
	{

		$nym('.previews li a').bind('click', function(){
			var target_li = $nym('#slideshow ul li[rel=' + $nym(this).parent().attr('rel') + ']:first').index() + 1;
			carousel.scroll(jQuery.jcarousel.intval(target_li));
			return false;
		});

		// Disable autoscrolling if the user clicks the prev or next button.
		carousel.buttonNext.bind('click', function() {
			carousel.startAuto(0);
		});

		carousel.buttonPrev.bind('click', function() {
			carousel.startAuto(0);
		});

		// Pause autoscrolling if the user moves with the cursor over the clip.
		carousel.clip.hover(function() {
			carousel.stopAuto();
		}, function() {
			carousel.startAuto();
		});
	};

	function carousel_itemVisibleInCallbackBeforeAnimation(carousel, item, idx, state) {
		var current_li = $nym('#slideshow ul.slides li:eq(' + (idx-1) + ')');
		var current_li_number = parseInt(current_li.attr('rel'));
		$nym('#slideshow ul.previews li').removeClass('selected');
		$nym('#slideshow ul.previews li[rel=' + current_li_number + ']').addClass('selected');
	}

	function gallery_slideshow_carousel_initCallback(carousel){
		$nym('#gallery-previews li a').click(function(){
			$nym('#gallery-previews li').removeClass('selected');
			$nym(this).parent().addClass('selected');
			var target_li = $nym('#gallery-slideshow li[rel=' + $nym(this).parent().attr('rel') + ']:first').index() + 1;
			carousel.scroll(jQuery.jcarousel.intval(target_li));
			return false;
		});
	}

	function gallery_previews_carousel_initCallback(carousel, item, idx, state){

		$nym('#gallery-slideshow li h3, #gallery-slideshow li .image-comment').each(function(){
			var heighter = -($nym(this).height()/2).toFixed(0);
			$nym(this).css({'marginTop':heighter});
		});

		check = function(current_rel){
			carousel.scroll($nym('#gallery-previews li[rel=' + current_rel + ']').index());
		}

	}

	function gallery_slideshow_visiblein_initClallback(carousel, item, idx, state){
		var current_li = $nym('#gallery-slideshow li:eq(' + (idx-1) + ')');
		$nym('#gallery-previews li').removeClass('selected');
		$nym('#gallery-previews li[rel="' + current_li.attr('rel') + '"]').addClass('selected');
		check(parseInt(current_li.attr('rel')));
	}

	jQuery(document).ready(function() {
		if($nym('#slideshow ul.slides').length){
			jQuery('#slideshow ul.slides').jcarousel({
				auto: 2,
				scroll:1,
				wrap: 'last',
				initCallback: mycarousel_initCallback,
				itemVisibleInCallback: {
					onBeforeAnimation: carousel_itemVisibleInCallbackBeforeAnimation
				},
				buttonNextHTML: '<a href="javascript:;"></a>',
				buttonPrevHTML: '<a href="javascript:;"></a>'
			});
		}
		if($nym('#gallery-previews').size()>0){
			jQuery('#gallery-previews').jcarousel({
				scroll:4,
				wrap: 'last',
				initCallback: gallery_previews_carousel_initCallback,
				buttonNextHTML: '<a href="javascript:;"></a>',
				buttonPrevHTML: '<a href="javascript:;"></a>'
			});
		}
		if($nym('#gallery-slideshow').size()>0){
			jQuery('#gallery-slideshow').jcarousel({
				scroll:1,
				wrap: 'last',
				initCallback: gallery_slideshow_carousel_initCallback,
				buttonNextHTML: '<a href="javascript:;"></a>',
				buttonPrevHTML: '<a href="javascript:;"></a>',
				itemVisibleInCallback: {
					onBeforeAnimation: gallery_slideshow_visiblein_initClallback
				}
			});
		}
	});

});

// GALLERY JCAOROUSEL INIT

function gallery_previews_carousel_initCallback(carousel, item, idx, state){

	check = function(current_rel){
		carousel.scroll($nym('#gallery-previews li[rel=' + current_rel + ']').index());
	}

}

jQuery(document).ready(function() {
	if($nym('.gallery .previews').size()>0){
		jQuery('.gallery .previews').jcarousel({
			auto:1,
			scroll:6,
			wrap: 'last',
			initCallback: gallery_previews_carousel_initCallback,
			buttonNextHTML: '<a href="javascript:;"></a>',
			buttonPrevHTML: '<a href="javascript:;"></a>'
		});
	}
});
