var fx;

function loadAjaxLink(url){
	if($('viewercontrol') != null){
		$('viewercontrol').setStyle('display', 'none');
	}

	if(fx != null) fx.cancel();
	$('page').setStyle('overflow', 'hidden');
	fx = new Fx.Morph('page', {
		duration: 'normal',
		transition: Fx.Transitions.Sine.easeOut,
		onComplete: function(){
			new Request.HTML({
				method: 'get',
				url: url,
				evalScripts: true,
				headers: {'X-REQUEST': 'AJAX'},
				update: $('content'),
				onComplete: function(){
					createAjaxLinks();
					createConceptThumbMouseovers();
					createMediaThumbMouseovers();
					createFenceartThumbMouseovers();
					createFotoveilingThumbMouseovers();
					createMedewerkersThumbMouseovers();
					createProjectThumbMouseovers();
					createReadMoreLinks();
					if(url.search('sponsors') > 0){
						EB.processAttributes();
					}
					if(url.search('page=site.home') > 0){
						if($('viewercontrol') != null){
							$('viewercontrol').setStyle('display', 'block');
							startRotator();
						}
					}
					tooltips = new Tips('.tooltip',{ title: 'tip' });
					Shadowbox.cache = new Array();
					Shadowbox.setup();
					fx.cancel();
					fx = new Fx.Morph('page', {
						duration: 'normal',
						transition: Fx.Transitions.Sine.easeIn,
						onComplete: function(){
							$('page').setStyle('overflow', 'auto');
						}
					});
					fx.start({
						'width': 466
					});
				}
			}).send();
		}
	});
	fx.start({
		'width': 0
	});
}

function loadNieuws(category_id, limit, offset){
	new Request.HTML({
		method: 'get',
		url: SITE_ROOT + 'page=ajax.nieuws/category_id=' + category_id + '/limit=' + limit + '/offset=' + offset,
		evalScripts: true,
		headers: {'X-REQUEST': 'AJAX'},
		update: $('nieuwscontent'),
		onComplete: function(){
			createReadMoreLinks();
			Shadowbox.cache = new Array();
			Shadowbox.setup();
		}
	}).send();
}

function setBackground(section){
	new Request.JSON({
		method: 'get',
		url: SITE_ROOT + 'page=ajax.background/section=' + section,
		onComplete: function(data){
			var pic = new Image(100,100); // preload
			pic.src = data.image;
			setTimeout(function(){
				$('body').setStyle('background-image', 'url(\'' + data.image + '\')');
			}, 1000);
		}
	}).send();
}

var nfx;
var nieuwsClosed = false;

function toggleNieuws(){
	if(nfx != null) nfx.cancel();
	nfx = new Fx.Morph('nieuws', {
		duration: 'normal',
		transition: Fx.Transitions.Sine.easeOut,
		onComplete: function(){
			nieuwsClosed = !nieuwsClosed;
			$('nieuws').setStyle('overflow', 'auto');
			if(nieuwsClosed){
				$('menu_nieuws').setProperty('src', SITE_ROOT + 'img/menu_nieuws.png');
				EB.processAttributes();
			}
		}
	});
	$('nieuws').setStyle('overflow', 'hidden');
	if(nieuwsClosed == true){
		nfx.start({
			'width': 270
		});
	} else {
		nfx.start({
			'width': 0
		});
	}
}

var tfx;
var twitterClosed = false;

function toggleTwitter(){
	if(tfx != null) tfx.cancel();
	tfx = new Fx.Morph('twitter', {
		duration: 'normal',
		transition: Fx.Transitions.Sine.easeOut,
		onComplete: function(){
			twitterClosed = !twitterClosed;
			if(twitterClosed == false) $('twitter').setStyle('overflow', 'auto');
		}
	});
	$('twitter').setStyle('overflow', 'hidden');
	if(twitterClosed == true){
		tfx.start({
			'height': 366
		});
		$('pijl_twitter').setProperty('src', SITE_ROOT + 'img/pijl_twitter_active.jpg');
	} else {
		tfx.start({
			'height': 32
		});
		$('pijl_twitter').setProperty('src', SITE_ROOT + 'img/pijl_twitter.jpg');
	}
}

function createConceptThumbMouseovers(){
	$$('.concept_thumb').each(function(item){
		item.removeEvents('mouseover');
		item.addEvent('mouseover', function(){
			new Request.HTML({
				method: 'get',
				url: SITE_ROOT + 'page=ajax.concept/id=' + item.getProperty('alt'),
				evalScripts: true,
				headers: {'X-REQUEST': 'AJAX'},
				update: $('concept_description'),
				onComplete: function(){
					createAjaxLinks();
					createReadMoreLinks();
					Shadowbox.cache = new Array();
					Shadowbox.setup();
				}
			}).send();
		})
	});
}

function createProjectThumbMouseovers(){
	/*
	$$('.project_thumb').each(function(item){
		item.removeEvents('mouseover');
		item.addEvent('mouseover', function(){
			new Request.HTML({
				method: 'get',
				url: SITE_ROOT + 'page=ajax.project/id=' + item.getProperty('alt'),
				evalScripts: true,
				headers: {'X-REQUEST': 'AJAX'},
				update: $('project_content'),
				onComplete: function(){
					createAjaxLinks();
					createReadMoreLinks();
					Shadowbox.cache = new Array();
					Shadowbox.setup();
				}
			}).send();
		})
	});
	*/
}

function createMediaThumbMouseovers(){
	$$('.media_thumb').each(function(item){
		item.removeEvents('mouseover');
		item.addEvent('mouseover', function(){
			new Request.HTML({
				method: 'get',
				url: SITE_ROOT + 'page=ajax.mediaitem/id=' + item.getProperty('alt'),
				evalScripts: true,
				headers: {'X-REQUEST': 'AJAX'},
				update: $('media_description'),
				onComplete: function(){
					createAjaxLinks();
					createReadMoreLinks();
					Shadowbox.cache = new Array();
					Shadowbox.setup();
				}
			}).send();
		})
	});
}

function createFenceartThumbMouseovers(){
	$$('.fenceart_thumb').each(function(item){
		item.removeEvents('mouseover');
		item.addEvent('mouseover', function(){
			new Request.HTML({
				method: 'get',
				url: SITE_ROOT + 'page=ajax.fenceart/id=' + item.getProperty('name'),
				evalScripts: true,
				headers: {'X-REQUEST': 'AJAX'},
				update: $('fenceart_description'),
				onComplete: function(){
					createAjaxLinks();
					createReadMoreLinks();
					Shadowbox.cache = new Array();
					Shadowbox.setup();
				}
			}).send();
		})
	});
}

function createFotoveilingThumbMouseovers(){
	$$('.fotoveiling_thumb').each(function(item){
		item.removeEvents('mouseover');
		item.addEvent('mouseover', function(){
			if($('photographer' + item.getProperty('name')) != null) $('photographer' + item.getProperty('name')).setStyle('color', '#FF0000');
		})
		item.removeEvents('mouseout');
		item.addEvent('mouseout', function(){
			if($('photographer' + item.getProperty('name')) != null) $('photographer' + item.getProperty('name')).setStyle('color', '#000000');
		})
	});
}

function createMedewerkersThumbMouseovers(){
	$$('.medewerkers_thumb').each(function(item){
		item.removeEvents('mouseover');
		item.addEvent('mouseover', function(){
			$('medewerker' + item.getProperty('alt')).setStyle('color', '#FF0000');
		})
		item.removeEvents('mouseout');
		item.addEvent('mouseout', function(){
			$('medewerker' + item.getProperty('alt')).setStyle('color', '#000000');
		})
	});
}

function createReadMoreLinks(){
	$$('.text_read_more').each(function(item){
		item.removeEvents('click');
		item.addEvent('click', function(){
			var content = item.getParent().getParent().getParent().getChildren('.text_content')[0];
			content.setStyle('display', 'block');
			item.setStyle('display', 'none');
			var read_less = content.getChildren('.text_read_less')[0];
			if(read_less != null){
				read_less.addEvent('click', function(){
					content.setStyle('display', 'none');
					item.setStyle('display', 'block');
				});
			}
		});
	});
}

function createAjaxLinks(){
	$$('a.xlink').each(function(item){
		item.removeEvents('click');
		item.addEvent('focus', function(e) {$(this).blur();});
		item.addEvent('click', function(e) {
			if (e) new Event(e).stop();
			history_add($(this).getProperty('href'));
			loadAjaxLink($(this).getProperty('href'));
			if($(this).hasClass('bglink')){
				setBackground($(this).getProperty('rel'));
			}
		});
	});
}

function setMenu(id){
	var surfix = '';
	if(LANGUAGE == 'GB') surfix = '_en';

	$('menu_home').setProperty('src', SITE_ROOT + 'img/menu_home.png');
	$('menu_dit_is_yip').setProperty('src', SITE_ROOT + 'img/menu_dit_is_yip' + surfix + '.png');
	$('menu_projecten').setProperty('src', SITE_ROOT + 'img/menu_projecten' + surfix + '.png');
	$('menu_yipart').setProperty('src', SITE_ROOT + 'img/menu_yipart.png');
	$('menu_support_yip').setProperty('src', SITE_ROOT + 'img/menu_support_yip.png');
	$('menu_extra_info').setProperty('src', SITE_ROOT + 'img/menu_extra_info.png');
	if(!nieuwsClosed && id != 'menu_nieuws') $('menu_nieuws').setProperty('src', SITE_ROOT + 'img/menu_nieuws_active' + surfix + '.png');
	else $('menu_nieuws').setProperty('src', SITE_ROOT + 'img/menu_nieuws' + surfix + '.png');

	$(id).setProperty('src', SITE_ROOT + 'img/' + id + '_active' + surfix + '.png');
	EB.processAttributes();
}

function createMenuEvents(){
	$$('.menu_image').each(function(item){
		item.removeEvents('click');
		item.addEvent('click', function(e) {
			setMenu($(this).getProperty('id'));
		});
	});
}

function fixCentering(){
	if($('body').getSize().x < $('container').getSize().x){
		$('container').setStyle('marginLeft', "0px");
		$('container').setStyle('left', "0px");
	} else {
		$('container').setStyle('marginLeft', (-1 * Math.round($('container').getSize().x/2)) + "px");
		$('container').setStyle('left', "50%");
	}
	if($('body').getSize().y < $('container').getSize().y){
		$('container').setStyle('marginTop', "0px");
		$('container').setStyle('top', "0px");
	} else {
		$('container').setStyle('marginTop', (-1 * Math.round($('container').getSize().y/2)) + "px");
		$('container').setStyle('top', "50%");
	}
}

var HistoryManager;
var hrefs = new Array();
var currentIndex = 0;

function history_init(){
	//	initiate object
	HistoryManager = new HistoryManagerX();
	//	initialize manager
	HistoryManager.initialize();
	//	register handlers
	reqHistory = HistoryManager.register(
		'page-index',
		[0],
		function(values){history_go(values[0]);},
		function(values) {
			return 'page-index(' + values[0] + ')';
		},
		/page-index\((\d+)\)/ // the regexp to match "page-index(0)"
	);
	//	start manager
	HistoryManager.start();
	//	store first page
	history_add(location.href);
}

var ajax_last_url = '';

function history_go(index){
	if(index.toInt() >= 0){
		if(hrefs[index]){
			loadAjaxLink(hrefs[index]);
		}
	}
}

function history_add(url) {
	try{
		if(url && currentIndex >= 0){
			//	set current index
			index = hrefs.length;
			hrefs[index] = url;
			currentIndex = index;
			// updating the history
			reqHistory.setValue(0, index);
		}
	} catch(e) {
		//	void;
	}
};

var tooltips;

function initJs(){
	createAjaxLinks();
	createConceptThumbMouseovers();
	createMediaThumbMouseovers();
	createFenceartThumbMouseovers();
	createFotoveilingThumbMouseovers();
	createMedewerkersThumbMouseovers();
	createProjectThumbMouseovers();
	createReadMoreLinks();
	createMenuEvents();
	setBackground('home');
	fixCentering();
	history_init();

	tooltips = new Tips('.tooltip', { title: 'tip' });

	// disable right click
	/*
	window.addEvent('domready', function() {
		document.body.addEvent('contextmenu',function(e) {
			e.stop();
		});
	});
	*/
	
	var count = 0, limit = 2;
	
	twitterlib.timeline('younginprison', { limit: 5 }, function (tweets) {

		document.getElementById('tweets').innerHTML="";
		
		
		
		for (i=0;i<tweets.length;i++){
		
	
			var d = new Date();
			
			var tweet = tweets[i];
			var datestr = tweet.created_at.substr(4,tweet.created_at.length-18); // "Fri Aug 05 14:40:36 +0000 2011"

			document.getElementById('tweets').innerHTML += '<div class="item"><h5><a target="_blank" href="http://twitter.com/younginprison/statuses/'+tweet.id+'/">younginprison</a> | '+datestr+'</h5><p>'+tweet.text+'</p></div>'
		}
	});
	
	/*
	getTwitters('tweets', {
		id: 'younginprison',
		count: 25,
		enableLinks: true,
		ignoreReplies: true,
		clearContents: true,
		template: '<div class="item"><h5><a target="_blank" href="http://twitter.com/%user_screen_name%/statuses/%id%/">%user_screen_name%</a> | %time%</h5><p>%text%</p></div>'
	});
	*/
}

Shadowbox.init({
	language:   "en",
	players:    ["img", "iframe"],
	overlayColor: '#FFFFFF',
	viewportPadding: 20
});

window.addEvent('domready', initJs);
window.addEvent('resize', fixCentering);

