$(document).ready(function(){
	$('.fancybox-zoom').fancybox({
			'transitionIn' : 'none', 
			'transitionOut' : 'none', 
			'titlePosition' : 'over', 	
			'titleFormat' : function(title, currentArray, currentIndex, currentOpts) { 
				return '<span id="fancybox-title-over">Zdjęcie ' +  (currentIndex + 1)  + ' / ' + currentArray.length + '</span>'; 	
			}
			});	

	$(".facebook_link").click( function(){
		$("#aplication").css("z-index", "1");
		$("#facebook").css("z-index", "10");
	});	

	$(".application_link").click( function(){
		$("#aplication").css("z-index", "11");
		$("#facebook").css("z-index", "1");
	});
	
	if ( $(".category_item").length > 0){	
		if( $("#lang").val() == 'pl' )
		{
			load_shops( "Moda", '36' );
		}
		if( $("#lang").val() == 'en' )
		{
			load_shops( "Fashion", '148' );
		}
	}	

	$('.category_item').click( function(){
		var header = $(this).text();
		load_shops( header, $(this).attr("id"), '', '0' );
	});	
	
	if ( $(".app_planner_content_list").length > 0){	
		show_note_shops();
	}

	if ( $(".inner_app_content").length > 0){	
		show_only_shops();
	}

	$(".logosy_apla_szpilka").click( function(){
		add_shop(  $(this).attr('id') );		
	});
	
	$(".szpilka_szop").click( function(){
		add_shop(  $(this).attr('id') );		
	});	
	
	// $(window).resize(function () 
	// {
		// initScrollPane();
		// });
});

function activeMenu(url, status)
{
	var url = url;
	var status = status;

	$("#menu > a").each(function(index, value)
		{
			if ($(value).attr("href") == url)
			{
				if (status == true)
				{
					$(value).find('.menu_nazwa1').show();
					$(value).find('.menu_nazwa2').hide();
				}
				else
				{
					$(value).find('.menu_nazwa1').hide();
					$(value).find('.menu_nazwa2').show();
				}
				
				console.log($(value).attr("href"));
			}
		}
	);
}

	function load_shops( header, id )
	{
		$.get( "app_planner.php?show=shop&id=" + id , function(data) {			
			$(".app_shop_content").html( data );
			$(".app_shop_content").scrollTop(0);	
			$(".box2_header").text( header );	

			$.get( "app_planner.php?show=desc&id=" +  $('.app_shop_content div:first').attr('id') , function(data) {
				$(".app_shop_description_content").html( data );
				$(".box3_header").text( $('.app_shop_content div:first').text() );
				$(".app_shop_description_content").scrollTop(0); 
			});				
			
			$('.app_shop_content .shop_item').click( function(){
				var header = $(this).text();
				$.get( "app_planner.php?show=desc&id=" + $(this).attr("id") , function(data) {
					$(".app_shop_description_content").html( data );
					$(".app_shop_description_content").scrollTop(0);
					$(".box3_header").text( header );
					//initScrollPane();
				});
			});

			
			
			$('.app_shop_content .button_add').click( function(){
				var idek = $(this).attr('id');
				add_shop( idek.split( "_" )[1] );
			});
			
			
			
		});
	}
	
	function add_shop( id )
	{
		$.get( "app_planner.php?add=truec&id=" + id , function(data) {
			show_note_shops();	
			show_only_shops();
			//initScrollPane();	
		});		
	}
	
	function show_note_shops()
	{
		$.get( "app_planner.php?show=note" , function(data) {
			$(".app_planner_content_list").html( data );
			
			$('.app_planner_content_list .savebtn').click( function(){
				var idek = $(this).attr('id');
				var note = $( "#n_"+idek ).html();
				save_note( idek, note ); 
			});
			
			$('.app_planner_content_list .delbtn').click( function(){
				var idek = $(this).attr('id');
				del_shop( idek.split( "_" )[1] );
			});	
			
			$('.app_planner_content_list .up').click( function(){
				var idek = $(this).attr('id');
				upshop( idek );
			});		
			$('.app_planner_content_list .down').click( function(){
				var idek = $(this).attr('id');
				downshop( idek );
			});					
			
		});	
	}
	
	function show_only_shops()
	{
		$.get( "app_planner.php?onlyshop=true" , function(data) {
			$(".inner_app_content").html( data );
		
			$('.inner_app_content .remove_shop').click( function(){
				var idek = $(this).attr('id');
				del_shop( idek );
			});	
						
		});	
	}	
	
	function save_note( id, note )
	{
		$.get( "app_planner.php?save=true&id="+id+"&note="+note , function(data) {
			show_note_shops();	
		});			
	}
	
	function del_shop( id )
	{
		$.get( "app_planner.php?del=true&id="+id , function(data) {
			show_note_shops();
			show_only_shops();
		});			
	}

	function upshop( id )
	{
		$.get( "app_planner.php?up=true&id="+id , function(data) {
			show_note_shops();
			show_only_shops();
		});			
	}	
	
	function downshop( id )
	{
		$.get( "app_planner.php?down=true&id="+id , function(data) {
			show_note_shops();
			show_only_shops();
		});			
	}	
	

var shopListScroll = null; 
var shopDescScroll = null; 
var shopQScroll = null;

function initScrollPane()
{
	if (shopListScroll == null)
	{
		shopListScroll = new ScrollPane('.app_shop_content');
	}
	else
	{
		shopListScroll.remove();
		shopListScroll = new ScrollPane('.app_shop_content');
	}
	
	if (shopDescScroll == null)
	{
		shopDescScroll = new ScrollPane('.app_shop_description_content');
	}
	else
	{
		shopDescScroll.remove();
		shopDescScroll = new ScrollPane('.app_shop_description_content');
	}
	
	if (shopQScroll == null)
	{
		shopQScroll = new ScrollPane('.app_planner_content_list');
	}
	else
	{
		shopQScroll.remove();
		shopQScroll = new ScrollPane('.app_planner_content_list');
	}
}

function ScrollPane(content)
{
	var that, content
	
	that = {
		
		mouseX: null,
		mouseY: null,
		
		handlerPercent: null,
		
		handrelMoveStatus: false,
		
		contentTop: null,
		contentLeft: null,
		contentWidth: null,
		contentHeight: null,
		
		ui: {
			content: null,
			scroll: {
				horizontal: {
					panel: null,
					handler: null,
					fix: null
				}
			}
		
		},
		
		init: function()
		{
			that.ui.content = $(content);
			that.contentTop = $(that.ui.content).offset().top;
			that.contentLeft = $(that.ui.content).offset().left;
			that.contentWidth = $(that.ui.content).get()[0].offsetWidth;
			that.contentHeight = $(that.ui.content).get()[0].offsetHeight;
			
			that.ui.scroll.horizontal.panel = document.createElement('div');
			that.ui.scroll.horizontal.handler = document.createElement('div');
			
			var tmpHeight = $(that.ui.content).height();
			$(that.ui.content).scrollTop(0);
			$(that.ui.content).css('height', 'auto');
			that.ui.scroll.horizontal.fix = $(that.ui.content).height() + tmpHeight;
			$(that.ui.content).css('height', tmpHeight + 'px');
			
			that.ui.scroll.horizontal.panel.className = "scroll-panel";
			that.ui.scroll.horizontal.handler.className = "scroll-handler";
			
			$().mousemove(that.setMousePosition);
			$(that.ui.scroll.horizontal.handler).mousedown(that.moveStart);
			$().mouseup(that.moveStop);
			that.create();
		},
		
		create: function()
		{
			$('body').append(that.ui.scroll.horizontal.panel);
			$(that.ui.scroll.horizontal.panel).append(that.ui.scroll.horizontal.handler);
						
			$(that.ui.content).css(
			{
				'overflow': 'hidden'
			});
			
			$(that.ui.scroll.horizontal.panel).css(
			{
				'height': that.contentHeight,
				'position': 'absolute',
				'top': that.contentTop,
				'left': that.contentLeft + that.contentWidth
			});

			$(that.ui.scroll.horizontal.handler).css(
			{
				'position': 'absolute',
				'left': ($(that.ui.scroll.horizontal.panel).width() - $(that.ui.scroll.horizontal.handler).width()) / 2
			});
		},
		
		remove: function()
		{
			$(that.ui.scroll.horizontal.panel).remove();
		},
		
		moveStart: function()
		{
			that.handrelMoveStatus = true;
		},
		
		moveStop: function()
		{
			that.handrelMoveStatus = false;
		},		
		
		setMousePosition: function(e)
		{
			that.mouseX = e.pageX;
			that.mouseY = e.pageY - that.contentTop;
			
			if (that.handrelMoveStatus == true)
			{
				that.handlerPercent = ($(that.ui.scroll.horizontal.handler).position().top / ($(that.ui.scroll.horizontal.panel).height())) * 100;
				$(that.ui.content).scrollTop((that.handlerPercent / 100) * that.ui.scroll.horizontal.fix);
				$(that.ui.scroll.horizontal.handler).css('top', that.mouseY);
			}
			
			if ($(that.ui.scroll.horizontal.handler).position().top > that.contentHeight - $(that.ui.scroll.horizontal.handler).height())
			{
				$(that.ui.scroll.horizontal.handler).css('top', that.contentHeight - $(that.ui.scroll.horizontal.handler).height());
			}
			
			if ($(that.ui.scroll.horizontal.handler).position().top < 0)
			{
				$(that.ui.scroll.horizontal.handler).css('top', 0);
			}
		}
		
		
	};
	
	that.init();
	return that;
}

