if (window.location.hash != '')
{
	var hash = window.location.hash;
	if (hash != '')
		window.location.href = hash.substring(1,hash.length);
	
};


function show(action, params)
{ 
	$('#' + params['target']).html('لطفا کمی صبر کنید...');
	postObject = new Object;
	postObject.ajax = 'true';
	postObject.breadcrumb = params['breadcrumb'];

    return $.ajax({
        type: "POST",
        url: action,
        data: postObject,
        success: function(data) {
			$('#' + params['target']).html(data);
			
			if (params['scroll'] != 'false')
				$('#' + params['target']).scrollTo(300);
			
						
			if (params['target'] == 'container')
				window.location.hash = action;			
        },
        error: function (e)
        {
        	window.location.href = action;
        }
    });


    return false;
	
};

function run(url, updateDiv)
{
	$.post(url, {"ajax": "true"}, function(obj){
		if (obj.status == "success")
		{
			$.dialog.showAlert(obj.message, 'info', obj.title);
			if (obj.newval)
				$('#' + updateDiv).html(obj.newval);
				
		}
		else
			$.dialog.showAlert(obj.message, 'error', obj.title);
		
	}, "json");
	
};

function rate(url, updateDiv)
{
	$.post(url, {"ajax": "true"}, function(obj){
		if (obj.status == "success")
		{
			$.dialog.showAlert(obj.message, 'info', obj.title);
			$('#' + updateDiv).html(obj.newval);
		}
		else
			$.dialog.showAlert(obj.message, 'error', obj.title);
		
	}, "json");
	
};



(function($){
	$.dialog = function(content) {	
		  $.dialog.showDialog(content);
	};

  $.extend($.dialog, {
	  dialogHtml  : '<div dir="ltr" id="dialog" class="dialog" style="display: none;"><div class="popup"><table><tbody><tr><td class="tl"/><td class="b"/><td class="tr"/></tr><tr><td class="b"/><td class="body"><div class="main"></div><div class="footer">Hello</div></td><td class="b"/></tr><tr><td class="bl"/><td class="b"/><td class="br"/></tr></tbody></table></div></div>',
  		isInit : false,
		initDialog : function()
		{
		     $('body').append($.dialog.dialogHtml);
		
		},
		show : function(data)
		{		
			if (data.ajax)
			{
				postObject = new Object;
				postObject.ajax = 'true';
				postObject.breadcrumb = 'false';
			    $.post(data.ajax, postObject, 
			            function(data){
							$('#dialog .main').html(data);
							$('#dialog .buttons-frame').prepend('<a href="javascript:void(0);" onclick="$.dialog.hide();" class="redbutton">ببند</a>');
							centerDialog();
			    		}); 
			}
			else
			{
				$('#dialog .main').html(data);
				centerDialog();
			}
			
			$(document).bind('scroll.dialog', function(e) { centerDialog(); });

			
			$(document).bind('keydown.dialog', function(e) {
				if (e.keyCode == 27) $.dialog.hide();
				return true;
			})
	        $(document).trigger('show.dialog');
			
			
		},
		hide : function()
		{
			$('#dialog').hide();
			$(document).unbind('scroll.dialog');
			$(document).unbind('keydown.dialog');
	        $(document).trigger('hide.dialog');
			
			
		},
		showAlert : function(data, type, title)
		{
			if (!type) type = 'info';
			if (!$.dialog.isInit)
				$.dialog.initDialog();
			
			
			$('#dialog .footer').html('<a class="button" href="javascript:void(0)" onclick="$.dialog.hide();" > باشه </a>').show();

			if (title) data = '<div class="alert-header">' + title + '</div>' + data; 
			data = '<div class="icon-' + type + '">&nbsp;</div>' + data;
			$.dialog.show(data);
			
		},
		showDialog : function(data)
		{
			if (!$.dialog.isInit)
				$.dialog.initDialog();
			
			$('#dialog .footer').hide();
			
			$.dialog.show(data);
			
		}		          
  });
  
  	function centerDialog()
  	{
	    $('#dialog').show().css({	left : $(window).width() / 2 - ($('#dialog table').width() / 2), 
			top:	getPageScroll()[1] + (getPageHeight() / 2.5) - ($('#dialog table').height() / 2)});        				
  	};
	
	function getPageScroll() {
	  var xScroll, yScroll;
	  if (self.pageYOffset) {
	    yScroll = self.pageYOffset;
	    xScroll = self.pageXOffset;
	  } else if (document.documentElement && document.documentElement.scrollTop) {	 // Explorer 6 Strict
	    yScroll = document.documentElement.scrollTop;
	    xScroll = document.documentElement.scrollLeft;
	  } else if (document.body) {// all other Explorers
	    yScroll = document.body.scrollTop;
	    xScroll = document.body.scrollLeft;	
	  }
	  return new Array(xScroll,yScroll);
	};
	
	function getPageHeight() {
	  var windowHeight;
	  if (self.innerHeight) {	// all except Explorer
	    windowHeight = self.innerHeight;
	  } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
	    windowHeight = document.documentElement.clientHeight;
	  } else if (document.body) { // other Explorers
	    windowHeight = document.body.clientHeight;
	  }	
	  return windowHeight;
	};	 
})(jQuery);

(function($){
	$.fn.extend({
		  scrollTo : function(speed, easing) {
		    return this.each(function() {
		      var targetOffset = $(this).offset().top;
		      $('html,body').animate({scrollTop: targetOffset}, speed, easing);
		    });
		  }
		});
	
})(jQuery);


$.fn.ajaxSubmit = function(id) {
	/* Change a form's submission type to ajax */
	this.submit(function(){
		$(this).find('*').filter("ul").hide();

		$.post(this.getAttribute("action"), $(this).serialize(), function(obj){
			if (obj.status == "success")
			{
				$().find('input')
				 .not(':button, :submit, :reset, :hidden')
				 .val('')
				 .removeAttr('checked')
				 .removeAttr('selected');
				$().find('textarea').val('');
				
				$.dialog.showAlert(obj.message, 'info', obj.title);
				if (obj.result && obj.target)
					$('#' + obj.target).prepend(obj.result);

				if (obj.redirect)
					window.location = obj.redirect;
			}
			else if (obj.message)
			{ 			
				$.dialog.showAlert(obj.message, 'error', obj.title);

			}
			else
				for (r in obj)
				{
					
					var err_list = "";
					for (err in obj[r])
					{
						err_list += "<li>" + obj[r][err] + "</li>";
					}

					if (id) r += '_' + id;
					if ($('#err' + r).length == 0)
					{
						$('#' + r).parent().append('<ul class="errors" id="err' + r + '">' + err_list +  '</ul>');
						$('#err' + r).hide();
					}
					else
						$('#err' + r).html(err_list);	
										
					$('#err' + r).fadeIn();				
				}

		}, "json");
		return false;
	});
	
	return this;
};



