jQuery.fn.topLink = function(settings) {
  settings = jQuery.extend({
    min: 1,
    fadeSpeed: 200
  }, settings);
  return this.each(function() {
     var el = jQuery(this);
    el.hide();
    jQuery(window).scroll(function() {
      if(jQuery(window).scrollTop() >= settings.min)
      {
        el.fadeIn(settings.fadeSpeed);
      }
      else
      {
        el.fadeOut(settings.fadeSpeed);
      }
    });
  });
};

jQuery(document).ready(function() {
	
	appendLink('h1', 'http://' + window.location.hostname + '/', false);
	appendLink('h5', 'http://www.qf.org.qa/', true);
	
	populate('#searchArea form table tbody tr:first-child input', 'Search');
	
	function appendLink(id, url, external) {
		var target;
		if (external)
			target = function() { window.open(url) };
		else
			target = function() { location.href = url };
		jQuery(id).click(target);
	}
	
	function populate(id, value) {
		jQuery(id).val(value);
		jQuery(id).focus(function() {
			if (jQuery(this).val() == value)
				jQuery(id).val('');
		}).blur(function() {
			if (jQuery(this).val() == '' || /^\s+$/.test(jQuery(this).val()))
				jQuery(id).val(value);
		});
	}
	
	if (jQuery.browser.webkit) { jQuery('#nav #vertnav > li a').css('text-indent', '10px'); }
	
	jQuery('#vertnav > li').not(':first-child').not(':has(ul)').children().css('background', 'none').parent().hover(
  		function () {
    		jQuery(this).css('background-position', '-314px 0');
  		}, 
  		function () {
    		jQuery(this).css('background-position', '-157px 0');
  		}
	);
	
	jQuery('#vertnav > li').filter(':first-child').hover(
  		function () {
    		jQuery(this).css('background-position', '-314px 0');
  		}, 
  		function () {
    		jQuery(this).css('background-position', '0 0');
  		}
	);
	
	jQuery('#vertnav a.menuactive').parent().css('background', 'url(/rsrc/img/nav_bg.png) no-repeat -314px 0')
		.hover(
  		function () {
    		jQuery(this).css('background-position', '-157px 0');
  		}, 
  		function () {
    		jQuery(this).css('background-position', '-314px 0');
  		}
	);
		
	jQuery('#sub-nav span').prev().css('padding-left', '15px').css('background', 'url(/rsrc/img/expand.png) no-repeat 3px -13px').filter('.menuactive, .submenu1active')
		.css('background-position', '3px 11px');
	
	jQuery('#top').topLink({
    	min: 400,
    	fadeSpeed: 500
  	});
	
  	jQuery('#top').click(function(e){
    	e.preventDefault();
    	jQuery.scrollTo(0, 300);
  	});
	
});

