
$(document).ready(function(){

	$(document).ajaxSend(function (){		
		$('#ajax-indicator').removeClass('hide');
	});
	
	$(document).ajaxStop(function (){
		$('#ajax-indicator').addClass('hide');
	});	
	
	// nahlad galerie
	$('.gallery-text').slideUp(1000);			
	$('.gallery-link').hover(
			
			function(){
				 
				$(this).children('.gallery-text').slideDown(500); 
				
			},
			function(){ 
				$(this).children('.gallery-text').slideUp(1000); 					
	});
	
	// lightbox
	$('a[rel="admin-lightbox"]').lightbox({
		fitToScreen: true,
		scaleImages: true,
		xScale: 1.2,
		yScale: 1.2,
		displayDownloadLink: true			
	});
	
	// lightbox
	$('a[rel="lightbox"]').lightbox({
		fitToScreen: true,
		scaleImages: true,
		//xScale: 1.2,
		//yScale: 1.2,
		displayDownloadLink: false			
	});
	
	$('.datepicker').datepicker({ 
		//appendText: '(dd-mm-yyyy)',
		dateFormat: 'dd-mm-yy',
		inline: true,
		firstDay: 1,					 
		showButtonPanel: true,
	});
	
	
	$('.wisiwyg-editor-basic').ckeditor({
		basePath: '/libs/ckeditor/',
		contentsCss: ['/css/public.screen.css','/css/public.screen.ckeditor.css'],
		width: '800px',
		height: '175px',
		toolbar:
			[
				['Source','SelectAll','RemoveFormat','-','Undo','Redo','-','Cut','Copy','Paste','PasteText','PasteFromWord','-','Bold', 'Italic', 'Underline','Strike','Subscript','Superscript','-', 'NumberedList', 'BulletedList', '-', 'Link','Unlink','-','Maximize', 'ShowBlocks','-','About'],
			],
			/*
			[
			    ['Source','-','Save','NewPage','Preview','-','Templates'],
			    ['Cut','Copy','Paste','PasteText','PasteFromWord','-','Print', 'SpellChecker', 'Scayt'],
			    ['Undo','Redo','-','Find','Replace','-','SelectAll','RemoveFormat'],
			    ['Form', 'Checkbox', 'Radio', 'TextField', 'Textarea', 'Select', 'Button', 'ImageButton', 'HiddenField'],
			    '/',
			    ['Bold','Italic','Underline','Strike','-','Subscript','Superscript'],
			    ['NumberedList','BulletedList','-','Outdent','Indent','Blockquote'],
			    ['JustifyLeft','JustifyCenter','JustifyRight','JustifyBlock'],
			    ['Link','Unlink','Anchor'],
			    ['Image','Flash','Table','HorizontalRule','Smiley','SpecialChar','PageBreak'],
			    '/',
			    ['Styles','Format','Font','FontSize'],
			    ['TextColor','BGColor'],
			    ['Maximize', 'ShowBlocks','-','About']
			];
			*/
		resize_enabled : true,
		resize_maxWidth : '800px',
		emailProtection : 'encode',
	});
	
	$('.wisiwyg-editor-advanced').ckeditor({
		basePath: '/libs/ckeditor/',
		contentsCss: ['/css/public.screen.css','/css/public.screen.ckeditor.css'],
		width: '800px',
		height: '300px',
		toolbar:
			[
			    ['Source','-','Save','NewPage','Preview','-','Templates'],
			    ['Cut','Copy','Paste','PasteText','PasteFromWord','-','Print', 'SpellChecker', 'Scayt'],
			    ['Undo','Redo','-','Find','Replace','-','SelectAll','RemoveFormat'],
			    ['Form', 'Checkbox', 'Radio', 'TextField', 'Textarea', 'Select', 'Button', 'ImageButton', 'HiddenField'],
			    '/',
			    ['Bold','Italic','Underline','Strike','-','Subscript','Superscript'],
			    ['NumberedList','BulletedList','-','Outdent','Indent','Blockquote'],
			    ['JustifyLeft','JustifyCenter','JustifyRight','JustifyBlock'],
			    ['Link','Unlink','Anchor'],
			    ['Image','Flash','Table','HorizontalRule','Smiley','SpecialChar','PageBreak'],
			    '/',
			    ['Styles','Format','Font','FontSize'],
			    ['TextColor','BGColor'],
			    ['Maximize', 'ShowBlocks','-','About']
			 ],
		resize_enabled : true,
		resize_maxWidth : '800px',
		emailProtection : 'encode',
	});
	
	/*
	CKEDITOR.addStylesSet( 'default',
			[

			   { name : 'Center text',
			   element : 'p',
			   attributes : { 'class' : 'center' },
			    styles : {    
			      'font-family' : 'Arial, Helvetica, sans-serif',
			      'font-size' : '12px',
			      'text-align' : 'center'
			   }},
			]);
	*/
	// ['Source','-','Save','NewPage','Preview','-','Templates'],
    // ['Cut','Copy','Paste','PasteText','PasteFromWord','-','Print', 'SpellChecker', 'Scayt'],
	// ['Undo','Redo','-','Find','Replace','-','SelectAll','RemoveFormat'],
	// ['Form', 'Checkbox', 'Radio', 'TextField', 'Textarea', 'Select', 'Button', 'ImageButton', 'HiddenField'],
	// '/',
	// ['Bold','Italic','Underline','Strike','-','Subscript','Superscript'],
	// ['NumberedList','BulletedList','-','Outdent','Indent','Blockquote'],
	// ['JustifyLeft','JustifyCenter','JustifyRight','JustifyBlock'],
	// ['Link','Unlink','Anchor'],
	// ['Image','Flash','Table','HorizontalRule','Smiley','SpecialChar','PageBreak'],
	// '/',
	// ['Styles','Format','Font','FontSize'],
	// ['TextColor','BGColor'],
	// ['Maximize', 'ShowBlocks','-','About']
	// formular -----------------------------------------------------------------------------------

	$(".tabForm").tabs({ fx: { opacity: 'toggle', duration: 100 } });



	jQuery.validator.setDefaults({ 
		errorLabelContainer: '#formError',
		errorElement: "span",
		onkeyup: false,
	});

	// rozsirenie validacie o UID pri uzivatelovi ----------------------------------------------------
	jQuery.validator.addMethod('uniqueUserUid',function(value,element,param){
	
		data = 'uid='+value;
	
	if (param !== true )
		data = data+'&id='+param;
	
	$.ajax({
		async:false,
		type: 'POST',
		url:  '/ajax/users/unique_uid',
		data: data,
		success: function(msg){

			if (msg == 'true') {
				result = true;
			}else{
				result = false;
			}
		},
	});
	
	return result;
	
}, 'User ID is already taken.');

// rozsirenie validacie o Email pri uzivateloch --------------------------------------------------
jQuery.validator.addMethod('uniqueUserEmail',function(value,element,param){
	
	data = 'email='+value;
	
	if (param !== true )
		data = data+'&id='+param;
	
	$.ajax({
		async:false,
		type: 'POST',
		url:  '/ajax/users/unique_email',
		data: data,
		success: function(msg){
			if (msg == 'true')
				result = true;
			else
				result = false;
		},
	});
	
	return result;
}, 'Email is already taken.');

// rozsirenie validacie o Login pri uzivateloch --------------------------------------------------
jQuery.validator.addMethod('uniqueUserLogin',function(data){
	
	result = false;
	$.ajax({
		async:false,
		type: 'POST',
		url:  '/ajax/users/unique_login',
		data: 'login='+data,
		success: function(msg){
			if (msg == 'true') result = true;
		}
	})
	return result;
}, 'Login is alreay taken.');


//rozsirenie validacie o UID pri uzivatelovi ----------------------------------------------------
jQuery.validator.addMethod('uniqueProjectUid',function(value,element,param){
	
	data = 'uid='+value;
	
	if (param !== true )
		data = data+'&id='+param;
	
	$.ajax({
		async:false,
		type: 'POST',
		url:  '/ajax/projects/unique_uid',
		data: data,
		success: function(msg){

			if (msg == 'true') {
				result = true;
			}else{
				result = false;
			}
		},
	});
	
	return result;
	
}, 'Project ID is already taken.');


// validacia pri formularoch ------------------------------------------------------------------
$.validator.addMethod("slovakDate",function(value, element) {
		// put your own logic here, this is just a (crappy) example
	return value.match(/^\d\d?\-\d\d?\-\d\d\d\d$/);
	
},'');

});
	
//pre-submit callback 
function showRequest(formData, jqForm, options) { 
	// formData is an array; here we use $.param to convert it to a string to display it 
    // but the form plugin does this for you automatically when it submits the data 
    var queryString = $.param(formData); 
 
    // jqForm is a jQuery object encapsulating the form element.  To access the 
    // DOM element for the form do this: 
    // var formElement = jqForm[0]; 
 
    alert('About to submit: \n\n' + queryString); 
  
    // here we could return false to prevent the form from being submitted; 
    // returning anything other than false will allow the form submit to continue 
    return true; 
}

//post-submit callback 
function showResponse(responseText, statusText)  { 
    // for normal html responses, the first argument to the success callback 
    // is the XMLHttpRequest object's responseText property 
 
    // if the ajaxSubmit method was passed an Options Object with the dataType 
    // property set to 'xml' then the first argument to the success callback 
    // is the XMLHttpRequest object's responseXML property 
 
    // if the ajaxSubmit method was passed an Options Object with the dataType 
    // property set to 'json' then the first argument to the success callback 
    // is the json data object returned by the server 
 
    alert('status: ' + statusText + '\n\nresponseText: \n' + responseText + 
        '\n\nThe output div should have already been updated with the responseText.'); 
    //alert('dokocil');
}

function ajaxFormDialog(dialogOptions)
{
	$(dialogOptions['id']+'Block').dialog({
		bgiframe: true,
		autoOpen: false,
		width: 800,
		height: 'auto',
		modal: true,
		title: dialogOptions['title'],
		buttons: {
			Create: function() {
					if ($(dialogOptions['id']).valid())
					{						
						if ($(dialogOptions['id']).ajaxSubmit({
									
									beforeSubmit:showRequest,
									success: dialogOptions['success'],
									error: showError,
									clearForm: true,
									url: dialogOptions['url'],
																		
									}))
						{		
							$(this).dialog('close');
						}
					}
				},
			Cancel: function() {
					$(this).dialog('close');
			}
		}
	});
	
	$(dialogOptions['id']+'Anchor').click(function(){
		$(dialogOptions['id']+'Block').dialog('open');
		}
	);
}

function showError (XMLHttpRequest,textStatus, errorThrown)
{
	alert(XMLHttpRequest +' - '+ textStatus +' - '+ errorThrown);
}


function showResult (responseText )
{
	if (responseText == 'false')
	{
		alert('NIECO ZLYHALO');
	}
	else
	{
		$('#formSuccess').html('<span>'+ responseText +'</span>');
		$('#formSuccess').slideDown(300);
		
		//$('#formSuccess').delay(3000).slideUp(300);
		$('#formSuccess').slideUp(300);
		
		alert(responseText);
	
	}	
}




//funkcia na update selectu
function ajaxUpdateSelect(options)
{
	$.ajax({
		type: options['type'],
		url: options['url'],
		data: options['data'],			
		success: function(result){
				$(options['target']).html(result);
				},
	});	
}




