﻿//
//
//
function dummy() { }
function no_progress_bar_dummy() { }

//
//
//
$(document).ready(function(){
	init_all();
	drawSlideBanner();

	$.ajaxSetup ({
	// Disable caching of AJAX responses */
		cache: false
	});

});

//
//
//
function drawSlideBanner() {
	var slideSpeed = 1000;
	var isIE='\v'=='v';
	var temp = '';

	$("#fbanner #close").click(function(){
		if ( $("#fbanner").css('background-image').indexOf('_.png') > 0 ) { 
			$("#TB_overlay").fadeOut();
			$("#fbanner").css('background-image', $("#fbanner").css('background-image').replace('_.png', '.png') );
		}
		$("#fbanner").animate({left:"-402px"},slideSpeed);
		$("#fbanner #close").hide();
		$(".qm_hidden").hide();
	});

	$(".qm_hidden input").focus(function(event){ temp = $(this).val(); $(this).val(''); });
	$(".qm_hidden input").blur(function(event){ if ( $(this).val() == '' ){ $(this).val(temp) } });
	$(".qm_hidden textarea").focus(function(event){ temp = $(this).val(); $(this).val('');  });
	$(".qm_hidden textarea").blur(function(event){ if ( $(this).val() == '' ){ $(this).val(temp) }  });
	
	$("#fbanner").mousedown(function(event){
		if ( $("#fbanner").css('background-image').indexOf('_.png') < 0 ) { 
			$("#TB_overlay").fadeIn();
			$("#fbanner").css('background-image', $("#fbanner").css('background-image').replace('.png', '_.png') );
		}
		if( $("#fbanner").css('left') < '0') { 
			$("#fbanner").animate({left:"35%"},slideSpeed);
		}
		$("#fbanner #close").show();
		$(".qm_hidden").show();
	})
}


//
//
//
function hide_modals () { 
	$("#modal_dialog_confirm").dialog("destroy");
	$("#modal_dialog_loading").dialog("destroy");
}
function hide_modals_with_init () { 
//	hide_modals();
	window.location = window.location;
}

//
//
//
function init_all() {
	hide_modals ();

	init_colorpicker();
	init_datepicker();
	init_sortable();
	init_animation();
	init_forms();
//	init_tabs();
	init_editor();
	init_bg();
}

//
//
//
function init_bg () {
	if(!jQuery("body").find("#TB_overlay").is("div")) {
		if(!jQuery.browser.msie)
		jQuery("body").append("<div id='TB_overlay'></div>");
			else
		jQuery("body").append("<div id='TB_overlay'><iframe scrolling='no' frameborder='0' style='position: absolute; top: 0; left: 0; width: 100%; height: 100%; filter:alpha(opacity=0)'></iframe></div>");
	}
}
//
//
//
function show_modal (dialog_uri, dialog_title, dialog_width, dialog_height) {
	$("#modal_dialog_loading").dialog({ modal: true });
	$("#modal_dialog").dialog("destroy");
	$.get( dialog_uri,  
		function(data){
			if(!dialog_title){ dialog_title = "Администрирование" }
			if(!dialog_width){ dialog_width = "760" }
			if(!dialog_height){ dialog_height = "400" }

			$("#modal_dialog").html(data);
			$("#modal_dialog").attr("title", dialog_title);
			$("#modal_dialog").dialog({
				width: "" + dialog_width + "px", 
				height: "" + dialog_height + "" 
//				,modal: true
			});
			init_all();
		}
	);
}
function update_modal (dialog_uri) {
	$("#modal_dialog_loading").dialog({ modal: true });
	$.get( dialog_uri,  
		function(data){
			$("#modal_dialog").html(data);
			init_all();
		}
	);
}

//
//
//
function init_editor () {
	$(".editor___").each(function(i){
//		alert( $(this).attr("id") );
		var oFCKeditor = new FCKeditor( $(this).attr("id") ) ;
		oFCKeditor.BasePath	= "/js/editor/" ;
		oFCKeditor.ToolbarSet = $(this).attr("toolbarset") ;
		oFCKeditor.Width = "730" ;
		oFCKeditor.Height = $(this).attr("height") ;
		oFCKeditor.ReplaceTextarea() ;
	});
}

//
//
//
function submit_form_ajax ( form_name, on_script_end ) {
	var is_correct = "1";
	$("#" + form_name + " .required,#" + form_name + " .required_").each(function (i) {
		if( $(this).attr("value") == "" ) {
			if( $(this).attr("className").indexOf("required_") == "-1") { $(this).attr("className", $(this).attr("className").replace("required", "required_") ); }
			$(this).focus();
			is_correct = "0";
			return false;
		} else {
			$(this).attr("className", $(this).attr("className").replace("required_", "required") );
		}
	});

	if( is_correct == "1" ) {
		$("#modal_dialog_loading").dialog({ modal: true, height: "100" });
		$("body").append("<iframe src='/admin/submit_form_ajax/' name='ajax_file_upload' id='ajax_file_upload' style='display: none'></iframe>");
		$("form#"+form_name+"").attr("target","ajax_file_upload");
		$("form#"+form_name+"").attr("action","/admin/submit_form_ajax/");
		$("form#"+form_name+"").submit();
		var timeout    = 3000;
		var closetimer = 0;
		if ( on_script_end ) {
			var myFunction = new Function( on_script_end );
			closetimer = window.setTimeout( myFunction , timeout);
		} else {
			closetimer = window.setTimeout( hide_modals_with_init , timeout);
		}
	}
}

//
//
//
function send_quick_message ( form_name ) {
	var is_correct = "1";
	$("#" + form_name + " .required,#" + form_name + " .required_").each(function (i) {
		if( $(this).attr("value") == "" || $(this).attr("value") == "Ваше имя" || $(this).attr("value") == "Контактная информация" || $(this).attr("value") == "Сообщение" ) {
			if( $(this).attr("className").indexOf("required_") == "-1") { $(this).attr("className", $(this).attr("className").replace("required", "required_") ); }
			$(this).focus();
			is_correct = "0";
			return false;
		} else {
			$(this).attr("className", $(this).attr("className").replace("required_", "required") );
		}
	});

	if( is_correct == "1" ) {
		$("body").append("<iframe src='/send_quick_message/' name='ajax_file_upload' id='ajax_file_upload' style='display: none'></iframe>");
		$("form#"+form_name+"").attr("target","ajax_file_upload");
		$("form#"+form_name+"").attr("action","/send_quick_message/");
		$("form#"+form_name+"").submit();
		$("#fbanner #close").hide();
		$(".qm_hidden").hide();
		$("#fbanner").css('background-image', $("#fbanner").css('background-image').replace('_.png', '_finish.png') );
		var timeout    = 2000;
		var closetimer = 0;
		closetimer = window.setTimeout( hide_quick_message_form , timeout);
	}
}
function hide_quick_message_form () {
	var slideSpeed = 1000;
	var isIE='\v'=='v';

//	$("#fbanner").fadeOut();
	if ( $("#fbanner").css('background-image').indexOf('_finish.png') > 0 ) { 
		$("#TB_overlay").fadeOut();
		$("#fbanner").css('background-image', $("#fbanner").css('background-image').replace('_finish.png', '.png') );
	}
	$("#fbanner").animate({left:"-402px"},slideSpeed);
	$("#fbanner #close").hide();
	$(".qm_hidden input").val('');
	$(".qm_hidden textarea").val('');
	$(".qm_hidden").hide();
}


//
//
//
function init_sortable () {
	$(".sortable").each(function(i){
		$(this).sortable({
			placeholder: 'ui-state-highlight',
			update: function (event, ui) {
				$.get( '/admin/edit_serialize/?string=' + $(this).sortable('serialize').replace(/&/gi, 'amp;'), 
					function(data){
//						alert(data);
					}
				);
			}
		});
		$(this).disableSelection();
	});
}
//
//
//
function init_datepicker () {
	$(".datapicker").each(function(i){
		$(this).datepicker($.datepicker.regional['ru']);
		$(this).datepicker('option', {dateFormat: 'yy-mm-dd'});
		if( $(this).attr("value_") ){
			$(this).val( $(this).attr("value_") );
		} else {
			var myDate = new Date();
			var prettyDate = myDate.getFullYear() + '-' + eval(myDate.getMonth() + 1) + '-' + myDate.getDate();
			$(this).val(prettyDate);
		}
	});
}

//
//
//
function init_wait_link () {
//	$("a").each(function(i){
//	$(this).click(function(){
//		if( $(this).attr("href").substr(0,1) == "#" || $(this).attr("href").substr(0,34) == "javascript:no_progress_bar_dummy()" ){
//		} else {
//			$("#modal_dialog_loading").dialog({ modal: true });
//		}
//	});
//	});

//	$(":button").each(function(i){
//	$(this).click(function(){
//		if( $(this).attr("onClick").indexOf("toggle") >= "0" ||  $(this).attr("onClick").indexOf("hide_modal") >= "0" || $(this).attr("onClick").indexOf("no_progress_bar_dummy()") >= "0" ) {
//		} else {
//			$("#modal_dialog_loading").dialog({ modal: true });
//		}
//	});
//	});
}

//
//
//
function please_confirm ( message_text, on_script_end ) {

	$("#modal_dialog_confirm").html( message_text );
	$("#modal_dialog_confirm").attr( "title",  $("#please_confirm_text").html() );
	$("#modal_dialog_confirm").dialog({
		resizable: false,
		height:140,
		modal: true,
		buttons: {
			'OK': function() {
				var myFunction = new Function( on_script_end );
				myFunction();
				$(this).dialog('close');
			},
			Cancel: function() {
				$(this).dialog('close');
			}
		}
	});

}

//
//
//
function delete_element ( table_name, key_id, key_value, element_id ) {
	$.get( '/admin/delete_element/?table_name=' + table_name + '&key_id=' + key_id + '&key_value=' + key_value, 
		function(data){
			$("#" + element_id + "").hide("slow");
//			alert(data);
		}
	);
}

//
//
//
function submit_form ( form_name, is_ajax, on_script_end ) {
	$("#form_verify").val("1");
	$("#" + form_name + " .required,#" + form_name + " .required_").each(function (i) {
		if( $(this).attr("value") == "" || $(this).attr("value") == "Ваше имя" || $(this).attr("value") == "Контактная информация" || $(this).attr("value") == "Сообщение" ) {
			if( $(this).attr("className").indexOf("required_") == "-1") { $(this).attr("className", $(this).attr("className").replace("required", "required_") ); }
			$(this).focus();
			$("#form_verify").val("0");
			return false;
		} else {
			$(this).attr("className", $(this).attr("className").replace("required_", "required") );
		}
	});

	if( $("#form_verify").val() == "1" ) {
		$("form#"+form_name+"").attr("onsubmit", "return false()");
		$("form#"+form_name+"").submit();
	}
}

//
//
//
function change_menu ( menu_id ) {
	var open_id = menu_id.substring( menu_id.indexOf('_') + 1 );
	var class_name = menu_id.split('_');

	$('.' + class_name[0] + '').each(function (i) {
		if ( $(this).attr('id') == menu_id) {
			if( $(this).attr('value_') == '1' ) {
				$(this).attr('value_', '0');
			} else {
				$(this).attr('value_', '1');
			}
		} else {
			$(this).attr('value_', '0');
			$(this).hide();
		}
	});

	if( $('#' + class_name[0] + '_' + open_id + '').attr('value_') == '1' ) { 
		$('#' + class_name[0] + '_' + open_id + '').show();
	} else {
		$('#' + class_name[0] + '_' + open_id + '').hide();
	}
}

//
//
//
function submit_email ( email ) {
	if ( CheckEmailFormat ( email ) == false ) {
		alert ( 'Неверный формат e-mail');
	} else {
		$.get( '/admin/subscribe_email/?email=' + email + '', 
			function(data){
				$("#subscribe_form_div").html("Ваша заявка успешно отправлена");
//				alert(data);
			}
		);
	}
}

//
//
//
function CheckEmailFormat( email ) {
	var s = '[A-Za-z0-9][-\\w]*(\\.[A-Za-z0-9][-\\w]*)*'
	var re = new RegExp( '^'+s+'@'+s+'\\.[a-zA-Z]{2,4}$')
	email = email.replace( new RegExp('/\(.*?\)/'), '' )
	return re.test(email)
}

//
//
//
function init_animation () {
	$(".clients_animation_change").each(function(i){
		$(this).ulslide({
			width: 380,
			height: 'auto',
			axis: 'x',
			autoslide: 4000
		});
	});
	$(".clients_animation_slide").each(function(i){
		$(this).ulslide({
			width: 190,
			height: 80,
			axis: 'y',
			autoslide: 4000
		});
	});
}

//
//
//
function init_colorpicker () {
	$(".colorpicker_").each(function(i){
		$(this).jPicker();
	});
}

//
//
//
function init_forms () {
	if( $("#form_verify").val() >= "0" ) {
		$("#form_verify").val("0");
		$("#current_action").val("0");
	} else {
		$("body").append("<input type=hidden id=form_verify name=form_verify value=0 />");
		$("body").append("<input type=hidden id=current_action name=current_action value=0 />");
	}
}

