var statements = null;
var current = -1;
var items = -1;
var statementState = 1;
var delay = 30000;
var tId = null;

$(window).load(function(){
	doResize();
});

$(document).ready(function(){

if ( $.browser.msie && $.browser.version < 9){
	Cufon.replace('#footer, #breadcrumbs, .statement');
	Cufon.replace('#navi li a, .sub-level li a', {hover: true});
}
checkNewsLoader();

if ($.fn.browserTouchSupport && $.fn.browserTouchSupport.touches) {
	$(".popup_menu").click( function(){
		_sl = $(this).children(".sub-level");
		if (_sl.css("display") == "block"){
			_sl.fadeOut();
		} else{
			_t = ( $(this).height() / 2) - ($(this).children("ul").height() / 2);
			_ts = _t + 10;
			$(".sub-level").css({top: _ts + "px", display: "block", opacity: 0}).stop(true,true).animate({opacity: 1, top: _t}, 300);
		}
	});
} else{
	$(".popup_menu").mouseenter(
		function(){
			
			_t = ( $(this).height() / 2) - ($(this).children("ul").height() / 2);
			_ts = _t + 10;
			$(".sub-level").css({top: _ts + "px", display: "block", opacity: 0}).stop(true,true).animate({opacity: 1, top: _t}, 300);
			
		}).mouseleave( 
		function(){
			$(".sub-level").fadeOut();
		}
	);
	
	$("a.iframe").fancybox({
		'width': 760,
		'height': '75%',
		
		'type': 'iframe',
		'autoScale': true
	});
}
});

var resizeTimer = null;
$(window).bind('resize', function() {
	//$("#wrapper").css("overflow", "hidden"); 
    doResize();
	//$("#content").css({minHeight: "100%"});
});

function doResize(){
	_main = $(".main").height();
	
	_full = $(window).height();
	_header = $("#header").height();
	_footer = $("#footer").height();
	
	_white = _full - _header - _footer;
	
	
	
		_new_header = _full - _header - _white;
		_space = _white  - _main;
		
		_pB =  $(".main").css("paddingBottom").replace("px", "");
		_pT =  $(".main").css("paddingTop").replace("px", "");
		
		_new_foo = _space + _footer - _pB - _pT + 15;
		
		if (_new_foo < 100)
			_new_foo = 100;

		//console.log(_new_foo);
		$("#content").css({ marginBottom: (-1 * _new_foo) + "px", minHeight: _full + "px"});
		$(".push_foo").css("height", _new_foo + "px");
		

		
		$("#footer").css("height", _new_foo + "px");
		
		_ca = $("#content").height() - _header;
		$(".leftmargin").css({height: _ca + "px", bottom: "none"});
		$(".rightmargin").css({height: _ca + "px", bottom: "none"});
}


function initChange(_statements){
	statements = _statements.items;
	current = 0;
	statementState = 0;
	items = statements.length;
	if (items > 1){
		tId = setTimeout ('NextStatement()', delay );
	}
	
	
}

function NextStatement(){
	
	if (statementState == 1)
		return false;
		
	clearTimeout(tId);
	_c = current + 1;
	
	if (_c >= items){
		_c = 0;
	}
	current = _c;
	
	loadStatement(_c)
}

function PrevStatement(){
	if (statementState == 1)
		return false;
	
	clearTimeout(tId);
	_c = current - 1;
	
	if (_c < 0){
		_c = items - 1;
	}
	current = _c;
	
	loadStatement(_c)
}

function loadStatement(_c){
	
	statementState = 1;
	if ($('#st-image').length){
		_prevImage = $('#st-image').clone().attr("id", "st-imageOut").appendTo('#statements');
		$('#st-image').remove();
		_prevImage.animate({opacity: 0, scale: '0.8'}, 500, function(){  $(this).remove() });
	}
	
	
	if (statements[_c]["Type"] == "Text"){
		
		_loader = $('#st-active').clone().attr("id", "st-loader");
		
		_loader.html("<h1>" + statements[_c]["Header"] + "</h1><p>" + statements[_c]["Content"] + "</p>").appendTo('#statements');
		_con = _loader.html();
		
		
		if ( $.browser.msie && $.browser.version < 9){
			Cufon.replace('#st-loader');
			
			$("#st-loader").css({display: "block", top: '300px'}).animate({top: '0px'}, 600);
			$("#st-active").animate({top: '-300px'}, 600, function(){
																	 
				$("#st-active").remove();
				$('#st-loader').attr("id", "st-active");
				statementState = 0;
			})			
	
			
			
			
		} else{	
		
			$("#st-loader").css({opacity: 0, display: "block", top: '8px'}).animate({opacity: 1, top: 0}, 500);
			$("#st-active").css({opacity: 1}).animate({opacity: 0, scale: 0.8}, 800, function(){
				$("#st-active").remove();
				$('#st-loader').attr("id", "st-active");
				statementState = 0;
			});
		}
	} else if (statements[_c]["Type"] == "Image"){
		
		var img = new Image();
		$(img)
			.load(function () {
							
				$(this).css({opacity: 0, display: "block", top: '18px'});
				$(this).animate({opacity: 1, top: 10}, 500, function(){
					$("#st-active h1").html("");
					$("#st-active p").html("");
				});
				$('#statements').append(this);
				statementState = 0;
			})
			.attr("src", statements[_c]["src"])
			.attr("id", "st-image")
			.hide();
	}
	
	
	
	
	tId = setTimeout ('NextStatement()', delay );
}

function checkNewsLoader(){
	
	if ($(".hidden-newsrow").length){
		$("#newsLoader").show();
	} else{
		$("#newsLoader").hide();
	}
}

function LoadNews(){
	
	$("#newsLoader .loader").show();
	$("#newsLoader .more").hide();
	
	
	setTimeout("hideLoader()", 400);
	return false;
}

function hideLoader(){
	_row = $(".hidden-newsrow:eq(0)");
	_row.show();
	_row.removeClass("hidden-newsrow");
	$("#newsLoader .loader").hide();
	$("#newsLoader .more").show();
	doResize();
	checkNewsLoader();
}

function fixMargins(){
	$(".leftmargin").css("height", 0);
	$(".rightmargin").css("bottom", 0);
}
