/**
 * Control main function
 * 
 */

$(document).ready(function() {
		var speed = 450;
		
		$('.section-list').click(function() {
			var selected = $(this);
			selected.removeClass('section-list');
			var active = $('.section-active');		
			$('.section-list').removeClass('section-active');
			selected.addClass('section-active');
			//lang změnit za id, vyzaduje zmenu v ccs a frontend
			var url = selected.attr('lang');
			var params = url.split('/');
			if ($('#menu-wrapper').is(":hidden")) 
			{
				$('#menu-wrapper').load('/menu/index/section/' + params[1] + '/page/' + params[4], function() {
					$('#menu-wrapper').slideDown(speed, function() {
						loadContent(url, true);
						$('#category-wrapper').slideDown(speed);
					});
				});
			} 
			else 
			{			
				$('#menu-wrapper').slideUp(speed, function() {
					$('#category-wrapper').hide();
					if (typeof active.attr('id') !== "undefined") 
					{
						if (selected.attr('id') !== active.attr('id')) 
						{
							$('#menu-wrapper').empty();
							$('#menu-wrapper').load('/menu/index/section/' + params[1] + '/page/' + params[3], function() {
								$('#menu-wrapper').slideDown(speed, function() {
									loadContent(url, true);
									$('#category-wrapper').slideDown(speed);
								});
							});
						}
					}
				});
			}
			selected.addClass('section-list');
		});

		
		$('a[href]', $('#subsection-wrapper')).live('click', function (event) {
			$('.subsection-item').removeClass('active');
			$(this).addClass('active');
			var href = $(this).attr('href');
			var params = href.split('/');
			if (!/[:#]/.test(href)) {
					$('#category-wrapper').slideUp(speed, function() {
						$('#category-wrapper').empty();
						$('#category-wrapper').load('/menu/subsection/section/' + params[1] + '/subsection/' + params[2] + '/page/' + params[4], function() {
							loadContent(href, true);
							$('#category-wrapper').slideDown(speed);							
						});
					});
					return false;			
			}
		});
		
		$('a[href]', $('#category-wrapper')).live('click', function (event) {
			$('.category-item').removeClass('active');
			$(this).addClass('active');
			var href = $(this).attr('href');
			var params = href.split('/');
			if (!/[:#]/.test(href)) {
				loadContent(href, true);
				return false;			
			}
		});
		
		$('a[href]', $('.paginationControl')).live('click', function (event) {
			var href = $(this).attr('href');
			var params = href.split('/');
			if (!/[:#]/.test(href)) {
				loadContent(href, true);
				return false;			
			}
		});
		
		if (history.pushState) {
			var ajaxState = 0;
		}
			
		function loadContent(url, push) {
			
			if (history.pushState) {
				ajaxState++;
				loader();
				$('#ajax-content').load(url, function (text, status, xhr) {
					if (status == 'success') {
						var title = xhr.getResponseHeader('title');
						document.title = title;
						if (push) {
							scrollTo(0, 0);
							history.pushState('', title, url);
						}
					}
					loader();
				});
			//}

			} else
				window.location = url;
		}
		
		onpopstate = function (event) {
			if (ajaxState && !/#/.test(location.href)) {
				loadContent(location.href);
			}
		};

		$('.filter-toggle').live('click', function()
		{
			//$(this).toggleClass('filter-show', true);
			$('.filter-box-toggled').slideToggle();
			
			if($(this).is('.filter-show'))
			{
				$(this).removeClass('filter-show');
			} else
			{
				$(this).addClass('filter-show');
			}	 
		});	
		
				
	   $('#fsection').change(function() {
		      $('.section-1').attr('disabled', 'disabled');
		      $('.section-2').attr('disabled', 'disabled');
		      $('.section-3').attr('disabled', 'disabled');
		      if ($(this).val() > 0) {
		         $('#fsubsection').attr('disabled', '');
		         $('.section-' + $(this).val()).attr('disabled', '');  
		      } else {
		         $('#fsubsection').attr('disabled', 'disabled');
		         $('#fsubsection').val(0); 
		      }
	   });
	   
	   $("#label-asc").click(function() {
			$("#label-desc").removeClass('active');
			$("#label-asc").addClass('active');
			$("#asc").attr('checked', true);
	   });

	   $("#label-desc").click(function() {
			$("#label-asc").removeClass('active');
			$("#label-desc").addClass('active');
			$("#desc").attr('checked', true);
	   });
	
    $('#revert-pass').blur(function() {
    	var email = $(this).val();
    	var link = $('#forgotten-password-link').attr('href');
    	if (email != '') {
    		$.get('/account/check-email/from/registration/email/' + $(this).val(), function(data) {
    			if (data) {    				
    				$('.revert-pass').slideDown(50);
    				$('#forgotten-password-link').attr('href', link + '/' + email);  
    			}
    			else
    				$('.revert-pass').slideUp(50);
    			return false;
    		});
    	} else 
    		$('.revert-pass').slideUp(50);
	});
	
	//set/remove default input values
	var clearMePrevious = "";
	// clear input on focus
	$(".clearMeFocus").focus(function() {
		if($(this).val()==$(this).attr("title")) {
			clearMePrevious = $(this).val();
			$(this).val("");
		}
	});
	// if field is empty afterward, add text again
	$(".clearMeFocus").blur(function() {
		if($(this).val()=="") {
			$(this).val(clearMePrevious);
		}
	});

	//loading games on homepage
	offsetOnline = 0;
	offsetDownload = 0;
	offsetMobile = 0;
   $('.best-game-button').click(function() {
      if ($(this).hasClass('active') == false) {
	      $(this).next('button').removeClass('active');
         $(this).addClass('active');
         var box = $(this).attr('id'); 
         var next = $('.' + box).next('div');
         $('.' + box).fadeOut(100, function(){
        	 next.fadeIn(100);
         });
      }  
         
   });

   $('.most-played-button').click(function() {
	   if ($(this).hasClass('active') == false) {
	     $(this).prev('button').removeClass('active');
	     $(this).addClass('active');
         var box = $(this).attr('id');
         var prev = $('.' + box).prev('div');
         //alert($('.' + box).prev('div').attr('class')); 
         $('.' + box).fadeOut(100, function(){
        	   prev.fadeIn(100);
         });
	   }       
   });


   $('#more-online').click(function() {
	   $('.more-online').addClass('loading');   
	   offsetOnline = offsetOnline + 6;
      $("#next-online-games").append($("<div>").load('/index/more-games/offset/' + offsetOnline + '/section/1', function() {
    	  $('.more-online').removeClass('loading');   
      }));  
   });

   $('#more-mobile').click(function() {
	   $('.more-mobile').addClass('loading'); 
	   offsetMobile = offsetMobile + 6;
      $("#next-mobile-games").append($("<div>").load('/index/more-games/offset/' + offsetMobile + '/section/3', function() {
    	  $('.more-mobile').removeClass('loading');   
      }));  
	});

   $('#more-download').click(function() {
	   $('.more-download').addClass('loading'); 
      offsetDownload = offsetDownload + 6;
      $("#next-download-games").append($("<div>").load('/index/more-games/offset/' + offsetDownload + '/section/2', function() {
    	  $('.more-download').removeClass('loading');   
      }));  
	});
});

function loader() {

}

