jQuery(document).ready(function(){

	/* Carousel */
	if($('#carousel li').length > 1){
		$('#carousel').infiniteCarousel({	
			transitionSpeed : 400,
			displayTime : 3000,		
			displayProgressBar : 0,
			textholderHeight : 0.25,
			displayThumbnails  : 1,
			thumbnailFontSize : '14px'
		});
	}
	
	/* Subnav */
	$('#subnav a').addClass('sublink');
	
	$("#subnav li:not(.subnavActive)").prepend('<span></span>'); 

	$("#subnav li").each(function() { 
		var linkText = $(this).find('a').html(); 
		var linkDirection = $(this).find('a').attr('href');
		$(this).find("span").show().html(linkText)
		.click(function(){
			location.href = linkDirection;
		}); 
	}); 

	$("#subnav li").hover(function() {	
		$(this).find("span").stop().animate({
			marginTop: "-30" 
		}, 200);
	} , function() { //On hover out...
		$(this).find("span").stop().animate({
			marginTop: "0"  
		}, 200);
	});

	$('#search_keywords').autocomplete( config.defaults.autocomplete.url, {
		delay: 1,
		minChars:2,		
		matchSubset:1,
		matchContains:1,
		extraParams: {
			type: 'keywords',
			limit: '10',
			dataType: 'plain'
		},
		minChars: 2 
	});
	
	$('#search_submit').mouseover(function(){		
		$(this).addClass('hsub');
	})
	.mouseout(function(){
		$(this).removeClass('hsub');
	})
	.click(function(){
		$('#search_form').submit();
	});
	
	$('#nSugg-x').click(function(){
		nbox.show(true);
	});
	
	$('#nSugg').mouseover(function(){		
		$(this).removeClass('imSuggv').addClass('imSuggh');
	})
	.mouseout(function(){
		$(this).removeClass('imSuggh').addClass('imSuggv');
	})
	.click(function(){		
		nbox.show();
	});
	
	$('#nSuggest').keyup(function(e){
		if (e.keyCode == 13){
			if(nSuggest.link){
				location.href = nSuggest.link;
				return false;
			}
		};
	
		if(nSuggest.progress){
			return false;
		}	
		var v = $(this).val();
		v = v.trim();
		if(v.length < 2){
			nSuggest.displayList();
			return false;
		}
		
		var r = {
			q: v,
			dataType: 'json',
			limit: '6',
			type: 'names'
		};
		
		$.ajax({
			type: "GET",
			url: config.defaults.autocomplete.url,
			data : r,
			dataType: 'json',
			beforeSend: function(){
				if(nSuggest.progress){
					return false;
				}
				nSuggest.progress = true;
			},
			success : function(resp,status){
				nSuggest.showResults(resp);
				nSuggest.progress = false;
			},
			error : function(){
				nSuggest.progress = false;
			}
		});
	})
	.focus(function(){
		$(this).addClass('suggActiv').val('');
	})
	.blur(function(){
		$(this).removeClass('suggActiv').val(nSuggest.label);
	});
	
	$('#gSugg-x').click(function(){
		$('#glist').scrollTo('#gl-0', 300);
		$('#gSugg-b2').fadeOut("fast", function(){
			$('#gSugg-b1').fadeIn("fast");
		});
	});
	
	$('#gSugg').mouseover(function(){
		if(gBox.status == 'c'){
			return false;
		}
		$(this).removeClass('imSuggv').addClass('imSuggh');
	})
	.mouseout(function(){
		if(gBox.status == 'c'){
			return false;
		}
		$(this).removeClass('imSuggh').addClass('imSuggv');
	})
	.click(function(){
		if(gBox.status == 'e' || gBox.status == 'c'){
			return false;
		}
		$('#gSugg-b1').fadeOut("fast", function(){
			$('#gSugg-b2').fadeIn("fast");
		});
	});

	$('#gList').mouseover(function(){
		if(gBox.status == 'c'){
			return false;
		}
		$(this).removeClass('imListv').addClass('imListh');
	})
	.mouseout(function(){
		if(gBox.status == 'c'){
			return false;
		}
		$(this).removeClass('imListh').addClass('imListv');
	})
	.click(function(){
		var status = gBox.status;
		if(status == 'c'){
			return false;
		}
		if(!gBox.init){
			gBox.init = true;
			var num = $('#gbox li').length;
			var bth = $('#gbox .boxt').height();			
			var h = num*30;			
			var bH = h + bth + 5;			
			gBox.lExtH = h;
			gBox.bExtH = bH;
		}
		
		status = status == 'o' ? 'e' : 'o';
		
		var box, list;
		switch(status){
			case 'o':
				box = gBox.bOpenH;
				list = gBox.lOpenH;
				$('#gSugg').removeClass('imSuggc').addClass('imSuggv');
			break;
			case 'e':
				box = gBox.bExtH;
				list = gBox.lExtH;
				$('#gSugg').removeClass('imSuggv').removeClass('imSuggh').addClass('imSuggc');
			break;
		};
		gBox.makeSize(box,list);
		gBox.status = status;
	})
	.rightClick(function(){
		var status = gBox.status;
		if(status != 'c'){
			status = 'c';
			gBox.makeSize(0,0, true);
			$(this).removeClass('imListv').removeClass('imListh').addClass('imListc');
			$('#gSugg').removeClass('imSuggv').removeClass('imSuggh').addClass('imSuggc');
		}else{
			status = 'o';
			gBox.makeSize(gBox.bOpenH,gBox.lOpenH, false);
			$(this).removeClass('imListc').addClass('imListv');
			$('#gSugg').removeClass('imSuggc').addClass('imSuggv');
		}
		gBox.status = status;
	});
	
	gSuggest.fillList($('#glist li').length);
	$('#gSuggest').keyup(function(e){
		if (e.keyCode != 13) {
	    	gSuggest.suggest($(this).val());
	    }else{
	    	gSuggest.redirect();
	    }
	})	
	.focus(function(){
		$(this).addClass('suggActiv').val('');
	})
	.blur(function(){
		$(this).removeClass('suggActiv').val(gSuggest.label);
	});
});

var nbox = function(){
	return{
	status : 'c',
	bOpenH: 220,
	lOpenH: 183,
	show : function(h){
		var box, list;
		box = h ? 34 : this.bOpenH;
		$('#nbox').animate({
			height: box
		});
		if(h){
			$('#nSugg-b2').fadeOut("fast",function(){
				$('#nSugg-b1').fadeIn("fast");
			});		
			$('#nboxm').slideUp("fast");
			$('#nboxd').slideUp("fast");
			this.status = 'c';
		}else{
			$('#nSugg-b1').fadeOut("fast",function(){
				$('#nSugg-b2').fadeIn("fast");
			});
			$('#nboxm').slideDown("fast");
			$('#nboxd').slideDown("fast");
			this.status = 'o';
		}
	}
	}
}();

var nSuggest = function(){
	return{	
	label : 'Podaj nazwę turnieju.',
	progress : false,
	showResults : function(res){
		if(res.length){
			$('#nlist ul').html(this.createList(res));
			this.displayList(true);
			this.linkTmp = res[0][1];
		}else{
			this.displayList();
		}
	},
	createList : function(data){
		var list = [];
		var l = data.length;		
		for(var i=0;i<l;i++){
			var cls = i%2 ? 'odd' : 'even';
			list.push('<li class="'+ cls +'"><div class="tbox m_t_5"><a href="'+ data[i][1] +'">'+ data[i][0] +'</a></div></li>');
		}
	  return list.join("");
	},
	clearList : function(){
		$('#nlist ul').html('');
	},
	displayList : function(s){
			window.clearTimeout(this.cTimer);
			window.clearTimeout(this.dcTimer);			
			this.dcTimer = {};this.cTimer = {};
		if(s){
			this.dcTimer = window.setTimeout("nSuggest.display()", this.timer);
		}else{
			this.dcTimer = window.setTimeout("nSuggest.hide()", this.timer);
		}
		window.setTimeout("nSuggest.check();", 500);
	},
	display : function(){
		nSuggest.link = nSuggest.linkTmp;
		nSuggest.progress = true;
		$('#emptnlist').slideUp("fast", function(){			
			$('#nlist').slideDown("fast", function(){				
				nSuggest.progress = false;
			});
		});		
	},
	hide : function(){
		nSuggest.link = '';
		nSuggest.progress = true;
		$('#nlist').slideUp("fast", function(){			
			$('#emptnlist').slideDown("fast", function(){
				nSuggest.progress = false;
			});
		});
	},
	check : function(){
		if(this.checkStatus){
			this.checkStatus = false;
			return false;
		}
		this.checkStatus = true;
		$('#nSuggest').keyup();
	},
	timer : 250,
	dcTimer : {},
	cTimer : {},
	checkStatus : false,
	link : '',
	linkTmp : ''
	}
}();


var glist = [];
var gSuggest = function(){

	return{
	label: 'Podaj nazwę gry.',
	suggest : function(v){
	    v = v.trim();
	    if(!v){
	    	this.id = '';
	    	this.link = '';
	    	return false;
	    }
	    v = v.toLowerCase();
	    var f = false;
	    var l = v.length;
	    for(var i=0;i<this.items;i++){
	    	if(glist[i][0].substr(0, l) == v){
	    		this.link = glist[i][1];
	    		var id = i;
	    		f = true;
	    		break;
	    	};
	    }
	    if(!f){
	    	this.link = '';	    	
	    }else{
	    	
	    	$('#glist').scrollTo('#gl-' + id, 300);
	    }
	},
	fillList : function(n){
		this.items = n;
		for(var i=0;i<n;i++){
			glist[i] = [];
			glist[i][0] = $('#gl-'+ i +' a').text().toLowerCase();
			glist[i][1] = $('#gl-'+ i +' a').attr('href');
		}
	},
	redirect : function(){
		if(this.link){
			location.href = this.link;
		}
	},
	items: 0,
	link : ''
	}
}();

var gBox = function(){
	return{
	init: false,
	status: 'o',
	bExtH: '',
	lExtH: '',
	bOpenH: 220,
	lOpenH: 183,
	makeSize : function(box, list, hide){
		if(hide){
			box = 34,
			list = 0;
		}
		$('#gbox').animate({
			height: box
		});
		$('#glist').animate({
			height: list
		});
		if(hide){
			$('#gboxd').slideUp();
		}else{
			$('#gboxd').slideDown();
		}
	}
	}
}();



String.prototype.trim = function(){
    var re = /^\s+|\s+$/g;
    return function(){ return this.replace(re, ""); };
}();


/** Right click extension **/
if(jQuery) (function(){
	
	$.extend($.fn, {
		
		rightClick: function(handler) {
			$(this).each( function() {
				$(this).mousedown( function(e) {
					var evt = e;
					$(this).mouseup( function() {
						$(this).unbind('mouseup');
						if( evt.button == 2 ) {
							handler.call( $(this), evt );
							return false;
						} else {
							return true;
						}
					});
				});
				$(this)[0].oncontextmenu = function() {
					return false;
				}
			});
			return $(this);
		},		
		
		rightMouseDown: function(handler) {
			$(this).each( function() {
				$(this).mousedown( function(e) {
					if( e.button == 2 ) {
						handler.call( $(this), e );
						return false;
					} else {
						return true;
					}
				});
				$(this)[0].oncontextmenu = function() {
					return false;
				}
			});
			return $(this);
		},
		
		rightMouseUp: function(handler) {
			$(this).each( function() {
				$(this).mouseup( function(e) {
					if( e.button == 2 ) {
						handler.call( $(this), e );
						return false;
					} else {
						return true;
					}
				});
				$(this)[0].oncontextmenu = function() {
					return false;
				}
			});
			return $(this);
		},
		
		noContext: function() {
			$(this).each( function() {
				$(this)[0].oncontextmenu = function() {
					return false;
				}
			});
			return $(this);
		}
		
	});
	
})(jQuery);	
