(function ($) {
    $.fn.hoverFix = function () {
        this.hover(function () {
            $(this).addClass('hover');
        }, function () {
            $(this).removeClass('hover');
        });
    };
})(jQuery);

$(function() {
	$("input.autoDelete").each(function(){
		if($(this).attr("value").length != 0){
			$(this).data("origVal", $(this).attr("value"));
		}else{
			$(this).data("origVal", this.value);
		}
	});
	
	$("input.autoDelete").focus(function(){
		if(this.value == $(this).data("origVal")){
			this.value='';					
		}
	});
	$("input.autoDelete").blur(function(){
		if(this.value==''){this.value=$(this).data("origVal");}
	});

	$('#homeBanner').after("<div id='homeBannerNav'><a id='homeBannerPrev' href='#'>Prev</a> <a id='homeBannerNext' href='#'>Next</a></div>").cycle({ 
		fx:     'fade', 
		speed:  500, 
		timeout: 5000, 
		next:   '#homeBannerNext', 
		prev:   '#homeBannerPrev' 
	});

	$('#twitterPosts').tweet({
		  count: 1,
		  fetch: 5,
		  filter: function(t){ return ! /^@\w+/.test(t["tweet_raw_text"]); },
		  username: "LawsocietyLSUC",
		  loading_text: '<div id="twitterLoading">Loading tweets...</div>',
		  template: function(t) {
			return '<div id="twitterTweetAge">Posted ' + t["time"] + '</div><div id="twitterPost">' + t["text"] + '</div>';
		  }
		}).bind("loaded",function(){$(this).find("a").attr("target","_blank");});

	
	function setCookie(name,value,days) {
		if (days) {
			var date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			var expires = "; expires="+date.toGMTString();
		}
		else var expires = "";
		document.cookie = name+"="+value+expires+"; path=/";
	}
	
	function getCookie(name) {
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for(var i=0;i < ca.length;i++) {
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
		}
		return null;
	}
	
	function size_text(i) {
		_sizes[i].addClass("here")
		$("#content").css('font-size', 100 + (10*i) + '%');
	}

	$sizes = $('#headerTextSizer li'),
	_sizes = new Array(),
	_size = 0;

	if(getCookie("font-size") != null && getCookie("font-size") != "null") _size = getCookie("font-size");
		
	$sizes.each(
		function(i){
			_sizes.push($(this));
			if (i == _size) size_text(i)
			$("a", this).bind("click", function(event){
				event.preventDefault();
				for (var j = 0; j < _sizes.length; j++) {
					_sizes[j].removeClass("here");
					if (i==j) setCookie('font-size',j, 365);
				}
				size_text(i);
			});
		}
	)

	$("#mainNav > ul > li").hoverFix();
	
	$('a[title]',".tooltip").qtip({
			content: {
				attr: 'title'
			},
			position: {
				my: 'bottom center',
				at: 'top center'
			},
			style: {
				tip: {
					corner: 'bottom center',
					height: 5,
					width: 10
				},
				classes: 'ui-tooltip-shadow'
			},
			show: {
			  effect: function(offset) {
				 $(this).fadeIn(400);
			  }
			}
		});

	$('#footerInfoIcon a').bind("click", function(event) {
		event.preventDefault();
	}).qtip({
		content: {			
			text: $('#footerInfoPanel')
		},
		position: {
				my: 'bottom right',
				at: 'top center',
				adjust: {
					 x: 33,
					 y: -3
				}
		},
		style: {
		  classes: 'ui-tooltip-none',
		  tip: {
			 corner: false
		  }
		},
		show: {
		  event: 'click mouseenter',
		  effect: function(offset) {
			 $(this).fadeIn(0);
		  }
		},
		hide: {
		  event: 'mouseleave',
		  fixed: true,
		  delay: 500,
		  effect: function(offset) {
			 $(this).fadeOut(0);
		  }
		}
	});

});

