$(document).ready(function(){
	
	$(document).pngFix();
	
	// flash inladen
	// als een div aangemaakt wordt met als class "flash",
	// en als inhoud het absolute pad naar de flash en de variable
	// wordt deze automatisch ingeladen
	$('.flash').each(function(){
		slideshowvars = $(this).find('.flashlink').html();
		slideshowvars = slideshowvars.replace(/^<!--/, "");
		slideshowvars = slideshowvars.replace(/-->$/, "");
		slideshowheight = $(this).css('height');
		slideshowwidth = $(this).css('width');
		$(this).html("");
		$(this).flash({
	    	src: slideshowvars,
	    	width: slideshowwidth,
	    	height: slideshowheight,
			wmode: 'transparent'
		});
	});

	$('.ie6_message').each(function(){
		message = $(this).html();
		message = message.replace(/^<!--/, "");
		message = message.replace(/-->/, "");
		$(this).html(message);
	});

	$('.videoplayer').each(function(){
		slideshowvars = $(this).find('.flashlink').html();
		slideshowvars = slideshowvars.replace(/^<!--/, "");
		slideshowvars = slideshowvars.replace(/-->$/, "");
		slideshowheight = $(this).css('height');
		slideshowwidth = $(this).css('width');
		$(this).html("");
		$(this).flash({
	    	src: slideshowvars,
	    	width: slideshowwidth,
	    	height: slideshowheight,
			allowFullScreen: 'true'
		});
	});

    $(document).pngFix().getScript('/public/js/jquery.colorbox-min.js', function()
	{
		//## colorbox ##
		if (typeof $().colorbox == 'function')
		{
			/**
			 *	-- Gebruik --
			 *	Geef een anchor de class .colorbox en de href de locatie van de afbeelding
			 *	Voor een slideshow:
			 *	Geef meerdere anchor's met .colorbox dezelfde rel="" waarde
			 *	Voorbeeld slideshow:
			 *	<a class="colorbox" rel="testblaat" href="/public/img/video2.jpg">klik</a>
			 *	<a class="colorbox" rel="testblaat" href="/public/img/banner.jpg">klik</a>
			 */
			//set
			$('.colorbox').colorbox({
				transition:'elastic'
			});

			/**
			* Html tonen in colorbox:
			* <a class="htmlcolorbox" rel="idvanjehtmldiv">Klik</a>
			*/
			$('.htmlcolorbox').click(function(){
				var targetid = $(this).attr('rel');
				if (typeof targetid == 'undefined' || targetid == '') return ;

				$(this).colorbox({
					open: true,
					inline:true,
					transition:'elastic',
					href:"#" + targetid
				});
			});

			/**
			* Youtube movie tonen in colorbox:
			 *	Voor een slideshow:
			 *	Geef meerdere anchor's met .colorbox dezelfde rel="" waarde
			 *	Voorbeeld slideshow:
			* <a class="youtubecolorbox" rel="testblaat" href="http://www.youtube.com/v/[movie-id]">Klik</a>
			*/
			$('.youtubecolorbox').colorbox({
				iframe:true,
				innerWidth: 640,
				innerHeight:505,
				transition:'elastic'
			});
			


			/* worldmap function */
			function worldmapHover (obj,img,url, target){
				obj.hover(function(){
					$('.worldmap').css('background-image','url('+img+')');
				});

				if(target=='self'){
					obj.click(function(){
						obj.attr('href',url);
						obj.attr('target','_self');
					});
				}
				else
				{
					obj.attr('href',url);

					obj.click(function(){
						obj.colorbox({}, initGMap);
					});
				}
			}

			function initGMap()
			{
				if(typeof($('#mapscontainer > input.noshow').val()) == 'undefined')
				{
					var event = new Array();
					var latValues		= $('#mapscontainer > input.lat');
					var lngValues		= $('#mapscontainer > input.lng');
					var titleValues		= $('#mapscontainer > input.title');
					var cityValues		= $('#mapscontainer > input.city');
					var locationValues	= $('#mapscontainer > input.location');
					var dateValues		= $('#mapscontainer > input.date');
					var timeValues		= $('#mapscontainer > input.time');
					var ticketValues	= $('#mapscontainer > input.ticket_url');
					var urlValues		= $('#mapscontainer > input.url');
					var country			= $('#mapscontainer > input.country').val();

					for(var i=0; i<latValues.length; i++)
					{
						event[i] = new Array();
						event[i]['lat']			= $(latValues[i]).val();
						event[i]['lng']			= $(lngValues[i]).val();
						event[i]['title']		= $(titleValues[i]).val();
						event[i]['location']	= $(locationValues[i]).val();
						event[i]['city']		= $(cityValues[i]).val();
						event[i]['date']		= $(dateValues[i]).val();
						event[i]['time']		= $(timeValues[i]).val();
						event[i]['ticket_url']	= $(ticketValues[i]).val();
						event[i]['url']			= $(urlValues[i]).val();
						event[i]['info']		= '';
					}

					gmaps.init('#mapscontainer', country);

					var curLng = 0;
					var curLat = 0;

					for(var i in event)
					{
						curLng = event[i]['lng'];
						curLat = event[i]['lat'];

						event[i]['info'] += '<div style="width: 98%; float: left; clear: left; margin: 5px 0 0 5px;overflow: visible;"><h1 style="color: #000000;font-size:20px;padding: 0 0 0 0;margin: 0 0 5px 0;width: 250px;">'+event[i]['title'] + '</h1>' +
							'<div id="bodyContent" style="color: #000000;">'+
							event[i]['location']+ '<br />' +
							event[i]['city']+ '<br />' +
							event[i]['date'] + "<br />" +
							event[i]['time']+ '<br />' +
							'<div style="float:left;clear:left;width: 100px; margin: 5px 0 0 0;">' + event[i]['url']+ '</div>' +
							'<div style="float:left;text-align: right; margin: 5px 0 0 0;">' + event[i]['ticket_url']+ '</div>' +
							'</div></div>';
						if(event[parseInt(i)+1] != undefined && curLng == event[parseInt(i)+1]['lng'] && curLat == event[parseInt(i)+1]['lat'])
						{
							event[parseInt(i)+1]['info'] += event[i]['info'] + '<hr style="float:left;clear:left;margin: 5px 0;color: #000000;width: 99%;" />';
						}
						else
							gmaps.addMarker(event[i]);
					}
				}
				else
				{
					$('#mapscontainer').width(600);
					$('#mapscontainer').height(40);
					$('#mapscontainer').html('<div style="color: #000; margin: 300px 0 0 0; text-align: center;width: 600px; height: 40px;">'+$('#mapscontainer > input.noshow').val()+' '+$('#mapscontainer > input.country').val()+'</div>');
				}
			}

			/* worldmap world */
			worldmapHover ($('.js_northamerica'),'/public/img/worldmap/northamerica_hover.png','tickets/north-america', 'self');
			worldmapHover ($('.js_southamerica'),'/public/img/worldmap/southamerica_hover.png','tickets/south-america', 'self');
			worldmapHover ($('.js_europe'),'/public/img/worldmap/europe_hover.png','tickets/europe', 'self');
			worldmapHover ($('.js_africa'),'/public/img/worldmap/africa_hover.png','tickets/africa', 'self');
//			worldmapHover ($('.js_asia'),'/public/img/worldmap/asia_hover.png','tickets/asia', 'self');
			worldmapHover ($('.js_australia'),'/public/img/worldmap/australia_hover.png','tickets/australia', 'self');

			/* worldmap northamerica */
			worldmapHover ($('.js_usa'),'/public/img/worldmap/usa_hover.png','/public/ajax/tickets/locations.php?country=us', 'colorbox');
			worldmapHover ($('.js_canada'),'/public/img/worldmap/canada_hover.png','/public/ajax/tickets/locations.php?country=ca', 'colorbox');
			worldmapHover ($('.js_mexico'),'/public/img/worldmap/mexico_hover.png','/public/ajax/tickets/locations.php?country=mx', 'colorbox');

			/* worldmap southamerica */
			worldmapHover ($('.js_chile'),'/public/img/worldmap/chile_hover.png','/public/ajax/tickets/locations.php?country=cl', 'colorbox');
			worldmapHover ($('.js_brasil'),'/public/img/worldmap/brasil_hover.png','/public/ajax/tickets/locations.php?country=br', 'colorbox');
			worldmapHover ($('.js_argentina'),'/public/img/worldmap/argentina_hover.png','/public/ajax/tickets/locations.php?country=ar', 'colorbox');
//			worldmapHover ($('.js_costa-rica'),'/public/img/worldmap/costa-rica_hover.png','/public/ajax/tickets/locations.php?country=cr', 'colorbox');

			/* worldmap europe */
			worldmapHover ($('.js_netherlands'),'/public/img/worldmap/netherlands_hover.png', '/public/ajax/tickets/locations.php?country=nl', 'colorbox');
			worldmapHover ($('.js_belgium'),'/public/img/worldmap/belgium_hover.png', '/public/ajax/tickets/locations.php?country=be', 'colorbox');
			worldmapHover ($('.js_germany'),'/public/img/worldmap/germany_hover.png', '/public/ajax/tickets/locations.php?country=de', 'colorbox');
			worldmapHover ($('.js_austria'),'/public/img/worldmap/austria_hover.png', '/public/ajax/tickets/locations.php?country=at', 'colorbox');
			worldmapHover ($('.js_swiss'),'/public/img/worldmap/swiss_hover.png', '/public/ajax/tickets/locations.php?country=ch', 'colorbox');
			worldmapHover ($('.js_france'),'/public/img/worldmap/france_hover.png', '/public/ajax/tickets/locations.php?country=fr', 'colorbox');
			worldmapHover ($('.js_england'),'/public/img/worldmap/england_hover.png', '/public/ajax/tickets/locations.php?country=gb', 'colorbox');
			worldmapHover ($('.js_ireland'),'/public/img/worldmap/ireland_hover.png', '/public/ajax/tickets/locations.php?country=ie', 'colorbox');
			worldmapHover ($('.js_denmark'),'/public/img/worldmap/denmark_hover.png', '/public/ajax/tickets/locations.php?country=dk', 'colorbox');

			/* worldmap africa */
			worldmapHover ($('.js_south-africa'),'/public/img/worldmap/south-africa_hover.png', '/public/ajax/tickets/locations.php?country=za', 'colorbox');

			/* worldmap asia */
//			worldmapHover ($('.js_japan'),'/public/img/worldmap/japan_hover.png', '/public/ajax/tickets/locations.php?country=jp', 'colorbox');

			/* worldmap australia */
			worldmapHover ($('.js_australiacount'),'/public/img/worldmap/australiacount_hover.png', '/public/ajax/tickets/locations.php?country=au', 'colorbox');
			worldmapHover ($('.js_new-sealand'),'/public/img/worldmap/new-sealand_hover.png', '/public/ajax/tickets/locations.php?country=nz', 'colorbox');


			function loadVideo()
			{
				$('.colorboxvideo').each(function(){
					slideshowvars = $(this).find('.videoplayerlink').html();
					slideshowvars = slideshowvars.replace(/^<!--/, "");
					slideshowvars = slideshowvars.replace(/-->$/, "");
					slideshowheight = $(this).css('height');
					slideshowwidth = $(this).css('width');

					$(this).html("");
					$(this).flash({
						src: slideshowvars,
						width: slideshowwidth,
						height: slideshowheight,
						allowFullScreen: 'true'
					});
				});
			}

			/**
			 * Flv movie tonen in colorbox:
			 *	Voor een slideshow:
			 *	Geef meerdere anchor's met .colorbox dezelfde rel="" waarde
			 *	Voorbeeld slideshow:
			 * <a class="flvcolorbox" rel="testblaat" href="/public/ajax/media/video_player.php?movie=<?=urlencode("/pad/naar/bestand.flv")?>">Klik</a>
			*/
			$('.flvcolorbox').colorbox({
				innerWidth: 725,
				innerHeight:429,
				transition:'elastic'
			}, loadVideo);
		}

		$.getScript('/public/js/jquery.jtweetsanywhere-1.2.1.min.js', function(xhr){
			try {
				initjTweet();
			} catch(err) {
				eval(xhr);
				initjTweet();
			}
		});
	});

	//## Klapblokken ##

	// subs in/uitklappen
	$('.uitklaplink').click(function () {
		var obj = $(this).parents('.uitklapblok:first').find('.uitklapdetail:first');

		if (!$(this).hasClass('keepothers')) //andere blokken sluiten
		{
			$(this).parents('.uitklapblokken').find('.uitklapdetail').each(function(){
				if (!$(this).hasClass('dontclose')) //dit blok niet altijd open houden
				{
					$(this).slideUp();
					$(this).find('.uitklaplink').removeClass('open');
				}
			})
		}

		//overal active verwijderen
		$(this).parents('.uitklapblokken').find('.uitklaplink').removeClass('active');

		if (obj.css('display')=='none') //geklikt item openen
		{
			obj.slideDown();
			$(this).addClass('active');
			$(this).addClass('open');
		}
		else //geklikt item sluiten
		{
			obj.slideUp();
			$(this).removeClass('open');
		}
	});
	//cookies opslaan bij verlaten van pagina
	$(window).unload(function(){
		$('.uitklapblokken').each(function(){
			if ($(this).hasClass('enablecookie'))
			{
				$(this).find('.uitklaplink').each(function()
				{
					var id = $(this).attr('id');
					if (id != undefined && id)
					{
						if ($(this).hasClass('open'))
							$.cookie(id, 1);
						else
							$.cookie(id, 0);
					}
				});
			}
		});
	});
	//cookies uitlezen
	$('.uitklapblokken').each(function(){
		if ($(this).hasClass('enablecookie'))
		{
			$(this).find('.uitklaplink').each(function()
			{
				var id = $(this).attr('id');
				if (id != undefined && id)
				{
					if ($.cookie(id) == 1) //was uitgeklapt
					{
						if (!$(this).hasClass('open'))
							$(this).trigger('click'); //uitklappen
					}
				}
			});
		}
	});

	// links in "_blank" i.p.v. via HTML i.v.m. valid XHTML
	$('.blankwindow').attr('target','_blank');

	//field hints
	$(".hintfield").each(function(i) {
        var beginStyle = $(this).attr("style");
		$(this).addClass("hint");
		$(this).val($(this).attr("title"));

        $(this).bind("focus", function(e) {
            if ($(this).val() == $(this).attr("title")) {
                $(this).val("");
				$(this).removeClass("hint");
            }
        });
        $(this).bind("blur", function(e) {
            if ($(this).val() == "") {
				$(this).addClass("hint");
				$(this).val($(this).attr("title"));
            } else {
				$(this).css("border-color", "");
				$(this).removeClass("hint");
			}
        });
    });
	

	//submit form link
	$('.submitform').click(function(){
		$("input[name=no_bots_accepted]").val("");
		$("input[name=redirect]").val("");
		$(this).parents('form:first').submit();
		$(this).hide();
		return false;
	})

	//submit form link
	$('.submitformredirect').click(function(){
		$("input[name=no_bots_accepted]").val("");
		$(this).parents('form:first').submit();
		$(this).hide();
		return false;
	})



	//AJAX FORMS automatisch
	if (typeof $().ajaxForm == 'function')
	{
		$.fn.initAjaxForms = function(){
			$('form.ajaxform').each(function(){
				$(this).append('<input type="hidden" name="ajax_post" value="oke" />');
				var form = $(this);
				$(this).ajaxForm({
					dataType: 'json',
					beforeSerialize: function(){
						form.find('.hint').val('');
					},
					beforeSubmit: function(){

						form.parent().find('.error').remove();

						//vereiste velden
						form.find('.required').each(function(){
							if ($(this).val().replace(' ', '') == '' || $(this).hasClass('hint')) //leeg of bevat nog de hint
							{
								return false;
								$(this).addClass('formerror');
							}
							else
								$(this).removeClass('formerror');
						});
						form.find('.required.email').each(function(){
							var regex = /^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/;
							if (!regex.test($(this).val()))
							{
								return false;
								$(this).addClass('formerror');
							}
						});
					},
					success: function(response){
						if (response.success)
						{
							if(response.hideform)
							{
								form.hide();
								$(".form_replacement").html(response.html);
								$(".backlink").click(function(){
									$(".form_replacement").hide();
									form.show();
								});
								$(".form_replacement").show();
							}
							else if(response.clearform)
							{
								form.remove();
								$(".form_replacement").html(response.html);
								$(".form_replacement").show();
							}
							else if(response.url)
								document.location = response.url;
							else
								form.html('<p>'+response.melding+'</p>');
						}
						else if (response.melding){
							form.before($('<div class="error"><p>'+response.melding+'</p></div>'));
						}
						else if (response.errors){
							form.before($('<div class="error"><p>'+response.errors+'</p></div>'));
						}
						form.find('.hint').trigger('blur');
						form.find('.submitform').show();
						$(this).initAjaxForms();
					},
					error: function()
					{
						form.find('.submitform').show();
					}
				})
			});
		}
		$(this).initAjaxForms();
	}

	$(".js_playlist > .track:first").each(function(){
		var filepath = $(this).children(".file").html();
		filepath = filepath.replace(/^<!--/, "");
		filepath = filepath.replace(/-->$/, "");

		$(".flashplaylist").each(function(){
			var slideshowvars = $('.playerlink').html();
			slideshowvars = slideshowvars.replace(/^<!--/, "");
			slideshowvars = slideshowvars.replace(/-->$/, "");
			slideshowheight = $(this).css('height');
			slideshowwidth = $(this).css('width');

			if(filepath != "")
			{
				$(this).flash({
					id: 'playlistplayer',
					src: slideshowvars+"?audioPad="+filepath+"&amp;kleur=0x000000&amp;autostart=false",
					width: slideshowwidth,
					height: slideshowheight,
					wmode: 'transparent'
				});
			}
		});
	});

	$(".js_playlisttrack").click(function(){
		var filepath = $(this).children(".file").html();
		filepath = filepath.replace(/^<!--/, "");
		filepath = filepath.replace(/-->$/, "");

		$(".flashplaylist").each(function(){
			var slideshowvars = $('.playerlink').html();
			slideshowvars = slideshowvars.replace(/^<!--/, "");
			slideshowvars = slideshowvars.replace(/-->$/, "");
			slideshowheight = $(this).css('height');
			slideshowwidth = $(this).css('width');
			
			if(filepath != "")
			{
				$("#playlistplayer").remove();
				$(this).flash({
					id: "playlistplayer",
					src: slideshowvars+"?audioPad="+filepath+"&amp;kleur=0x000000&amp;autostart=true",
					width: slideshowwidth,
					height: slideshowheight,
					wmode: "transparent"
				});
			}
		});
		if(filepath != "")
		{
			var i = $(this).children("input[name='tracknumber']").val();
			if(i != undefined)
			{
				$('.js_infoitem').hide();
				$('.js_infoitem_'+i).show();
			}

			$(".js_playlisttrack").children(".playingtrack").addClass("hidden");
			$(".js_playlisttrack").children(".playtrack").addClass("visible");
			$(".js_playlisttrack").children(".playtrackicon").removeClass("visible");
			$(this).children(".playtrack").removeClass("visible");
			$(this).children(".playingtrack").removeClass("hidden");
			$(this).children(".playtrackicon").addClass("visible");
		}
	});


	if($(".playlist").height() > $("#cdplayer").height())
		$("#tracklistbackground").height($(".playlist").height()+20);
	else
		$("#tracklistbackground").height($("#cdplayer").height()+45);
});

function initjTweet()
{
	$('#jTweets').jTweetsAnywhere({
						username: 'andrerieu',
						count: 3,
						showFollowButton: true
	});
}

/**
 * slide an element up without causing blinking flash content
 */
function slideUp(object, speed, slideTime)
{
    if (parseInt(speed) == NaN) return false;
    if (object.jquery == undefined) object = $(object);
	if (object.height() <= 0) return true;
    object.css('overflow', 'hidden');
    var ppf = (object.height() / speed * 20);
    var orgh = object.height();
    var timeout = setInterval(function(){
        object.height(object.height() - ppf);
        if (object.height() <= 0){
            object.hide();
            object.height(orgh);
            clearInterval(timeout);
        }
    }, 10);
    return true;
}

/**
 * slide an element down without causing blinking flash content
 */
function slideDown(object, speed)
{
    if (parseInt(speed) == NaN) return false;
    if (object.jquery == undefined) object = $(object);
    object.css('overflow', 'hidden');
    var ppf = (object.height() / speed * 20);
    var orgh = object.height();
    object.height(0);
    object.show();
    var timeout = setInterval(function(){
        object.height(object.height() + ppf);
        if (object.height() >= orgh){
            object.height(orgh);
            clearInterval(timeout);
        }
    }, 10);
    return true;
}
