(function($) {
	$().ready(function() {
		var dsi = 68.75;
		var stp = 6.25; /*12.5*/
		var eq = "vbt-fontsize=";
		var ca = document.cookie.split(';');

		if (document.location.href.indexOf("translate") == -1) {
			if (!top.moozcms ||!top.moozcms.ContentManager) {
				$('h1').each(function(i, n) {
					var size;

					n = $(this);

					size = n.parents('div#performanceInfo').length > 0 ? 35 : 50;

					n.replaceWith('<div class="headerImg"><img src="/img/font/generate.php?size=' + size + '&text=' + escape(n.text()) + '" title="' + n.text() + '" /></div>');
				});
			}
		} else {
			$('h1').each(function(i, n) {
				$(this).css("visibility", "visible");
			});
		}

		// Set default font-size from cookie
		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(eq) == 0)
				dsi = parseFloat( c.substring(eq.length,c.length) );
			
			document.getElementsByTagName("body")[0].style.fontSize = dsi+"%";
		}

		var field = $('#searchField,#cSearchField');

		field.focus(function() {
			if ($(this).val() == 'S\u00f6k')
				$(this).val('');
		}).blur(function() {
			if (!$(this).val())
				$(this).val('S\u00f6k');
		});
		
		// mouseover på bilder med klassen .ro
		$(".ro").hover(
		 function()
		 {
		  this.src = this.src.replace("_off","_on");
		 },
		 function()
		 {
		  this.src = this.src.replace("_on","_off");
		 }
		);
		// END
		
		$('a.votePositive').click(function(e) {
			var a = $(this);

			$.jsonRPC('vote.positive', [{"key" : a.attr("rel")}], function(data) {
				a.find('img').attr('src', '/img/vote/generate.php?text=' + data.result.v_positive + '&positive=true');
			});

			e.preventDefault();
		}).mouseenter(function() {
			$(this).find('img').attr('src', $(this).find('img').attr('src') + '&over=true');
		}).mouseleave(function() {
			$(this).find('img').attr('src', $(this).find('img').attr('src').replace(/&over=true/g, ''));
		});

		$('a.voteNegative').click(function(e) {
			var a = $(this);

			$.jsonRPC('vote.negative', [{"key" : a.attr("rel")}], function(data) {
				a.find('img').attr('src', '/img/vote/generate.php?text=' + data.result.v_negative + '&positive=false');
			});

			e.preventDefault();
		}).mouseenter(function() {
			$(this).find('img').attr('src', $(this).find('img').attr('src') + '&over=true');
		}).mouseleave(function() {
			$(this).find('img').attr('src', $(this).find('img').attr('src').replace(/&over=true/g, ''));
		});
		
		// Increase default font-size
		$('#toolBarRc .incsize').click(function(e) {
			if (dsi) {
				if (dsi < 81.25) {	/* 75.0 */
					dsi += stp;
					// create expire time
					var date = new Date();
					date.setTime(date.getTime()+(7*24*60*60*1000));
					var expires = "; expires="+date.toGMTString();
					// save to cookie
					document.cookie = "vbt-fontsize="+dsi+expires+"; path=/";
					// update css code
					document.getElementsByTagName("body")[0].style.fontSize = dsi+"%";
				}
			}

			e.preventDefault();
		});

		// Decrease default font-size
		$('#toolBarRc .decsize').click(function(e) {
			if (dsi) {
				if (62.5 < dsi) {
					dsi -= stp;
					
					// create expire time
					var date = new Date();
					date.setTime(date.getTime()+(7*24*60*60*1000));
					var expires = "; expires="+date.toGMTString();
					// save to cookie
					document.cookie = "vbt-fontsize="+dsi+expires+"; path=/";
					// update css code
					document.getElementsByTagName("body")[0].style.fontSize = dsi+"%";
				}
			}
			e.preventDefault();
		});

		$('#toolBarRc .print').click(function(e) {
			print();
		});

		$('#toolBarRc .bookmark').click(function(e) {
			addthis_open(e.target, '', '[URL]', '[TITLE]')
		}).mouseout(addthis_close);

		// Register click
		$.jsonRPC.url = "/moozcms/core/rpc/index.php";
		$.jsonRPC.errorHandler = function(err) {
			//alert(err.code + ',' + err.message + ',' + err.data);
		};

		$('a.newsReadMore').click(function() {
			$($(this).parent().find('div')).toggle();
		});

		$('a.rss').click(function() {
			document.location.href = 'http://vbtdev.moxiecode.com/rss.php';
			return;
		});

		$('.cycleShow').cycle({
			fx: 'fade',
			timeout: 4000
		});
	});
})(jQuery);