var COOKIE_NAME = 'mr_fontsize';
var options = { path: '/', expires: 99 };
        


       
$(document).ready(function(){
		if (jQuery.browser == "msie" && jQuery.browser.version < 9)
		  $("#unsuported").show();

		$("ul.sf-menu").superfish({
					delay:       100,                            // one second delay on mouseout
					animation:   {opacity:"show",height:"show"},  // fade-in and slide-down animation
					speed:       "fast",                          // faster animation speed
					autoArrows:  true,                           // disable generation of arrow mark-up
					dropShadows: true                            // disable drop shadows
				});


		$('#featuresBig').innerfade({
				animationtype: 'fade',
				speed: 1500,
				timeout: 7500,
				type: 'sequence',
				containerheight: '1em'
			});
			$('#relatedpictures').innerfade({
				animationtype: 'fade',
				speed: 1000,
				timeout: 4000,
				type: 'sequence',
				containerheight: '1em'
			});

    $("#slider").slider({min:0, max:100, step:5, slide: function(event, ui) 
    													{
    														if (ui.value == 0)
    															$("#score").html("No Score");
    														else
    															$("#score").html(ui.value+"%");
    													}});



	//Column height fix, minimum height is 800
		var current = 12;
		try{
			current = parseFloat($.cookie('mr_fontsize'));	
		}catch(e){
			current = parseFloat(12);
		}
		
		$("div.text").css("font-size", (current));
		        
		var biggest = 1000;
		var defaultHeight = 1000;
		
		if ($("#contentmiddle").height() > biggest) biggest = $("#contentmiddle").height();
		if ($("#contentright").height() > biggest) biggest = $("#contentright").height();
		
		$("#contentleft").css("height", biggest);
		$("#contentmiddle").css("height", biggest);
		$("#contentright").css("height", biggest);
		defaultHeight = biggest;
			
		var max = 21;
		var min = 9;
		var normal = 12;
		var change = 1.5;
		var startHeight = $("div.text").height();
		
		$("a.font").click(function(event)
		{
			event.preventDefault();

			var action = $(this).attr("rel");
			//alert(action);
			if (action == "increase" && current < max)
			{
				current = current+change;
				$("div.text").css("font-size", (current));
			}
			else if (action == "decrease" && current > min)
			{
				current = current-change;

				$("div.text").css("font-size", (current));
			}
			else if (action == "normal")
			{
				current = normal;
				$("div.text").css("font-size", (current));
				
			}	
			
			$.cookie(COOKIE_NAME, current, options);
			
			var biggest = 1000;
			if ($("#contentmiddle").height()-startHeight+$("div.text").height() > biggest) biggest = $("#contentmiddle").height()-startHeight+$("div.text").height();
			if ($("#contentright").height() > biggest) biggest = $("#contentright").height();
			
			if (action == "normal")
			{
				$("#contentleft").css("height", defaultHeight);
				$("#contentmiddle").css("height", defaultHeight);
				$("#contentright").css("height", defaultHeight);
			}
			else
			{
				$("#contentleft").css("height", biggest);
				$("#contentmiddle").css("height", biggest);
				$("#contentright").css("height", biggest);
			}
			 startHeight = $("div.text").height();
		});
		
		var clickSearch = false;
		$("#searchbtn").click(function(e) 
		{
			e.preventDefault();
			clickSearch = true;
			$("#bar-top-searchresults").fadeIn();
			$(".searchfield").select();
			$(".banner").css("display", "none");
		});
		
		$("#bar-top-searchresults").click(function(e) 
		{
			clickSearch = true;
		});
		
		$(document).click(function(e) 
		{
			if (clickSearch == false)
			{
				//e.preventDefault();
				$("#bar-top-searchresults").fadeOut();
				$(".banner").css("display", "block");
			}
			clickSearch = false;
		});

		$(".searchfield").keyup(function(e) 
		{
			// if user press esc close searchbar
			if (e.which == 27)
			{
				if ($("#searchresults").html() != "")
				{
					$(".searchfield").attr("value", "");
					$(".searchfield").select();
					$("#searchresults").html("");			
				}
				else
					$(document).click();
					
				return true;
			}
				
			//else start searching data
			//alert($(".searchfield").attr("value"));
			if ($(".searchfield").attr("value") == "")
				$("#searchresults").html("");
			else
				$.get("/site/index.php",  { search: $(".searchfield").attr("value")},
				function(data){
				  $("#searchresults").html(data);
				});
	
		});
		
		$(".cancelRegForm").click(function(e)
		{
			e.preventDefault();
			$("#regFormBG").hide();
			$("#regForm").fadeOut();
		});
		
		$("#submitOldNick").click(function(e)
		{
			e.preventDefault();
			var username = $("#oldnick").attr("value");
			var password = $("#oldpass").attr("value");

			$.get("/site/index.php", {authenticateUser: "true", username: username, password: password}, function(data) 
			{
				$("#regFormMessage").html(data);
			});
		});
		
		$("#submitNewNick").click(function(e)
		{
			e.preventDefault();
			var username = $("#newnick").attr("value");

			$.get("/site/index.php", {createuser: "true", username: username}, function (data) 
			{
				$("#regFormMessage").html(data);
			} );
		});

		$("#reactionSubmit").click(function(e)
		{
			$("#reactionSubmit").fadeOut();
			e.preventDefault();
			var text = $("#reactionField").attr("value");
			var userid = $("#reactionUserid").attr("value");
			var table = $("#reactionTable").attr("value");
			var item = $("#reactionItem").attr("value");
			var currentHeight = $("#reactions").height();
			var score = $("#score").html();
			if (score == 'No Score' || score == null) score = '-1';
			score = score.replace("%", "");

			$.get("/site/index.php", {submitReaction: "true", text: text, userid: userid, table: table, item: item, score: score}, function (data) 
			{

				$("#reactions").append(data);	
				var newHeight = $("#reactions").height();
				
				var add = Math.ceil(newHeight - currentHeight);
				$("#contentleft").css("height", defaultHeight+add);
				$("#contentmiddle").css("height", defaultHeight+add);
				$("#contentright").css("height", defaultHeight+add);	
				defaultHeight = defaultHeight+add;
				
				$(".reactionForm").fadeOut();	
			} );
			
		});
		
		$("#there").click(function(e)
		{
			$("#there").attr("disabled", "disabled");
			
			e.preventDefault();
			var type = $("#type").val();
			var id = $("#id").val();
			var total = $("#totalusers").val();
			var button = $(this).val();

			var rownumber = Math.ceil(total/6);

			$.get("/site/index.php", {present: type, id: id}, function(data)
			{
				if (button == "I wasn't there")
				{
					button = "I was there";
					$("#totalusers").attr("value", (parseInt(total)-1));
					$("#user-"+data).remove();
				}
				else if (button == "I was there")
				{
					button = "I wasn't there";
					$("#totalusers").attr("value", (parseInt(total)+1));
					if (total%6 == 0)
						$("#present").append("</tr><tr class=\"row"+rownumber+"\"><td></td>");	
					$("#present tr.row"+rownumber).append(data);
				}
				else if (button == "I'm not going")
				{
					button = "I'm going";	
					$("#totalusers").attr("value", (parseInt(total)-1));
					$("#user-"+data).remove();
				}
				else if (button == "I'm going")
				{
					button = "I'm not going";
					$("#totalusers").attr("value", (parseInt(total)+1));
					if (total%6 == 0)
						$("#present").append("</tr><tr class=\"row"+rownumber+"\"><td></td>");	
					$("#present tr.row"+rownumber).append(data);
				}
				$("#there").attr("value", button);				
			});
			$("#there").attr("disabled", "");
		});
		
		$(".homearticle").hover(
	      function () {
	     // alert('down');
	     	$(".articletitle").css('display', 'none');
	        $(this).find(".articletitle").fadeIn();
	      }, 
	      function () {
		    //  alert('up');
	        $(this).find(".articletitle").hide();
	      }
	    );
	    
	    $("#enterContestBtn").click(function(e)
	    {
	    	e.preventDefault();
	    	
	    	var firstname 		= $("#firstname").attr("value");
	    	var lastname 		= $("#lastname").attr("value");
	    	var address 		= $("#address").attr("value");
	    	var nr 				= $("#nr").attr("value");
	    	var zipcode 		= $("#zipcode").attr("value");
	    	var city 			= $("#city").attr("value");
	    	var country 		= $("#country").attr("value");
	    	var email 			= $("#email").attr("value");
	    	var phone 			= $("#phone").attr("value");
	    	var answer 			= $("#answer").attr("value");	  
	    	var id 				= $("#id").attr("value");
	    	
	    	$.post("/site/index.php", {contest: 'enter', id: id, firstname: firstname, lastname: lastname, address: address, nr: nr, zipcode: zipcode, city: city, country: country, email: email, phone: phone, answer: answer}, function(data)
			{  	
				if (data == "error")
					alert("Something went wrong, please try again");
				else
					$("#contestform").html("<b style=\"color:#003500;\">Thanks for entering this contest, we will contact you if you have won!</b>");
			});
	    });
	    
	    $("#sendMail").click(function(e)
	    {
	    	e.preventDefault();
	    	
	    	var name 			= $("#name").attr("value");
	    	var email 			= $("#email").attr("value");
	    	var subject 		= $("#subject").attr("value");
	    	var text	 		= $("#text").attr("value");
			var uc 				= $("#uc").attr("value");
			
			if (uc > 5)
			{
				$.post("/site/index.php", {contact: 'send', name: name, email:email, subject: subject, text: text}, function(data)
				{  	
					if (data == "error")
						alert("Something went wrong, please try again");
					else
						$("#contact").html("<b style=\"color:#003500;\">Thanks for sending an E-mail to Metalrage. We will try to get back to you as soon as possible</b>");
				});
			}
		});
		
		$("#trackBtn").click(function(e)
	    {
	    	e.preventDefault();
	    	
	    	var code 			= $("#code").attr("value");
			
			$.post("/site/index.php", {status: 'send', code: code}, function(data)
			{  	
				if (data == "error")
					alert("Something went wrong, please try again");
				else
					$("#result").html(data);
			});

		});
		if ($("#code").attr("value") != "")
			$("#trackBtn").click();
		
		$("#btn").hover(
	      function () {
	     // alert('down');
	        $("#rpxexplain").fadeIn();
	      }, 
	      function () {
	        $("#rpxexplain").fadeOut();
	      }
	    );
 });
 
