/**
 * Javascript init file
 * Initiates all javascript functionality used on this site
 *
 * @version 1.0
 * @copyright Sitemind Internet & Nieuwe Media, 18 February, 2010
 **/

/**
 * Cufon replacement
 **/
Cufon.replace('.replace');

/**
 * Banner functions
 * prevents statusbar from showing the link of the banner
 */
function ss()
{
	window.status = '';
	return true;
}

/**
 * Internet Explorer does not have Array.indexOf implemented
 * 
 * @param {Mixed} obj The object to find
 * @return {Number} The index of obj or -1
 */
if(!Array.indexOf) {
	Array.prototype.indexOf = function(obj) {
		for(var i = 0; i < this.length; i++) {
			if(this[i] == obj) {
				return i;
			}
		}
		return -1;
	}
}



$(document).ready(function(){
	
	/**
	 * Autoclear search field
	 */
	$("#q")
	.click(function() {
		if ($(this).val() == "Zoeken") $(this).val("");
	})
	.blur(function() {
		if ($(this).val() == "") $(this).val("Zoeken");
	});
	
	/**
	 * Homepage themeblocks
	 */
	if ($("#jongin-theme-youtube").length > 0){
		$("#jongin-theme-youtube").colorbox({iframe:true, innerWidth:425, innerHeight:344});
	}
	
	if ($("#slider-holder").length > 0){
		$("#slider").easySlider({
			auto: true,
			continuous: true,
			speed: 700,
			pause: 5000,
			numeric: true,
			numericId: 'controls',
			prevText: 'Vorige',
			nextText: 'Volgende'
		});
	}
	
	/**
	 * Win actie
	 **/
	if($('#win_yt_player').length > 0)
	{
		var win_player = new YouTubePlayer('win_yt_player', {
		videoId: false,
		width: 460,
		height: 282,
		autoPlay: false,
		onEnd: function(p) {
		},
		onPlay: function(p) {		
			$('#video_meta_duration').html(this.getDuration());
			$('#video_meta_duration').time_from_seconds();
			$('#win_pause_button span').html('Pauze');
		},
		onPause: function(p) {
			$('#win_pause_button span').html('Afspelen');
		}});
		
		$('#win_pause_button').click(function(){
			win_player.togglePlay();
			return false;
		});
		
		$('#youtube_link').blur(function(){	
			
			var regex = /http\:\/\/www\.youtube\.com\/watch\?v=([\w-]{11})/;
			//regex = /http\:\/\/www\.youtube\.com\/watch\?v=(\w{11})/;
			var url = $('#youtube_link').val();
			var yt_match = url.match(regex);
			var youtube_id = (yt_match != null) ? (yt_match[1]) : (url);
			
			if(youtube_id.length > 0) {
				win_player.loadVideoById(youtube_id);
				$('#win_yt_holder').animate({'height' : '326px'});
			}			
		});

	}
	
	/**
	 * Experience button add
	 */
	if($('.button-add').length > 0) {
		$('.button-add').click(function(){
			$('.exp-form').slideToggle();
			return false;
		})
	}
	
	
	/**
	 * Poll submit
	 */
	if($('.poll-vote-btn').length > 0) {
		$('.poll-vote-btn').click(function(){
			$(this).prev().submit();
			return false;
		})
	}
	
	/**
	 * Spotlight tabs
	 */
	if ($("#spotlight-tabs").length > 0){
		$("#spotlight-tabs").tabs({ event: 'mouseover' });
	}
	
	/**
	 * Bios tabs
	 */
	if ($(".bios-toggle").length > 0){
		$('.bios-accordion').accordion({header: '.bios-toggle', collapsible: true, autoHeight: false });
	}
	
	/**
	 * Colorbox implementation
	 */
	if($("a[class='window-popup']").length) {
		$("a[class='window-popup']").colorbox({iframe:true, title:" ", width: '580px', height: '550px', initialWidth: '150px', initialHeight: '150px'});
	}
	if($("a[class~='hyves-popup']").length) {
		$("a[class~='hyves-popup']").colorbox({iframe:true, scrolling: true, title:" ", width: '580px', height: '500px', initialWidth: '150px', initialHeight: '150px'});
	}
	if($("a[class='image-popup']").length) {
		$("a[class='image-popup']").colorbox({title:" ", initialWidth: '150px', initialHeight: '150px'});
	}
	
	/**
	 * Auto shorten experiences using jQuery plugin expander
	 */
	if($('.exp-item.expander').length > 0) {
		$('.exp-item.expander>div').expander({
			slicePoint: 150, 
			widow: 8,
			expandEffect: 'show',
			expandText: 'lees meer',
			expandPrefix: '&hellip;',
			userCollapseText: '(minder)',
			afterExpand: function(el) {
				current_id = $(el).parent().attr('id')
				$('#react_' + current_id).show();
			},
			onCollapse: function(el) {
				current_id = $(el).parent().attr('id')
				$('#react_' + current_id).hide();
			}
		});
		
		$('.exp-item').click(function(){
			if($('span.details', $(this)).length == 0) {
				current_id = $(this).attr('id');
				$('#react_' + current_id).toggle();
			};
		})
	}

	/**
	 * Stream holder
	 */
	if(('.stream-btn').length > 0) {
		$('.stream-btn').click(function(){
			var stream_pos = $('.stream-holder').position();
			
			if(stream_pos.top > -130) {
				$('.stream-holder').animate({top: '-130'});
			} else {
				$('.stream-holder').animate({top: '0'});
			}
			return false;
		});
	}
	
	/**
	 * Event quick view
	 */
	if(('.event-row').length > 0) {
		$('.event-row').mouseover(function(){
			var pos = $(this).position();

			var q_data = $('#quickview_data_' + $(this).attr('id')).html();

			$('#event_quickview:hidden').show();

			$('#event_quickview_data').html(q_data);
			$('#event_quickview').stop();
			$('#event_quickview').animate({top: pos.top-24})
		})
	}	
	
	/**
	 * Photoalbum arrow navigation
	 */
	/*if($('.photobase .big-image').length >  0) {
		
		var current_src = $('.photobase .big-image').attr('src').lastIndexOf("_");
		current_src = $('.photobase .big-image').attr('src').substr(0, current_src) + '_';
		
		$(document).keydown(function(e) {
			
			var current_src_loc = new String(document.location);
			current_src_loc = current_src_loc.substr(0, current_src_loc.lastIndexOf("/")) + '/';
			
			var code = (e.keyCode ? e.keyCode : e.which);
			
			var next_id = parseInt(current_no) + 1;
			var prev_id = parseInt(current_no) - 1;
			
			var next_url = current_src + next_id + '.jpg';
			var prev_url = current_src + prev_id + '.jpg';
			
			var prev_url_btn = current_src_loc + (prev_id-2) + '.html#photo';
			var next_url_btn = current_src_loc + (next_id-2) + '.html#photo';
			
			switch(code) { 
				// User pressed "left" arrow
				case 37:
				if((current_no - 1) <= 0) {
					return false;
				}
				if((current_no - 1) == 1) {
					$('#btn-prev').hide();
				} else {
					$('#btn-prev').show();
					$('#btn-next').show();
				}
				current_no--;
				
				if(prev_url != '') {
					$('.photobase .big-image').attr('src', prev_url);
					$('#btn-next').attr('href', next_url_btn);
					$('#btn-prev').attr('href', prev_url_btn);
				}
				break;
				
				// User pressed "right" arrow
				case 39:
				if((current_no + 1) > max_no) {
					return false;
				}
				if((current_no + 1) == max_no) {
					$('#btn-next').hide();
				} else {
					$('#btn-next').show();
					$('#btn-prev').show();
				}
				current_no++;
				
				if(next_url != '') {
					$('.photobase .big-image').attr('src', next_url);
					$('#btn-next').attr('href', next_url_btn);
					$('#btn-prev').attr('href', prev_url_btn);
				}
				
				break;
			}
			$('.photobase .current').html(current_no);
		});
	}*/
	
	
	/**
	 * News pagination
	 */	
	if($('#news-block').length > 0){
		
		var current_page = $('.controls').attr('id')/10;
		$('#news_later, #news_earlier').click(function(){
			var act_id = $(this).attr('id');
			var use_anim = '';
			
			if(act_id == 'news_later') {
				current_page++;
				use_anim_out = '-=220px';
				use_anim_in = '222px';
			} else {
				if(current_page - 1 < 0) {
					return false;
				}
				current_page--;
				use_anim_out = '+=220px';
				use_anim_in = '-222px';
			}
			
			$('#news_loading').show();
			
			$('#news-block ul').animate({left: use_anim_out}, "fast", function(){
				$.get('nieuws/loadarchive/' + current_page + '.html', function(data) {						
					$('#news_loading').hide();
					$('#news-block ul').html(data);
					$('#news-block ul').css({left: use_anim_in})
					$('#news-block ul').animate({left: '0', opacity: 1});
				});
			});
			return false;
		});
	}
	
	/**
	 * Links block expand/collapse
	 */
	if($('#toggle_links').length > 0 ){
		$('#toggle_links').click(function(){
			
			if($('#overflow-box').height() > 105) {
				// slide up
				var total = $('#overflow-box ul li').length;
				$("#overflow-box").animate( { height: 98 }, 1500 , function() {
					$('#toggle_links').removeClass("expanded");
					$('#toggle_links').html("en nog "+(total-5)+" links meer");
				}); 								
			} else {
				//slide down
				var slide_to = $('#link-list').height();
				var new_slide_to = parseInt(slide_to + 14);
				
				$("#overflow-box").animate( { height: new_slide_to + 'px'  }, 1500, function(){
					$('#toggle_links').addClass("expanded");
					$('#toggle_links').html("inklappen");
				});
			}			
			return false;
		});
	}
});

