/*$.extend({
	initFeed: function($type, $feed) {
		var $path = new Array();
		console.log($rss[$type][$feed]);
		$.getJSON($rss[$type][$feed], null, function($data) {
			console.log($data);
			return $data;
		});
	},
	parseItems: function($xml) {
		console.log($xml);
	}
});

$.fn.extend({
	loadItems: function($type, $feed, $page) {
		var $xml = $.initFeed($type, $feed);
		var $items = $.parseItems($xml);
	}
});*/

var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{
			string: navigator.userAgent,
			subString: "Chrome",
			identity: "Chrome"
		},
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari",
			versionSearch: "Version"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			   string: navigator.userAgent,
			   subString: "iPhone",
			   identity: "iPhone/iPod"
	    },
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();

$.extend({
	activateLinks: function($params) {
		var $ids = new Array();
		$ids[1] = 'nav';
		$ids[2] = 'subnav';
		$ids[3] = 'itemsnav';
		
		$.each($ids, function($i, $id) {
			var $link = $('#'+$id+' a.item-'+$params[$i]);
			$('#'+$id+' a').removeClass('active');
			if($link.size() > 0) {
				$link.addClass('active');
			} else {
				$('#'+$id+' a').eq(0).addClass('active');
			}
		});
	},
	createCookie: function(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=/";
	},
	readCookie: function(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() {
	$('a').live('mouseover', function() {
		var $this = $(this);
		$this.addClass('mouseover');
	});
	
	$('a').live('mouseout', function() {
		var $this = $(this);
		$this.removeClass('mouseover');
	});
	
	$('#nav a, #subnav a, #itemsnav a').live('click', function() {
		var $this = $(this);
		if(!$this.hasClass('feature') && !$this.hasClass('bubble')) {
			var $pars = $this.parents('ul:first');
			var $id = $pars.attr('id');
			
			var $url = $this.attr('href');
			
			if(BrowserDetect.browser == 'Explorer' && BrowserDetect.version == 7) {
				var $hash = $url.replace($base_url, '');
			//} else if(BrowserDetect.browser == 'Explorer' && BrowserDetect.version == 8) {
			} else {
				var $hash = $url.replace($rel_url, '');
			//} else {
			//	var $hash = $url;
			}
			
			var $params = $hash.split('/');
			
			$('#content').load($url, {'tab': $tab}, function() {
				$.activateLinks($params);
				//stats.track('#'+$hash);
				stats.track($hash);
				window.location.hash = '#'+$hash;
			});
			
			return false;
		}
	});
	
	$('.item .rating').live('mouseout', function() {
		var $this = $(this);
		
		$this.removeClass('hovered');
		$('a', $this).removeClass('hover');
	});
	
	$('.item .rating').live('mouseover', function() {
		var $this = $(this);
		
		$this.addClass('hovered');
	});
	
	$('.item .rating a').live('mouseover', function() {
		var $this = $(this);
		
		var $prev = $this.parents('li').prevAll('li');
		var $next = $this.parents('li').nextAll('li');
		
		$('a', $prev).add($this).addClass('hover');
		$('a', $next).removeClass('hover');
	});
	
	$('.item .rating a').live('click', function() {
		var $this = $(this);
		var $pars = $this.parents('.rating');
		
		var $id = $this.parents('.item').attr('id');
		
		var $url = $this.attr('href');
		var $hash = $url.replace($rel_url, '');
		
		$pars.append('<div class="thanks"></div>');
		var $thanks = $('.thanks', $pars);
		$thanks.hide();
		
		if($.readCookie('rating'+$id) == null) {
			$.post($url, null, function($data) {
				$thanks.html($data);
				$('ul', $pars).hide();
				$('ul', $pars).fadeOut('fast', function() {
					$thanks.fadeIn('fast', function() {
						window.setTimeout(function() {
							$thanks.fadeOut('slow', function() {
								$thanks.remove();
								$('ul', $pars).fadeIn('slow');
							});
						}, 2500);
					});
				});
			});
			
			$.createCookie('rating'+$id, true, 999);
		} else {
			$thanks.html('<p>Allerede stemt på</p>');
			$('ul', $pars).hide();
			$('ul', $pars).fadeOut('fast', function() {
				$thanks.fadeIn('fast', function() {
					window.setTimeout(function() {
						$thanks.fadeOut('slow', function() {
							$thanks.remove();
							$('ul', $pars).fadeIn('slow');
						});
					}, 2500);
				});
			});
		}
		
		return false;
	});
	
	$('.item > a').live('click', function() {
		var $this = $(this);
		
		var $url = $this.attr('href');
		var $rel = $this.attr('rel');
		
		$.post($rel);
		
		if(!$this.hasClass('downloads')) {
			var $appid = $url.replace($base_url, '').replace('#', '');
			imIt($appid);
			return false;
		}
	});
	
	$('a[href="#help"]').live('click', function() {
		$.get($base_url+'help', null, function($data) {
			$('body').prepend($data);
		});
		return false;
	});
	
	$('a.close').live('click', function() {
		$('#help').remove();
		return false;
	});
	
	$('a.feature').live('mouseenter', function() {
		var $this = $(this);
		if($this.next('.bubble').size() > 0) {
			$this.next('.bubble').css('display', 'block');
			$('div.ad iframe, #eyeDiv').css('visibility', 'hidden');
		}
	}).live('mouseleave', function() {
		var $this = $(this);
		if($this.next('.bubble').size() > 0) {
			$this.next('.bubble').hide();
			$('div.ad iframe, #eyeDiv').css('visibility', 'visible');
		}
	});
	
	$('.bubble').live('mouseenter', function() {
		var $this = $(this);
		$this.css('display', 'block');
		$('div.ad iframe, #eyeDiv').css('visibility', 'hidden');
	}).live('mouseleave', function() {
		var $this = $(this);
		$this.hide();
		$('div.ad iframe, #eyeDiv').css('visibility', 'visible');
	});
	
	$('.change-status a').live('click', function() {
		$('#overlay').fadeIn("fast", function() {
			$('#status-changer').show();
			$('div.ad iframe, #eyeDiv').css('visibility', 'hidden');
			PSM();
		});		
		return false;
	});
	
	$('#overlay, a.hideoverlay').live('click', function() {
		$('#overlay').hide();
		$('#status-changer').hide();
		$('div.ad iframe, #eyeDiv').css('visibility', 'visible');
		return false;
	});
	
	$('.send-friend a').live('click', function() {
		window.open("http://1clicksend2friend.com/?campaignId=f6f64161-7a08-8deb-905a-c077ffbac3a1&market=da-DK","1ClickSend2Friend","width=920,height=720,scrollbars=yes,toolbar=no,location=no");
		return false;
	});
	
	var $init_hash = window.location.hash;
	if($init_hash != '') {
		var $hash_split = $init_hash.replace('#', '');
		$hash_split = $hash_split.split('/');
		$('#content').load($rel_url+$init_hash.replace('#', ''), {'tab': $tab}, function() {
			$.activateLinks($hash_split);
		});
	} else {
		$.activateLinks(new Array());
	}
});