
// constructor function //called from /includes/js/HU-GLOBAL/functions.js
function initSite() {	
    /*jQuery('.innerlabel', '.search').inputLabel();
    jQuery('.innerlabel', '#research').inputLabel();*/
	if (jQuery('#hu-showcase').length != 0) { initShowcase(); }
	if (jQuery('#ctrSearchfilter').length != 0) { initSearchFilters(); }
	if (jQuery('#persona').length != 0) { initPersona(); }
	slideShow();
	//init lightbox
	jQuery("a[rel='cboximages']").colorbox({
		slideshow:true,
		width:"75%",
		height:"75%",
		slideshowAuto:false,
		opacity:".8"
	});
}

//lightbox loader
function loadRestOfGallery() {
	jQuery.get('http://bachelors.hu.local18/handlers/hu-global/photogallery.ashx?id={62DB8789-F3E0-4245-863A-F37A390D27BB}', function(response) {
        var $html = jQuery(response);
		console.log($html);
        //$html.find('a.usr').colorbox({ width: '960px', height: '90%', iframe: true });
        //$('#results').append($html);
    },"html");
}

//tabs
function inittabs() {
	jQuery('#tabs').tabs();
	var $tabs = jQuery('#tabs').tabs();
	if (jQuery('.ui-tabs-nav li', '#tabs').length > 1) {
		jQuery('#tabs1').children(":last").prepend(
			jQuery(document.createElement('a'))
				.addClass('morelink')
				.attr('href', 'javascript:void(0)')
				.append(jQuery(document.createTextNode('Uitgebreide informatie')))
				.bind("click", function(){$tabs.tabs('select', 1)})
		);
		jQuery('#tabs2').children(":last").append(
			jQuery(document.createElement('a'))
				.addClass('backbutton')
				.attr('href', 'javascript:void(0)')
				.append(jQuery(document.createTextNode('Terug')))
				.bind("click", function() { $tabs.tabs('select', 0) })
		);
	}
}

//init showcase
function initShowcase() {

    jQuery('ul.thumbs', '#lead').children().each(
		function() { initthumb(this); }
	);
    jQuery('ul.thumbs', '#proflisting').children().each(
		function() { initthumb(this); }
	);

    function initthumb(e) {

		//show popup
		jQuery(e).find("span.video")
			.append(jQuery(document.createElement('img')).attr('src','/includes/img/HU-GLOBAL/playbut.png').addClass('play'))
			.mouseenter(function() {jQuery(this).find(".play").attr('src','/includes/img/HU-GLOBAL/playbutover.png');})
    		.mouseleave(function() {jQuery(this).find(".play").attr('src','/includes/img/HU-GLOBAL/playbut.png');})
			.click(
				function() {

					//check if swfobject embedding has taken place 
					if (jQuery(this).parent().find('object').length == 0) { return false };
					//set the currentflashID to pass to flash video player
					currentflashID = jQuery(this).siblings(':last').find('object').attr('id');

					jQuery(this).siblings(":last").css({
						display: 'block',
						opacity: 0.7
					}).animate({
						width: '380px',
						height: '310px',
						top: '0',
						left: '20px',
						paddingTop: '15px',
						paddingBottom: '15px',
						paddingLeft: '10px',
						paddingRight: '10px',
						opacity: 1
					}, { duration: 400 });
				}
			);

        //add close button and set behaviour
        jQuery(e).children(":last").append(
			jQuery(document.createElement('a'))
			.addClass('closebutton')
			.attr('href', 'javascript:void(0)')
			.click(function() {
				jQuery(this).parent().animate({
					width: '0',
					height: '0',
					top: '125px',
					left: '220px',
					marginTop: '15px',
					opacity: '0.1',
					paddingTop: '0',
					paddingBottom: '0',
					paddingLeft: '0',
					paddingRight: '0'
				}, { duration: 600,
					easing: 'easeOutExpo',
					complete: function() { window.document[currentflashID].pauseMovie(); jQuery(this).hide(); }
				});
			})
		);

    } //close init-thumb function
		    	    
} //end initShowcase()


function initSearchFilters()
{
	target = jQuery('div.filters', '#ctrSearchfilter');
			
	//BEGIN EXPAND
	target.find('h3 a').attr('href', 'javascript:void(0)');
	if (target.children('ul').size() != 0) {
		target.children('ul').each(
		function()
		{
			//Hide Collapsed UL
			if (jQuery(this).attr('class') == "collapsed") {
				if (jQuery(this).find('li.active').size() == 0) {
					jQuery(this).hide().attr('class', 'collapsed');
				}
			}
			else {
				//Hide listitems
				jQuery(this).find('li.hide').hide()

				//Create more button
				if (jQuery(this).find('li.hide').size() != 0) {
					jQuery(this).append(
				jQuery(document.createElement('li'))
					.append(
						jQuery(document.createElement('a'))
							.attr('href', 'javascript:void(0)')
							.append(
								jQuery(document.createTextNode('meer...'))
							)
							.click(function()
							{

								jQuery(this).parent().siblings(':hidden').slideDown(400).end().hide()

							})
						)
					)
				}
			}
		});

		jQuery('h3', target).click(function()
		{
			var next = jQuery(this).next();
			if (next.is('ul')) {
				jQuery(this).toggleClass('collapsed');
				next.slideToggle();
			}
		});
	}
	//END EXPAND

}

//init weblog listing in persona
function initPersona() {

	if (jQuery('.flickrthumbs', '#persona').length > 0) {
		var target = jQuery('.flickrthumbs', '#persona');

		target.addClass('clearfix');
		target.children().find('a').attr('href', 'javascript:void(0)');
		target.children().find('a>img').css({ width: '60px' }).end().find('div').hide();

		target.children().find('p.comments').after(
		jQuery(document.createElement('a'))
			.addClass('closebutton')
			.attr('href', 'javascript:void(0)')
			.append(jQuery(document.createTextNode('sluiten')))
	);
	
	target.children().find('a.closebutton').hide();

	target.children().toggle(
	function() {

		jQuery(this).siblings().hide();
		var all = jQuery(this).parent().find(">li");
		var position = "0";
		if (all.index(this) == 1) { position = "72px"; }
		if (all.index(this) == 2) { position = "144px"; }
		if (all.index(this) == 3) { position = "216px"; }

		jQuery(this).css({ // make <li> wider 
			width: "285px"
		}).children(':first').css({ // give margin to <a> 
			marginLeft: position
		}).animate({ // animate <a> to full width
			width: "285px",
			marginLeft: "0px"
		}).children(':first').animate({ // animate <img> to full width
			width: "285px"
		});

		jQuery(this).children('div.reactions').show();
		jQuery(this).children('a.closebutton').show();

	},
	function() {

		var all = jQuery(this).parent().find(">li");
		var position = "0";
		if (all.index(this) == 1) { position = "72px"; }
		if (all.index(this) == 2) { position = "144px"; }
		if (all.index(this) == 3) { position = "216px"; }

		jQuery(this).css({ // make <li> original width
			width: "60px"
		}).children(':first').animate({ // animate <a> to original width
			width: "60px",
			marginLeft: position
		}).children(':first').animate({	// animate <img> to original width
			width: "60px"
		}, "1500",
			function() {
				jQuery(this).parent().css({ margin: "0" });
				jQuery(this).parent().parent().siblings().show();
			}
		);

		jQuery(this).children('div.reactions').hide();
		jQuery(this).children('a.closebutton').hide();
	});
}


if (jQuery('.youtubethumbs', '#persona').length > 0) {
	var target = jQuery('.youtubethumbs', '#persona');

	target.addClass('clearfix');
	target.children().find('a').attr('href', 'javascript:void(0)');
	target.children().find('a>img').css({ width: '60px' }).end().find('div').hide();
	target.children().find('div.movie').css({ position: 'absolute', zIndex: '1', top: '0', left: '0' }).hide();

	target.children().find('p.comments').after(
	jQuery(document.createElement('a'))
		.addClass('closebutton')
		.attr('href', 'javascript:void(0)')
		.append(jQuery(document.createTextNode('sluiten')))
);
target.children().find('a.closebutton').hide();


	target.children().toggle(
	function() {

		jQuery(this).siblings().hide();

		var all = jQuery(this).parent().find(">li");
		var position = "0";
		if (all.index(this) == 1) { position = "72px" }
		if (all.index(this) == 2) { position = "144px" }
		if (all.index(this) == 3) { position = "216px" }


		jQuery(this).css({ // make <li> wider 
			width: "285px"
		}).children(':first').css({ // give margin to <a> 
			marginLeft: position
		}).animate({ // animate <a> to full width and fixed height (equal to movie)
			width: "285px",
			height: "231px",
			marginLeft: "0px"
		}).children(':first').animate({ // animate <img> to full width and fixed height (equal to movie)
			width: "285px",
			height: "231px"
		}, "1500",
			function() {
				jQuery(this).parent().parent().children('div.movie').show();
			}
		);

		jQuery(this).children('div.reactions').show();
		jQuery(this).children('a.closebutton').show();


	},
	function() {

		var all = jQuery(this).parent().find(">li");
		var position = "0";
		if (all.index(this) == 1) { position = "72px"; }
		if (all.index(this) == 2) { position = "144px"; }
		if (all.index(this) == 3) { position = "216px"; }


		jQuery(this).css({ // make <li> original width
			width: "60px"
		}).children(':first').animate({ // animate <a> to original width and auto height
			width: "60px",
			height: "100%",
			marginLeft: position
		}).children(':first').animate({	// animate <img> to original width and auto height
			width: "60px",
			height: "100%"
		}, "1500",
			function() {
				jQuery(this).parent().css({ margin: "0" });
				jQuery(this).parent().parent().siblings().show();
			}
		);

		jQuery(this).children('div.reactions').hide();
		jQuery(this).children('a.closebutton').hide();
		jQuery(this).children('div.movie').hide();
	});


	}

	if (jQuery('.weblog', '#persona').length > 0) {
		var target = jQuery(".weblog", '#persona');
		jQuery("ol", target).slideUp();
		jQuery("div a", target).attr('href', 'javascript:void(0)')
		jQuery("div", target).click(function() {
		    jQuery(this).nextAll('ol').toggleClass("expanded");
		    jQuery(this).nextAll('ol').slideToggle();
		})
	}
}

//slideshow on homepage middle block;

	function slideShow() {

    var slideshow = jQuery("#slideShow");
    
		if(slideshow.length==0){
			return false;
		}
		
		var canvasWidth = 0;
    var slides = jQuery(".slide", slideshow);
    var canvas = jQuery(".canvas", slideshow);
		var slideTime = 4;
		var WaitTime = 15;
		var timeUnit = 310;
		var slideTimer = null;
		var position = 0;
    
    slides.each(function() { canvasWidth += jQuery(this).width(); });
    canvas.width(canvasWidth);
			
		jQuery("#ctrMorelink a:not(:first)").hide();

		slides.each(function(i){
			jQuery(".directaccess ul").append("<li><a href='#'>"+i+"</a></li>");
		});

		var tempPos = jQuery(".directaccess li:first",slideshow).position().left;
		
		jQuery(".currentslide",slideshow).css("left",tempPos-20).css("margin-left",20);
			
		jQuery(".directaccess a").bind("click",function(e){
			stop(e);
			position = jQuery(".directaccess a").index(jQuery(this));

			canvas.animate({"margin-left":-position*310},timeUnit);				    
			jQuery(".currentslide",slideshow).animate({"margin-left":(position+1)*20},timeUnit);
			
			updateMorelink(position)
			clearInterval(slideTimer);
			slideTimer = setInterval(function(){nextSlide()},WaitTime * 3 * timeUnit)
		});			

		canvas
      .bind("mouseover",function(e){
        clearInterval(slideTimer);
      })
      .bind("mouseout",function(e){
        slideTimer = setInterval(function(){nextSlide()},WaitTime * 3 * timeUnit)
      });
		
    var margin = 0;
		function nextSlide(from){
			if (margin - 310 <= -canvasWidth) {
				position = 0;
        margin = 0;
				canvas.animate({"margin-left":margin},slideTime * timeUnit)				    
				jQuery(".currentslide",slideshow).animate({"margin-left":20},timeUnit);
			} else {
				position = position + 1;
        margin -= 310;
				canvas.animate({"margin-left":margin},slideTime * timeUnit)				    
				jQuery(".currentslide",slideshow).animate({"margin-left":"+=20"},timeUnit);
			}
			updateMorelink(position)
		}

		function updateMorelink(position) {		
			jQuery("#ctrMorelink a").hide();
			jQuery("#ctrMorelink a:eq(" + position + ")").show();
		}

		slideTimer = setInterval(function(){nextSlide()},WaitTime * timeUnit)
	}

