
/*
* Coded by Orhaan
*/
	
(function($){
	$.fn.clearDefault = function(){
		return this.each(function(){
			var default_value = $(this).attr('title');
			$(this).val(default_value);
			$(this).focus(function(){
				if ($(this).val() == default_value) $(this).val("");
			});
			$(this).blur(function(){
				if ($(this).val() == "") $(this).val(default_value);
			});
		});
	};
})(jQuery);

$(document).ready(function(){
	var videoPos = $("#video-box").position();
	var signUpUri = $("#signup-box a").attr("href");
	
	// input hints hide on focus stuff
	$("#login-box input, .hint-input").clearDefault();
	
	/*
	* Games pop-up menu
	*/
	$('#games-menu').hover(
		function(){
			//$('#games-list').slideDown(500);
			$('#games-list').stop(true, true).animate({opacity: 'show', height: 'show'}, 400, "swing");
			if (typeof(videoPos) != "undefined") {
				$('#video-inner').css('visibility', 'hidden');
			}
		},
		function(){
			var pos = $(window).scrollTop();
			$('#games-list').animate({opacity: "hide", height: "hide"}, 200, "swing");
			if (typeof(videoPos) != "undefined") {
				if (pos > videoPos.top) {
					$('#video-inner').css('visibility', 'hidden');
				}
				else {
					$('#video-inner').css('visibility', 'visible');
				}
				
			}
		}
	);
	
	/*
	* Download Button atractions
	*/

	$('#dllink, #dllink2').click(function() {
		//$this = $('#download-icon');
		var uri = $(this).attr("href")+'ajax_1/';
		//$this.text('Yükleniyor...').wrapInner("<span></span>").fadeIn(500);
		$.getJSON(uri,
			function(data){
				if (data.result == -1) { // not-logged-in
					if (data.err == "notlogon")
					{
						if (confirm(data.errMsg))
						{
							document.location = signUpUri;
						}
					}
					else
					{
						alert(data.errMsg);
					}
				}
				else
				{
					//alert(data.url);
					document.location = data.url;
				}
				
		});
		return false;
	});
	
	
	/*
	* Top fixed menu video box manipulations
	*/
	//var bigBrother = ($.browser.msie == true && $.browser.version < 8) ? body : window;
	//alert(bigBrother.scrollTop());
	$(window).scroll(function() {
		var pos = $(window).scrollTop();
		if (jQuery.browser.msie === true && parseInt(jQuery.browser.version.substr(0,1)) < 7) {
			$('#topfixedmenu').css('top', pos+'px');
		}
		if (typeof(videoPos) != "undefined") {
			if (pos > videoPos.top) {
				$('#video-inner').css('visibility', 'hidden');
			}
			else {
				$('#video-inner').css('visibility', 'visible');
			}
		};
		$('#footer img').css('opacity', 0.5);
		$('#footer img').hover(
			function(){
				$(this).stop(true, true).animate({opacity: 1}, 700);
			},
			function(){
				$(this).animate({opacity: 0.5}, 500);
			}
		);
	});
	
	/*
	* Activation button of games
	*/
	var $nicknameBox = $('#gunbound-nickname-form');
	$('#activate-game a').click(function() {
		//return false;
		$('html,body').animate({scrollTop: 0}, 1000);
		$this = $('#activate-game');
		$nicknameBox.animate({opacity: 'show', 'top': '50px'}, 700);
		$this.hide();
		return false;
	});
	$('#gunbound-nickname-form form').submit(function() {
		//$('#loading').empty();
		$inpt = $('#gunbound-nickname');
		
		if ($inpt.val() == '' || $inpt.val() === $inpt.attr("title")) {
			$('#err-box').text('Lütfen bir takma ad seçiniz').slideDown();
		}
		else {
			$('#loading').html("<p>Lütfen bekleyiniz...</p>");
			$('#savebutton').attr('disabled', true);
			var uri = $(this).attr('action');
			$.post(uri,
				{ // posting datas
					nickname: $inpt.val() 
				},
				function(data){ // callback
					if (data.result == 1) {
						//alert("done");
						$('#loading').html("<p>Bilgiler kaydediliyor...</p>");
						var insert_uri = $('#activate-game a').attr("href");
						$.getJSON(insert_uri,
							function(data){
								if (data.result == 1) {
									alert('Zaten Kayıtlı bir oyuncusunuz!');
								}
								else {
									$('#gunbound-nickname-form').html("<p>İşlem başarılı.</p>");
									//.css({'backgroundImage': 'none', 'background-color': '#000'}
									alert('Tebirkler oyuncu kaydınızı başarıyla gerçekleştirdiniz.');
									$('#activate-game').html('Oyunu bilgisayarınıza indirerek hemen oynamaya başlayabilirsiniz.').wrapInner("<span></span>").fadeIn(700);
									$('#gunbound-nickname-form').animate({'top': '0px', 'opacity': 'hide'}, 1300);
								}
								
						});
					}
					else if (data.result == 7) {
						$('#err-box').text('Kullanıcı adınız sadece alfabetik karakterler ile rakam içerebilir ve en az 5 karakter uzunluğunda olmalıdır. (Türkçe karakter ve boşluk içeremez)!').slideDown();
						$('#savebutton').attr('disabled', false);
						$('#loading').empty();
					}
					else if (data.result == 3) {
						$('#err-box').text('Bu kullanıcı adı zaten kayıtlı lütfen başka bir nickname seçiniz!').slideDown();
						$('#savebutton').attr('disabled', false);
						$('#loading').empty();
					}
					else if (data.result == 2) {
						$('#err-box').text('Bilgiler kaydedilirken bir hata oluştu lütfen site yöneticilerine durumu bildiriniz!').slideDown();
						$('#savebutton').attr('disabled', false);
						$('#loading').empty();
					}
					else if (data.result == 4) {
						$('#err-box').text('Lütfen bir kullanıcı adı seçiniz!').slideDown();
						$('#savebutton').attr('disabled', false);
						$('#loading').empty();
					}
					else if (data.result == 0) {
						$('#err-box').text('Oyuna kayıt olabilmek için JoyGame üyesi olmanız gerekmektedir!').slideDown();
						$('#savebutton').attr('disabled', false);
						$('#loading').empty();
					}
					else if (data.result == 6) {
						$('#err-box').text('Database cnn error!').slideDown();
						$('#savebutton').attr('disabled', false);
						$('#loading').empty();
					}
			}, "json");
		}
		
		return false;
	});
	
	/*
	$('#activate-game a').click(function() {
		$this = $('#activate-game');
		var uri = $(this).attr("href");
		//alert(uri);
		var content = $this.html();
		$this.text('Yükleniyor...').wrapInner("<span></span>").fadeIn(500);
		$.getJSON(uri,
			function(data){
				if (data.result == 0) { // not-logged-in
					alert('Yalnızca JoyGame üyeleri oyunları oynayabilir!');
					 $this.hide().html('JoyGame\'e<br> üye olmak için<br> <a href="http://www.joygame.com/tr/signup/"><strong>buraya</strong></a> tıklayınız').wrapInner("<span></span>").fadeIn(700);
				}
				else if (data.result == 1) {
					alert('Zaten Kayıtlı bir oyuncusunuz!');
					$this.fadeOut(500).remove();
				}
				else {
					//.css({'backgroundImage': 'none', 'background-color': '#000'}
					alert('Tebirkler oyuncu kaydınızı başarıyla gerçekleştirdiniz.');
					$this.hide().html('Oyunu bilgisayarınıza indirerek hemen oynamaya başlayabilirsiniz').wrapInner("<span></span>").fadeIn(700);
				}
				
		});
		return false;
	});
	*/
});