$(document).ready(function() {

	//handle centering screen
	handleScreenPosition();

	if ($('.letter').length != 0) {
		$('.letter').hide();
		$('.abc').click(function() {
			var abcLetter = $(this).attr('id').split('_');
			if ($('#abcLetter_'+abcLetter[1]).css('display') == 'none') {
				$('#abcLetter_'+abcLetter[1]).slideDown();
			} else {
				$('#abcLetter_'+abcLetter[1]).slideUp();
			}
		});
	}

	addFormAction();

	//scrollbalk
	if ($("div.content_bb").length > 0)	{
		$('div.content_bb').jScrollPane().css("opacity","0.7");
		//scrollbar ook transparant
		if ($("div.jScrollPaneTrack").length > 0)	{
			$("div.jScrollPaneTrack").css("opacity","0.7");
		}
	}
	//submenu
	$("ul.submenu").css("opacity","0.8");

	//achtergrond afhandelen
	handleBackground();

	//fotopicker
	if ($("table#fotomenu").length > 0)	{
		handlePhotoPicker();
	}

	//show the close/open button for the content
	if ($("div.jScrollPaneContainer").length > 0)	{
		handleCloseOpenButton();
	}

});


function handleCloseOpenButton()
{
	var wE = $("div.jScrollPaneContainer"); //workelement
	var hE = $("div#contentHandler"); //handle element
	//show the button and set its postion
	var wLeft = wE.width() + 0;
	hE.css('left',wLeft+'px').show();

	//handle clicks
	hE.toggle(function(event)	{
		wE.hide(1000);
		$(this).animate({
			left: '0px'
		}, 1000, function()	{
			$(this).html('<a href="Javascript:void(0);" title="" class="open_editor">Open</a>');
		});
	}, function(event)	{
		wE.show(1000);
		$(this).animate({
			left: wLeft+'px'
		}, 1000, function()	{
			$(this).html('<a href="Javascript:void(0);" title="" class="close_editor">Sluit</a>');
		});
	})

}

function handleScreenPosition()
{
	var clientScreenHeight = $(document).height();
	//var clientScreenWidth = $(document).width();
	var curContainerHeight = $("div#container").height();
	//var curContainerWidth = $("div#container").width();
	//alert(clientScreenWidth);

	//height, only if screenheight is bigger than container height
	if (clientScreenHeight > curContainerHeight)	{
		//calculate the diff
		var hDiff = clientScreenHeight - curContainerHeight;
		//split
		var hDiffEach = hDiff / 2;
		//set the margin
		$("div#container").css("marginTop",hDiffEach);
	}
	//width, only if screenwidth is bigger than container width
	//if (clientScreenWidth > curContainerWidth)	{
		//calculate the diff
		//var bDiff = clientScreenWidth - curContainerWidth;
		//split
		//var bDiffEach = bDiff / 2;
		//set the margin
		//$("div#container").css("marginLeft",bDiffEach);
	//}

}

function handleBackground()
{
	if ($("div#content").length > 0)	{

		var cId = '1';
		//decide witch page where on
		if ($("input.cbackScroll").length > 0)	{
			var pId = $("input.cbackScroll").val();
			if (pId != '')	{
				cId = pId;
			}
		}

		$("div#content").ccmsFadeBackground({
			animationSpeed: 800,
			structuurid: cId,
			ajaxGetImagesScript: '/javascript/ajaxRequests/getImgUl.php',
			timeOut: 5000,
			showBreakOption: true
		});
	}
}

function handlePhotoPicker()
{
	//toggle slide
	$("div.close_button").toggle(function(event)	{
		$("table#fotomenu").hide(1000);
		$(this).animate({
			bottom: '0px'
		},1000);

	}, function(event)	{
		$("table#fotomenu").show(1000);
		$(this).animate({
			bottom: '75px'
		},1000);

	})

	//click handle
	$("table#fotomenu a[rel=photoPicker]").click(function()	{
		var imgPick = $(this).attr("href");
		//check if the images holder exists
		if ($("div.imgAbsHolder").length == 0)	{
			//create the node
			var imgShowHolder = $('<div class="imgAbsHolder"/>');
			$("div#content").after(imgShowHolder);
		}
		//prevent the background from fading futher
		if ($("div#imgHold").length > 0)	{
			//$("body div#imgHold").remove();
			$("body div#imgHold").attr('id','imgHoldOff');
		}
		//remove break button
		if ($("div.breakHolder").length > 0)	{
			$("div.breakHolder").remove();
		}
		var imgEl = $('<img src="'+imgPick+'">');
		$("div.imgAbsHolder img").fadeOut('fast', function()	{
			$("div.imgAbsHolder").html(imgEl);
			$("div.imgAbsHolder img").fadeIn('fast');
		})

		return false;
	})
}

$.preloadImages = function()
{
  for(var i = 0; i<arguments.length; i++)
  {
	$("<img>").attr("src", arguments[i]);
  }
}

function showResultsPerPage(results)
{
	$('#productsPerpage').val(results);
	$('#theFilterForm').submit();
}

function submitFilterForm()
{
	$('#page').val('1');
	$('#theFilterForm').submit();
}

//add action to filterform
function addFormAction()
{
	$('#theFilterForm').submit(function() {
		//display loadingdiv
		$("#loadingFilter").css('opacity', '0.5');
		$("#loadingFilter").css('display','block');
		$("#loadingProducts").css('opacity', '0.5');
		$("#loadingProducts").css('display','block');
		var inputs = $("#theFilterForm :input");
		var filterUrl = '';
		//create filterAjaxurl
		inputs.each(function(i){
			if ($(this).attr('type') == 'checkbox' && $(this).attr('checked') == true) {
				if (filterUrl == '') {
					filterUrl =  $(this).attr('name') + '=' + $(this).val()
				} else {
					filterUrl =  filterUrl + '&' + $(this).attr('name') + '=' + $(this).val()
				}
			} else if ($(this).attr('type') == 'text' || $(this).attr('type') == 'hidden') {
				if (filterUrl == '') {
					filterUrl =  $(this).attr('name') + '=' + $(this).val()
				} else {
					filterUrl =  filterUrl + '&' + $(this).attr('name') + '=' + $(this).val()
				}
			}
 		});

 		//process ajaxrequest
		$.ajax({
			url: "/filterProducts.php",
			type: "POST",
			data: filterUrl,
			dataType: "html",
			//cache: false,
			success: function(responseHtml) {
				var $holder = $('<div/>').html(responseHtml);
				//update filterDiv
				$('#productFilter').html($('#productFilter', $holder).html());
				//update productresultdiv
				$('.content_bb').html($('#productsFound', $holder).html());

				$("#loadingFilter").css('display','none');
				$("#loadingProducts").css('display','none');
				addFormAction();
			}
		});
		return false;
	});
}

function showLoginError(elementName){

	var element = document.getElementById(elementName);

	$.modal($(element),{

    	minHeight:17,
		containerCss:{backgroundColor:"#fff",height:130,padding:0,width:322},

		onOpen: function(dialog)
		{
			dialog.overlay.fadeIn('fast', function () {
				dialog.container.slideDown('normal', function () {
					dialog.data.fadeIn('normal');

					element.style.visibility = 'visible';


				});
			});
		},
		onClose: function(dialog)
		{
			dialog.overlay.fadeOut('fast', function () {
				dialog.container.fadeOut('fast', function () {
					dialog.data.fadeOut('fast', function () {

						$.modal.close(); // must call this!
						$("#klantLoginFoutMeldingen").remove(); // and this too!

					});
				});
			});
		}

	});
}

function showResultPage(number)
{
	$('#page').val(number);
	$('#theFilterForm').submit();
}

function uncheckFilter(id)
{
	$('#' + id + 'Fieldset > input').attr('checked', false);
	$('#theFilterForm').submit();
}

// check whether fieldvalue is unique or not
function checkFieldValue( structuurid, eigenschapnaam, element ){

		// error span verwijderen
		var id = $(element).attr('id') + '_error';
		var value = element.value;

		if (value.length > 0) {

			filterUrl =  'action=checkduplicate&id='+ structuurid + '&esnaam=' + eigenschapnaam + '&value=' + value;

 			//process ajaxrequest
			$.ajax({
				url: "/ajaxrequests.php",
				type: "POST",
				data: filterUrl,
				dataType: "html",
				cache: false,
				success: function(responseHtml) {


					if (responseHtml != 'false') {

						$("#"+id+"").remove();
						// add span
						$(element).after('<span id="' + id +'" class="error">' + responseHtml + '</span>');
						//for (var x in element ) alert(x);

					}
				}
			});

		}
}

// check length of field value
function checkFieldLength( element, min, max ){

		// error span verwijderen
		var id = $(element).attr('id') + '_error';
		var value = element.value;

		$("#"+id+"").remove();

		if ((value.length < min) || (value.length > max)){
			// add span
			$(element).after('<span id="' + id +'" class="error">Aanbevolen lengte is tussen ' + min + ' en ' + max + '.</span>');
			//for (var x in element ) alert(x);

		}
}


//laden van netto prijzen, in structuurIDPrices zitten 1 of meerdere structuren met hun prijs structuurID:prijs;structuurID2;prijs2
function loadDiscountPrices(structuurIDPrices)
{
	$.ajax({
		url: "/ajaxrequests.php",
		type: "POST",
		data: 'action=loadDiscountPrices&structuurIDPrices='+structuurIDPrices,
		dataType: "html",
		success: function(responseHtml) {
			var $holder = $('<div/>').html(responseHtml);
			var $children = $holder.children('div');
			$.each($children, function(val, i) {
				$('#' + i.id).html(i.innerHTML);
			});
		}
	});

}