/*
 * jQuery Snuko actions
 * Copyright 2010 Darius Orvidas
 */

(function( $ ){
	$.fn.snuko_actions = function(obj) {
		var methods={
			redirect:function(value){
				document.location=value;
			},
			bad_input:function(obj){
				$.each(obj, function(index, value){
					$('#err-'+index).html(value).attr('class', 'error');
				});
			},
			alert:function(obj){
				$.each(obj, function(index, value){
					alert(value);
				});
			},
			html:function(value){
				document.location=value;
			}

		};
		$.each(obj, function(index, value){
			if (methods[index] !== undefined)
				methods[index](value);
		});
	};
})( jQuery );

(function( $ ){
	$.fn.close_open_modal = function(html, opt) {
		var overlay=$('#simplemodal-overlay').clone();
		$.modal.close();
		$('body').append(overlay);
		$(document).oneTime(200, function() {
			$.modal(html, opt);
			overlay.remove();
			//$('.simplemodal-container').css('height', 'auto');
		});
	};
})( jQuery );
(function( $ ){
	$.fn.ajax_call = function(ajax_settings) {
		if(ajax_settings['url'] === undefined){
			alert('Please define URL');
			return false;
		}
		
		var el=$(this);
		el.attr('disabled','disabled');

		var settings={
			dataType:'json',
			async:false,
			type:'GET',
			error:function(){
				alert('An error occured. Please contact support team');
			}
		};
		$.extend(settings, ajax_settings);

		$.ajax(settings);
		el.removeAttr('disabled');
		return this;
	};
})( jQuery );
(function( $ ){
	$.fn.jquery_lang = function(method, args) {
		var methods = {
			'store':function(data){
				$.each(data, function(key, value){
					$('body').data(key, value);
				});
			},
			'get':function(key){
				return $('body').data(key);
			}
		}
		
		//Making shure method exists
		if(methods[method] !== undefined){
			if(method=='store') methods['store'](args);
		}
		else{
			return methods['get'](method);
		}
	};
})( jQuery );
(function( $ ){
	$.fn.example_text = function() {
		$(this).each(function(){
			var input=$(this);
			if(input.val().length==0) input.attr('value', input.attr('title')).addClass('grey');
			input.blur();
			input.bind('focus', function(){
				input.removeClass('grey');
				if(input.val()==input.attr('title')) input.val('');
			}).bind('blur', function(){
				if(input.val().length==0) input.attr('value', input.attr('title')).addClass('grey');
			});
		});
		return this;
	};
})( jQuery );
(function( $ ){
	$.fn.slider = function() {
		$(this).next().hide();

		$(this).click(function(){
			var item = $(this);
			if(item.hasClass("selected")){
				item.next().slideUp();
				item.removeClass("selected");
			} else {
				item.next().slideDown();
				item.addClass("selected");
			}
		});
	}
})( jQuery );

