var map;
var logo = [];
var markers = [];
var geocoder;
var admin;
var iconHover = '/pics/icon_here.png';

function initialize() {
 geocoder = new google.maps.Geocoder(); 
 var myLatlng = new google.maps.LatLng(48.8514746, 2.2845351);
 var myOptions = { 
  zoom: 13, 
  center: myLatlng, 
  mapTypeId: google.maps.MapTypeId.ROADMAP, 
  zoomControlOptions: { position: google.maps.ControlPosition.LEFT_BOTTOM }, 
  mapTypeControl: false,
  StreetViewControlOptions: { position: google.maps.ControlPosition.LEFT_BOTTOM}, 
  addressControlOptions: {position: google.maps.ControlPosition.BOTTOM}

 }
 map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
 createMarkers();
 var pointLatLng = new google.maps.LatLng(50.8333,4.3333);
 var schoollogo = new google.maps.Marker({ position: pointLatLng, map: map, title: 'logo', icon: iconHover });
 schoollogo.setMap(map);
 logo.push(schoollogo);
 logo[0].setVisible(false);
}	

function createMarkers() {
 for (var i = 0; i < schools.length; i++) {
  var school   = schools[i];
  var iconIdle = "/pics/icon_diploma.png";
  if ( school[3][2] === 1 ){
   iconIdle = "/pics/icon_mba.png";
  } else if ( school[3][1] === 1 ){
   iconIdle = "/pics/icon_bachelor.png";
  } else if ( school[3][4] === 1 ){
   iconIdle = "/pics/icon_default.png";
  }
  var pointLatLng = new google.maps.LatLng(school[0], school[1]);
  var marker = new google.maps.Marker({
   position: pointLatLng, 
   map: map, 
   title: school[4],
   icon: iconIdle
  });
  animateMarkers(marker, map, i, school[2], iconIdle);
  marker.setMap(map);
  markers.push(marker);
 }
}

function animateMarkers(marker, map, i, schoolid, icon) {
 google.maps.event.addListener(marker, 'click', function() {
  $("#overlay").show();
  $("#info").load('/school/' + schoolid + '.html');
  $("#info").show();
  $("#result").css("overflow","hidden");
  $.address.path('/school/' + schoolid + '.html');
  return false;
 });
 google.maps.event.addListener(marker, 'mouseover', function() {
  marker.setIcon(iconHover);
 });
 google.maps.event.addListener(marker, 'mouseout', function() {
  marker.setIcon(icon);
 });
}

function findAddress(address,type) {
 geocoder.geocode( { 'address': address}, function(results, status) {
  if (status == google.maps.GeocoderStatus.OK) {

   // alert(results.toSource());

   $("#result").html('');
   var count     = 0;
   var diploma   = $("#diploma").attr('checked');
   var bachelor  = $("#bachelor").attr('checked');
   var master    = $("#master").attr('checked');
   var phd       = $("#phd").attr('checked');
   var executive = $("#executive").attr('checked');

   // If result is not a country/state/city, search for a school instead
   if (results[0].address_components[0].types[0] == 'point_of_interest'){
    $("#address").val(address);
    count = findSchool(address);
   } else {
    // Count the level of addresses returned; 1 is country level.
    var accuracy    = results[0].address_components.length;
    var countrycode = results[0].address_components[0].short_name;

    // Fix for Australia
    if (results[0].address_components[0].types[0] == 'natural_feature'){
     if (results[1]){
      countrycode = results[1].address_components[0].short_name;
     }
    }

    var screenwidth  = $(window).width() - 505;
    $('#map_canvas').css("width",screenwidth);
    google.maps.event.trigger(map, 'resize')
    map.setCenter(results[0].geometry.location);
    map.fitBounds(results[0].geometry.viewport);

    // Set different zoomlevel for US and UK
    if ( accuracy == '1' && countrycode == 'US' ){
     map.setZoom(4);
    } else if ( accuracy == '1' && countrycode == 'GB' ){
     map.setZoom(6);
    } else if ( accuracy == '1' && countrycode == 'DE' ){
     map.setZoom(6);
    } else {
     map.setZoom(map.getZoom() + 1);
    }

    var bounds       = results[0].geometry.bounds;
    var screenwidth  = $(window).width();
    $('#map_canvas').css("width",screenwidth);
    google.maps.event.trigger(map, 'resize')

    $("#result").html('');

    for (var i = 0; i < schools.length; i++) {

     var school = schools[i];

     // Only show markers that match users search options
     markers[i].setVisible(false);

     // if ( (diploma && school[3][0] === 1) || (bachelor && school[3][1] === 1) || (master && school[3][2] === 1) || (phd && school[3][3] === 1) || (executive && school[3][4] === 1) ){
     if ( (diploma && school[3][0] === 1) || (bachelor && school[3][1] === 1) || (master && school[3][2] === 1) || (phd && school[3][3] === 1) || (executive && school[3][4] === 1) || ((diploma && bachelor && master && phd && executive) && (school[3][0] === 0 && school[3][1] === 0 && school[3][2] === 0 && school[3][3] === 0 && school[3][4] === 0)) ){
      markers[i].setVisible(true);
     }

     if ( accuracy == '1' && countrycode != '' && school[7] == countrycode ) {
      // if ( (diploma && school[3][0] === 1) || (bachelor && school[3][1] === 1) || (master && school[3][2] === 1) ){
      if ( (diploma && school[3][0] === 1) || (bachelor && school[3][1] === 1) || (master && school[3][2] === 1) || (phd && school[3][3] === 1) || (executive && school[3][4] === 1) || ((diploma && bachelor && master && phd && executive) && (school[3][0] === 0 && school[3][1] === 0 && school[3][2] === 0 && school[3][3] === 0 && school[3][4] === 0)) ){
       listSchool(school,i);
       // Create a logo marker for the first result.
       // This marker will be showed if there is only one result
       if(count == 0){
        var pointLatLng = new google.maps.LatLng(school[0],school[1]);
        var icon = "/picture/marker_" + school[2] + ".png";
        logo[0].setPosition(pointLatLng);
        logo[0].setIcon(icon);
        logo[0].setZIndex(10000);
       }
       count ++; 
      }
     } else if ( accuracy > '1' ){
      var pointLatLng = new google.maps.LatLng(school[0], school[1]);
      if (bounds == undefined) {
       break;
      } else if (bounds.contains(pointLatLng)) {

       // if ( (diploma && school[3][0] === 1) || (bachelor && school[3][1] === 1) || (master && school[3][2] === 1) ){
       if ( (diploma && school[3][0] === 1) || (bachelor && school[3][1] === 1) || (master && school[3][2] === 1) || (phd && school[3][3] === 1) || (executive && school[3][4] === 1) || ((diploma && bachelor && master && phd && executive) && (school[3][0] === 0 && school[3][1] === 0 && school[3][2] === 0 && school[3][3] === 0 && school[3][4] === 0)) ){
        listSchool(school,i);
        // Create a logo marker for the first result.
        // This marker will be showed if there is only one result
        if(count == 0){
         var pointLatLng = new google.maps.LatLng(school[0],school[1]);
         var icon = "/picture/marker_" + school[2] + ".png";
         logo[0].setPosition(pointLatLng);
         logo[0].setIcon(icon);
         logo[0].setZIndex(10000);
        }
        count ++; 
       }

      }
     }
    }
    if (type == 'search'){
     $("#address").val(results[0].formatted_address);
    }
   }

   if(count == 1){
    logo[0].setVisible(true);
   }

   if(count == 0){

    // If there are no results based on the address, try finding schools by name.
    // unless this was already done
    if (results[0].address_components[0].types[0] !== 'point_of_interest'){
     $("#address").val(address);
     count = findSchool(address);
    }

    if(count == 1){
     logo[0].setVisible(true);
    }

    if(count == 0){
     var newcontent = '';
     newcontent += '<div class="school">';
     newcontent += ' <b>No Hotelschools were found in ' + results[0].formatted_address + '</b></br>';
     newcontent += ' Please try searching for a different location.<br/>Is your school not listed? <a href="addschool.html" class="showpage">Click here</a> to add your school.<br/><br/>';
     newcontent += '</div>';
     $("#result").append(newcontent);
    }

   }

   $("#result").animate({scrollTop:0});
   initBinding();

   var addresspath = $.address.path();

   if( addresspath == '/' || addresspath == '/update/index.html' || addresspath == '/facebook-register.html' ){
    document.title = 'Hotelschools - ' + results[0].formatted_address;
    $("#overlay").hide();
    $("#info").hide();
    $("#info").html('');
   } else {
    $("#overlay").show();
    $("#info").load(addresspath);
    $("#info").show();
    $("#result").css("overflow","hidden");
   }

  } else {

   // Find Schools when address is unknown
   $("#result").html('');
   var count = findSchool(address);
   if(count == 1){
    logo[0].setVisible(true);
   }
   if(count == 0){
    var newcontent = '';
    newcontent += '<div class="school">';
    newcontent += ' <b>No Hotelschools were found</b></br>';
    newcontent += ' Please try searching for a location or change your <a href="search.html" class="searchlink">search options</a>.<br/>Is your school not listed? <a href="addschool.html" class="showpage">Click here</a> to add your school.<br/><br/>';
    newcontent += '</div>';
    $("#result").append(newcontent);
    initBinding();
   }
  }
 });

 initBinding();
}

$(window).load(function () {
// alert('loaded');
})


function findSchool(address) {
 var diploma   = $("#diploma").attr('checked');
 var bachelor  = $("#bachelor").attr('checked');
 var master    = $("#master").attr('checked');
 var phd       = $("#phd").attr('checked');
 var executive = $("#executive").attr('checked');
 var bounds   = new google.maps.LatLngBounds();
 var search   = removeDiacritics( address.toLowerCase() );
 var count    = 0;
 for (var i = 0; i < schools.length; i++) {
  markers[i].setVisible(false);
  var school = schools[i];
  var schoolname = removeDiacritics( school[4].toLowerCase() ) + ' ' + removeDiacritics( school[5].toLowerCase() );
  var pointLatLng = new google.maps.LatLng(school[0],school[1]);
  if ( schoolname.indexOf(search) !== -1 ) {
   if ( (diploma && school[3][0] === 1) || (bachelor && school[3][1] === 1) || (master && school[3][2] === 1) || (phd && school[3][3] === 1) || (executive && school[3][4] === 1) || ((diploma && bachelor && master && phd && executive) && (school[3][0] === 0 && school[3][1] === 0 && school[3][2] === 0 && school[3][3] === 0 && school[3][4] === 0)) ){
    bounds.extend(pointLatLng);
    markers[i].setVisible(true);
    if(count == 0){
     // Create a logo marker for the first result.
     // This marker will be showed if there is only one result
     var icon = "/picture/marker_" + school[2] + ".png";
     logo[0].setPosition(pointLatLng);
     logo[0].setIcon(icon);
     logo[0].setZIndex(10000);
    }
    listSchool(school,i);
    count ++;
   }
  }
 }

 if( count == 0 ){
  // No schools found, turn on all markers again
  logo[0].setVisible(false);
  for (var i = 0; i < schools.length; i++) {
   if ( (diploma && school[3][0] === 1) || (bachelor && school[3][1] === 1) || (master && school[3][2] === 1) ){
    markers[i].setVisible(true);
   }
  }
 } else {
  var screenwidth  = $(window).width() - 505;
  $('#map_canvas').css("width",screenwidth);
  google.maps.event.trigger(map, 'resize')
  map.fitBounds(bounds);
  if( count === 1 ){
   map.setZoom(11);
  }
  var screenwidth  = $(window).width();
  $('#map_canvas').css("width",screenwidth);
  google.maps.event.trigger(map, 'resize')
 }
 return count;
}



function removeDiacritics (str) {
 if(!changes) {
  changes = defaultDiacriticsRemovalMap;
 }
 for(var i=0; i<changes.length; i++) {
  str = str.replace(changes[i].letters, changes[i].base);
 }
 return str;
}

function listSchool(school,i){
 var newcontent = '';
 newcontent += '<div class="school" id="' + i + '|' + school[2] + '">';
 if (school[8] > 2){
  newcontent += ' <div class="thumb-right"><div class="wraptocenter"><span></span><img id="graphic" src="/picture/' + school[13] + '" width="60" border="0"/></div></div>';
 }
 newcontent += ' <div class="name"><span class="ellipsis_text">' + school[4] + '</span></div>';
 if (school[5] != ''){
  newcontent += ' <div class="schoollocation"><span class="ellipsis_text"><em>' + school[5] + '</em></span></div>';
 }
 newcontent += ' <div class="schoollocation"><span class="ellipsis_text">' + school[6] + '</span></div>';

 newcontent += ' <ul class="schoolicons">';

 newcontent +=  '<li><div class="moreinfo">More Information</div></li>';

 if (school[3][0] == '1'){
  newcontent +=  '<li class="toolTip" title="Diploma"><img src="/pics/diploma.png" height="13"/></li>';
 }
 if (school[3][1] == '1'){
  newcontent +=  '<li class="toolTip" title="Bachelor"><img src="/pics/bachelor.png" height="13"/></li>';
 }
 if (school[3][2] == '1'){
  newcontent +=  '<li class="toolTip" title="Masters"><img src="/pics/mba.png" height="13"/></li>';
 }
 if (school[3][3] == '1'){
  newcontent +=  '<li class="toolTip" title="PhD"><img src="/pics/phd.png" height="13"/></li>';
 }
 if (school[3][4] == '1'){
  newcontent +=  '<li class="toolTip" title="Executive Education"><img src="/pics/executive.png" height="13"/></li>';
 }


 if (school[12][2] == '1'){
  newcontent +=  '<li class="toolTip" title="Offering Online - Distance Learning"><img src="/pics/online.png" height="13"/></li>';
 } 


 if (school[9][0] == '1'){
  newcontent +=  '<li class="toolTip" title="Hospitality Management"><div class="focus">H</div></li>';
 } 
 if (school[9][1] == '1'){
  newcontent +=  '<li class="toolTip" title="Tourism Management"><div class="focus">T</div></li>';
 }
 if (school[9][2] == '1'){
  newcontent +=  '<li class="toolTip" title="Sports Management"><div class="focus">S</div></li>';
 }
 if (school[9][3] == '1'){
  newcontent +=  '<li class="toolTip" title="Event Management"><div class="focus">E</div></li>';
 }
 if (school[9][4] == '1'){
  newcontent +=  '<li class="toolTip" title="Facility Management"><div class="focus">F</div></li>';
 }
 if (school[9][5] == '1'){
  newcontent +=  '<li class="toolTip" title="Destination Management"><div class="focus">D</div></li>';
 }
 if (school[9][6] == '1'){
  newcontent +=  '<li class="toolTip" title="Food &amp; Beverage"><div class="focus">F</div></li>';
 }
 if (school[9][7] == '1'){
  newcontent +=  '<li class="toolTip" title="Culinairy Arts"><div class="focus">C</div></li>';
 }

 if (school[11][2] == '1'){
  newcontent +=  '<li class="toolTip" title="Facebook"><img src="/icons/facebook.png" height="13"/></li>';
 }
 if (school[11][1] == '1'){
  newcontent +=  '<li class="toolTip" title="Twitter"><img src="/icons/twitter.png" height="13"/></li>';
 }
 if (school[11][0] == '1'){
  newcontent +=  '<li class="toolTip" title="Video"><img src="/icons/youtube.png" height="13"/></li>';
 }
 if (admin == true){
  newcontent +=  '<li class="adminlink"></li>';
 }
 newcontent += ' </ul>';
 newcontent += '</div>';
 $("#result").append(newcontent);
}

function search(){
 logo[0].setVisible(false);
 $("#overlay").hide();
 $("#result").css("overflow","auto");
 $("#info").hide();
 $("#info").html('');
 address = $("#address").val();
 findAddress(address,'search');
}

function onStatus(response) {

  // https://developers.facebook.com/blog/post/525/

  if (response.status === 'connected') {
    // the user is logged in and connected to your
    // app, and response.authResponse supplies
    // the user’s ID, a valid access token, a signed
    // request, and the time the access token 
    // and signed request each expire
    var uid = response.authResponse.userID;
    var accessToken = response.authResponse.accessToken;



   FB.api({ method: 'fql.query', query: 'SELECT name, email, current_location FROM user WHERE uid=' + response.authResponse.userID }, function(response) {
    if (response[0].current_location != null) {
     if( response[0].current_location.state != '' && (response[0].current_location.country == 'United States' || response[0].current_location.country == 'Canada')){
      address = response[0].current_location.state;
      if ( response[0].current_location.country != '' ){
       address = address + ', ' + response[0].current_location.country;
      }
     } else if ( response[0].current_location.country != '' ){
      address = response[0].current_location.country;
     }
    }
    $('#user').html(response[0].name);
   });

   FB.api({ method: 'fql.query', query: 'SELECT developer_id FROM developer WHERE application_id=213019372053164 AND developer_id=' + response.authResponse.userID }, function(response) {
    if(response.length === 1){
     admin = true;
     $(".schoolicons").append('<li class="adminlink"></li>');
     initBinding();
    };
   });

   FB.api({ method: 'fql.query', query: 'SELECT pic_square FROM profile WHERE id=' + response.authResponse.userID }, function(response) {
    $('#usericon').html('<img src="' + response[0].pic_square + '" width="28" height="28" style="margin: 0; padding: 0; border: 0;"/>' );
   });
   $('#usericon').show();
   $('#userinfo').show();
   $('#logout').show();
   initialize();
   findAddress(address);





  } else if (response.status === 'not_authorized') {
    // the user is logged in to Facebook, 
    //but not connected to the app

    var uid = response.authResponse.userID;
    var accessToken = response.authResponse.accessToken;

   FB.api({ method: 'fql.query', query: 'SELECT name, email, current_location FROM user WHERE uid=' + response.authResponse.userID }, function(response) {
    if (response[0].current_location != null) {
     if( response[0].current_location.state != '' && (response[0].current_location.country == 'United States' || response[0].current_location.country == 'Canada')){
      address = response[0].current_location.state;
      if ( response[0].current_location.country != '' ){
       address = address + ', ' + response[0].current_location.country;
      }
     } else if ( response[0].current_location.country != '' ){
      address = response[0].current_location.country;
     }
    }
    $('#user').html(response[0].name);
   });
   FB.api({ method: 'fql.query', query: 'SELECT pic_square FROM profile WHERE id=' + response.authResponse.userID }, function(response) {
    $('#usericon').html('<img src="' + response[0].pic_square + '" width="28" height="28" style="margin: 0; padding: 0; border: 0;"/>' );
   });
   $('#login').hide();
   $('#usericon').show();
   $('#userinfo').show();
   $('#logout').show();
   initialize();
   findAddress(address);


 } else {

  $('#login').show();
  $('#logout').hide();
  initialize();
  findAddress(address);

 }

}

function mouseOver(id,i) {
 if( id != undefined){
  var icon = "/picture/marker_" + id + ".png";
  markers[i].setIcon(icon);
  markers[i].setZIndex(10000);
 }
}

function mouseOut(i) {
 if( i != ''){
  var school = schools[i];
  var icon = "/pics/icon_diploma.png";
  if ( school[3][2] === 1 ){
   icon = "/pics/icon_mba.png";
  } else if ( school[3][1] === 1 ){
   icon = "/pics/icon_bachelor.png";
  } else if ( school[3][4] === 1 ){
   icon = "/pics/icon_default.png";
  }
  markers[i].setIcon(icon);
  markers[i].setZIndex(1);
 }
}

function login() {
 alert('login');
 $.colorbox({href: "/facebook-register.html", width: 602, height: 350 });
}

function logout() {
 FB.logout();
 $('#usericon').hide();
 $('#userinfo').hide();
 $('#logout').hide();
 $('#login').show();
}

$(document).ready(function(){

 $(".toolTip").live('mouseenter', function() {
  this.tip = this.title;
  $(this).append(
    '<div class="toolTipWrapper">'
   +' <div class="toolTipBorder">'
   +'  <div class="toolTipText">'
   +    this.tip
   +'  </div>'
   +' </div>'
   +'</div>'
  );
  this.title = "";
  this.width = $(this).width();
  $('.toolTipWrapper').fadeIn(250);
 }).live('mouseleave', function () {
  $('.toolTipWrapper').fadeOut(100);
  $(this).children(".toolTipWrapper").remove();
  this.title = this.tip;
 }); 

 var showabout = $.cookie('showabout');
 if ( showabout === 'false' ){
  // $.cookie('showabout', null);
 } else {
  $(".welcomeWrapperBlue").show();
  $(".dosearch").addClass('red');
  $("#address").addClass('red');
  $(".searchoptions").addClass('red');
 }

 $("#closeWelcomeBlue").click( function(){
  $(".welcomeWrapperBlue").hide();
  $(".dosearch").removeClass('red');
  $("#address").removeClass('red');
  $(".searchoptions").removeClass('red');
  $.cookie('showabout', 'false', { expires: 365, path: '/' });
 });

 $('#resources').bind('mouseover', menu_open)
 $('#resources').bind('mouseout',  menu_timer)

 $("#logo").click(function(){
  $.address.path('/');

//  $("#overlay").hide();
//  $("#info").hide();
  $("#overlay").hide();
  $("#info").css('display','none')

  $("#info").css('display','none')

//  $("#infobis").hide();
  $("#infobis").css('display','none')

  $("#infobis").html('');
  document.title = 'Hotelschools - ' + $("#address").val();
 });

 $(".link").click(function(){
  var href = $(this).attr('href')
  $("#info").css("max-width", '485px' );
  $("#overlay").show();
  $("#info").load(href);
  $("#info").show();
  $.address.path(href);
  return false;
 });

 $("#result").css("max-height", $(window).height() - 190 );
 $("#info").css("width", $(window).width() - 185 );

 $("#infobis").css("width", $(window).width() - 185 );
 $("#searchform").load('/search.html');

 $('#address').focus( function() {
  if ($(this).attr('value') == 'Enter a school name or location') $(this).attr({ value : '' }).addClass('edit');
  $(this).select();
 }).blur( function() {
  if ($(this).attr('value') == '' ) $(this).attr({ value : 'Enter a school name or location' }).removeClass('edit');
 });

 $(".searchoptions").mouseover(function(){
  $(this).addClass("advanced");
 }).mouseout(function(){
  $(this).removeClass("advanced");
 });

 $(".searchoptions").click(function(){
  $("div#searchoptionsform").slideToggle("slow");
  return false;
 });

 $("#likebutton").click(function(){
  var like = $("#likes").html();
  if( like === '' ){
   $("#likes").load('/like.html');
  }
  $("#likes").slideToggle("slow");
  return false;
 });


 $(function(){
  $.address.externalChange(function(){
   if( $.address.path() === '/facebook-register.html' ){
    var href = $.address.value();
    $.fn.colorbox({'width': 602, 'height': 350, 'href':href, iframe:true, transition:'none', opacity:'0.7', overlayClose:true, scrolling:false, rel:'nofollow'});
   } else if( $.address.path() == '/update/index.html' || $.address.path() == '/update/' || $.address.path() == '/update' ){
    $("#result").css("overflow","auto");
    $("#overlay").hide();
    $("#info").hide();
    $("#info").html('');
    var href = $.address.value();
    var top  = ($(window).height() - 600) / 2;
//    $.fn.colorbox({fixed: true, top: top, 'width': 463, 'height': 650, 'href':href, iframe:true, transition:'none', opacity:'0.7', overlayClose:false, scrolling:false, rel:'nofollow'});
    $.fn.colorbox({'width': 463, 'height': 650, 'href':href, iframe:true, transition:'none', opacity:'0.7', overlayClose:false, scrolling:false, rel:'nofollow'});
//    $.address.value('');
   } else if ( $.address.path() === '/academic.html' ) {
    $("#infobis").hide();
    $("#overlay").show();
    $("#info").load($.address.path());
    $("#info").show();
    $("#result").css("overflow","hidden");
   } else if ( $.address.path() != '/' ) {
//    alert($.address.path());
//    $("#overlay").show();
//    $("#info").load($.address.path());
//    $("#info").show();
//    $("#result").css("overflow","hidden");
   } else {
    // logo[0].setVisible(false);
    $("#result").css("overflow","auto");
    $("#overlay").hide();
    $("#info").hide();
    $("#info").html('');
   }
  });
 });

});

$(window).resize(function() {
 $("#result").css("max-height", $(window).height() - 190 );
 var screenwidth  = $(window).width();
 $('#map_canvas').css("width",screenwidth);
 google.maps.event.trigger(map, 'resize')
});

function initBinding() {

 $(".adminlink").unbind('click');
 $(".searchlink").unbind('click');
 $(".school").unbind('click');
 $(".closeInfo").unbind('click');
 $(".showpage").unbind('click');

 $(".searchlink").click(function(){
  $("#overlay").css("z-index","149999").show();
  $("#advancedsearch").show();
  return false;
 });

 $(".adminlink").click(function(){
  var schoolid = $(this).closest("div").attr('id').split('|');

//  alert(schoolid[1]);
//alert( $(this).closest("div").attr("id") );
//  $(this).closest("div").css('border', 'solid 10px red');

  $("div").removeClass("highlight highlightadmin")


  $("#adminpanel").hide(200).remove();
  $(this).closest("div").addClass("highlightadmin");
  $(this).closest("div").after('<div id="adminpanel">Test</div>');
  $("#adminpanel").load('/admin/' + schoolid[1] + '.html').show(200);


  return false;
 });

 $(".school").mouseover(function(){
  var marker = $(this).attr('id').split('|');
  mouseOver(marker[1],marker[0]);
  $(this).addClass("highlight");
 }).mouseout(function(){
  var marker = $(this).attr('id').split('|');
  mouseOut(marker[0]);
  $(this).removeClass("highlight");
 });

 $(".school").click(function(){
  var marker = $(this).attr('id').split('|')
  $("#info").css("max-width", '1200px' );
  $("#overlay").show();
  $("#info").load('/school/' + marker[1] + '.html');
  $("#info").show();
  $.address.path('/school/' + marker[1] + '.html');
  return false;
 });

 $(".showpage").click(function(){
  var href = $(this).attr('href');
  alert(href);
  return false;
 });

 $(".closeInfo").mouseover(function(){
  $(this).addClass("closehi");
 }).mouseout(function(){
  $(this).removeClass("closehi");
 });

 $(".closeInfoBis").mouseover(function(){
  $(this).addClass("closehi");
 }).mouseout(function(){
  $(this).removeClass("closehi");
 });

 $(".closeInfo").click(function(){
  $.address.path('/');
  $("#overlay").hide();
  $("#info").hide();
  $("#info").html('');
  document.title = 'Hotelschools - ' + $("#address").val();
 });

 $("#info .closeInfoBis").click(function(){
  $.address.path('/');
  $("#overlay").hide();
  $("#info").hide();
  $("#info").html('');
  document.title = 'Hotelschools - ' + $("#address").val();
 });

 $("#infobis .closeInfoBis").click(function(){
  var address = $.address.parameter('ref');
  $.address.parameter('ref','');
  $.address.path(address);
  $("#infobis").hide();
  $("#infobis").html('');
 });

 $(".closebutton").click(function(){
  $.address.path('/');
  $("#overlay").hide();
  $("#info").hide();
  $("#info").html('');
  document.title = 'Hotelschools - ' + $("#address").val();
  return false;
 });

}

jQuery.cookie = function (key, value, options) {
 if (arguments.length > 1 && String(value) !== "[object Object]") {
  options = jQuery.extend({}, options);
  if (value === null || value === undefined) {
   options.expires = -1;
  }
  if (typeof options.expires === 'number') {
   var days = options.expires, t = options.expires = new Date();
   t.setDate(t.getDate() + days);
  }
  value = String(value);
  return (document.cookie = [
   encodeURIComponent(key), '=',
   options.raw ? value : encodeURIComponent(value),
   options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
   options.path ? '; path=' + options.path : '',
   options.domain ? '; domain=' + options.domain : '',
   options.secure ? '; secure' : ''
  ].join(''));
 }
 options = value || {};
 var result, decode = options.raw ? function (s) { return s; } : decodeURIComponent;
 return (result = new RegExp('(?:^|; )' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie)) ? decode(result[1]) : null;
};


// document.onclick = menu_close();




var timeout    = 500;
var closetimer = 0;
var menuitem = 0;

function menu_open(){
 menu_canceltimer();
 menu_close();
 menuitem = $(this).find('ul').css('visibility', 'visible');
 $(this).addClass('active');
}

function menu_close(){
 if(menuitem) menuitem.css('visibility', 'hidden');
 $('#resources').removeClass('active');
}

function menu_timer(){
 closetimer = window.setTimeout(menu_close, timeout);
}

function menu_canceltimer(){
 if(closetimer){
  window.clearTimeout(closetimer);
  closetimer = null;
 }
}

jQuery.cookie = function (key, value, options) {
 if (arguments.length > 1 && String(value) !== "[object Object]") {
  options = jQuery.extend({}, options);
   if (value === null || value === undefined) {
    options.expires = -1;
   }
   if (typeof options.expires === 'number') {
    var days = options.expires, t = options.expires = new Date();
    t.setDate(t.getDate() + days);
   }
   value = String(value);
   return (document.cookie = [
    encodeURIComponent(key), '=',
    options.raw ? value : encodeURIComponent(value),
    options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
    options.path ? '; path=' + options.path : '',
    options.domain ? '; domain=' + options.domain : '',
    options.secure ? '; secure' : ''
   ].join(''));
  }

  options = value || {};
  var result, decode = options.raw ? function (s) { return s; } : decodeURIComponent;
  return (result = new RegExp('(?:^|; )' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie)) ? decode(result[1]) : null;
};


