(function($) {
  $.fn.isBefore = function(elem) {
    if(typeof(elem) == "string") elem = $(elem);
    return this.add(elem).index(elem) > 0;
  }
})(jQuery)


$.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];
  }
});


var mouse_is_inside_teamFilter = false;
var mouse_is_inside_monthFilter = false;
var mouse_is_inside_monthFilterTeamPage = false;

$(document).ready(function() {
    $('.level1Faq:first').css("border-top", "solid 1px #dadada");
    $('.level1Faq:first').css("border-left", "solid 1px #dadada");
    $('.level1Faq:first').css("border-right", "solid 1px #dadada");
    $('.level1Faq:first').css("border-bottom", "solid 1px #eeeeee");
    $('.level1Holder:first').show();
    var monthFilter = "";
    if($.getUrlVars("month")["month"] != undefined) {
        monthFilter = $.getUrlVars("month")["month"];
    }
    $('#monthFilter').val(monthFilter);

    if(monthFilter.length > 0) {
        doMonthFilter(monthFilter);
    }

    
    $("input").focus(function() {
       $(this).val(""); 
    });
    
    $("textarea").focus(function() {
       $(this).val(""); 
    });
    
});

$('.level1Faq').click(function() {
   var clickedID = $(this).attr("id");
   $('#' + clickedID + 'Holder').show();
   $('#' + clickedID + 'Holder').siblings().hide();
   
   $(this).siblings().css("border", "none");
   $(this).css("border-top", "solid 1px #dadada");
    $(this).css("border-left", "solid 1px #dadada");
    $(this).css("border-right", "solid 1px #dadada");
    $(this).css("border-bottom", "solid 1px #eeeeee");
});

$('.level2Holder h1').click(function() {
    if($(this).hasClass('up')) {
        $(this).removeClass('up').addClass('down');
        $(this).removeClass('downArrow').addClass('upArrow'); 
        $(this).next().slideDown('slow');
    }
    else {
        $(this).removeClass('down').addClass('up'); 
        $(this).next().slideUp('slow');
        $(this).removeClass('upArrow').addClass('downArrow'); 
    }
});

$('.level2Holder h2').click(function() {
    if($(this).hasClass('up')) {
        $(this).removeClass('up').addClass('down'); 
        $(this).next().slideDown('slow');
        $(this).removeClass('downArrow').addClass('upArrow'); 
    }
    else {
        $(this).removeClass('down').addClass('up'); 
        $(this).next().slideUp('slow');
        $(this).removeClass('upArrow').addClass('downArrow'); 
    }
});



function doMonthFilter(filterValue) {
    $('.hiddenMonth').each(function() {
        var monthValue = $(this).html();
        if(($(this).prev().html() == filterValue && $(this).prev().hasClass("hiddenMonth"))) {
            filterValue = monthValue;
        }
        if(filterValue == '') {
            $(this).closest('.matchRow').show();
        }
        else if(filterValue != monthValue) {
            $(this).closest('.matchRow').hide();
        }
        else {
            $(this).closest('.matchRow').show();
        }
    });

	var oneFound = false;
	$('.matchRow').each(function() {
		if(!$(this).is(':hidden')) {
			oneFound = true;
			$('#ingenHidden').hide();
		}
		if(!oneFound) {
			$('#ingenHidden').show();
		}
	});
}

$('.matchRow').click(function() {
	window.location = $(this).attr("url");
});

$('#submitSearch').click(function() {
   var teamFilterURL = $('#teamFilter').prev().attr("id");
   var monthFilter = $('#monthFilter').prev().attr("id");
   var monthText = "";
   if(monthFilter.length > 0) {
    monthText = "#month=" + monthFilter;
   }
   
   window.location.href = teamFilterURL + monthText;
});

$('.dropdownBox').click(function() {
    var theEl = this;
    if($(this).next().hasClass('showingDropdown')) {
        $(this).next().slideUp('slow').removeClass('showingDropdown');
	doSelection($('.currentlySelected'));
    }
    else {
        $(this).next().slideDown('slow').addClass('showingDropdown'); 
        $('.dropdownBox').each(function() {
			if(theEl != this) {
				$(this).next().slideUp('slow').removeClass('showingDropdown'); 
			}
	});
    }
});

$(document).keydown(function(event) {
  $('.showingDropdown').each(function() {
    var key = event.keyCode;
    
    var pressedLetter = String.fromCharCode(event.keyCode);
    if(event.preventDefault()) {
      event.preventDefault()
    }
    else {
      event.returnValue = false;
    }
    if(key == 40) {
      if($('.currentlySelected').next().length > 0) {
	$('.currentlySelected').removeClass('currentlySelected').next().addClass('currentlySelected');
      }
      else {
	$('.currentlySelected').removeClass('currentlySelected');
	$('.showingDropdown li:first').addClass('currentlySelected');
      }
    }
    else if(key == 38) {
      if($('.currentlySelected').prev().length > 0) {
	$('.currentlySelected').removeClass('currentlySelected').prev().addClass('currentlySelected');
      }
      else {
	$('.currentlySelected').removeClass('currentlySelected');
	$('.showingDropdown li:last').addClass('currentlySelected');
      }
    }
    else if(key == 13) {
      if($('.currentlySelected').length > 0) {
	doSelection($('.currentlySelected'));
      }
    }
    else if(key == 27) {
      $(this).slideUp('slow').removeClass('showingDropdown');
      doSelection($('.currentlySelected'));
    }
    else {
      $('.showingDropdown li').each(function() {
	var theText = $(this).html();
	
	var firstLetter = theText.substring(0, 1);
	var firstFoundValid = "";
	
	if(pressedLetter == firstLetter) {
	  firstFoundValid = $(this);
	  if($('.currentlySelected').length > 0) {
	    if($('.currentlySelected').html().substring(0,1) == pressedLetter) {
	        var numberOfElements = $('.showingDropdown li').length;
		var counter = 0;
		$('.showingDropdown li').each(function() {
		  if(($(this).html().substring(0,1) == pressedLetter) && ($('.currentlySelected')[0] != $(this)[0])) {
		    if($('.currentlySelected').isBefore($(this))) {
		      $('.currentlySelected').removeClass('currentlySelected');
		      $(this).addClass('currentlySelected');
		      return false;
		    }
		  }
		  counter++;
		});
		if(numberOfElements == counter) {
		  $('.currentlySelected').removeClass('currentlySelected');
		  $(this).addClass('currentlySelected');  
		}
	      return false;
	    }
	    else {
	      $('.currentlySelected').removeClass('currentlySelected');
	      $(this).addClass('currentlySelected');
	    }
	    return false;
	  }
	  else {
	    $('.currentlySelected').removeClass('currentlySelected');
	    $(this).addClass('currentlySelected');
	    return false;
	  }
	}
      });
    }
  });
});

$('#teamFilter li').click(function() {
    doSelection($(this));
});

$('#monthFilter li').click(function() {
    doSelection($(this));
})

function doSelection(theEl) {
    var theTeam = theEl.html();
    
    var theURL = theEl.attr("id");
    
    theEl.parent().prev().attr("id", theURL);
    theEl.parent().prev().html(theTeam);
    theEl.parent().slideUp('slow').removeClass('showingDropdown');
    theEl.removeClass('currentlySelected');
}
 
$('.bestillingButton').click(function() {
  var theSelect = $(this).parent('td').prev().children('select');

  if(theSelect != undefined) {
	  if(theSelect[0].selectedIndex == 0) {
    	theSelect.css("border", "3px solid red");
	    return false;
	  }
  }
});

$('#ticketSelect').change(function() {
  var selectedValue = $(this).val();
  
  var currentHref = $(this).parent('td').next().children('a').attr("href");
  
  var productPosition = currentHref.indexOf("&product");
  
  var newHref = "";
  if(productPosition != -1) {
    newHref = currentHref.substring(0, productPosition);
  }
  else {
    newHref = currentHref;
  }

  $(this).parent('td').next().children('a').attr("href", newHref + "&product=" + selectedValue);
});

$('#monthFilterTeamPage li').click(function() {
    var theMonth = $(this).html();
    
    var theMonthAsNum = $(this).attr("id");
    
    $(this).parent().prev().attr("id", theMonthAsNum);
    $(this).parent().prev().html(theMonth);
    $(this).parent().slideUp('slow').removeClass('showingDropdown');
    
    doMonthFilter(theMonthAsNum);
})

$('#kontaktSend').click(function() {
    var name = $('#navn').val();
    var email = $('#epost').val();
    var question = $('#sporsmal').val();
    
    $.ajax({
		type: "POST",
		url: "ajax/kontaktOss.php",
		data: "name=" + name + "&email=" + email + "&question=" + question,
		success: function(data) {
		    if(data) {
                        $('#kontaktOss .sidebarBox').html("Sendt");
                    }
		}
    }); 
});

$('#nyhetsbrevSend').click(function() {
    var email = $('#epostNyhetsbrev').val();
    
    $.ajax({
		type: "POST",
		url: "ajax/nyhetsbrev.php",
		data: "email=" + email,
		success: function(data) {
		    if(data) {
                        $('#nyhetsBrev .sidebarBox').html("Sendt");
                    }
		}
    });
});


$('.level1Faq').click(function() {
   var clickedID = $(this).attr("id");

   $('#theQuestions' + clickedID).show();
   $('#theQuestions' + clickedID).siblings().hide();
   
   $(this).siblings().css("border", "none");
   $(this).siblings().removeClass("currentTab");
   $(this).css("border-top", "solid 1px #dadada");
    $(this).css("border-left", "solid 1px #dadada");
    $(this).css("border-right", "solid 1px #dadada");
    $(this).css("border-bottom", "solid 1px #eeeeee");
    
    $(this).addClass("currentTab");
});

$('.level2Holder h1').click(function() {
    if($(this).hasClass('up')) {
        $(this).removeClass('up').addClass('down');
        $(this).removeClass('downArrow').addClass('upArrow'); 
        $(this).next().slideDown('slow');
    }
    else {
        console.log($(this));
        $(this).removeClass('down').addClass('up'); 
        $(this).next().slideUp('slow');
        $(this).removeClass('upArrow').addClass('downArrow'); 
    }
});

$('.question h2').click(function() {
    if($(this).hasClass('up')) {
        $(this).removeClass('up').addClass('down'); 
        $(this).next().slideDown('slow');
        $(this).removeClass('downArrow').addClass('upArrow'); 
    }
    else {
        $(this).removeClass('down').addClass('up'); 
        $(this).next().slideUp('slow');
        $(this).removeClass('upArrow').addClass('downArrow'); 
    }
});

