$(document).ready( function() {
	var datepicker = $( "#datepicker" ).datepicker();
	
	$("#selectAll").click(function() {
		$(".imagelist_downloads input").attr('checked', 'true');
	});
	
	 $('#NewsletterForm #email').click(function(){
		 openNewsletterDialog();
	 });
	 
	var height = ($(window).height()/2) - 71+'px';
	var width = $(window).width() -27+'px';
	$('.Feedbackbutton').css('top', height);
	$('.Feedbackbutton').css('left', width);
	$('.Feedbackbutton').css('display', 'block');
	
});

function markFields(ids, className)
{
	var idArray = ids.split("¶");

	for(var i = 0; i < idArray.length; i++)
	{
		document.getElementById(idArray[i]).className += ' '+ className;
	} // for(var i = 0; i < idArray.length; i ++)
	
} // function markFields()

function openNewsletterDialog()
{
	var postData = 'screen=dstore.misc.newsletter_overlay';
	
	$.ajax({
		   type: "POST",
		   url: DSTORE_BASE_URL + "/index.php",
		   data: postData,
		   dataType: 'json',
		   success: function(msg)
		   {
				$('#AjaxContainer').html(unescape(msg.MAIN));
				
				ModalDialog = $('#AjaxContainer').modal({
					overlayCss: {
				    backgroundColor: '#000',
				    cursor: 'wait'
				  },
				  containerCss: {
				    height: 380,
				    width: 330,
				    border: '1px solid #3a3a3a',
				    backgroundColor: '#FFF'
				  },
				  onShow : function(dialog){
					  $('#newsletterOverlayForm .submit').click(function (e) {
					  		e.preventDefault();
					  		sendNewsletterOverlayData(ModalDialog);
					  	});
					  
					  $('#newsletterOverlayForm input:text:first').focus();
				  }
				});
		   },
		   error: function(xhr)
		   {
			   alert(xhr.responseText);
		   }
		});
}

function sendNewsletterOverlayData(ModalDialog)
{
	$.ajax({
	   type: "POST",
	   url: DSTORE_BASE_URL + "/index.php",
	   data: $('#newsletterOverlayForm').serialize(),
	   dataType: 'json',
	   success: function(msg)
	   {
			ModalDialog.close();
			
		   	$('#AjaxContainer').html(unescape(msg.MAIN));
				
		   	ModalDialog = $('#AjaxContainer').modal({
					overlayCss: {
				    backgroundColor: '#000',
				    cursor: 'wait'
				  },
				  containerCss: {
				    height: 380,
				    width: 330,
				    backgroundColor: '#FFF',
				    border: '1px solid #3a3a3a'
				  },
				  onShow : function(dialog){
					  $('#newsletterOverlayForm .submit').click(function (e) {
					  		e.preventDefault();
					  		sendNewsletterOverlayData(ModalDialog);
					  	});
					  
					  $('#close_button').click(function (e) {
						  ModalDialog.close();
					  	});
				  }
				});
	   },
	   error: function(xhr)
	   {
		   alert(xhr.responseText);
	   }

   });
}

function openRecommendDialog(TreeNodeID)
{
	if( TreeNodeID > 0 ) {
		var postData = "screen=dstore.shop.recommend&TreeNodeID=" + TreeNodeID; 
	}
	else {
		var postData = "screen=dstore.shop.recommend";
	}
	
	$.ajax({
		   type: "POST",
		   url: DSTORE_BASE_URL + "/index.php",
		   data: postData,
		   dataType: 'json',
		   success: function(msg)
		   {
				$('#AjaxContainer').html(unescape(msg.MAIN));
				
				ModalDialog = $('#AjaxContainer').modal({
					overlayCss: {
				    backgroundColor: '#000',
				    cursor: 'wait'
				  },
				  containerCss: {
				    height: 380,
				    width: 330,
				    border: '1px solid #3a3a3a',
				    backgroundColor: '#FFF'
				  },
				  onShow : function(dialog){
					  $('#recommend_submit').click(function (e) {
					  		e.preventDefault();
					  		sendRecommendData(ModalDialog);
					  	});
				  }
				});
		   },
		   error: function(xhr)
		   {
			   alert(xhr.responseText);
		   }
		});
}

function sendRecommendData(ModalDialog)
{
	$.ajax({
	   type: "POST",
	   url: DSTORE_BASE_URL + "/index.php",
	   data: $('#recommendForm').serialize(),
	   dataType: 'json',
	   success: function(msg)
	   {
			ModalDialog.close();
			
		   	$('#AjaxContainer').html(unescape(msg.MAIN));
				
		   	ModalDialog = $('#AjaxContainer').modal({
					overlayCss: {
				    backgroundColor: '#000',
				    cursor: 'wait'
				  },
				  containerCss: {
				    height: 380,
				    width: 330,
				    backgroundColor: '#FFF',
				    border: '1px solid #3a3a3a'
				  },
				  onShow : function(dialog){
					  $('#recommend_submit').click(function (e) {
					  		e.preventDefault();
					  		sendRecommendData(ModalDialog);
					  	});
					  
					  $('#close_button').click(function (e) {
						  ModalDialog.close();
					  	});
				  }
				});
	   },
	   error: function(xhr)
	   {
		   alert(xhr.responseText);
	   }

   });
}
var openoverlay = '';
function openFaecherOverlay(id) {
	if (openoverlay != '') {
		$(openoverlay).css('display', 'none');
		openoverlay = '';
	}
	$('#faecheroverlay'+id).css('display', 'block');
	openoverlay = '#faecheroverlay'+id;
 	$('#faecheroverlay'+id).mouseleave(function(){
 		$('#faecheroverlay'+id).css('display', 'none');
 	});
 	$('.head').mouseleave(function() {
 		$('#faecheroverlay'+id).css('display', 'none');
 	});
}
