function checkIssuuDocument(){
	var sb_player = $('sb-player');
	var issuu_box = sb_player.getElement('.issuu-box');
	if(issuu_box){
		var domain = document.domain;
		var issuu_document_id = issuu_box.getElement('.issuu_document_id').get('value');
		var width = sb_player.getElement('.issuu_document_width').get('value');
		var height = sb_player.getElement('.issuu_document_height').get('value');
		var title = sb_player.getElement('.issuu_document_title').get('value');
	
		var issuu_viewer = 'http://static.issuu.com/webembed/viewers/style1/v1/IssuuViewer.swf?pageNumber=0';
		var issuu_content_id = 'issuu_content';
	
		var attributes = {
			id: 'issuuViewer'
		};
		var params = {
			allowfullscreen: 'true',
			allowScriptAccess: 'always',
			menu: 'true'
		};

		var flashvars = {
			jsAPIClientDomain: domain,
			mode: 'embed',
			layout: 'http%3A%2F%2F'+domain+'%2Fissuu%2Fcustom_dark%2Flayout.xml',
			showFlipBtn: 'false',
			documentId: issuu_document_id,
			username: 'themacommunications'
		};
	
		var issuu_div_wrapper = new Element('div',
			{
				'id': 'issuu_wrapper'
			}
		);
		var issuu_div = new Element('div',
			{
				'id': issuu_content_id
			}
		);
	
		issuu_div.inject(issuu_box);
	
		swfobject.embedSWF(issuu_viewer, issuu_content_id, width, height, "9.0.0","expressInstall.swf", flashvars, params, attributes);
	}
}

function loadFlashCatalog(){
	var anchor = this;
	var domain = document.domain;
	
 	var issuu_document_id = anchor.get('rel');
	var issuu_document_title = anchor.get('title');
	var width = 800;
	var height = 500; // 500

	//onFinish:   function(){
	//	alert($('sb-body-inner').get('html'));
	//	swfobject.embedSWF(issuu_viewer, issuu_content_id, width, height, "9.0.0","expressInstall.swf", flashvars, params, attributes);
	//}
	var shadowbox_content = '<div class="issuu-box" style="width:'+width+'px;height:'+height+'px">';
	shadowbox_content += '<input type="hidden" name="document_id" value="'+issuu_document_id+'" class="issuu_document_id" />';
	shadowbox_content += '<input type="hidden" name="width" value="'+width+'" class="issuu_document_width" />';
	shadowbox_content += '<input type="hidden" name="height" value="'+height+'" class="issuu_document_height" />';
	shadowbox_content += '<input type="hidden" name="title" value="'+issuu_document_title+'" class="issuu_document_title" />';
	shadowbox_content += '</div>';
	Shadowbox.open(
		{
	        content:    shadowbox_content,
	        player:     'html',
	        title:      issuu_document_title,
	        height:     height,
	        width:      width
	    }
	);
	

	return false;
}

function initBigTeaser(){
	var big_teaser = $$('.teaser_big');
	big_teaser.each(
		function(teaser){
			var text = teaser.getElement('.text');
			var size = text.getSize();
			if(size.y < 100){
				text.addClass('rows_2');
			}
		}
	);
}

function gotoNextPage(){
	var anchor = this;
	window.location.href = anchor.get('href');
}

function initSmallTeaser(){
	var teaser_rows = $$('.teaser_row');
	teaser_rows.each(
		function(row){
			var all_teaser = row.getElements('.teaser_propheten');
			all_teaser.each(
				function(teaser){
					var anchor = teaser.getElement('.link a');
					if(anchor){
						teaser.addEvent('click', gotoNextPage.bind(anchor));
					}
				}
			);
		}
	);
}

function initIssuuDocuments(){
	$$('.issuu_document').each(
		function(issuu_document){
			issuu_document.addEvent('click', loadFlashCatalog);
		}
	);
}

function initHeights(){
	var navbar = $('navbar');
	var navbar_height = 0;
	var navbar_wrapper = navbar.getElement('.navbar_wrapper');
	var navbar_children = navbar_wrapper.getChildren('div');
	navbar_children.each(
		function(div, index){
			if(index > 0){
				navbar_height += div.getSize().y;
			}else{
				navbar_height += 400;
			}
		}
	);
	navbar.setStyle('min-height', navbar_height);
	var leftbar_size = $('leftbar').getSize();
	var outbox_size = $('outbox').getSize();
	if(navbar_height > outbox_size.y + 44){
		$('inbox').setStyle('min-height', (navbar_height - 40));
	}else{
		navbar.setStyle('min-height', (outbox_size.y + 44));
	}
}

function initTicker(){
	var marquee = new mooquee($('mooquee1'),
		{
			'marWidth': $('mooquee1').getSize().x,
			'direction': 'left',
			'speed': 40
		}
	);
}

function initReflections(){
	$$('.reflect').reflect();
}

function initLogo(){
	var logo_anchor = $('logo').getElement('a');
	if(logo_anchor){
		logo_anchor.set('id', 'logo_anchor');
		var attributes = {
			id: 'logo_anchor'
		};
		var params = {
			wmode: 'transparent'
		};
		var flashvars = {

		};
		swfobject.embedSWF('pics/logo.swf', 'logo_anchor', 206, 377, "9.0.0","expressInstall.swf", flashvars, params, attributes);
	}
}

function checkNavigationPosition(){
	var navbar = $('navbar');
	var navbar_wrapper = navbar.getElement('.navbar_wrapper');
	var scroll_size = window.getScrollSize();
	var window_size = window.getSize();
	var scroll_position = window.getScroll();
	if(window_size.y + scroll_position.y + 10 > scroll_size.y){
		navbar_wrapper.setStyle('position', 'relative');
	}else{
		navbar_wrapper.setStyle('position', 'fixed');
	}
}

function initAll(){
	initLogo();
	initSmallTeaser();
//	initBigTeaser();
	Shadowbox.init(
		{
			onFinish: checkIssuuDocument
		}
	);
	initIssuuDocuments();
	initHeights();
	initTicker();
//	initReflections();
	checkNavigationPosition();
}

window.addEvent('domready', initAll);
window.addEvent('scroll', checkNavigationPosition);
