document.documentElement.className = "js";

$.extend({
  getUrlVars: function(){
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
      hash = hashes[i].split('=');
      vars.push(hash[0]);
      vars[hash[0]] = hash[1];
    }
    return vars;
  },
  getUrlVar: function(name){
    return $.getUrlVars()[name];
  }
});

/*popup*/
function popit(url, height, width){
  var left = parseInt((screen.availWidth/2) - (width/2));
  var top = parseInt((screen.availHeight/2) - (height/2));
  var windowFeatures = "width=" + width + ",height=" + height + ",status,scrollbars=1,resizable,left=" + left + ",top=" + top + "screenX=" + left + ",screenY=" + top;
  window.open(url, "subWind", windowFeatures);
}

$(document).ready(function(){	

	/*----------------------------------FULTON--------------------------------------*/
	
	$("#sidebar a.fulton").click(function(e){
		e.preventDefault();
		$(".cntFulton").slideToggle("slow");	
	})	
	
	function isInt(value){		
		return (value != 0) && (value == parseInt(value, 10));
	}
	
	// set default input text
	$("input[name=weight]").attr({"value":"Vikt i gram"});	
	$("input[name=length]").attr({"value":"Längd i cm"});	
	
	// clear input text on focus, unless it is INT
	$("form.fulton input").focus(function(){
		if($(this).val() == "Vikt i gram" || $(this).val() == "Längd i cm" || !isInt($(this).val()))
			$(this).attr({"value":""})	
	})


	$("form.fulton input").focusout(function(){
		if($("input[name=weight]").val() == ""){
			$("input[name=weight]").attr({"value":"Vikt i gram"});
			
			$(".fultonInfo span.fulton").html("");
			$(".viceMenSays").html("");				
		}
			
		if($("input[name=length]").val() == ""){
			$("input[name=length]").attr({"value":"Längd i cm"});
			
			$(".fultonInfo span.fulton").html("");
			$(".viceMenSays").html("");				
		}				
	})
	
	$("form.fulton input").focus(function(){
		if($(this).val() == ""){
			$(".fultonInfo span.fulton").html("");
			$(".viceMenSays").html("");			
		}
	})
		
	$("form.fulton input").keyup(function(){    
		
		if($(this).attr("name") == "weight" && $("input[name=length]").val() != "" && isInt($("input[name=length]").val()) && ($(this).val() * 100) / ($("input[name=length]").val() * $("input[name=length]").val() * $("input[name=length]").val()) > 0){
			$(".fultonInfo span.fulton").html(($(this).val() * 100) / ($("input[name=length]").val() * $("input[name=length]").val() * $("input[name=length]").val()));
		}
		
		if($(this).attr("name") == "length" && $("input[name=weight]").val() != "" && isInt($("input[name=weight]").val()) && ($("input[name=weight]").val() * 100) / ($(this).val() * $(this).val() * $(this).val()) > 0 ){
			$(".fultonInfo span.fulton").html(($("input[name=weight]").val() * 100) / ($(this).val() * $(this).val() * $(this).val()));
		}
		
		if($("input[name=fulton]").val() == "" || $("input[name=fulton]").val() == "Infinity" || $("input[name=fulton]").val() == "NaN"){
			$(".fultonInfo span.fulton").html("");			
		}
		
		if($(".fultonInfo span.fulton").html() == "" || $(".fultonInfo span.fulton").html() == "Infinity" || $(".fultonInfo span.fulton").html() == "NaN" || $("input[name=weight]").val() == ""){			
			$(".fultonInfo span.fulton").html("");
			$(".viceMenSays").html("");					
		}		
		
		
		
		
		
		if($(".fultonInfo span.fulton").html() >= 1.1 && $(".fultonInfo span.fulton").html() <= 2.0){
			$(".fultonInfo span.fulton").css({"color":"green"});
			
			$(".viceMenSays").html(
				"<strong>Teamet tycker till:</strong><br />Berndt: <i>Naaah, det är nog en besa...</i><br />Slaune: <i>Snorblank och fullsmetad med räkor va?</i><br />Berra: <i>Måsarna kalasar!</i><br />Båga: <i>Vad tog den på?</i>"			
			);			
		}
			
		if($(".fultonInfo span.fulton").html() < 1.1){
			$(".fultonInfo span.fulton").css({"color":"red"});		
			
			$(".viceMenSays").html(
				"<strong>Teamet tycker till:</strong><br />Berndt: <i>Naaah, det är nog en besa...</i><br />Slaune: <i>Perfekt kondition!</i><br />Berra: <i>Här går dom upp!</i><br />Båga: <i>Slepps!</i>"			
			);
		}
		
		if($(".fultonInfo span.fulton").html() >= 2.0){
			$(".fultonInfo span.fulton").css({"color":"green"});
			
			$(".viceMenSays").html(
				"<strong>Stadig bit!</strong><br />Teamet rekommenderar dig att rapportera fångsten <a href=\"http://www.edgeflyfishing.com/ipb/index.php?app=forums&module=post&section=post&do=reply_post&f=32&t=120844\" target=\"_blank\">här</a>"			
			);			
		}
				
		if($(".fultonInfo span.fulton").html() == "")
			$(".viceMenSays").html("");		
			
	})
	/*----------------------------------FULTON--------------------------------------*/



	/*load the file with images to preload*//*
	$.get('http://teamcommando.com/commando-includes/preload-bg-images.php', function(data) {
	  the_array_to_preload = data.split(",");	  
		var nrOfImg = the_array_to_preload.length;		
		$(the_array_to_preload).each(function(ind){	  	
			$('<img/>')[0].src = this;
		});
	});		
	*/
	

	

	$("#content .postTop a").live("click",function(e){
		e.preventDefault();		
				
		var conexp 			= $(this).attr("class");
		var divToChange = $(this).parent().siblings().children("div:first");		
		//var contentHeight = $("#content").innerHeight();
	
		$(divToChange).slideToggle("slow",function(){	
			//alert(contentHeight);								
			//$("#cntContent").css({"height": (contentHeight+500) + "px"});
		});		
		 		
		if (conexp == "con"){
			var closedDestination = $(this).attr("href");
		 		closedDestination = closedDestination.substring(2);
				closedDestination--;
				if(closedDestination < 1)
					closedDestination++;				 
				closedDestination = $("#content .postTop a#a" + closedDestination).offset().top;
									
			$(this).removeClass("con");
			$(this).addClass("exp");
			
			$("html:not(:animated),body:not(:animated)").animate({ scrollTop: closedDestination-10}, 700 );
	   	return false;			
		}
		else if(conexp == "exp"){
	 		var elementClicked = $(this).attr("href");		
   		var destination = $("#content .postTop a" + elementClicked).offset().top;
					
			$(this).removeClass("exp");
			$(this).addClass("con");
			
			$("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination-10}, 700 );
	   	return false;			
		}	
								
	})
	
	$("#sidebar .postTop a").click(function(e){
		e.preventDefault();		
				
		var conexp 			= $(this).attr("class");
		var divToChange = $(this).parent().siblings().children("div:first");

		if($(this).attr("id") == "forum_posts" && $(this).attr("class") == "exp"){
			$.get('http://teamcommando.com/commando-includes/forum_latest_posts.php',{from:"script"}, function(data) {
				$("a#forum_posts").parent("div").siblings("div.postBody").children("div.entry").html(data);
			})
		}
		
	 	$(divToChange).slideToggle("slow");
			
		if (conexp == "con"){
			$(this).removeClass("con");
			$(this).addClass("exp");
		}
		else if(conexp == "exp"){
			$(this).removeClass("exp");
			$(this).addClass("con");
		}			
	})	

	$("a.grooveshark").click(function(e){
		e.preventDefault();
		popit("http://www.teamcommando.com/commando-includes/pop-grooveshark.php", 565, 331)	
	});



	$("#content .nav a").live("click",function(e){
		e.preventDefault();		
						
		var conexp 			= $(".postTop a" + $(this).attr("href")).attr("class");	
		var divToChange = $(".postTop a" + $(this).attr("href")).parent().siblings().children("div:first");		
		
			
				
		 		
		if (conexp == "con"){			
			/*var closedDestination = $(this).attr("href");
		 		closedDestination = closedDestination.substring(2);
				alert(closedDestination)
				closedDestination--;
				if(closedDestination < 1)
					closedDestination++;				 
				closedDestination = $("#content .postTop a#a" + closedDestination).offset().top;
									
			$(".postTop a" + $(this).attr("href")).removeClass("con");
			$(".postTop a" + $(this).attr("href")).addClass("exp");
			*/
			var prevDestination = $("#content .postTop a" + $(this).attr("href")).offset().top;
			
			$("html:not(:animated),body:not(:animated)").animate({ scrollTop: prevDestination-10}, 700 );
	   	return false;			
		}
		else if(conexp == "exp"){
			$(divToChange).slideToggle("slow",function(){});
	 		var elementClicked = $(this).attr("href");		
   		var destination = $("#content .postTop a" + elementClicked).offset().top;
					
			$(".postTop a" + $(this).attr("href")).removeClass("exp");
			$(".postTop a" + $(this).attr("href")).addClass("con");
			
			$("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination-10}, 700 );
	   	return false;			
		}	
								
	})



	if ($('#bg img').length > 0) {		
		var nrOfImages = $('#bg img').length;
		var counter = nrOfImages;		
		var imgs = [];		
		$('#bg img').each(function (index) {			
			var i = new Image();
			i.onload = function () {
				var h = i.height;
				var w = i.width;
				imgs[index] = [w, h];
				$(window).trigger('resize');
			}
			i.src = $(this).attr('src');		
		});
				
		$(window).resize(function () {
			
			var height = $(window).height();
			var width = $(window).width();
			
			$('#bg').height(height);
			
			$('#bg').css({
				'width': width,
				'height': height
			});
			
			$('#bg img').each(function (index) {
				var self = this;
				
				if (imgs[index] && imgs[index].length === 2) {
					var w = imgs[index][0];
					var h = imgs[index][1];
					
					if ((height/width) > (h/w)) {
						$(self).css({
							'width': (w/h) * height,
							'height': height
						});						
					} else {
						$(self).css({
							'width': width,
							'height': (h/w) * width
						});
					}
				}
			});			
		}).trigger('resize');
	}	


	$("a.onoff").click(function(e){
		e.preventDefault();		
		
		if($("#wrapper").css("display") == "block") {
			
		if ($("#bg img").length < 2) {
			var filterOut = $("#bg img").attr("src");
			
			$.get('http://teamcommando.com/commando-includes/load-bg-images.php', {quantity: "all", filterOut: filterOut}, function(data){
				//$("#bg").html(data);
				$(data).appendTo($("#bg"));
				
				if ($('#bg img').length > 0) {
					var nrOfImages = $('#bg img').length;
					var counter = nrOfImages;
					var imgs = [];
					$('#bg img').each(function(index){
						var i = new Image();
						i.onload = function(){
							var h = i.height;
							var w = i.width;
							imgs[index] = [w, h];
							$(window).trigger('resize');
						}
						i.src = $(this).attr('src');
					});
					
					$(window).resize(function(){
					
						var height = $(window).height();
						var width = $(window).width();
						
						$('#bg').height(height);
						
						$('#bg').css({
							'width': width,
							'height': height
						});
						
						$('#bg img').each(function(index){
							var self = this;
							
							if (imgs[index] && imgs[index].length === 2) {
								var w = imgs[index][0];
								var h = imgs[index][1];
								
								if ((height / width) > (h / w)) {
									$(self).css({
										'width': (w / h) * height,
										'height': height
									});
								}
								else {
									$(self).css({
										'width': width,
										'height': (h / w) * width
									});
								}
							}
						});
					}).trigger('resize');
				}
				
				$('#bg').cycle({
					fx: 'fade',
					prev: '.prev',
					next: '.next',
					timeout: 0
				});
			});
		}
			
			$("#wrapper,#mainMenu,.test,.test2").fadeOut("fast");
			$("a.goHome").css({"display":"none"});
			$("a.next,a.prev").fadeIn("fast");
	  }
	  else{
			
			$("#bg img").each(function(){
				if($(this).css("display") == "block")
					$.post("http://teamcommando.com/commando-includes/set_session.php", {chosenImage: $(this).attr('src')}, function(){});	
			})			
			
			$("#wrapper,#mainMenu,.test,.test2").fadeIn("fast");
			$("a.goHome").css({"display":"block"});
			$("a.next,a.prev").fadeOut("fast");
		}
	})	
	
	//Count it down
	$('#countdown_dashboard2').countDown({
		targetDate: {
			'day': 		1,
			'month': 	4,
			'year': 	2012,
			'hour': 	00,
			'min': 		0,
			'sec': 		0,
			'utc': false
		},
		omitWeeks:true				
	});	
	
		

	// make some of the links open in new tabs/windows
	$(".forumpost h3 a").click(function(){
		$(this).attr({"target": "_blank"});
	})	
	
	
	
	//Continous scrolling....	
	
	function getPathFromUrl(url,split_by,first_or_last) {
	  
	  switch(first_or_last){
	  	case "first":
	  		return url.split(split_by)[0];
	  		break;
	  	case "last":
	  		var last_one = url.split(split_by).length;
	  		return url.split(split_by)[last_one-2];
	  		break;
	  }
	  
	}
	
	// Only if NOT mobile visitor
	if(type_of_visitor == "ordinary"){		
	
		//Dölj navigeringen iom att den bara ska synas om js inte är aktiverat
		$(".prevNext").css({"display":"none"});
		
		$(window).scroll(function(){
			if($(window).scrollTop() == $(document).height() - $(window).height()){
				
				// Make loader visible
				$("#loading_block").css({"display":"block"});
				
				if($("#postPagination a").length == 1)
					var the_ref = $("#postPagination a").attr("href");
				else{
					var the_ref = $("#postPagination a")[1].attr("href");
				}
				
				// Remove query parameter
				var link 										= getPathFromUrl(the_ref,"?","first");
				
				var page_to_load 						= parseInt(getPathFromUrl(link,"/","last"));		
				var next_page_page_to_load 	= "http://teamcommando.com/page/" + (page_to_load + 1) + "/"; // gör en koll här om sidan finns.....
				
				
				// Add container for new posts
				$("#content #postPagination").before("<div class='added_content" + page_to_load + " content'></div>");
				
				// Fill the recently added container with posts
				$(".added_content" + page_to_load).load(link + ' #content .cntPost', function(){
					// Hide loader
					$("#loading_block").css({"display":"none"});
				});		
				
				// Change the url to the "next" page...
				$("#postPagination a").attr('href',next_page_page_to_load);		   
			}
		});
	}
	
})



