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) {
			$('#' + params['target']).html(data);
        },
        error: function (e)
        {
        	window.location.href = action;
        }
    });


    return false;
	
}

function show(action, params)
{
	$('#' + params['target']).html('Please wait...');
	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);
			$('#' + params['target']).scrollTo(300);
			
						
			if (params['target'] == 'container')
				window.location.hash = action;			
        },
        error: function (e)
        {
        	window.location.href = action;
        }
    });


    return false;
	
}

function rate(url, updateDiv, errorMessage)
{
	
	$.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);
			if (errorMessage)
			{
				$('#' + updateDiv).html(errorMessage);
			} else {
				$('#' + updateDiv).html(obj.message);
			}
		}
	}, "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';
			    $.post(data.ajax, postObject, 
			            function(data){
							$('#dialog .main').html(data);
							$('#dialog .buttons-frame').prepend('<a href="javascript:void(0);" onclick="$.dialog.hide();" class="redbutton"> Cancel </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();" > Close </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.lruSubmit = function(e) {
	/* Change a form's submission type to ajax */
	this.submit(function(){
		$.post(this.getAttribute("action"), $(this).serialize(), function(obj){
			if (obj.status == "success")
			{
				if (obj.reset == "true")
				{
					$().find('input')
					 .not(':button, :submit, :reset, :hidden')
					 .val('')
					 .removeAttr('checked')
					 .removeAttr('selected');
					$().find('textarea').val('');
				}
				
				$('#share').show();
				$('#short-url').val(obj.shortUrl);
				$('#sh-lru-div').animate({top: '0'},1000);
				$.opened = true;
				$.post(document.getElementById('lru-form').getAttribute("action") + 'index/cache', $('#lru-form').serialize(), function(obj) { });
				
			}
			else
			{
				$('#share').hide();
				$('#short-url').val("Invalid Url!");
				$('#sh-lru-div').animate({top: '0'},1000);
				$.opened = true;
			}

		}, "json");
		
		return false;
	});
	
	return this;
}


$.fn.ajaxSubmit = function(e) {
	/* 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")
			{
				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 && obj.resulttarget)
				{
					$('#' + obj.resulttarget).prepend(obj.result);
				}
				
//				if (obj.result && obj.target)
//					$('#' + obj.target).prepend(obj.result);
//
				if (obj.newval && obj.target)
					$('#' + obj.target).html(obj.newval);
					
				if (obj.redirect)
					window.location = obj.redirect;
				
				$.dialog.hide();
			}
			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 ($('#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;
}


$.extend({URLEncode:function(c){var o='';var x=0;c=c.toString();var r=/(^[a-zA-Z0-9_.]*)/;
while(x<c.length){var m=r.exec(c.substr(x));
  if(m!=null && m.length>1 && m[1]!=''){o+=m[1];x+=m[1].length;
  }else{if(c[x]==' ')o+='+';else{var d=c.charCodeAt(x);var h=d.toString(16);
  o+='%'+(h.length<2?'0':'')+h.toUpperCase();}x++;}}return o;},
URLDecode:function(s){var o=s;var binVal,t;var r=/(%[^%]{2})/;
while((m=r.exec(o))!=null && m.length>1 && m[1]!=''){b=parseInt(m[1].substr(1),16);
t=String.fromCharCode(b);o=o.replace(m[1],t);}return o;}
});


