var ClientCore = function(){
	
	this.language;
	this.host_url;
	this.CSS_FOLDER;
	this.openedForm = false;
	this.textField = '.textField';
	this.hiddenField = '.hiddenField';
	this.ajaxDialogSelector = '#ajax-client-form-dialog'; // NB! dont forget to change in the initAjaxDialog function
	
	this.init = function(c_lang, c_url, c_css){
		if(typeof(c_lang) != 'undefined' && c_lang != '') this.language = c_lang;
		if(typeof(c_url) != 'undefined' && c_url != '') this.setURL(c_url);		
		if(typeof(c_css) != 'undefined' && c_css != '') {
			this.CSS_FOLDER = c_css; 
		}
		this.initGalleryBox();
		this.initFileLink();
		this.initInlinePages();
		this.initPopPages();
		this.initOtherLinks();
		this.initHTML5();
	}
	
	this.initHTML5 = function() {
		if(typeof(Modernizr) == 'undefined') return;
		
		// Init: autofocus
		if (!Modernizr.input.autofocus) {
			$('[autofocus=""]').focus();
		}
		
		// Init: placeholder
		if (!Modernizr.input.placeholder && $.fn.placeholder) {
			$('input[placeholder]').placeholder();
		}
		
		// Init: forcenumber
		if (!Modernizr.inputtypes.number && $.fn.forcenumber) {
			$('input[type=number]').forcenumber();
		}
		
		// Init: email
		if (!Modernizr.inputtypes.email && $.fn.forceemail) {
			$('input[type=email]').forceemail();
		}
		
		// Init: url
		if (!Modernizr.inputtypes.url && $.fn.forceurl) {
			$('input[type=url]').forceurl();
		}
		
		// Init: datepicker
		if (!Modernizr.inputtypes.date && $.fn.datepicker) {
			if($('input[type=date]').length > 0) cmsClient.initDateField( $('input[type=date]') );
		}
		
		// Init: datetimepicker
		if (!Modernizr.inputtypes.date && $.fn.datetimepicker) {
			if($('input[type=datetime]').length > 0) cmsClient.initDateTimeField( $('input[type=datetime]') );
		}
	}
	
	this.initImageCarusel = function(element_selector, element_count, left_button, right_button) {
		if(typeof(element_count) == 'undefined') element_count = null;
		if(typeof(left_button) == 'undefined') left_button = '<div></div>';
		if(typeof(right_button) == 'undefined') right_button = '<div></div>';
		$(element_selector).jcarousel({
	        vertical: false,
	        start: 1,
	        offset: 1,
	        size: element_count,
	        scroll: 1,
	        visible: null,
	        animation: 'normal',
	        easing: 'swing',
	        auto: 0,
	        wrap: null,
	        initCallback: null,
	        reloadCallback: null,
	        itemLoadCallback: null,
	        itemFirstInCallback: null,
	        itemFirstOutCallback: null,
	        itemLastInCallback: null,
	        itemLastOutCallback: null,
	        itemVisibleInCallback: null,
	        itemVisibleOutCallback: null,
	        buttonNextHTML: left_button,
	        buttonPrevHTML: right_button,
	        buttonNextEvent: 'click',
	        buttonPrevEvent: 'click',
	        buttonNextCallback: null,
	        buttonPrevCallback: null			
		});
	}
	
	this.initGalleryBox = function() {
		$('.galleryBox').each(function(i, val){
			$(this).removeClass('galleryBox');
			cmsClient.loadForm('gallery', 'viewInLine', this, {id: $(this).attr('rel')});
		});
	}
	
	this.initFileLink = function() {
		$('.cms-file-link').each(function(i, val){
			var file_id = $(this).attr('rel');
			$(this).attr("href", cmsClient.getURL('files', 'get', {id: file_id }));
		});
		$('.cms-file-view').each(function(i, val){
			var file_id = $(this).attr('rel');
			$(this).attr("href", cmsClient.getURL('files', 'view', {id: file_id }));
		});
	}
	
	this.initOtherLinks = function() {
		$('.cms-css-link').each(function(i, val){
			var old_src = $(this).attr('src');
			$(this).attr("src", cmsClient.CSS_FOLDER + old_src);
		});
	}
	
	/*
	 * Example: 
	 * <div class="cms-inline-page" rel="page.contact:id=2&someparam=somevalue">CONTACT FORM</div>
	 */
	this.initInlinePages = function() {
		$('.cms-inline-page').each(function(i, val){
			var raw_url = $(this).attr('rel');

			if(typeof(raw_url) == 'undefined' || raw_url.length < 1) return;
			
			var raw_params = '';
			var application = '';
			var action = '';
			var temp;
			
			// Split the url and the params
			raw_url = raw_url.split(":");
			// If we have any params, copy them
			if(raw_url.length > 1) raw_params = raw_url[1];
			
			// Now split the application and action apart
			raw_url = raw_url[0];
			raw_url = raw_url.split(".");
			
			// Now copy the application and action
			application = raw_url[0];
			action = raw_url[1];
			
			// Unset this box rel, so that it doesnt init agen if some ajax call is made
			$(this).attr('rel', '');
			
			// Load the data
			cmsClient.loadForm(application, action, this, raw_params);
			
			
		});
	}
	
	/*
	 * Example:
	 * <div class="cms-pop-page" rel="page.contact:id=2&someparam=somevalue">OPEN CONTACT FORM</div>
	 */
	this.initPopPages = function() {
		$('.cms-pop-page').each(function(i, val){
			$(this).bind('click', function() {
				var raw_url = $(this).attr('rel');
				
				if(raw_url.length < 1 || $(this).hasClass('isOpen')) return;
				
				var raw_params = 'show=pop_up';
				var application = '';
				var action = '';
				var temp;
				
				// Split the url and the params
				raw_url = raw_url.split(":");
				// If we have any params, copy them
				if(raw_url.length > 1) raw_params += "&" + raw_url[1];
				
				// Now split the application and action apart
				raw_url = raw_url[0];
				raw_url = raw_url.split(".");
				
				// Now copy the application and action
				application = raw_url[0];
				action = raw_url[1];
				
				var dialog_buttons = {};
				
				// Load the data
				var res = cmsClient.showAjaxForm(application, action, raw_params, dialog_buttons, {
					beforeclose: function() {
						$('[rel=' + $(this).data('parentElement') + ']').removeClass('isOpen');
					}
				});
				
				$(res).data('parentElement', $(this).attr('rel'));
				
				if($(res).css('display') == 'block') {
					$(this).addClass('isOpen');
				}				
			});
		});
	}
	
	this.initImageBox = function(element_selector) {
		 $(element_selector).fancybox({ 
			 'zoomSpeedIn': 300
			 , 'zoomSpeedOut': 300
			 , 'overlayShow': true 
			 , 'padding' : 0
		}); 
	}
	
	this.initSelectBox = function(element_selector) {
		jQuery(element_selector).selectbox();
	}
	
	this.initCustomTabs = function(element_selector) {
		jQuery(element_selector).click(function () {
			var tabID = '#' + jQuery(".test-li-active").attr('rel');
			jQuery(tabID).css({ display : 'none'});

			var tabID = '#' + jQuery(this).attr('rel');
			jQuery(tabID).css({ display : 'block'});

			jQuery(".test-li-active").removeClass('test-li-active').addClass('test-li-disable');
			jQuery(this).removeClass('test-li-disable').addClass('test-li-active');
			
		});
	}
	
	this.initAjaxDialog = function()
	{
		var new_dialog_id = this.randomString(4);
		$("body").append("<div id='ajax-client-form-dialog-" + new_dialog_id + "'></div>");
		return 'ajax-client-form-dialog-' + new_dialog_id;
	}
	
	this.initToolTips = function(element){
		jQuery(element).each(function(i, val) {
			$(this).tooltip({position: {my: "left bottom", at: "left top"}});
		});
	}
	
	this.initDateField = function(main_filed_selector, custom_options){
		var default_options = {
			dateFormat: 'dd.mm.yy'
			, changeMonth: true
			, changeYear: true 
		};
		
		$(main_filed_selector).datepicker($.extend(default_options, custom_options));
	}
	
	this.initDateTimeField = function(field_selector, custom_options) {
		var default_options = {
			changeMonth: true
			,changeYear: true
			,dateTimeFormat: 'dd.mm.yy hh:ii'
		};

		jQuery(field_selector).datetimepicker($.extend(default_options, custom_options));
	}
	
	this.initDateRange = function(main_filed_selector, start_field_selector, end_field_selector){
		$(main_filed_selector).DatePicker({
			format:'d.m.Y',
			date: new Date(),
			current: new Date(),
			starts: 1,
			position: 'bottom',
			calendars: 3,
			mode: 'range',
			onBeforeShow: function(){
				if($(main_filed_selector).val().length > 0) {
					$(main_filed_selector).DatePickerSetDate( $(main_filed_selector).val().split(' - ') , true);
				}
			},
			onChange: function(formated, dates){
				$(main_filed_selector).val( formated.join(' - ') );
				$(start_field_selector).val( formated[0] );
				$(end_field_selector).val( formated[1] );
			}
		});
	}
	
	this.initAutoComplete = function(element_selector, url, custom_options) {
		var default_options = {
			dataType: "json",
			parse: function(data) {
				return $.map(data, function(row) {
					return {
						data: row,
						value: row,
						result: row
					}
				});
			},
			formatItem: function(item) {
				return item;
			}	
		};
		
		$(element_selector).autocomplete(url, $.extend(default_options, custom_options));
	}
	
	/* Add / Set / Show Actions */
	this.setURL = function(url){
		this.host_url = url;
	}
		
	/* Get & Load Actions */
	this.getURL = function(action, subaction, params){
		var url = this.host_url;
		
		if(action != '' && subaction != '' && typeof(action) != 'undefined' && typeof(subaction) != 'undefined')
		{
			url += "?action=" + action + "." + subaction;
		}
		
		if(params != '' && typeof(params) != 'undefined')
		{
			if(typeof(params) == "string") {
				url += "&" + params
			} else {
				var additionalParams = "";
				for(key in params) {
					additionalParams += "&" + key + "=" + encodeURIComponent(params[key]);
				}
				url += additionalParams
			}
		}
		return url;
	}
	
	this.getCurrentURL = function(params, extend) {
		var temp;
		var url = window.location.href;
		var url_data = parseUri(url);
		var queryKey = url_data.queryKey;
		
		var application = null;
		var action = null;

		// 1. Get current action
		// If not mod-rewrite then the action is in params
		if(url_data.file == 'index.php' || url_data.directory == '/' || this.host_url.indexOf(url_data.directory) !== -1) {
			temp = url_data.queryKey.action;
			if(temp.length > 0) {
				temp = temp.split('.');
				application = temp[0];
				action = temp[1];
			}
			delete(url_data.queryKey.action);
		} else {
			temp = url_data.directory;
			temp = temp.split('/');
			application = temp[ (temp.length - 2 ) ];
			temp = url_data.file;
			temp = temp.split('.');
			action = temp[0];
		}
		
		// Do we need to extend current params?
		if(typeof(extend) != 'undefined' && extend == true) {
			params = $.extend(url_data.queryKey, params);
		}
		
		return cmsClient.getURL(application, action, params);
	}
	
	this.initLoading = function()
	{
		$("body").append("<div id='loading-box'><span class='ui-icon-loading' style='float: left; margin-right: 0.3em;'/><div class='loading-message'>" + $t('msg.loading') + "</div></div>");
		$("#loading-box").dialog({
			autoOpen: false,
			bgiframe: true,
			closeOnEscape: false,
			draggable: false,
			modal: true,
			resizable: false,
			minHeight: 10,
			title: $t('lbl.loading')
		});
	}
	
	this.initError = function()
	{
		$("body").append("<div id='error-box' title='" + $t('msg.error') + "'><div id='error-message' class='ui-state-error ui-corner-all'></div></div>");
		
		var dialog_buttons = {};
		dialog_buttons[$t('button.ok', '', false)] = function() {
			$(this).dialog('close');
		};
		
		$("#error-box").dialog({
			autoOpen: false,
			bgiframe: true,
			draggable: false,
			modal: true,
			resizable: false,
			minHeight: 10,
			buttons: dialog_buttons
		});
	}
	
	this.initMessage = function()
	{
		$("body").append("<div id='message-box' title='" + $t('msg.message') + "'><div id='message' class='ui-state-highlight ui-corner-all'></div></div>");
		
		var dialog_buttons = {};
		dialog_buttons[$t('button.ok', '', false)] = function() {
			$(this).dialog('close');
		};
		
		$("#message-box").dialog({
			autoOpen: false,
			bgiframe: true,
			draggable: false,
			modal: true,
			resizable: false,
			minHeight: 10,
			buttons: dialog_buttons
		});
	}
	
	this.initConfirm = function()
	{
		$("body").append("<div id='confirm-box' title='" + $t('msg.confirm') + "'><div id='confirm-message' class='ui-corner-all'></div></div>");
		
		var dialog_buttons = {};
		dialog_buttons[$t('button.yes', '', false)] = function() {
			$(this).dialog('close');
			var success_handler = $("#confirm-box").data('success_handler');
			success_handler();			
		};
		dialog_buttons[$t('button.no', '', false)] = function() {
			$(this).dialog("close");
		};
		
		$("#confirm-box").dialog({
			autoOpen: false,
			bgiframe: true,
			draggable: false,
			modal: true,
			resizable: false,
			minHeight: 10,
			buttons: dialog_buttons
		});
	}
	
	this.showLoading = function(message)
	{
		if($("#loading-box").length == 0) this.initLoading();
		if(typeof(message) != 'undefined' && message != false) $("#loading-box").find(".loading-message").html(message);
		$("#loading-box").dialog("open");
	}
	
	this.hideLoading = function() {
		$("#loading-box").dialog("close");
	}
	
	this.showError = function(error_message)
	{
		if($("#error-box").length == 0) this.initError();
		$("#error-box").find('#error-message').html('<span class="ui-icon ui-icon-alert" style="float: left; margin-right: 0.3em;"/>' + error_message);
		$("#error-box").dialog("open");
		if(typeof(cmsAdministration) != 'undefined') cmsAdministration.init();
	}
	
	this.showMessage = function(message)
	{
		if($("#message-box").length == 0) this.initMessage();
		$("#message-box").find('#message').html('<span class="ui-icon ui-icon-info dialog-message-set" style="float: left; margin-right: 0.3em;"/>' + message);
		$("#message-box").dialog("open");
		if(typeof(cmsAdministration) != 'undefined') cmsAdministration.init();
	}
	
	this.showConfirm = function(message, success_handler)
	{
		if($("#confirm-box").length == 0) this.initConfirm();
		$("#confirm-box").find('#confirm-message').html('<span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 20px 0;"></span>' + message);
		$("#confirm-box").data('success_handler', success_handler);
		$("#confirm-box").dialog("open");
		if(typeof(cmsAdministration) != 'undefined') cmsAdministration.init();
	}
	
	this.showConfirmLink = function(message, link)
	{
		var success_handler = function() {
			window.location = link;
		};
		
		if($("#confirm-box").length == 0) this.initConfirm();
		$("#confirm-box").find('#confirm-message').html('<span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 20px 0;"></span>' + message);
		$("#confirm-box").data('success_handler', success_handler);
		$("#confirm-box").dialog("open");
		if(typeof(cmsAdministration) != 'undefined') cmsAdministration.init();
	}
	
	this.showHiddenForm = function(form_id){
		if(this.openedForm) {
			$(this.hiddenField, $(this.openedForm)).hide();
			$(this.textField, $(this.openedForm)).show();
			$('.save-button', $(this.openedForm)).hide();
			$('.show-button', $(this.openedForm)).show();
		}
		
		$(this.textField, $(form_id)).hide();
		$(this.hiddenField, $(form_id)).show();
		$('.show-button', $(form_id)).hide();
		$('.save-button', $(form_id)).show();
		
		this.openedForm = form_id;
	}
	
	this.showAjaxEdit = function(application, action, params, after_edit_handler, dialog_params) {
		if(typeof(after_edit_handler) == 'undefined' || after_edit_handler == '') {
			after_edit_handler = function() {}
		}
		
		var dialog_buttons = {};
		dialog_buttons[$t('button.save', '', false)] = function() {
			cmsClient.postForm(application, action, $('form', $(this)).attr('id'), $.extend({operation: 'save'}, params), function() {
				after_edit_handler();
			});
			$(this).dialog("close");
		};
		dialog_buttons[$t('button.cancel', '', false)] = function() {
			$(this).dialog("close");
		};
		
		this.showAjaxForm(application, action, params, dialog_buttons, dialog_params);
	}
	
	this.showAjaxForm = function(application, action, params, buttons, dparams)
	{
		var new_dialog_id = this.initAjaxDialog();
		
		var dialog_buttons = {};
		
		var dialog_params = {
			id: new_dialog_id
			,bgiframe: true
			,draggable: false
			,modal: true
			,resizable: true
			,minHeight: 10
			,open: function(event, ui) {
				var title = $('.form-title', $(this)).html();
				if(title != null && typeof(title) != 'undefined' && title.length > 0) {
					$('.ui-dialog-title', $(this).parent()).html( title );
					$('.form-title', $(this)).hide();
					//$('.form-button', $('.form-container', $(this))).hide();
					
					if($('.form-container', $(this)).length > 0 && $('.form-container', $(this)).attr('style').length > 0) {
						var temp;
						var style = $('.form-container', $(this)).attr('style');
						
						style = style.split(';');
						
						for( key in style) {
							temp = style[key];
							temp = temp.replace(/(^\s+)|(\s+$)/g, "");
							temp = temp.split(':');
							
							if(temp[0].toLowerCase() == 'width') {
								var width = new Number( temp[1].substring(0, temp[1].length-2) );
								$(this).dialog( 'option' , temp[0].toLowerCase() , width.valueOf() );
							}
							
							if(temp[0].toLowerCase() == 'height') {
								var height = new Number( temp[1].substring(0, temp[1].length-2) );
								$(this).dialog( 'option' , temp[0].toLowerCase() , ( height.valueOf() + 20 )  );
							}
						}
						
						$('.form-container', $(this)).attr('style', '');
					}
				}
				
				if(typeof(cmsAdministration) != 'undefined') cmsAdministration.init();
			}
			,close: function() {
				$(this).remove();     
			}
			,buttons: $.extend(dialog_buttons, buttons)
		};
		
		$.extend(dialog_params, dparams);
		
		if(typeof(params) == "string") {
			var url = this.getURL(application, action, params);
			params = {layout: 'ajax'};
		} else {
			var url = this.getURL(application, action);
			params = $.extend({layout: 'ajax'}, params)
		}
		
		return $("#" + new_dialog_id).load(
			url
			, params
			, function() {  
				$("#" + new_dialog_id).dialog(dialog_params);
			}
		);
	}
	
	this.ajaxGetData = function(application, action, params, successHandler, returnDataType){
		var urlParams = "layout=ajax&action=" + application + "." + action;
		
		if(typeof(params) != 'undefined')
		{
			var additionalParams = "";
			jQuery.each(params, function(i, val) {
				if(i == 'form_id') additionalParams += "&" + $("#"+val).serialize();
				additionalParams += "&" + i + "=" + val;
			});
			urlParams += additionalParams;
		}
		
		if(typeof(returnDataType) == 'undefined') returnDataType = 'html';

		$.ajax({
			type: "POST",
			url: this.getURL(),
			data: urlParams,
			cache: false,
			dataType: returnDataType,
			error: function(XMLHttpRequest, textStatus, errorThrown) {
				if(typeof(params.show_errors) == 'undefined' || params.show_errors != false) {
					cmsClient.hideLoading();
					cmsClient.showError(XMLHttpRequest + " " + textStatus + " " + errorThrown);
				}
			},
			success: function(response, textStatus) {
				if(successHandler != '' && typeof(successHandler) != 'undefined')
				{
					successHandler(response, textStatus);
				}
				return response;
			}
		});
	}
	
	this.loadForm = function(application, action, body, params, successHandler, returnDataType){
		var urlParams = "layout=ajax&action=" + application + "." + action;
	
		if(typeof(params) != 'undefined')
		{
			if(typeof(params) == "object") {
				var additionalParams = "";
				jQuery.each(params, function(i, val) {
					additionalParams += "&" + i + "=" + val;
				});
				urlParams += additionalParams;
			} else {
				urlParams += "&"+params; 
			}
			
			if(params.show_loading != false ) this.showLoading();
		}

		if(typeof(returnDataType) == 'undefined') returnDataType = 'html';
		
		if(body == 'window'){
			window.location = this.getURL('', '', urlParams);
			return;
		}
		
		$.ajax({
			type: "POST",
			url: this.getURL(),
			data: urlParams,
			cache: false,
			dataType: returnDataType,
			error: function(XMLHttpRequest, textStatus, errorThrown) {
				cmsClient.hideLoading();
				cmsClient.showError(XMLHttpRequest + " " + textStatus + " " + errorThrown);
			},
			success: function(response, textStatus) {
				if(body == 'reload'){
					window.location.reload();
				} else if(body !== null && body !== false) {
					var before = $(body).html();
					$(body).html(response);
				}
				cmsClient.hideLoading();
				if(successHandler != '' && typeof(successHandler) != 'undefined')
				{
					successHandler(response, textStatus);
				}
				cmsClient.init();
				if(typeof(cmsAdministration) != 'undefined') cmsAdministration.init();
			}
		});
	}
	
	this.postForm = function(application, action, form_id, params, successHandler, ignoreErrors){
		if( ( form_id == '' || typeof(form_id) == 'undefined' ) && typeof(params) == 'undefined') return false;
		
		var urlParams = "layout=ajax&action=" + application + "." + action;
		if(form_id != '' && typeof(form_id) != 'undefined') urlParams += "&" + $("#"+form_id).serialize();
		
		if(params != '' && typeof(params) != 'undefined')
		{
			var additionalParams = "";
			jQuery.each(params, function(i, val) {
				additionalParams += "&" + i + "=" + val;
			});
			urlParams += additionalParams;
		} else {
			params = '';
		}
		
		if(typeof(params.show_loading) == 'undefined' || params.show_loading != false){
			this.showLoading();
		}
		
		$.ajax({
			type: "POST",
			url: this.getURL(),
			data: urlParams,
			error: function(XMLHttpRequest, textStatus, errorThrown) {
				cmsClient.hideLoading();
				cmsClient.showError(XMLHttpRequest + " " + textStatus + " " + errorThrown);
			},
			success: function(response, textStatus) {
				if(typeof(params.show_responce) != 'undefined' && params.show_responce != false){
					if(params.show_responce == 'reload'){
						window.location.reload();
					} else if(params.show_responce !== null && params.show_responce !== false) {
						$(params.show_responce).html( response );
					}
				}
				
				if(typeof(params.show_loading) == 'undefined' || params.show_loading != false){
					cmsClient.hideLoading();
				}
				
				// Have we got an error?
				if(response.length > 0 && $(response).find('#error-list').length > 0 && ignoreErrors != false)
				{
					cmsClient.showError($(response).find('#error-list').html());
				} else {
					// cmsAdministration.removeTinyMCE(); /// remove old editor if exists --- !!! removed because after posting ajax form, all tinyMCE were removed
					// if we have a handler send the response there and leave the  message output to the handler
					if(successHandler != '' && typeof(successHandler) != 'undefined')
					{
						successHandler(response, textStatus);
					} else {
						if(response.length > 0 && $(response).find('#message-list').length > 0) {
							cmsClient.showMessage($(response).find('#message-list').html());
						}
					}
				}
			}
		});
	}
	
	this.postLoadForm = function(post_application, post_action, post_form, post_params, load_application, load_action, load_body, load_params, successHandler) {
		this.postForm(post_application, post_action, post_form, post_params, function(response, textStatus) {
			if(typeof(load_params) != 'undefined' && load_params.redirect_response == true){
				var response_data = cmsClient.pareseXMLResponse(response);
				response_data = $(response_data).find('data').text();
				if(response_data.length > 0) $.extend(load_params, $.evalJSON(response_data));
			}
			cmsClient.loadForm(load_application, load_action, load_body, load_params, successHandler);
		});
	}
	
	this.submitForm = function(validation_rules, post_application, post_action, post_form, post_params, load_application, load_action, load_body, load_params, successHandler){		
		if(typeof(validation_rules) != 'undefined' && validation_rules != '' && validation_rules != false)
		{
			var validator = new FormValidator();
			var res = validator.validate(validation_rules);
		} else {
			var res = true; // no validation needed
		}
		
		if(res == true)
		{
			if(typeof(post_application) != 'undefined' && post_application != '' && (typeof(load_application) == 'undefined' || load_application == ''))
			{
				cmsAdministration.postForm(post_application, post_action, post_form, post_params, successHandler);
			} else {
				cmsAdministration.postLoadForm(post_application, post_action, post_form, post_params, load_application, load_action, load_body, load_params, successHandler);
			}
		}
		
		return false;
	}
	
	this.pareseXMLResponse = function(response) {
		var response_data;
		if ( $.browser.msie && typeof(response) == 'string' ) {
			// IF it`s IE, convert string to XML using ActiveX
			response_data = new ActiveXObject( 'Microsoft.XMLDOM');
			response_data.async = false;
			response_data.loadXML( response );
		} else {
			response_data = response;
		}
		return response_data;
	}
	
	this.randomString = function(length)
	{
		if(typeof(length) == 'undefined') length = 6;
	  	chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
	  	string = "";
		for(x=0;x<length;x++)
		{
			i = Math.floor(Math.random() * 62);
			string += chars.charAt(i);
		}
		return string;
	}
	
	this.limitTextArea = function(field, countfield, maxlimit) {
		// if too long...trim it!
		if ($(field).val().length > maxlimit) {
			$(field).val( $(field).val().substring(0, maxlimit) );
		// otherwise, update 'characters left' counter
		} else {
			$(countfield).html( (maxlimit - $(field).val().length) );
		}
	}
	
	this.showMailForm = function() {
		
		var dialog_buttons = {};
		dialog_buttons[$t('button.send_info', '', false)] = function() {
			var dialogID = $(this);
			cmsClient.postForm('page', 'showMailForm', $('form', $(this)).attr('id'), '', function(){
				$(dialogID).dialog("close");
			});
			
		};
		
		dialog_buttons[$t('button.cancel', '', false)] = function() {
			$(this).dialog("close");
		};
		
		cmsClient.showAjaxForm('page', 'showMailForm' ,{}, dialog_buttons, {width: 510, height: 360, resizable: false});
		return false;
	}
	
	this.showMailFormToFriend = function(link) {
		
		var dialog_buttons = {};
		dialog_buttons[$t('button.send_info', '', false)] = function() {
			var dialogID = $(this);
			cmsClient.postForm('page', 'mailToFriend', $('form', $(this)).attr('id'), '', function(){
				$(dialogID).dialog("close");
			});
		};
		
		dialog_buttons[$t('button.cancel', '', false)] = function() {
			$(this).dialog("close");
		};
		
		cmsClient.showAjaxForm('page', 'mailToFriend' ,{fUrl: link}, dialog_buttons, {width: 510, height: 410, resizable: false});
		return false;
	}
	
	this.pageNext = function(form_selector) {
		var current_page = jQuery('input[name="page"]', form_selector).val();
		
		jQuery('input[name="page"]', form_selector).val( cmsMath.sum(current_page, 1) );
		
		jQuery(form_selector).submit();
	}
	
	this.pageBack = function(form_selector) {
		var current_page = jQuery('input[name="page"]', form_selector).val();
		
		jQuery('input[name="page"]', form_selector).val( cmsMath.minus(current_page, 1) );
		
		jQuery(form_selector).submit();
	}
	
    this.sendToActionScript = function(movieName, value) {
    	if (navigator.appName.indexOf("Microsoft") != -1) {
    		if(typeof(window[movieName]) != 'undefined') {
    			return window[movieName].changePlayer(value);
    		} else {
    			return false;
    		}
        } else {
        	if(typeof(document[movieName]) != 'undefined') {
        		return document[movieName].changePlayer(value);
        	} else {
        		return false;
        	}
        }
    }
    
    this.checkLoginForm = function(l_id, p_id, f_id){
		var res = true;
		$('#' + l_id).removeClass('error');
		$('#' + p_id).removeClass('error');

		if( $('#' + l_id).val() == '' || $('#' + l_id).val() == 'Login'){
			res = false;
			$('#' + l_id).addClass('error');
		}
		
		if( $('#' + p_id).val() == '' || $('#' + p_id).val() == 'password'){
			res = false;
			$('#' + p_id).addClass('error');
		}
		
		if(res){
			$('#'+f_id).submit();
		}else{
			return false;
		} 
	}
    
    
    this.loginFormFiller = function(l_id, p_id){
    	
    	$('#' + l_id).val('Login');
		$('#' + p_id).val('password');
		

		$('#' + l_id).focus( function () {
				if($(this).val() == 'Login'){
					$(this).val('');
				}
		}).blur( function () {
			if($(this).val() == ''){
				$(this).val('Login');
			}
		});


		$('#' + p_id).focus( function () {
			if($(this).val() == 'password'){
				$(this).val('');
			}
		}).blur( function () {
			if($(this).val() == ''){
				$(this).val('password');
			}
		});
    }
}

var cmsClient = new ClientCore();
