/* ----------------------------------------------------------------------------------------------------------
*
*	JAVASCRIPT INIT
*	Michal Pelech (www.michalpelech.cz, info@michalpelech.cz)
*
/* ---------------------------------------------------------------------------------------------------------- */

$(document).ready(function(){

	// Cufon replace
	Cufon.replace('#nav a, ul.tabs li a, #content h1, #content h2, #content h3', { hover: true });
	Cufon.replace('.weather .header, #aside h2, .header span');
	
	// Set news slider width
	var sliderWidth = 0;
	$('.news-slider .news-item').each(function(){
		sliderWidth += $(this).width() + 20;
	});
	$('.news-slider').css('width', sliderWidth + 'px');

	// info-panel ul
	$('.info-panel > ul.top > li:last').addClass('last');
	
	// search panel active
	$('.search input[type="text"], .contact-form input[type="text"], .contact-form textarea').focus(function(){
		$(this).parent().addClass('active');
	});
	$('.search input[type="text"], .contact-form input[type="text"], .contact-form textarea').blur(function(){
		$(this).parent().removeClass('active');
	});
	
	// Paging centering
	var pagingWidth = 0;
	$('.paging a').each(function(){
		pagingWidth += $(this).outerWidth();
	});
	$('.paging').css('width', (pagingWidth + 50) + 'px');
	
	// Gallery slider
	//if ($('.photogallery-content .slider ul li').length > 4) {
		$(".photogallery-content .slider").jcarousel({
			scroll: 1
		});
	//}
	
    $(".header-slider").jcarousel({
		auto: 5,
        wrap: 'last',
		animation: 'slow',
		scroll: 1,
        initCallback: initialize,
		itemLoadCallback: change,
        // This tells jCarousel NOT to autobuild prev/next buttons
        buttonNextHTML: null,
        buttonPrevHTML: null
    });

	// Header slider
	var headerSliedrWidth = 0;
	$('.slider-control a').each(function(){
		headerSliedrWidth += 15;
	});
	$('.slider-control').css({
		'width': headerSliedrWidth + 'px',
		'margin-left': '-' + (headerSliedrWidth/2) + 'px'
	});
	
	// PrettyPhoto
	$("a[rel^='prettyWhy']").prettyPhoto({
		theme: 'why-live',
		default_width: 745,
		allow_resize: false,
		fixed_height: 130
	});
	$("a[rel^='prettyCam']").prettyPhoto({
		theme: 'webcam',
		allow_resize: false
	});
});

function initialize(carousel) {
    jQuery('.slider-control a').bind('click', function() {
        carousel.scroll(jQuery.jcarousel.intval(jQuery(this).text()));
        return false;
    });
};

function change(carousel) {
    for (var i = carousel.first; i <= carousel.last; i++) {
		$('.slider-control a').each(function(){
			if ($(this).html() == i){
				$(this).addClass('active');
			} else {
				$(this).removeClass('active');
			}
		});
    }
}

function slider() {
	//scrollpane parts
	var scrollPane = $( ".scroll-pane" ),
		scrollContent = $( ".scroll-content" );
	
	//build slider
	var scrollbar = $( ".scroll-bar" ).slider({
		slide: function( event, ui ) {
			if ( scrollContent.width() > scrollPane.width() ) {
				scrollContent.css( "margin-left", Math.round(
					ui.value / 100 * ( scrollPane.width() - scrollContent.width() )
				) + "px" );
			} else {
				scrollContent.css( "margin-left", 0 );
			}
		}
	});
	
	//append icon to handle
	var handleHelper = scrollbar.find( ".ui-slider-handle" )
	.mousedown(function() {
		scrollbar.width( handleHelper.width() );
	})
	.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 remainder = scrollContent.width() - scrollPane.width();
		var proportion = remainder / scrollContent.width();
		var handleSize = scrollPane.width() - ( proportion * scrollPane.width() );
		scrollbar.find( ".ui-slider-handle" ).css({
			width: handleSize,
			"margin-left": -handleSize / 2
		});
		handleHelper.width( "" ).width( scrollbar.width() - handleSize );
	}
	
	//reset slider value based on scroll content position
	function resetValue() {
		var remainder = scrollPane.width() - scrollContent.width();
		var leftVal = scrollContent.css( "margin-left" ) === "auto" ? 0 :
			parseInt( scrollContent.css( "margin-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.width() + parseInt( scrollContent.css( "margin-left" ), 10 );
			var gap = scrollPane.width() - showing;
			if ( gap > 0 ) {
				scrollContent.css( "margin-left", parseInt( scrollContent.css( "margin-left" ), 10 ) + gap );
			}
	}
	
	//change handle position on window resize
	$( window ).resize(function() {
		resetValue();
		sizeScrollbar();
		reflowContent();
	});
	//init scrollbar size
	setTimeout( sizeScrollbar, 10 );//safari wants a timeout
};
