// variable to remember the position of the yearscroll

function getQuery() {
	var loc = document.location.toString();
	var query = null;
	var anchor = getAnchor();
	
	if (loc.lastIndexOf('?')) {
		if (anchor) {
			loc = loc.substr(0,loc.lastIndexOf(anchor) - 1);
		}
		query = loc.substr(loc.lastIndexOf('?') + 1,loc.length);
	}
	
	return query;
}

// Similar to getQuery above, but sincs I wasn't sure this isn't used
// as is somewhere, I created a different function.
function getQueryString() {
	var loc = document.location.toString();
	var query = null;
	var anchor = getAnchor();
	
	if (loc.lastIndexOf('?') != -1) {
		if (anchor) {
			loc = loc.substr(0,loc.lastIndexOf(anchor) - 1);
		}
		query = loc.substr(loc.lastIndexOf('?') + 1,loc.length);
	}
	
	return query;
}

function getAnchor() {
	var loc = document.location.toString();
	var anchor = null;
	
	if (loc.lastIndexOf('#')) {
		anchor = loc.substr(loc.lastIndexOf('#') + 1,loc.length);
	}
	// do not return the full string if there is no anchor
	if (anchor == loc)
		anchor = null;	

	return anchor;
}


	function openslide(slide){
		var slides = $('#slides').children('li');
		var count = slides.length - 1;

		for (var i = 0; i <= count; i++){
			$(slides[i]).hide();
		}	
		var img = $(slides[slide-1]);	
		img.fadeIn('slow');

		var links = $('#slide-links').children('li');
		var count = links.length - 1;

		for (var i = 0; i <= count; i++){
			links[i].className = '';
		}	
	
		links[slide -  1].className = 'selected';

	}

	// have some slideshow init function	
	function next(){
	
		var slides = $('#slides').children('li');
		var count = slides.length - 1;
	 	var current = null;
	
		for (var i = 0; i <= count; i++){
			if ($(slides[i]).is(":visible")){
				current = i;		
			}	
		}	
	
		$(slides[current]).hide();
	
		var links = $('#slide-links').children('li');
		var linkcount = links.length - 1;

		for (var i = 0; i <= linkcount; i++){
			links[i].className = '';
		}			

		if (current < count) {	
			$(slides[current + 1]).fadeIn('slow'); 	
			links[current +  1].className = 'selected';
		} else {
			$(slides[0]).fadeIn('slow');
			links[0].className = 'selected';
		}	

	}


	function previous(){
	
		var slides = $('#slides').children('li');
		var count = slides.length - 1;
	 	var current = null;
	
		for (var i = 0; i <= count; i++){
			if ($(slides[i]).is(":visible")){
				current = i;		
			}	
		}	
	
		$(slides[current]).hide();

	
		var links = $('#slide-links').children('li');
		var linkcount = links.length - 1;

		for (var i = 0; i <= linkcount; i++){
			links[i].className = '';
		}			


		if (current > 0) {	
			$(slides[current - 1]).fadeIn('slow');  	
			links[current - 1].className = 'selected';
		} else {
			$(slides[count]).fadeIn('slow');	
			links[count].className = 'selected';
		}	
	}

/*

	function previous(){
	
		var slides = $('slides').getElementsByTagName('li');
		var count = slides.length;
		var top_start = 0;
		var left_start = 45;
		var top_interval = 15;
		var left_interval = 15;

		var max_top = top_start + ((count - 1) * top_interval);
			
		for (var i = 0; i < count; i++){
			var new_top = slides[i].offsetTop + top_interval;
			var new_left = slides[i].offsetLeft + left_interval;
			if ( new_top < max_top){
				//slides[i].morph('top:' + new_top + ';' + 'left:' + new_left + ';');
				slides[i].style.zIndex = slides[i].style.zIndex - 1;
				slides[i].style.top = new_top;
				slides[i].style.left = new_left;
			} else {
				//slides[i].morph('top:' + top_start + ';' + 'left:' + left_start + ';');
				slides[i].style.top = top_start;
				slides[i].style.left = left_start;
				slides[i].style.zIndex = count;
			}
		}	
	}



	function next(){
	
		var slides = $('slides').getElementsByTagName('li');
		var count = slides.length - 1;
		var top_start = 0;
		var left_start = 45;
		var top_interval = 15;
		var left_interval = 15;

		var max_top = top_start + (count  * top_interval);
		var max_left = left_start + (count * left_interval);

		for (var i = 0; i <= count; i++){
			var new_top = slides[i].offsetTop - top_interval;
			var new_left = slides[i].offsetLeft - left_interval;
			if (new_top < top_start){
				//slides[i].morph('top:' + max_top + ';' + 'left:' + max_left + ';');
				slides[i].style.zIndex = count - (max_top / top_interval) + 1;
				slides[i].style.top = max_top;
				slides[i].style.left = max_left;
			} else {
				//slides[i].morph('top:' + new_top + ';' + 'left:' + new_left + ';');
				slides[i].style.zIndex = count - (new_top / top_interval) + 1;
				slides[i].style.top = new_top;
				slides[i].style.left = new_left;
			}
		}	
	}

	function bestel(eventid){
	
		var frame = document.getElementById('ticket_frame');
		if (eventid != null) {
			frame.src = 'https://www.ticketscript.com/shop_plus.php?view=order&event_id=' + eventid + '&org_id=940';
		} else {
			frame.src = 'https://www.ticketscript.com/shop_plus.php?org_id=940';
		}
		
		var tickets = document.getElementById('ticket');	
		if (tickets.style.display == 'none'){  
		 tickets.show();	
		} else {
		 tickets.hide();	
		}
	}
*/
	// some doxa related functions

	current_id = null;
	current_user = null;

	current_nav = null;

	function load(){
		var anchor = getAnchor();
		var query = getQueryString();
		if (!query) {
			if (anchor != null && anchor != '' ) {
				loadContent(anchor);	
			} else{
				loadContent('/mc/home');	
			}
		} else {
			// It's seems impossible to remove ?/.. without reloading
			//location.search = null;
			// The hash could be set, but that's not really useful
			//location.hash = query;
		}
    		processGraphics();
	}

	function login(user, pass) {
	
		var url = './doxa/proof.php?1=' + user + '&2=' + pass;
		$.ajax({
			url: url,
			success: function(result) {
				if (result == user && user!='') {
					$('#login').hide();					
					$('#logged_in').show();
					// show user id somewhere					

					current_user = user;
					setEditRights();
					
				} else {
					current_user = null;
			
					$('#login').show();					
					$('#logged_in').hide();					
					document.forms.login_form['username'].value = '';
					document.forms.login_form['password'].value = '';
				}
			},
			error: function(result) {
				alert("Login error");
			}
		});		
	
	}
		
	// TODO
	// combine this function with the login.
	function setEditRights(){
	
		var url = './doxa/writable.php?' + current_id ;
		$.ajax({
			url: url,
			success: function(result) {
				if (result == 'TRUE'){
					$('#edit_link').show();
				} else {
					$('#edit_link').hide();
				} 
			},
			error: function(result) {
				alert('Er is een fout opgetreden.');
			}
		});		
	
	}

	function setIdentifier(id) {
		current_id = id;	
		location.hash = id;
		if ($('#edit_link').length > 0) {
			document.getElementById('edit_link').href =  "./doxa/edit.php#" + id;
		}
		setEditRights();
	}

	function processGraphics(){
		Cufon.refresh();
	}

	function subnavLink(parentNode, label, subnavid, id ){
		
		var link = document.createElement('a');
		link.appendChild(document.createTextNode(label));	
		//link.href= '#' + subnavid;
		link.href= '#';
		link.id = id;

		// also make the selection
		link.onclick = function () {
			$(subnavid).show();
			// Explicitly remove all other subnav's if the
			// clicked object is 'contact'
			if ($(subnavid).id == 'nav_5_nav') {
				$('#nav_2_nav').hide();
				$('#nav_4_nav').hide();
				$('#nav_7_nav').hide();
			}
		};
		parentNode.append(link);
	}

	function htmlpartLink(parentNode, label, htmlpart, id ){
		var link = document.createElement('a');
		link.appendChild(document.createTextNode(label));	
		link.href= '#' + htmlpart;
		link.id = id;
		link.onclick = function () { loadContent(htmlpart, link); } ;
		parentNode.append(link);
	}
	
	function htmlpart_subnavLink(parentNode, label, htmlpart, id, subnavid ){
		var link = document.createElement('a');
		link.appendChild(document.createTextNode(label));	
		link.href= '#' + htmlpart;
		link.id = id;
		link.onclick = function () { loadContent(htmlpart, link); $(subnavid).show();  } ;
		parentNode.append(link);
	}
	
	function setNav(id){
		current_nav = id;
	}
	
	function loadContent(htmlpart, link) {

		// set some stuff in case it is a mixin page
		if (htmlpart.substr(0,16) == '/mixincreativity') {
		  $('#mixerlogo').show();
		  $('#navigatie').addClass('mixinnavigatie');
		  // if this is the main entrance, init the special overview
		  if (htmlpart == '/mixincreativity')
		    initMixin();
		} else {
		  $('#mixerlogo').hide();
		  $('#navigatie').removeClass('mixinnavigatie');
		}

		// make some seperate navigation function
		
		var links = $('a.selected'); 
		for (var i = 0; i < links.length; i++){
			$(links[i]).removeClass('selected');
		}
		if (link){
			cur_nav = link.id;
			// Explicitly remove non-active submenu's
			if ((cur_nav.substr(0,9) != 'nav_2_nav')){
				$('#nav_2_nav').hide();
			}
			if ( (cur_nav.substr(0,9) != 'nav_5_nav')){
				$('#nav_5_nav').hide();
			}	
			if ( (cur_nav.substr(0,9) != 'nav_7_nav')){
				$('#nav_7_nav').hide();
			}	
			if ((cur_nav.substr(0,9) != 'nav_4_nav')){
				$('#nav_4_nav').hide();
			}
		}
		$(link).addClass('selected');
		var contentDiv = $('#container1');
		while (contentDiv.firstChild)	
			contentDiv.removeChild(contentDiv.firstChild);

		loader =  document.createElement('IMG');
		loader.src = 'img/ajax-loader.gif';
		loader.id = 'ajax-loader';
		contentDiv.append(loader);	
		var url = './view/?' + htmlpart ;	
		
		$.ajax({
			url: url,
			success: function(result) {
				setIdentifier(htmlpart);	
				contentDiv.html(result);
				processGraphics();
			},
			error: function(result) {
				alert('Er is een fout opgetreden.');
			}
		});		

		// TODO: where should the read rights be located?
	}


	function resize(){
		//$('sidebar').style.height = getPageHeight()- 10;
	}

	// some specific functions for this site	
		function checkMcForm(){
			
			// email
			if (document.forms[0].elements[1].checked){
				if(document.forms[0].elements[0].value == ""){
					alert("Er is geen naam ingevoerd.");
					return false;
				}
				if(document.forms[0].elements[2].value == ""){
					alert("Er is geen email adres ingevoerd.");
					return false;
				}
			}
	
			// post
			if (document.forms[0].elements[3].checked){
				if(document.forms[0].elements[0].value == ""){
					alert("Er is geen naam ingevoerd.");
					return false;
				}
				if(document.forms[0].elements[4].value == ""){
					alert("Er is geen adres ingevoerd.");
					return false;
				}
				if(document.forms[0].elements[5].value == ""){
					alert("Er is geen postcode ingevoerd.");
					return false;
				}
				if(document.forms[0].elements[6].value == ""){
					alert("Er is geen woonplaats ingevoerd.");
					return false;
				}
			}

			return true;
		}
		
function folder(elm_id){
	$('#'+elm_id).toggle();
}



// Functions and variable for the 'voorstellingen' years scrollbar
      function moveToPrevious(moveamount)
      {
        if (totalmove < 0) {
//          new Effect.Move('years', { x: 110*moveamount, y: 0, transition: Effect.Transitions.full });
          var attryears = { points: { by: [110*moveamount, 0] } };
          var animyears = new YAHOO.util.Motion('years',attryears,0);
          animyears.animate();
          totalmove = totalmove + 1;
        }
      }

      function moveToNext(moveamount)
      {
        var totallength = document.getElementById('years').getElementsByTagName('li').length;
        // 110: length of a year block
        // totallength: total number of year blocks
        // -6: number of displayed year blocks
        if (totalmove > -1*(totallength-6)) {
//          new Effect.Move('years', { x: -110*moveamount, y: 0, transition: Effect.Transitions.full });
          var attryears = { points: { by: [-110*moveamount, 0] } };
          var animyears = new YAHOO.util.Motion('years',attryears,0);
          animyears.animate();
          totalmove = totalmove - 1;
        }
      }

      function moveToCurrent()
      {
//      	console.log(totalmove); // ?
        if (document.getElementById('years')) {
          var thislength = document.getElementById('years').getElementsByTagName('li').length;
          if (currentlistlength != thislength) { // i.e. different yearlist
            currentlistlength = thislength;
            totalmove = 0;
          } else {
  	    var currentmove = totalmove;
            var attryears = { points: { by: [110*currentmove, 0] } };
            var animyears = new YAHOO.util.Motion('years',attryears,0);
            animyears.animate();
            totalmove = currentmove; // reset totalmove to what it was
          }
        }
      }


// Some popup functionality for the /mc/peoples.

var peoples = {

  popup: function(el) {
    var popup = YAHOO.util.Dom.getElementsByClassName('peoples_popup','div',el)[0];
    YAHOO.util.Dom.setStyle(popup,'display','block');
  },

  popdown: function(el) {
    var popup = YAHOO.util.Dom.getElementsByClassName('peoples_popup','div',el)[0];
    YAHOO.util.Dom.setStyle(popup,'display','none');
  } 
}
