var _cw;
var _ml;
var _tw;
var _pw;

var _cw2;
var _ml2;
var _tw2;

var _aslp = 0;

var _avl = 0;

jQuery(window).load(function(){

    if (jQuery("#otherPosts").size() == 1) {

	// Compute Banner inner size
	var _posts = jQuery("#otherPosts div.hentry");
	var _l = jQuery(_posts).size();
	var _w = jQuery(_posts).outerWidth();

	_tw = _l*_w;
	_cw = _w;

	_ml = (_tw - jQuery("#otherPosts").outerWidth())*-1;

	_pw = jQuery("#otherPosts").outerWidth();

	//vars
	var conveyor = jQuery(".content-conveyor", jQuery("#sliderContent")),
	item = jQuery(".hentry", jQuery("#sliderContent"));

	//set length of conveyor
	conveyor.css("width", _tw);

	// Banner initial position
	var _oapost = jQuery("#otherPosts .active").position();
	
	if (_oapost.left > _pw && _oapost.left < (_tw - _pw)) {
	    jQuery("#otherPosts .inner").css('left',(-_oapost.left + _w * 3)+'px');
	    _avl = Math.abs(_oapost.left * 100 / _ml);
	} else if (_oapost.left > (_tw - _pw)) {
	    jQuery("#otherPosts .inner").css('left', _ml+'px');
	    _avl = 100;
	}

	if (_tw > _pw) {

	    //scrollpane parts
	    var scrollPane = jQuery( "#sliderContent" ),
	    scrollContent = jQuery( ".content-conveyor" );

	    //build slider
	    var scrollbar = jQuery( "#slider" ).slider({
		slide: function( event, ui ) {
		    if ( scrollContent.outerWidth() > ( scrollPane.outerWidth() - 10 ) ) {
			scrollContent.css( "left", Math.round(
			    ui.value / 100 * _ml
			    ) + "px" );
		    } else {
			scrollContent.css( "left", 0 );
		    }
		},
		value: _avl
	    });

	    //append icon to handle
	    var handleHelper = scrollbar.find( ".ui-slider-handle" )
	    .mousedown(function() {
		scrollbar.width( handleHelper.outerWidth() );
	    })
	    .mouseup(function() {
		scrollbar.width( "100%" );
	    })
	    .append( "<span class='ui-icon ui-icon-grip-dotted-vertical'></span>" )
	    .wrap( "<div class='ui-handle-helper-parent'></div>" ).parent();

	    //change overflow to hidden now that slider handles the scrolling
	    scrollPane.css( "overflow", "hidden" );

	    //size scrollbar and handle proportionally to scroll distance
	    function sizeScrollbar() {
                var scrollSize = scrollPane.outerWidth() - 100;
		var remainder = Math.abs(scrollContent.outerWidth() - scrollSize);
		var proportion = remainder / scrollContent.outerWidth();
		var handleSize = scrollSize - ( proportion * scrollSize );
		scrollbar.find( ".ui-slider-handle" ).css({
		    width: handleSize,
		    "margin-left": -handleSize / 2
		});
		handleHelper.width( "" ).width( scrollbar.outerWidth() - handleSize );
	    }

	    //reset slider value based on scroll content position
	    function resetValue() {
		var remainder = scrollPane.outerWidth() - scrollContent.outerWidth();
		var leftVal = scrollContent.css( "left" ) === "auto" ? 0 :
		parseInt( scrollContent.css( "left" ) );
		var percentage = Math.round( leftVal / remainder * 100 );
		scrollbar.slider( "value", percentage );
	    }

	    //if the slider is 100% and window gets larger, reveal content
	    function reflowContent() {
		var showing = scrollContent.outerWidth() + parseInt( scrollContent.css( "left" ), 10 );
		var gap = scrollPane.outerWidth() - showing;
		if ( gap > 0 ) {
		    scrollContent.css( "left", parseInt( scrollContent.css( "left" ), 10 ) + gap );
		}
	    }

	    //change handle position on window resize
	    jQuery( window ).resize(function() {
		resetValue();
		sizeScrollbar();
		reflowContent();
	    });
	    //init scrollbar size
	    setTimeout( sizeScrollbar, 10 );//safari wants a timeout
	}

	jQuery("#sliderContent .Prev, #sliderContent .Next").click(function(){
	    if (_tw > _pw) {

		var dir = 1;
		if (jQuery(this).hasClass('Next'))
		    dir = -1;

		var _clp;

		if (jQuery("#otherPosts .inner:animated").length == 0) {
		    var _cp = jQuery("#otherPosts .inner").position();
		    _clp = _cp.left;
		} else {
		    _clp = _aslp;
		}

		var _nlp = _clp + (dir * _cw);

		if (_nlp > 0)
		    _nlp = 0;

		if (_nlp < _ml)
		    _nlp = _ml;

		_aslp = _nlp;

		jQuery("#otherPosts .inner").stop().animate({
		    left: _nlp+'px'
		},500,"linear");
		// Update slider
		jQuery("#slider").slider("option", "value", Math.abs(_nlp * 100 / _ml));
	    }
	});

    }




    if (jQuery(".Gallery").size() == 1) {

	// Compute Banner inner size
	_posts = jQuery(".Gallery .ngg-gallery-thumbnail-box");
	_l = jQuery(_posts).size();
	_w = jQuery(_posts).outerWidth();

	_tw2 = _l*_w;
	_cw2 = _w;

	_ml2 = (_tw2 - jQuery(".Gallery").width())*-1;

	jQuery(".Gallery .inner").width(_tw2);

	jQuery(".GalleryPrev, .GalleryNext").click(function(){
	    if (_tw2 > jQuery(".Gallery").width()) {
		var dir = 1;
		if (jQuery(this).hasClass('GalleryNext'))
		    dir = -1;

		var _cp = jQuery(".Gallery .inner").position();
		var _clp = _cp.left;

		var _nlp = _clp + (dir * _cw2);

		if (_nlp > 0)
		    _nlp = 0;

		if (_nlp < _ml2)
		    _nlp = _ml2;

		jQuery(".Gallery .inner").stop().animate({
		    left: _nlp+'px'
		},500,"linear");
	    }
	});
    }

    jQuery("a#HeaderNewsLink").click(function(){
	jQuery("#HeaderNewsInner").toggle();
    });
    jQuery("a#HeaderNewsInnerClose").click(function(){
	jQuery("#HeaderNewsInner").hide();
    });
});

var launchLightbox = function(hrf) {
   if (hrf == "null") {
      hrf = "";
   }
    var elements = jQuery("a[rel=\"lightbox\\[rea\\]\"][href$=\""+hrf+"\"]");
    if (elements.length == 0) {
       elements = jQuery("a[rel=\"lightbox\\[rea\\]\"]");
    }
    myLightbox.start(elements[0]);
}

