var playerState;
var siteDomain = 'http://www.arrow.nl';
var playerLoaded = false;

function playerLoadingDone() {
	playerLoaded = true;
}

function resizeContentBg() {
	var _h = $('#main').find('.player-box').height();
	$('#content-bg .content-bg-main').height(_h+16);
}
function resizeContentBg2(h) {
	//alert(h);
	$('#content-bg .content-bg-main').css({height: h+16});
	//$('#content-bg .content-bg-main').height(h+16);
}

function switchPlayer(full) {
	var objPlayer = document.getElementById('player');
	switch(full) {
		case 'y':
			if(hasFlashPlayer) {
				if(playerLoaded) objPlayer.maximize();
			} else {
				if(playerState != 'y') {
					$('.player img').attr('src', bigPlayerIMG);
				}
				$('.player img').bind("load", resizeContentBg);
			}
			break;

		case 'n':
		default:
			if(hasFlashPlayer) {
				if(playerLoaded) objPlayer.minimize();
			} else {
				if(playerState != 'n') {
					$('.player img').attr('src', '/images/img-player-small.png');
				}
				$('.player img').bind("load", resizeContentBg);
			}
			break;
	}
	playerState = full;
}

function loadContent(content_url) {
	if(!content_url){
		return false;
	}
	//alert('asdf');
	$.ajax({
		url: content_url,
		cache: false,
		type: "POST",
		data: "_ajax_call=1",
		success: function(html){
			//alert(content_url);
			
			$('#content').html(html);
			var title = "";
			if($('input[name=title]').attr('value')){
				title += $('input[name=title]').attr('value');
			}
			if(title == "" || title == "undefined"){
				title = siteTitle;
			}
			document.title = title;
			postLoadInit();
			pageTracker._trackPageview(content_url);
			pageTracker2._trackPageview(content_url);
			switchPlayer($('input[name=fullplayer]').val());
		}
	});
}

function postLoadInit() {
	initCufon();

	$("a[href]").each(function(){
		if(this.href.indexOf(siteDomain + siteRoot) == 0 || this.href.indexOf('/') == 0) {
			thishref = $(this).attr('href');
			if(($(this).attr('rel') != 'no_ajax') && (thishref.indexOf('#') < 0) && (thishref.indexOf('.pdf') < 0)) {
				thishref = thishref.replace(siteDomain, '');	// Domein uit url halen
				if(thishref == siteRoot) {
					$(this).attr('href', '#' + siteRoot);
				} else {
					$(this).attr('href', '#' + thishref);
				}
			}
		}
	});

	$("a[href*='://']").attr('target', '_blank');

	$('form#frequentiezoeker').submit(function(){
		$('#frequentiezoeker .illustration span').html('<img src="/images/ajax-loader.gif" alt="" />');
		$.ajax({
			url: siteRoot + 'frequentiezoeker/' + $('#txt-postcode').val(),
			cache: false,
			type: "GET",
			success: function(data){
				if(data == '') {
					alert('Helaas geen doorgifte, neem contact op met jouw kabelmaatschappij');
					$('#frequentiezoeker .illustration span').text('');
				} else {
					$('#frequentiezoeker .illustration span').text(data.replace(',', '.') + ' FM');
				}
			}
		});

		return false;
	});

	$('#btn-submitpostcode').click(function(){
		$('form#frequentiezoeker').trigger('submit');
		return false;
	});


	$('#frm_contact, #frm_adverteren').submit(function(){
		return_path = $('input[name=return]', this).val();
		$.post(siteRoot, $(this).serialize());
		$.historyLoad(return_path);
		return false;
	});


	$('#txt-postcode').focus(function(){
		if($(this).val() == 'postcode') $(this).val('');
		$(this).attr('maxlength', 4);
	}).blur(function(){
		if($(this).val() == '') $(this).val('postcode');
		$(this).attr('maxlength', 8);
	});
}

$(document).ready(function(){
	postLoadInit();


	if(document.location.pathname != siteRoot) {
		document.location = siteRoot + '#' + document.location.pathname;
	} else {
		if(document.location.hash == '') {
			document.location.hash = '#' + siteRoot;
		}
		$.historyInit(loadContent, document.location.hash);
	}

});