$(document).ready(
	$(function() {
		//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

		// toggles the slickbox on clicking the noted link  
  $('#nav-szukaj').click(function() {
    $('#ukryte').toggle(400);
    return false;
  });
  
  $('#search').click(function() {
	$('#search').val('');
	return false;
  });
	
  $("button").click(function () {
		var src = "http://www.youtube.com/watch?v=" + $(this).val();
		var srchtml = $(this).attr('data-val');
		$("#player").html(function() {
			$('#player').youTubeEmbed({
				video	: src,
				width	: 620,
				height	: 355
			});
		});
		$("#facebook").html('<iframe src="http://www.facebook.com/plugins/like.php?href='+srchtml+'&amp;send=false&amp;layout=box_count&amp;width=260&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;font=tahoma&amp;height=90&amp;appId=159059820784115" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:80px; height:70px;float:right;position: relative;left: 10px;top: 270px;" allowTransparency="true"></iframe>');
	});
  
  $(function(){
	    var url = $('#hide').attr('data-val');
      	if( url == 'wideo'){
      		$('#nav-wideo a').removeClass('non');
      		$('#nav-wideo a').addClass('active');
	    }
      	else if( url == 'audio'){
      		$('#nav-audio a').removeClass('non');
      		$('#nav-audio a').addClass('active');
	    }
      	else if( url == 'foto'){
      		$('#nav-foto a').removeClass('non');
      		$('#nav-foto a').addClass('active');
	    }
      	else if( url == 'faq'){
      		$('#nav-faq a').removeClass('non');
      		$('#nav-faq a').addClass('active');
	    }
      	else if( url == 'slownik'){
      		$('#nav-slownik a').removeClass('non');
      		$('#nav-slownik a').addClass('active');
	    }
	    
	});
	$(function(){
      	var str = $('#sort_hide').attr('data-val');
      	if (str != null) { 
	      	var substr = str.split('&');
	      	if( substr[0] == 'tekst'){
	      		$('#tekst a').addClass('active');
	      		$('#tekst').addClass('active');
		    } else if( substr[0] == 'wideo'){
	      		$('#wideo a').addClass('active');
	      		$('#wideo').addClass('active');
		    } else if( substr[0] == 'all'){
	      		$('#all a').addClass('active');
	      		$('#all').addClass('active');
		    }
	      	if( substr[1] == 'tematycznie'){
	      		$('#tematycznie a').addClass('active');
	      		$('#tematycznie').addClass('active');
		    } else if( substr[1] == 'date'){
	      		$('#date a').addClass('active');
	      		$('#date').addClass('active');
		    } else if( substr[1] == 'program'){
	      		$('#program a').addClass('active');
	      		$('#program').addClass('active');
		    }
      	}
	});
	
	$(function() {
		// Hide all
		$('.hide-enabled').hide();
		$('.wideo').hide();
		$('.audio').hide();
		$('.foto').hide();
		
		$('.click-enabled').click(function() {
			$('.hide-enabled').hide();
			$(this).next('.hide-enabled').show();
		});

		$('#categories > ul').filter(function (index) { 
		    if ($.trim($(this).children().text()) == "" )
		    	$(this).parent("#categories").remove();
		});
	});
	
	$(function() {
		flashMovie = $('#flashContent');

		flashMovie.flash(
			{
				swf: 'http://omultimediach.pl/wp-content/themes/oMultimediachPL/images/omultimediach.swf',
				width: 600,
				height: 350,
				flashvars: {
					clickTARGET: '_self',
					clickTAG: 'konkurs'
				}
			}
		);
	});
	
	$(function() {
		$('#reklama-panel-toggle > p#open').click(function() {
			$('#czesciowa').fadeIn('fast');
			$("#reklama-panel-toggle p").toggle();
		});
		
		$('#reklama-panel-toggle > p#close').click(function() {
			$('#czesciowa').fadeOut('fast');
			$("#reklama-panel-toggle p").toggle();
		});

	});
	

}));
