$(document).ready(function() {

	$("input[name=seo_meta\\[\\]]").click(function(e){

		
		{

			switch ($(this).val()) {
				case "noindex":
					if ($(this).prop("checked"))
						$.uniform.update($("#seo_meta\\[\\]_index").attr("checked" , false));
					else 
						$.uniform.update($("#seo_meta\\[\\]_index").attr("checked" , true));
				break;			

				case "index":
					if ($(this).prop("checked"))
						$.uniform.update($("#seo_meta\\[\\]_noindex").attr("checked" , false));
					else 
						$.uniform.update($("#seo_meta\\[\\]_noindex").attr("checked" , true));
				break;			

				case "follow":
					if ($(this).prop("checked"))
						$.uniform.update($("#seo_meta\\[\\]_nofollow").attr("checked" , false));
					else 
						$.uniform.update($("#seo_meta\\[\\]_nofollow").attr("checked" , true));
				break;			

				case "nofollow":
					if ($(this).prop("checked"))
						$.uniform.update($("#seo_meta\\[\\]_follow").attr("checked" , false));
					else 
						$.uniform.update($("#seo_meta\\[\\]_follow").attr("checked" , true));
				break;			

			}
		}
		
	});

	$(".form-capcha-img").click(function(e){
		$(this).find("img").attr("src" , $(this).attr("href") + "?" + $.now() );				
		return false;
	});

	$(document).on(
		"submit" , 
		"form.ajax",
		function(e){
			var frm=$(this);
			var formData = frm.serialize();
			var formStatus = frm.find(".formSubmit");

			frm.find('.responsebox').slideUp();	

		
			if (formStatus.length)
				formStatus.html("Sending Data...");


				$.ajax({
					url:$(this).attr("action"),
					type:'post',
					data:formData,
					success: function(msg){

						if (msg.indexOf("captcha:") != -1){
							$(".captcha-line").slideDown();
							frm.find(".form-capcha-img").trigger("click");
							frm.find(".responsebox").html(msg.replace("captcha:" , ""));
							frm.find('.responsebox').slideDown();	

							frm.find("input[name=image_code]").val("");
						} else {
							frm.find('.responsebox').html(msg);
							frm.find('.responsebox').slideDown();	
						}
						//frm.find('.responsebox').html("").hide();
						
					}
				});

				if (formStatus.length)
					formStatus.html("");

			return false;

		}
	);		


	//forms
	if ($.fn.uniform) {
//		$(".check, input:checkbox:not(.iButton), input:radio:not(.iButton), input:file").uniform(); 
		$(".check, input:checkbox:not(.ui-helper-hidden-accessible,.no-uniform), input:radio:not(.ui-helper-hidden-accessible,.no-uniform), input:file:not(.ui-helper-hidden-accessible,.no-uniform)").uniform(); 
		//$("input:checkbox").uniform(); 
	}

	if ($.fn.chosen) {
		$(".select").each(function(e){
			$(this).chosen({
				width:						$(this).attr("data-width"),
				disable_search_threshold:	10,
				allow_single_deselect:		true
			}); 
		});
	}

	$(".doubleselect").each(function(e) {

		if ($(this).hasClass("sortable")){
			$(this).multiselect( { sortable : true , availableListPosition : "left" , splitRatio: 0.5});
		} else {
			$(this).multiselect({ sortable: false , availableListPosition : "left" , splitRatio: 0.5});
		}
	});


});


$( window ).resize(function() {
  $(".doubleselect").multiselect("refresh");
});



function formCheckboxShowHide(elem , shown , hidden) {
	elem.change(formCheckboxShowHideHandler(shown, hidden));
	//run for the first time
	elem.trigger("change");
}

function formCheckboxShowHideHandler(shown, hidden){
	return function() {

		if ($(this).prop('checked')) {
			var fields = shown;
		} else {
			var fields = hidden;
		}


		for (i in fields){
			//check if fields exists
			if ($("#" + "XML_BODY_FIELD_" + i).length) {
				if (fields[i] == 1) {
					$("#" + "XML_BODY_FIELD_" + i).slideDown();
				} else {
					$("#" + "XML_BODY_FIELD_" + i).slideUp();
				}
			} else if ($("#" + i).length) {
				if (fields[i] == 1) {
					$("#" + i).slideDown();
				} else {
					$("#" + i).slideUp();
				}
			}
			
		}

	}
}


function formPageUrl(elem, url) {

	elem.blur(formPageUrlHandler(elem, url));
	//run for the first time
	elem.trigger("blur");

}


function formPageUrlHandler(parent, url){
	return function() {
		
		if (!url.length){
			return false;
		}

		if (!url.val().length ){
			url.val(Name2Url(parent.val().trim()).toLowerCase() );
		}
	}
}



function formFieldAutoFill() {

	var parent = arguments[0];
	//arguments.shift();

	parent.blur(formFieldAutoFillHandler(parent, arguments));
	//run for the first time
	parent.trigger("blur");


}

function formFieldAutoFillHandler(parent , elements) {
	return function () {
		for (i in elements){

			if (elements[i].length){
				if ( !elements[i].val().length){
					elements[i].val(parent.val());

					if (elements[i].attr("name") == "seo_title"){
						$("#seo_title").trigger("change");
					}
				}
			}
		}		
	}
}



function formSelectShowHide(elem , elements ) {
	elem.change(formSelectShowHideHandler(elements));
	//run for the first time
	elem.trigger("change");
}

function formSelectShowHideHandler(elements){
	return function() {
		

		if ($(this).is(':radio')){

			if ($(this).filter(':checked').val()){
				var type = $(this).filter(':checked').val();
			} else {
				return false;

			}

		} else {

			if ($(this).is(':checkbox')) {

				if ($(this).is(':checked')){
					var type = 1;
				} else {
					var type = 0;

				}


			}else {
				if ($(this).val() == ""){
					var type = 0;
				} else {
					var type = $(this).val();
				}
			}
		}

		if (!elements[type]){

			if (typeof elements["default"] != "undefined"){
				type = "default";
			} else {
				return false;
			}
		}

		var fields = elements[type];


		for (i in fields){
			//check if fields exists


			if (i.indexOf("#") != -1){
				if (fields[i] == 1) {
					$(i).slideDown();
				} else {
					$(i).slideUp();
				}
			} else {
				if ($("#" + "XML_BODY_FIELD_" + i).length) {
					if (fields[i] == 1) {
						$("#" + "XML_BODY_FIELD_" + i).slideDown();
					} else {
						$("#" + "XML_BODY_FIELD_" + i).slideUp();
					}
				}
			}
		}

	}
}


function UpdateCKE(){
	for ( instance in CKEDITOR.instances )
		CKEDITOR.instances[instance].updateElement();
}


if (typeof CKEDITOR != "undefined"){
	CKEDITOR.on('dialogDefinition', function(ev) {
		var dialogName = ev.data.name,
			dialogDefinition = ev.data.definition;
		if (dialogName === 'image') {
			dialogDefinition.removeContents('Upload');
		}
	});
}
