$(document).ready(function() {

  if ($('div.brandbox').length != 0) {
    var anticache = Math.floor(Math.random() * 10000000000);
    swfobject.embedSWF("flash/slider.swf?t=" + anticache, "brandbox", "100%", "340", "9.0.0", "flash/expressInstall.swf");
  }

  // sign up
	$('div.header div.sign-up a').hover(
		function() {
			$('div.header div.sign-up').css('background-position','left -40px');
		}, 
		function() {
			$('div.header div.sign-up').css('background-position','left 0');
		}
	);	
	function sendemail(shake) {
		var text = $('div.header div.sign-up input').val();		
		var regex = /^[A-Za-z0-9\._%+\-]+\@([A-Za-z0-9\-]+\.)+([A-Za-z0-9]{2,4}|museum)$/;
		if (text != '' && text != 'Voer je e-mail adres in...' && text.match(regex)) {
			$('form:first').submit();
		} else {
			$('div.header div.sign-up').stop(1,1);
			if (shake == true) {
        $('div.header div.sign-up')
          .animate({ right: '26px' },10)
          .animate({ right: '32px' },50)
          .animate({ right: '26px' },10)
          .animate({ right: '32px' },50)
          .animate({ right: '26px' },10)
          .animate({ right: '29px' },25); 
      }
			$('div.header div.sign-up input').focus();
		}
	}
	$('div.header div.sign-up a').click(function(){
		sendemail(true);
		return false;
	});
	$('div.header div.sign-up input').focus(function(){
		var text = $(this).val();
		if (text == 'Voer je e-mail adres in...') {
			$(this).val('').css('color','#000000');
		}
	});
	$('div.header div.sign-up input').blur(function(){
		var text = $(this).val();
		if (text == '') {
			$(this).val('Voer je e-mail adres in...').css('color','#bababa');
		}
	});
	$('div.header div.sign-up input').keypress(function(e){
    if (e.keyCode == 13) {
    	sendemail(false);
			return false;
    }
	});
	
	// home reflection
	$('img.reflection').reflect();
	
	// home replace
	Cufon.replace('div.home span.event-replace');
	$('div.home span.event-replace').css('display','block');
	
	// newsletter sign up
	$('div.newsletter div.sign-up a').hover(
		function() {
			$('div.newsletter div.sign-up').css('background-position','left -43px');
		}, 
		function() {
			$('div.newsletter div.sign-up').css('background-position','left 0');
		}
	);
	$('div.newsletter div.unsubscribe a').hover(
		function() {
			$('div.newsletter div.unsubscribe').css('background-position','left -43px');
		}, 
		function() {
			$('div.newsletter div.unsubscribe').css('background-position','left 0');
		}
	);
	function sendnewsletter(container,shake) {
		var text = $('div.newsletter div.' + container + ' input').val();		
		var regex = /^[A-Za-z0-9\._%+\-]+\@([A-Za-z0-9\-]+\.)+([A-Za-z0-9]{2,4}|museum)$/;
		if (text != '' && text != 'Voer je e-mail adres in...' && text.match(regex)) {
			$('form.' + container).submit();
		} else {
			$('div.newsletter div.' + container).stop(1,1);
			if (shake == true) {
        $('div.newsletter div.' + container)
          .animate({ left: '-3px' },10)
          .animate({ left: '3px' },50)
          .animate({ left: '-3px' },10)
          .animate({ left: '3px' },50)
          .animate({ left: '-3px' },10)
          .animate({ left: '0' },25);
      }
			$('div.newsletter div.' + container + ' input').focus();
		}
	}
	$('div.newsletter div.sign-up a').click(function(){
		sendnewsletter('sign-up',true);
		return false;
	});
	$('div.newsletter div.unsubscribe a').click(function(){
    sendnewsletter('unsubscribe',true);
		return false;
	});
	$('div.newsletter div.sign-up input').focus(function(){
		var text = $(this).val();
		if (text == 'Voer je e-mail adres in...') {
			$(this).val('').css('color','#000000');
		}
	});
  $('div.newsletter div.unsubscribe input').focus(function(){
    var text = $(this).val();
		if (text == 'Voer je e-mail adres in...') {
			$(this).val('').css('color','#000000');
		}
	});
	$('div.newsletter div.sign-up input').blur(function(){
		var text = $(this).val();
		if (text == '') {
			$(this).val('Voer je e-mail adres in...').css('color','#bababa');
		}
	});
  $('div.newsletter div.unsubscribe input').blur(function(){
    var text = $(this).val();
		if (text == '') {
			$(this).val('Voer je e-mail adres in...').css('color','#bababa');
		}
	});
	$('div.newsletter div.sign-up input').keypress(function(e){
    if (e.keyCode == 13) {
    	sendnewsletter('sign-up',false);
			return false;
    }
	});
	$('div.newsletter div.unsubscribe input').keypress(function(e){
    if (e.keyCode == 13) {
    	sendnewsletter('unsubscribe',false);
			return false;
    }
	});
	
	// event replace
	Cufon.replace('div.eventbox span.event-replace');
	$('div.eventbox span.event-replace').css('display','block');
	
	// show random stage
	var n = $('div.event div.wrapper-left td.stages a.stage-replace').length;
	var number = Math.floor(Math.random() * n);
	$('div.event div.wrapper-left td.stages a.stage-replace').eq(number).css('color','#000000');	
	$('div.event div.wrapper-left td.artists div.stage').eq(number).css('display','block');
	
	// replace stages
	Cufon.replace('div.event div.wrapper-left td.stages a.stage-replace');
	$('div.event div.wrapper-left td.stages a.stage-replace').css('display','block');
	
	// replace info
	Cufon.replace('div.eventbox div.text p.info-replace');
	$('div.eventbox div.text p.info-replace').css('display','block');	
	
	// event line-up remove first and last padding
	$('div.event div.wrapper-left td.stages div:first').css({'padding-top' : '0','border-top' : 'none'});
	$('div.event div.wrapper-left td.stages div:last').css('padding-bottom','0');
	$('div.event div.wrapper-left td.artists div.stage').each(function(){
		if ($.browser.msie && $.browser.version.substr(0,1) <= 7){
			$('div.artist:last', this).css('margin-bottom','0');
		} else {
			$('div.artist:last', this).css({'padding-bottom' : '0','margin-bottom' : '0','border-bottom' : 'none'});
		}		
	});
	
	// photo slider script
	var countimages = $('div.photoslider img').length;	
	var totalimages = countimages - 3;
	var borders = (countimages - 6) * 12;
	var photoslider = (totalimages * 180) + borders + 780;	
	var oddeven = totalimages % 2;
	var extra;
  if (oddeven == 1) {
		extra = 180;
	} else {
		extra = 84;
	}	
	var left = ((30960 - photoslider) / 2) + extra;
	var photoCurrent = (Math.ceil(countimages / 2)) - 1;
	var prev2 = photoCurrent - 2;
	var prev1 = photoCurrent - 1;
	var next1 = photoCurrent + 1;
	var next2 = photoCurrent + 2;
	$('div.photoslider img').css({
		'width':'180px',
		'opacity':'0',
		'margin-top':'40px',
		'z-index':'1'
	});	
	$('div.photoslider img').eq(prev2).css({
		'opacity':'0.7',
		'margin-right':'-57px',
		'z-index':'2'
	});
	$('div.photoslider img').eq(prev1).css({
		'width':'240px',
		'opacity':'1',
		'margin-top':'20px',
		'margin-right':'-63px',
		'z-index':'3'
	});
	$('div.photoslider img').eq(photoCurrent).addClass('index').css({
		'width':'300px',
		'opacity':'1',
		'margin-top':'0',
		'z-index':'4'
	});
	$('div.photoslider img').eq(next1).css({
		'width':'240px',
		'opacity':'1',
		'margin-top':'20px',
		'margin-left':'-63px',
		'z-index':'3'
	});
	$('div.photoslider img').eq(next2).css({
		'opacity':'0.7',
		'margin-left':'-57px',
		'z-index':'2'
	});
	$('div.photoslider').css('left',left + 'px').show();
	// click left
	$('div.photoslider-wrapper a.next').click(function(){
		var index = $('div.photoslider img').index($('.index'));
		var length = $('div.photoslider img').length - 1;
    if (index < length) {
			$('div.photoslider img').stop(1,1);
			$('div.photoslider').stop(1,1);
			if (index == 1 ) {
        var left = parseFloat($('div.photoslider').css('left')) - 129;
      } else {
        var left = parseFloat($('div.photoslider').css('left')) - 192;
      }
			$('div.photoslider').animate({ 
				left: left + 'px'
			}, 250);	
			// hidden > next-2
			$('div.photoslider img').eq(index+3).css('z-index',2).animate({ 
				opacity: 0.7,
				marginLeft: '-57px'
			}, 250);
			// next-2 > next-1
			$('div.photoslider img').eq(index+2).css('z-index',3).animate({ 
				opacity: 1.0,
				width: '240px',
				marginTop: '20px',
				marginLeft: '-63px'			
			}, 250);
			// next-1 > current
			$('div.photoslider img').eq(index+1).css('z-index',4).addClass('index').animate({ 
				width: '300px',
				marginTop: 0,
				marginLeft: 0
			}, 250);
			// current > prev-1
			$('div.photoslider img').eq(index).css('z-index',3).removeClass('index').animate({ 
				width: '240px',
				marginTop: '20px',
				marginRight: '-63px'
			}, 250);
			// prev-1 > prev-2
			$('div.photoslider img').eq(index-1).css('z-index',2).animate({ 
				opacity: 0.7,
				width: '180px',
				marginTop: '40px',
				marginRight: '-57px'
			}, 250);
			// prev-2 > hidden
			$('div.photoslider img').eq(index-2).css('z-index',1).animate({ 
				opacity: 0.0,
				marginRight: 0
			}, 250);
		}
		return false;
	});
	// click right
	$('div.photoslider-wrapper a.previous').click(function(){
		var index = $('div.photoslider img').index($('.index'));    
		if (index > 0) {		
			$('div.photoslider img').stop(1,1);
			$('div.photoslider').stop(1,1);			
			if (index == 2 ) {
        var left = parseFloat($('div.photoslider').css('left')) + 129;
      } else {
        var left = parseFloat($('div.photoslider').css('left')) + 192;
      }
			$('div.photoslider').animate({ 
				left: left + 'px'
			}, 250);			
			// next-2 > hidden
			$('div.photoslider img').eq(index+2).css('z-index',1).animate({ 
				opacity: 0.0,
				marginLeft: 0
			}, 250);
			// next-1 > next-2
			$('div.photoslider img').eq(index+1).css('z-index',2).animate({ 
				opacity: 0.7,
				width: '180px',
				marginTop: '40px',
				marginLeft: '-57px'			
			}, 250);
			// current > next-1
			$('div.photoslider img').eq(index).css('z-index',3).removeClass('index').animate({ 
				width: '240px',
				marginTop: '20px',
				marginLeft: '-63px'
			}, 250);
			// prev-1 > current
			$('div.photoslider img').eq(index-1).css('z-index',4).addClass('index').animate({ 
				width: '300',
				marginTop: 0,
				marginRight: 0
			}, 250);
			// prev-2 > prev-1
			$('div.photoslider img').eq(index-2).css('z-index',3).animate({ 
				opacity: 1.0,
				width: '240px',
				marginTop: '20px',
				marginRight: '-63px'
			}, 250);
			// hidden > prev-2
			$('div.photoslider img').eq(index-3).css('z-index',2).animate({ 
				opacity: 0.7,
				marginRight: '-57px'
			}, 250);
		}
		return false;
	});
	
	// line-up script
	var i = 0;
	$('div.event div.wrapper-left td.stages a.stage-replace').each(function(){
		$(this).attr('rel',i);
		$(this).click(function(){
			$(this).blur();
			$(this).closest('td').find('a').css('color','#bababa');
			$(this).css('color','#000000');
			Cufon.replace('div.event div.wrapper-left td.stages a.stage-replace');			
			var object = $(this).attr('rel');
			$('div.event div.wrapper-left td.artists div.stage').css('display','none');
			$('div.event div.wrapper-left td.artists div.stage').eq(object).css('display','block');
			return false;
		});
		i++;
	});
	$('div.event div.wrapper-left td.artists div.artist div.name').hover(		
		function () {
      $(this).parent('div.artist').prev().css('border-bottom','1px solid #ffffff');
			var height = $(this).height() + 29;
      $('div.tooltip', this).css('bottom',height).css('display','block');
		}, 
		function () {
			$(this).parent('div.artist').prev().css('border-bottom','1px solid #d7d7d7');
      $('div.tooltip', this).css('display','none');
		}
	);																																		
	$('div.event div.wrapper-left td.artists div.artist div.name div.tooltip').mouseover(function(){
		$(this).css('display','none');
	});
	jQuery.preloadImages = function() {
		for(var i = 0; i<arguments.length; i++) {
			jQuery("<img>").attr("src", arguments[i]);
		}
	}
	$.preloadImages('images/event/tooltip-top.png', 'images/event/tooltip-bottom.png');
	
	// event related news remove first line
	$('div.event div.related-news-enjoy div.related-news div.small-article:first').css('background','none');
	
	// events past events remove first styles
	$('div.events div.wrapper-right div.past-event:first').css('background','none');
	
	// events replace
	Cufon.replace('div.events div.wrapper-right div.past-event a span.text span.replace');
	$('div.events div.wrapper-right div.past-event a span.text span.replace').css('display','block');
	Cufon.replace('div.events span.event-replace');
	$('div.events span.event-replace').css('display','block');

  // events archive years
	Cufon.replace('div.events div.event-archive div.years a.replace');
	$('div.events div.event-archive div.years a.replace').css('display','inline');
  Cufon.replace('div.events div.event-archive div.past-event a span.text span.replace');
	$('div.events div.event-archive div.past-event a span.text span.replace').css('display','block');
  $('div.events div.event-archive div.year').each(function(){
    $('div.past-event:lt(2)', this).css('border-top', 'none');
  });
  $('div.events div.event-archive div.year').each(function(){
    $('div.past-event:even', this).css('margin-right', '92px');
  });
	var i = 0;
	$('div.events div.event-archive div.years a.replace').each(function(){
		$(this).attr('rel',i);
		$(this).click(function(){
			$(this).blur();
			$(this).closest('div').find('a').css('color','#bababa');
			$(this).css('color','#000000');
			Cufon.replace('div.events div.event-archive div.years a.replace');
			var object = $(this).attr('rel');
			$('div.events div.event-archive div.year').css('display','none');
			$('div.events div.event-archive div.year').eq(object).css('display','block');
			return false;
		});
		i++;
	});

  // photos event replace
	Cufon.replace('div.photos span.event-replace');
	$('div.photos span.event-replace').css('display','block');

  // photos pagination
  Cufon.replace('div.photos div.pagination a');
	Cufon.replace('div.photos div.pagination strong');
  if ($.browser.msie && $.browser.version.substr(0,1) == 6){
    if ($('div.photos div.pagination div.first').length != 0) {
      $('div.photos div.pagination span.prev').css('margin-left','-117px');
    }
  }

  // photos overlay
  $('div.overlay-photo a.close').live('click', function(){
		$.closeOverlay();
    $('div.overlay-photo div.photo img').attr('src','');
    $('div.overlay-photo a.close').css('background-position','left top');
		return false;
	});

  $('div.photos div.thumbnails a').live('click', function(){
    var currentid = $(this).attr('href').substr(1);
    launchoverlay(currentid);
    return false;
  });

  // photos archive years
	Cufon.replace('div.photos div.photos-archive div.years a.replace');
	$('div.photos div.photos-archive div.years a.replace').css('display','inline');
  Cufon.replace('div.photos div.photos-archive div.past-photos a span.text span.replace');
	$('div.photos div.photos-archive div.past-photos a span.text span.replace').css('display','block');
  $('div.photos div.photos-archive div.year').each(function(){
    $('div.past-photos:lt(2)', this).css('border-top', 'none');
  });
  $('div.photos div.photos-archive div.year').each(function(){
    $('div.past-photos:even', this).css('margin-right', '92px');
  });
	var i = 0;
	$('div.photos div.photos-archive div.years a.replace').each(function(){
		$(this).attr('rel',i);
		$(this).click(function(){
			$(this).blur();
			$(this).closest('div').find('a').css('color','#bababa');
			$(this).css('color','#000000');
			Cufon.replace('div.photos div.photos-archive div.years a.replace');
			var object = $(this).attr('rel');
			$('div.photos div.photos-archive div.year').css('display','none');
			$('div.photos div.photos-archive div.year').eq(object).css('display','block');
			return false;
		});
		i++;
	});

  // submit form contact
  $('div.contact a.submit').click(function(){
    var error = false;
    if ($('input#name').val() == '') {
      $('input#name').css('border', '1px solid #ff0000');
      error = true;
    } else {
      $('input#name').css('border', '1px solid #b2b2b2');
    }
    var email = $('input#email').val();
    var regex = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    if (email != '' && email.match(regex)) {
      $('input#email').css('border', '1px solid #b2b2b2');
    } else {
      $('input#email').css('border', '1px solid #ff0000');
      error = true;
    }
    if ($('input#subject').val() == '') {
      $('input#subject').css('border', '1px solid #ff0000');
      error = true;
    } else {
      $('input#subject').css('border', '1px solid #b2b2b2');
    }
    if ($('textarea#message').val() == '') {
      $('textarea#message').css('border', '1px solid #ff0000');
      error = true;
    } else {
      $('textarea#message').css('border', '1px solid #b2b2b2');
    }
    if (error == true) {
      $('div.contact div.error').css('display', 'block');
    } else {
      $('div.contact form:first').submit();
    }
    return false;
  });

  // open 3voor12 player
  $('div.event div.liveset a').click(function(){
    var url = $(this).attr('href');
    var player = window.open(url,'player','resizable,toolbar=no,location=no,scrollbars=no,width=800,height=409,left=50,top=50');
    player.focus();
    return false;
  });

});

$(window).load(function() {

	if ($('input#launchoverlay').length != 0) {
    var currentid = $('input#launchoverlay').val();
    launchoverlay(currentid);
  }

});

// overlay launch
function launchoverlay(currentid) {
  // get info
  var currentid = currentid;
  var ids = $('input#ids').val().split(',');
  var total = ids.length - 1;
  var filenames = $('input#filenames').val().split(',');
  var image_dir = $('input#image_dir').val();
  var image_dir_parts = image_dir.split('/');
  // get index
  var index;
  for (var i=0;i<ids.length;i++) {
    if (ids[i] == currentid) {
      index = i;
    };
  };
  $('input#index').val(index);

  // photo slide function
  function slide(index) {
    // hide buttons
    if (index == 0) {
      $('div.overlay-photo a.previous').css('visibility','hidden');
    }
    if ((index + 1) == total) {
      $('div.overlay-photo a.next').css('visibility','hidden');
    }
    // set attributes
    $('div.overlay-photo div.photo img').attr('src','t/?src=' + image_dir + filenames[index] + '&h=430&zc=0');
    $('div.overlay-photo a.download').attr('href','images/event_albums/' + image_dir_parts[2] + '/' + filenames[index]);
    $('div.overlay-photo div.number').html((index + 1) + ' / ' + total);
    var thisid = ids[index];
    $('div.overlay-photo div.id').html('id: ' + thisid);
  }

  if (index == 0) {
    $('div.overlay-photo a.previous').css('visibility','hidden');
  }

  // button actions
  if ($('input#firstrun').val() == 0) {
    $('div.overlay-photo a.previous').live('click', function(){
      var gotophoto = parseFloat($('input#index').val()) - 1;
      $('input#index').val(gotophoto);
      slide(gotophoto);
      $('div.overlay-photo a.next').css('visibility','visible');
      $(this).blur();
      return false;
    });
    $('div.overlay-photo a.next').live('click', function(){
      var gotophoto = parseFloat($('input#index').val()) + 1;
      $('input#index').val(gotophoto);
      slide(gotophoto);
      $('div.overlay-photo a.previous').css('visibility','visible');
      $(this).blur();
      return false;
    });
    $(document).keydown(function(e){
      if (e.keyCode == 37 && $('div#jOverlay').length != 0) {
        var gotophoto = parseFloat($('input#index').val()) - 1;
        if (gotophoto >= 0) {
          $('input#index').val(gotophoto);
          slide(gotophoto);
          $('div.overlay-photo a.next').css('visibility','visible');
        }
      }
      if (e.keyCode == 39 && $('div#jOverlay').length != 0) {
        var gotophoto = parseFloat($('input#index').val()) + 1;
        if (gotophoto < total) {
          $('input#index').val(gotophoto);
          slide(gotophoto);
          $('div.overlay-photo a.previous').css('visibility','visible');
        }
      }
    });
    $('input#firstrun').val(1);
  }

  // load picture + overlay
  $('div.overlay-photo div.photo img').attr('src','t/?src=' + image_dir + filenames[index] + '&h=430&zc=0');
  $('div.overlay-photo a.download').attr('href','images/event_albums/' + image_dir_parts[2] + '/' + filenames[index]);
  $('div.overlay-photo div.number').html((index + 1) + ' / ' + total);
  $('div.overlay-photo div.id').html('id: ' + currentid);
  $('div.overlay-photo').jOverlay({opacity : 0.8});
};