/**
 * @author: Jan De Wilde - www.alotofpixels.be
 * @date: 06.02.2010
 * @description: File that handles all website function.
 */
 
$(document).ready(function() {
						   
	/**
	 * @description: Bind event that detects where to scroll and checks if the parent exists.
	 */
	$('.hplogo a').bind('click',function() {
		$.scrollTo('#pageWrapper', 800);
		return false;
	});
	$('#menu li').bind('click',function(){
		var scrollToId = $(this).attr('id');
		if($('#'+scrollToId).length > 0) {
			$.scrollTo('#'+scrollToId.replace(/a-/,''), 800);
		}
		return false;
	});
	
	/**
	 * @description: Bind event that scrolls the user back to the top.
	 */
	$('.homeDark, .homeLight').bind('click',function(){
		$.scrollTo('#pageWrapper', 800);
		return false;
	});
	
	bindAjaxItems();
	generateOverviewScrollbar();
	
	/**
	 * @description: Bind AJAX requests to archive links.
	 */
	var fireArchiveBackup = true;
	$('.archive a.request').bind('click',function() {
		if(fireArchiveBackup) {
			$('#pageWrapper').append('<div id="backup" style="display:none;"></div>');
			$('#backup').html($('#twoColEven').html());
			fireArchiveBackup = false;
		}
		var url = $(this).attr('href').replace('http://www.hp-decor.be/','');
		$.ajax({
		  url: url,
		  beforeSend: function() {
			  $('#twoColEven').append('<div id="ajaxOverlay" class="loading"></div>');
		  },
		  success: function(data) {
				$('#twoColEven').empty().append(data);
				$('#twoColEven #ajaxOverlay').fadeOut('normal',function(){ $(this).remove(); });
				bindAjaxItems();
				generateOverviewScrollbar();
		  }
		});
		return false;
	});
	
	$('#laatsteNieuws').bind('click',function(){
		if(!fireArchiveBackup) {
			$('#twoColEven').empty().append('<div id="ajaxOverlay" class="loading"></div>').prepend($('#backup').html()).remove('#sliderVerticalOverview');
			$('#twoColEven #ajaxOverlay').fadeOut('normal',function(){ $(this).remove(); });
			bindAjaxItems();
			generateOverviewScrollbar();
		}
		return false;
	});

});


/**
 * @description: Bind AJAX requests to the news items.
 */
function bindAjaxItems() {
	$('.newsitem a.request').bind('click',function(){
		var url = $(this).attr('href').replace('http://www.hp-decor.be/','');
		$.ajax({
		  url: url,
		  beforeSend: function() {
			  $('#twoColEven').append('<div id="ajaxOverlay" class="loading"></div>');
		  },
		  success: function(data) {
			var ajaxItemContentClose = '<a href="#" class="closeAjaxItem" title="Sluit">Sluit</a>';
			  
			var ajaxItemContent = '';
			ajaxItemContent += ajaxItemContentClose;
			ajaxItemContent += '<div id="ajaxContent">';
			ajaxItemContent += data;
			ajaxItemContent += '</div>';
			$('#ajaxOverlay').removeClass('loading').append(ajaxItemContent);
			$('#ajaxContent').fadeIn("normal");
			
			$('.closeAjaxItem').bind('click',function(){
				$('#ajaxOverlay').fadeOut("'normal",function(){$(this).remove();});
				return false;
			});
			
			if($('#innerContentWrapper').height() > $('#contentWrapper').height()) {
				$('#ajaxContent').append('<div id="sliderVertical"></div>');
				$('#sliderVertical').slider({
					orientation: "vertical",
					range: "min",
					min: 0,
					max: 100,
					value: 100,
					slide: function(event, ui) {
						var diffH = $('#innerContentWrapper').height() - $('#contentWrapper').height();
						$('#innerContentWrapper').css('top',(diffH / 100) * (ui.value-100));
					}
				});
			}
		  }
		});
		return false;
	});
}


/**
 * @description: Set-up vertical scrollbar if content is longer than surrounding box.
 */
function generateOverviewScrollbar() {
	if($('#twoColEven .left').height() > $('#twoColEven').height() || $('#twoColEven .right').height() > $('#twoColEven').height()) {
		$('#twoColEven').append('<div id="sliderVerticalOverview"></div>');
		$('#sliderVerticalOverview').slider({
			orientation: "vertical",
			range: "min",
			min: 0,
			max: 100,
			value: 100,
			slide: function(event, ui) {
				var height = $('#twoColEven .left').height();
				if($('#twoColEven .right').height() > height) { height = $('#twoColEven .right').height(); }
				var diffH = height - $('#twoColEven').height();
				$('#twoColEven .left, #twoColEven .right').css('top',(diffH / 100) * (ui.value-100));
			}
		});
	}
}