/**
*	@author http://chalker.ru/ ChALkeR
*	@copyright (c) ChALkeR
*	@version 2010-08-15
**/

$.fn.extend({
	eval_scripts: function() {
		this.find('script').each(function() {
			eval(this.innerHTML);
		});
		return this;
	},
	cms: {}
});

$.cms = {
	effects: 200,
	menu_effects: 600,
	menu_easing: 'swing',
	style: {},
	config: {
		root:'/', path:'', uri:'', username:'',
		popup:'', editor:'',
		paths:{plugins:'', style_generic:''},
		enabled:{}
	},

	init: function() {
		$.cms.init_form_categories();
		$.cms.init_menus();
		$.cms.setup_plugins();
		return $.cms;
	},
	setup: function(data) {
		$.extend(true,$.cms.config,data);
		return $.cms;
	},
	site_url: function() {
		return location.protocol + '//' + location.host + $.cms.config.root;
	},
	go_back: function() {
		history.go(-1);
		return false;
	},
	print: function() {
		window.print();
		return false;
	},
	media_toggle: function(arg) {
		if (arg) {
			$('object').not('.no_toggle').show();
		} else {
			$('object').not('.no_toggle').hide();
		}
		return $.cms;
	},
	field_category_toggle: function(field) {
		if ($('input[name=\''+field+'\']:checked').val()) {
			$('.field_category_'+field).hide().slideDown($.cms.effects);
		} else {
			$('.field_category_'+field).show().slideUp($.cms.effects);
		}
		return false;
	},
	menu_toggle: function(arg) {
		var menu = $($(arg).parents('li')[0]);
		if (menu.hasClass('closed')) {
			menu.removeClass('closed');
			menu.children('ul').hide().slideDown($.cms.effects);	
		} else {
			menu.addClass('closed');
			menu.children('ul').show().slideUp($.cms.effects);
		}
		return false;
	},
	init_menus: function() {
		$('li.cat.closed ul').hide();
		return $.cms;
	},
	init_form_categories: function() {
		$('.field_category').each(function () {
			if (!$('input[name=\''+this.name+'\']:checked').val()) {
				$('.field_category_'+this.name).hide();
			}
		});
		return $.cms;
	},
	setup_plugins: function() {
		if ($.scrollTo) {
			$.extend($.scrollTo.defaults, {
					easing: 'easeInOutQuad',
					axis: 'y'
				});
		}
	},
	html_equals: function(htmlb, html) {
		return (htmlb.replace(/[,\t\n\r \/]/g, '') == html.replace(/[,\t\n\r \/]/g, ''));
	}
};

$($.cms.init);
