//if (window.location.hash != '')
//{
//	var hash = window.location.hash;
//	if (hash != '')
//		window.location.href = hash.substring(1,hash.length);
//	
//}

function search(action, params)
{
	postObject = new Object;
	postObject.ajax = 'true';
	postObject.breadcrumb = 'false';
	postObject.query = params['query'];
	return $.ajax({
        type: "POST",
        url: action,
        data: postObject,
        success: function(data) {
			$('#' + target).html(data);
        },
        error: function (e)
        {
        	window.location.href = action;
        }
    });


    return false;
	
}

function changeGridPage(action, prefix)
{
	if (typeof prefix === undefined)
		prefix = '';
		
	$('#' + prefix + 'gridloading').show();
	postObject = new Object;
	postObject.ajax = 'true';
	$('#' + prefix + 'body').scrollTo(500);
	return $.ajax({
        type: "GET",
        url: action,
        success: function(data) {
			$('#' + prefix + 'body').html(data);
			$('#' + prefix + 'gridloading').hide();
        },
        error: function (e)
        {
        	window.location.href = action;
			$('#' + prefix + 'gridloading').hide();
        }
    });
}

function changeGridOrder(action, prefix)
{
	if (typeof prefix === undefined)
		prefix = '';
		
	$('#' + prefix + 'gridloading').show();
	postObject = new Object;
	postObject.ajax = 'true';
	return $.ajax({
        type: "GET",
        url: action,
        success: function(data) {
			$('#' + prefix + 'grid').html(data);
			$('#' + prefix + 'gridloading').hide();
		},
        error: function (e)
        {
        	window.location.href = action;
			$('#' + prefix + 'gridloading').hide();
        }
    });
}


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);
			$("#dialog .tr").click(function(){
				$.dialog.hide();
			});
	};

  $.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> \
                  </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(e) {
	/* Disabling ajax submit plugin */
	if (Nid.forms.ajaxSubmitDisable)
		return this;
	
	/* Change a form's submission type to ajax */
	this.submit(function(){
		var id = this.id;
		Nid.forms[id] = this;
		if (Nid.forms[id].disable === true){
			return false;
		}else{
			Nid.forms[id].disable= true;
		}
		$(this).find('*').filter("ul.errors").hide();
		$.post(this.getAttribute("action"), $(this).serialize(), function(obj){
			if (obj.status == "success")
			{	
				var target = obj.target;
				if (target)
					if (target.substring(0,1) != '.' && target.substring(0,1) != '#')
						target = '#' + target;
				if (obj.reset == "true")
				{
					$().find('input')
					 .not(':button, :submit, :reset, :hidden')
					 .val('')
					 .removeAttr('checked')
					 .removeAttr('selected');
					$().find('textarea').val('');
				}
				if(obj.message && obj.title)
					$.dialog.showAlert(obj.message, 'info', obj.title);
				if (obj.result && target)
				{
					if (obj.resultAddType == 'replace')
						$(target).replaceWith(obj.result);
					else if (obj.resultAddType == 'prepend')
						$(target).prepend(obj.result);
					else
						$(target).append(obj.result);
				}
				
				if (obj.redirect)
					window.location = obj.redirect;
				
				setTimeout("$.dialog.hide()", 4000);
			}
			else if (obj.message)
			{ 			
				Nid.forms[id].disable = false;
				$.dialog.showAlert(obj.message, 'error', obj.title);
			}
			else
				Nid.forms[id].disable = false;
				for (r in obj)
				{
					var err_list = "";
					for (err in obj[r])
					{
						err_list += "<li>" + obj[r][err] + "</li>";
					}
					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();				
					submitFlag = 0;
				}
                if (obj.captcha) {
                    $('#' + obj.captcha.field).attr('src', Nid.baseUrl + '/img/captcha/' + obj.captcha.id + '.png');
                    $('#' + obj.captcha.field + '-id').attr('value', obj.captcha.id);
                    $('#' + obj.captcha.field + '-input').val('');
                }
		}, "json");
		return false;
	});
	
	return this;
}

