﻿$(function() {
    $("#globalsearch").keypress(function(event) {
        if (event.keyCode == 13) {
            SearchFromGlobal();
            return false;
        }
    });
    
    $("#globalsearchbutton").click(function(event) {
        SearchFromGlobal();
        return false;
    });
});

function SearchFromGlobal() {
    var url = "/Tools/Soeg.aspx?";
    url += addToQs("q", $("#globalsearch").val());
    url = url.substr(0, url.length - 1);
    window.location = url;
}

$(document).ready(function() {

  $('#search .texttype').focus(function() {

    if ($(this).val() == $(this).attr('title')) {
      $(this).val('');
    }
  });
  $('#search .texttype').blur(function() {

    if ($(this).val() == '') {
      $(this).val($(this).attr('title'));
    }
  });


  $('.togglePasswordRequest .toggleaction').click(function() {
    $(this).siblings('.togglearea').slideToggle('fast');
    $(this).blur().toggleClass('close');
    $(this).blur().toggleClass('open');
    return false;
  });

  $('.toggleSubextra .toggleaction').click(function() {
    $(this).siblings('.togglearea').slideToggle('fast');
    $(this).blur().toggleClass('close');
    $(this).blur().toggleClass('open');
    return false;
  });


  $('.toggleextra .toggleaction').click(function() {
    $(this).siblings('.toggleNewsletter').slideToggle('fast');
    $(this).blur().toggleClass('minus');

    return false;
  });


  $('.togglequestion .toggleaction').click(function() {
    $(this).siblings('.togglearea').slideToggle('fast');
    $(this).blur().toggleClass('close');
    $(this).blur().toggleClass('open');
    return false;
  });
  $('.toggleAppendiks .toggleaction').click(function() {
    $(this).siblings('.togglearea').slideToggle('fast');
    $(this).blur().toggleClass('close');
    $(this).blur().toggleClass('open');
    return false;
  });


  $('#toggleAllAction').click(function() {

    if (!$(this).hasClass('minus')) {

      $(this).siblings('.chapter .togglearea').slideDown();
      $(this).blur().addClass('minus');

      $('.toggleSubextra .toggleaction').each(function() {

        $(this).siblings('.togglearea').slideDown();
        $(this).blur().addClass('close');
        $(this).blur().removeClass('open');
      });


    } else if ($(this).hasClass('minus')) {
      $(this).blur().removeClass('minus');

      $('.toggleSubextra .toggleaction').each(function() {

        $(this).siblings('.togglearea').slideUp();
        $(this).blur().addClass('open');
        $(this).blur().removeClass('close');
      });
    }
    return false;
  });

  $('#toggleAllChapters').click(function() {

    if ($(this).hasClass('open')) {

      $(this).blur().addClass('close');
      $(this).blur().removeClass('open');

      $('.toggleextra .toggleaction').each(function() {
        $(this).siblings('.togglearea').slideDown();

        $(this).blur().addClass('close');
        $(this).blur().removeClass('open');
      });


    } else if ($(this).hasClass('close')) {

      $(this).blur().removeClass('close');
      $(this).blur().addClass('open');

      $('.toggleextra .toggleaction').each(function() {
        $(this).siblings('.togglearea').slideUp();

        $(this).blur().addClass('open');
        $(this).blur().removeClass('close');
      });

    }
    return false;
  });

  $('#toggleAllChapterslist').click(function() {
    $('.chapter').toggle();
    if ($(this).hasClass('open')) {
      $(this).blur().addClass('close');
      $(this).blur().removeClass('open');
    }
    else if ($(this).hasClass('close')) {
      $(this).blur().addClass('open');
      $(this).blur().removeClass('close');
    }

  });


  $('a.external').newWin();

  $('.chapter .togglearea').each(function() {
    $(this).css('display', 'block');
  });

  $('.toggleAppendiks .togglearea').each(function() {
    $(this).css('display', 'none');
  });
  $('.toggleSubextra .togglearea').each(function() {
    $(this).css('display', 'none');
  });
  $('.toggleNewsletter').each(function() {
    $(this).css('display', 'none');
  });

  $('.togglePasswordRequest .togglearea').each(function() {
    $(this).css('display', 'none');
  });
});


$('#region').change(function() {

    $('div').each(function() {
        $('.region', this).each(function() {
            $(this).css('display', 'none');
        })
    });
    var selectVal = $('#region').val();
    var id = '#' + selectVal;
    $(id).css('display', 'block');
});
    
    
$(function() {
    $(".removeDefaultInputValue").removeDefaultInputValue();
});

$.fn.newWin = function() {
    return this.each(function() {
        $(this).attr('target', '_blank');
    })
};
	
function addToQs(name, val) {
  if (val != "") {
    return name + "=" + encodeURIComponent(val) + "&";
  }
  return "";
}

$.fn.removeDefaultInputValue = function() {
    $(this).each(function(i) {
     
        var obj = $(this);
        if (obj.val() != "") {
            obj.css('color', '#999999');
            if (obj.attr('title') == "") {
                obj.attr('title', obj.val());
            }
            $(this).bind("focus", function(event) {
                if (obj.val() == obj.attr('title')) {
                    obj.val('');
                    obj.css('color', '#000000');
                }
            }),
			$(this).bind("blur", function(event) {
			    if (obj.val() == '' && obj.attr('stopInsertDefaultValue') != "true") {
			        obj.val(obj.attr('title'));
			        obj.css('color', '#999999');
			    }
			});
            $(this).parents('form').bind("submit", function(event) {
                obj.attr('stopInsertDefaultValue', "true");
                if (obj.val() == obj.attr('title')) {
                    obj.val('');
                }
            });
        }
    });
    return this;
};