
// js for ie 6
if(navigator.userAgent.toLowerCase().indexOf('msie 6') != -1){
	document.write('<script type="text/javascript" src="iepngfix/iepngfix_tilebg.js"></script>'+
		'<link rel="stylesheet" type="text/css" href="iepngfix/iepngfix.css" />');
}
function FlashPlayer(type) {
	site = location.href.substring(location.href.lastIndexOf('/')+1);
	if(site == 'start.php?fb=0') {
		document.getElementById('playerSelector').style.visibility = type; // hide OR show flash player; needed for safari		
	}
	if(site == 'start.php') {
		document.getElementById('me').style.visibility = type; // hide OR show flash player; needed for safari		
	}
}

function FlashGame(type) {
	site = location.href.substring(location.href.lastIndexOf('/')+1);
	if(site == 'game.php' || site == 'game.php?fb=0') {
		if ($.browser.msie) {
	 	} else {
			document.getElementById("isogame").style.visibility = type; // hide OR show game
		}
	}	
}
function close(id) {
	document.getElementById(id).style.visibility = "hidden";
	FlashPlayer('visible');
}
function hide(id) {
	 $(id).hide();
}
function closeFbWindow() {
	$('#fb-wallpost-div').hide();		
}
function toggleSwapFriends() {
	FlashPlayer('hidden');
	document.getElementById("fb_friendselect").style.visibility = "visible";
	$('.ajax_indicator').show();
	$("#fb_friendselect").load("inc_content/friend_selector.php", function() {
	  $('.ajax_indicator').hide();
		$('#id_search').quicksearch('ul#friends_select li');
	});
}
function toggleInstructions() {
	dcsMultiTrack(
		"DCS.dcsuri", "/buddyguard/anleitung",
		"WT.ti", "Buddy Guard Game Anleitung"
	);
	if($('#best10').css('visibility')=='visible') {
		$('#best10').css('visibility','hidden');
	}
	FlashGame('hidden');
	FlashPlayer('hidden');
	document.getElementById("instructions").style.visibility = "visible";
	$('.ajax_indicator').show();
	$("#instructions").load("inc_content/instructions.php", function() {
	  $('.ajax_indicator').hide();
	});
}

function instructionsClose() {
	FlashGame('visible'); // show game
	document.getElementById('instructions').style.visibility = "hidden";
	document.getElementById("isogame").isogame_resumeGame();
}

HTTP_GET_VARS=new Array();
strGET=document.location.search.substr(1,document.location.search.length);
if(strGET!='')
    {
    gArr=strGET.split('&');
    for(i=0;i<gArr.length;++i)
        {
        v='';vArr=gArr[i].split('=');
        if(vArr.length>1){v=vArr[1];}
        HTTP_GET_VARS[unescape(vArr[0])]=unescape(v);
        }
    }

function GET(v) {
	if(!HTTP_GET_VARS[v]){ return 'undefined'; }
	return HTTP_GET_VARS[v];
}

function popup(w,h, url) {
	// popup(580, 320, obj);
	x = screen.availWidth/2-w/2;
	y = screen.availHeight/2-h/2;
	var popupWindow = window.open(url,'','width='+w+',height='+h+',left='+x+',top='+y+',screenX='+x+',screenY='+y);
}

function jokerClose() {
	$('#joker').hide();
	FlashGame('visible'); // show game
	document.getElementById("isogame").isogame_jokerCallback(false);	
}

function eventHandler(state, obj) {
	switch(state) {
		case 'help':
			toggleInstructions();
			break;
		case 'highscore':
			showTop10();
			break;
		case 'handlauftips':
			showHandlauftips();
			break;
		case 'joker':
			// do magic if joker is available
			if ($.browser.msie) {
		 	} else {
				document.getElementById("isogame").style.visibility = "hidden"; // hide game
			}
			$("#joker").show();
			break;
		case 'gameOver':
			dcsMultiTrack(
				"DCS.dcsuri", "/buddyguard/gameover",
				"WT.ti", "Buddy Guard Game Over",
				"WT.i_fb_connect", (obj.facebook ? 1 : 0),
				"WT.i_game_time", (new Date()-initDate)/1000,
				"WT.i_game_score", obj.score
			);
			document.getElementById('isogame').style.visibility = "hidden";		
			document.getElementById("gameOver").style.visibility = "visible";
			$('#isogame').hide();
			$('.ajax-loader-gameover').show();
			$.post(
				"inc_content/highscore.php",
				{ 
					score : obj.score,
					obj : JSON.stringify(obj)
				},
      			function(data){
					$('.ajax-loader-gameover').show();
				 	$('#gameOver').html(data);
				}
			);
			break;
		default: 
			alert('wrong state');
	}
}

$('#fb-save-user-data').click(function() {
	$.post('inc_code/save_fb_user_data.php', function(data) {
		$('#fb-save-user-data-return').html(data);
		$('#gewinnspiel').hide();
		$('.gewinnspiel-h2').hide();
		$('#gewinnspiel-teilnahme-ohne-fb').hide();
		$('#fb-wallpost').hide();
		$('#fb-wallpost-div').hide();
		$('#ajax-loader-fb').hide();
		
	});
});

$(function() {
	var v = $("#fb-wallpost").validate({
		submitHandler: function(form) {
			dcsMultiTrack(
				"DCS.dcsuri", "/buddyguard/empfehlung",
				"WT.ti", "Buddy Guard Game Empfehlung",
				"WT.i_fb_connect", 1
			);
			$(form).ajaxSubmit({
				target: "#fb-wall-posted",
				success: function(){	
					$('#gewinnspiel-teilnahme-ohne-fb').hide();
					$('.gewinnspiel-h2').hide();
					$('#fb-wallpost').hide();
					$('#fb-wallpost-div').hide();
					$('#ajax-loader-fb').hide();
				},
				beforeSubmit: function() {
					$('#ajax-loader-fb').show();
				}
			});
		}
	});
	
	$("#reset").click(function() {
		v.resetForm();
	});
});

function closeFbLikeBox() {
	FlashPlayer('visible');
	site = location.href.substring(location.href.lastIndexOf('/')+1);
	if(site == 'game.php' || site == 'game.php?fb=0') {
		document.getElementById('isogame').style.visibility = "visible"; // show game
		if(document.getElementById('isogame').isogame_resumeGame) {
			document.getElementById('isogame').isogame_resumeGame();
		}
	}
	document.getElementById("fb_like_box").style.visibility = "hidden";
}
function showFbLikeBox() {
	FlashPlayer('hidden');
	site = location.href.substring(location.href.lastIndexOf('/')+1);
	if(site == 'game.php' || site == 'game.php?fb=0') {
		document.getElementById('isogame').style.visibility = "hidden"; // hide game
		if(document.getElementById('isogame').isogame_pauseGame && document.getElementById('isogame').isogame_pauseGame()) {
			document.getElementById('isogame').isogame_pauseGame();		
		}
	}
	document.getElementById("fb_like_box").style.visibility = "visible";
}

function useJokerManually() {
	document.getElementById("isogame").isogame_jokerCallback(true);	
	$('#joker').hide();
}
$('#open_more_email').click(function() {
	$('.open_more_email').hide();
	$('#block_more_email').show();
});

function showTop10() {
	FlashGame('hidden') // hide game
	$('#best10').css('visibility','visible');
	$('#top10-content').load('inc_content/top10.php');
}
function top10Close() {
	FlashGame('visible'); // show game
	$('#best10').css('visibility','hidden');
	document.getElementById('isogame').isogame_resumeGame();
}
function showHandlauftips() {
	FlashGame('hidden') // hide game
	$('#handlauftips').css('visibility','visible');
}
function hideHandlauftips() {
	FlashGame('visible'); // show game
	$('#handlauftips').css('visibility','hidden');
}
function hideRechtliches() {
	FlashGame('visible'); // show game
	$("#rechtliches").hide();
}

function showRechtliches() {
	$("#teilnahmebedinnungen").hide();
	FlashGame('hidden'); // hide game
	$("#rechtliches").show();
}

function hideTeilnahmebedinnungen() {
	FlashGame('visible'); // show game
	$("#teilnahmebedinnungen").hide();
}

function showTeilnahmebedinnungen() {
	$("#rechtliches").hide();
	FlashGame('hidden'); // hide game
	$("#teilnahmebedinnungen").show();
}


function showNutzungsbedinungen() {
	$("#nutzungsbedinungen").show();
}	
function showEmailForm() {
	$("#result_sendMail").hide();
	$("#emailForm").show();
	$('#email_1').val('');
	$('#email_2').val('');
	$('#email_3').val('');
	$("#errorMessageEmailForm").text("");
	$("#errorMessageEmailForm").removeClass("errorMessage");
	$('#errorMessageEmailForm').show();
}

function changeLang(lang) {
	$('.ajax_indicator_langs').show();
	$('.ajax_indicator_langs').load("inc_code/lang.php?l="+lang, function() {
	  $('.ajax_indicator_langs').hide();
	});
}

$(document).ready(function() {
	var lang = $('#fconnect_lang').val();
	$('#fconnect_img')
	.mouseenter(function() {
		$(this).attr('src', 'img/gewinnspiel/fconnect_buttons/fconnect-hover-' + lang + '.png');
	})
	.mouseleave(function() {
		$(this).attr('src', 'img/gewinnspiel/fconnect_buttons/fconnect-' + lang + '.png');
	});
});
