﻿/* EVENTHANDLER FOR DOMONLOAD
/* ADD FUNCTIONS THAT SHOULD RUN ON DOM LOAD
----------------------------------------------------*/
$(document).ready(function() {
	//##>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>##

	$('body').addClass('js');
	$('#footer').RenderMainNavigation();
	$('.tab-box').RenderTabbox();
	$('fieldset').TextfieldPlaceholders();
	$('.box:last',$('.content')).addClass('last')
	
	//$('#content p.datestamp').append("<span class=\"separator\">|</span>");
	//
	//$('html[lang=en] #content p.datestamp').append("<a class=\"addthis_button\" href=\"http://www.addthis.com/bookmark.php?v=250&amp;username=xa-4c7586d707680eeb\">Share</a>");
	//$('html[lang=sv] #content p.datestamp').append("<a class=\"addthis_button\" href=\"http://www.addthis.com/bookmark.php?v=250&amp;username=xa-4c7586d707680eeb\">Dela</a>");
	//
	//$("#content p.datestamp a.addthis_button").each(function(){
    //    $("head").append("<script type=\"text/javascript\" src=\"http://s7.addthis.com/js/250/addthis_widget.js#username=xa-4c7586d707680eeb\"></script>");
	//});
	
	$('#content p.datestamp').append("<span class=\"separator\">|</span>");
	
	$('html[lang=en] #content p.datestamp').append("<a class=\"printthis_button\" href=\"#\">Print</a>");
	$('html[lang=sv] #content p.datestamp').append("<a class=\"printthis_button\" href=\"#\">Skriv ut</a>");
	$("#content p.datestamp a.printthis_button").click(function(){
	    window.print();
	});
	
	$('#content div.listing-unit h2 a').each(function(){
	    var link = $(this);
	    
	    var heading = link.parent();
	    
	    var listingUnit = heading.parent();
	    
	    listingUnit.hover(function(){
	        $(this).addClass("listing-unit-dynamic-hover");
	    }, function(){
	        $(this).removeClass("listing-unit-dynamic-hover");
	    }).click(function(){
	        location.href = link.attr("href");
	    });
	});

	//##>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>##
});

(function($) {
	$.fn.RenderTabbox = function() {
		return this.each(function() {

			var $box = $(this);
			$box.addClass('js');
			var $tabs = $('<ul class="tabs"></ul>');

			$box.find('h3').each(function() {
				var $tab = $('<li><span><strong>' + $(this).text() + '</strong></span></li>');
				$tab.bind('click', function() {
					$box.find('.active').removeClass('active');
					$box.find('.tab').hide();
					$(this).addClass('active');
					var id = $(this).text().toLowerCase();
					$box.find('#' + id).parent().show('fast');
				});

				$tab.find('strong').bind('mouseover', function() {
					$(this).addClass('hover');
				}).bind('mouseout', function() {
					$(this).removeClass('hover');
				});
				$tabs.append($tab);
				$(this).remove();
			});
			$box.find('h2').replaceWith($tabs);
			$box.find('.tabs li:eq(0)').addClass('active');
			$box.find('.tab:gt(0)').hide();
		});
	};
})(jQuery);

(function($) {
	$.fn.RenderMainNavigation = function() {
		var $nav = $('<div id="nav"></div>');
		var $navList = $('<ul></ul>');
		var $length = $(this).find('ul:first').find('h3').size();
		var $footer = $('#footer');
		var activeTopic = $footer.find('.selected').parents('ul:not(ul:last):last').parent().find('h3').text();
		$(this).find('ul:first').find('h3').each(function() {
			var active = '';
			var topic = $(this).text();
			var link = $(this).parent().find('a:first').attr('href');
			if (activeTopic === topic) {
				active = ' class="active"';
			}
			$navList.append('<li' + active + '><a href="' + link + '">' + topic + '<span></span></a></li>');
		});
		$nav.append($navList);
		$('#header hr').before($nav);
		var height = 0;
		$nav.find('li:first').addClass('first');
		$nav.find('li:last').addClass('last');
		$footer.find('ul li ul li ul').parent().addClass('parent');
		$footer.find('ul:first').children('li:last').addClass('last');
		$footer.find('.parent').find('a:first').before('<a href="javascript:;" class="icon"></a>');
		$footer.find('.parent').find('ul').hide();

		$footer.find('.selected').parents().filter('ul:hidden').parent().find('a.icon:first').toggleClass('open');
		$footer.find('.selected').parents().filter('ul:hidden').parent().find('a.icon:first').toggleClass('iconopen');
		$footer.find('.selected').parents().filter('ul:hidden').parent().find('a:icon:first').next().toggleClass('h');
		$footer.find('.selected').parents().filter('ul:hidden').show();

		$footer.find('.selected').find('ul:first').show();
		$footer.find('.selected').find('a.icon:first').addClass('iconopen')
		$footer.find('.selected').find('a:first').addClass('open')
		$footer.find('.selected').addClass('open');

		$nav.find('li').each(function(i) {
			var hheight = $('#footer ul:first').children().eq(i).height();
			if (hheight > height) height = hheight;
		});
		$footer.find('.parent').find('a.icon').bind('click', function(e) {
			$footer.find('ul:first').children().css('height', 'auto');
			CancelEvent(e);
			$(this).parent().find('ul:first').toggle();
			$(this).toggleClass('open');
			$(this).toggleClass('iconopen');
			$(this).next().toggleClass('h');
			$(this).parent().toggleClass('open');
			$footer.find('ul:first').children().css('height', $footer.find('ul:first').height());
		});
		$footer.find('ul:first').children().css('height', $footer.find('ul:first').height());
		if ($('#content').length) {
			var $submenu = $footer.find('.selected').parents('ul:not(ul:last):last').clone(true);
			if ($submenu.size() > 0) {
				$('#submenu').html('<ul>' + $submenu.html() + '</ul>');
				$submenu = $('#submenu');
			}
			$submenu.find('.parent').find('a.icon').bind('click', function(e) {
				CancelEvent(e);
				$(this).parent().find('ul:first').toggle();
				$(this).toggleClass('open');
				$(this).toggleClass('iconopen');
				$(this).next().toggleClass('h');
				$(this).parent().toggleClass('open');
			});
		}

	};
})(jQuery);


/* Textfield Placeholders - jQuery Plugin
* Author: Palle Zingmark, www.palleman.nu
* Released with the MIT License: http://www.opensource.org/licenses/mit-license.php
*/
(function($) {
	$.fn.TextfieldPlaceholders = function() {
		return this.each(function() {
			var $parent = $(this);
			$parent.find('input:text[value=""]').each(function(i) {
				var $input = $(this);
				var $placeholder = $($input).attr('placeholder');
				var $basecolors = ['#000000', 'rgb(0, 0, 0)'];
				var $color = $($input).css('color');
				var $hascolor = jQuery.inArray($color, $basecolors);
				if (typeof $placeholder == 'undefined' || $placeholder == '') {
					$placeholder = $parent.find('label[for="' + $($input).attr('id') + '"]').text();
				}
				if (typeof $placeholder == 'string' || $placeholder != '') {//## OR ABORT
					$($input).attr('value', $placeholder);
					$($input).attr('title', $placeholder);
					if ($hascolor || $hascolor != -1) {
						$(this).css('color', '#aeaeae');
					}
					$(this).addClass('placeholder');
					$($input).bind('focus', function() {
						if ($(this).attr('value') == $placeholder) {
							$(this).attr('value', '');
							$(this).css('color', $color);
							$(this).removeClass('placeholder');
						}
					}).bind('blur', function() {
						if ($(this).attr('value') == $placeholder || $(this).attr('value') == '') {
							$(this).attr('value', $placeholder);
							if ($hascolor || $hascolor != -1) {
								$(this).css('color', '#aeaeae');
							}
							$(this).addClass('placeholder');
						}
					});
				}
			});
		});
	};
})(jQuery);


/* WRITE TO FIREBUG CONSOLE
----------------------------------------------------*/
var Console = function() {
	return {
		Log: function(msg, args) { if (typeof console == 'undefined') return false; if (!args) args = ''; console.log(msg, args) },
		Info: function(msg, args) { if (typeof console == 'undefined') return false; if (!args) args = ''; console.info(msg, args) },
		Warn: function(msg, args) { if (typeof console == 'undefined') return false; if (!args) args = ''; console.warn(msg, args) },
		Error: function(msg, args) { if (typeof console == 'undefined') return false; if (!args) args = ''; console.error(msg, args) }
	}
} ();


/* CANCEL EVENT
----------------------------------------------------*/
function CancelEvent(e) {
	try {
		if (e) {
			e.returnValue = false;
			e.cancelBubble = true;
			if (e.stopPropagation) {
				e.stopPropagation();
			}
			if (e.preventDefault) {
				e.preventDefault();
			}
		}
	} catch (c) { alert(c); }
	return false;
}