var animation_speed = 800;

$(document).ready(function(){
  //submit button hover class
  $('input.submit').mouseover(function(){
    $(this).addClass('hover');
  }).mouseout(function(){
    $(this).removeClass('hover');
  });

  $('#main_menu').superfish({
    delay: 500,
    autoArrows: false,
    animation: {height: 'toggle'},
    speed: animation_speed,
    dropShadows: false
  });

  $.address.init(function(event) {
  }).change(function(event) {
    //the replace isn't necessary for live site
    url = event.value.replace('/','');
    if((url != '') && ($('.loading').length == 0)){
      $('#main_menu li').removeClass('current');
      $('a[href*="'+url+'"]').each(function(){
        parent_obj = $(this).parent();
        if(parent_obj.get(0).tagName.toLowerCase() == 'li'){
          parent_obj.addClass('current');//I'm current!
          if(parent_obj.parent().hasClass('submenu'))
            parent_obj.parent().parent().addClass('current');//if I'm li of submenu then my parent li is current too
        }else{
          $(this).addClass('current');
        }
      });
      ajax_link(url);//load the link by ajax
      oloc= window.location.protocol+'//'+window.location.host+''+window.location.pathname;
      loc = window.location.protocol+'//'+window.location.host+''+window.location.hash.substr(1);
      //$('.addthis_button').attr('addthis:url', loc);
      $('.addthis_button').each(function () {
        $(this).attr('href', $('script.addthis').attr('href').replace(escape(oloc), escape(loc)));
      });
      addthis.init();
    }
  });

  $('#main_menu a').click(function(){
    if($(this).attr('rel') == '')
      return;
    return false;
  });

  //get total window height - the viewable area of browser
  var myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myHeight = document.body.clientHeight;
  }
  //set content min-height to maximum possible value, the site then doesn't jump when content is loaded with ajax
  max_content_height = myHeight;
  if(document.getElementById('main_menuCover'))
    max_content_height -= document.getElementById('main_menuCover').clientHeight;
  if(document.getElementById('middleCoverBottomShadow'))
    max_content_height -= document.getElementById('middleCoverBottomShadow').clientHeight;
  if(document.getElementById('footer'))
    max_content_height -= document.getElementById('footer').clientHeight;
  $('#middleCover').css('minHeight', max_content_height+'px');

  function ajax_link(href){
    container_to_fade = $('#middle');
    content_container = $('#middle_contentCover');
    //old_height = content_container.height();
    CleanUpBoxes(href);
    container_to_fade
      .fadeOut(animation_speed/2, function(){//hide content
        content_container.html('');//remove all content
        //fade in loading box on container parent
        container_to_fade.parent().append('<div class="loading">&nbsp;</div>').find('.loading').fadeIn(animation_speed/2, function(){
          //loading is shown now
          content_container.append(ajax_load_url(href));//ajaxed lod of new content
          container_to_fade.fadeIn(animation_speed/2, function(){//fade content in
              container_to_fade
                //find the loading box and fade it out
                .parent().find('div.loading').fadeOut(animation_speed/2, function (){
                  $(this).remove();//remove loading box(it's already not visible)
                  ShowBoxes(href);
                });
            });
        });
      });
  }

  function ajax_load_url(href){
    var result = ' ';
    var title_start_str = 'page_title_start';
    var title_end_str = 'page_title_end';
    params = 'ajax=1';
    hash_mark_pos = href.indexOf('#');
    if(hash_mark_pos >= 0)
      href = href.substr(0, hash_mark_pos);
    $.ajax({
      type: "GET",
      url: href,
      data: params,
      async: false,
      dataType: "text",
      success: function(response){
        result = response;
        title_start_pos = result.indexOf(title_start_str);
        title_end_pos = result.indexOf(title_end_str);
        if((title_start_pos >= 0) && (title_end_pos > 0)){
          title = result.substr(title_start_pos+title_start_str.length, title_end_pos-title_start_pos-title_start_str.length);
          $.address.title(title);
        }
      },
      error: function(xhr, desc, exception){ result = '<script type="text/javascript">location.href="'+href+'"</script>'; }
    });
    return result;
  }


  try {
    $('.galleries_list .galleries_list_item').corner('15px cc:#845466');
    //$('#middle input, #middle select, #middle textarea').corner('5px cc:#845466');
  } catch(e){}

  InitializeVirtualRoomScroller();
});

function CleanUpBoxes(current_url){
  //clean up
  if(current_url.indexOf('gallery-') < 0){
    $('body').removeClass('gallery_detail_page');
    $('.thumbnails_listCover').slideUp(animation_speed/2, function(){
      $(this).remove();
    });
    $('.galleries_list').slideUp(animation_speed/2)
  }
  if(current_url.indexOf('Tower_Gallery.html') < 0){
    $('body').removeClass('galleries_page');
    $('#virtual_tourCover').slideUp(animation_speed/2);
  }
  if(current_url.indexOf('Availability.html') < 0){
    $('#booking_box').slideUp(animation_speed/2);
  }
}

function ShowBoxes(current_url){
  if(current_url.indexOf('gallery-') >= 0){
    $("body").addClass("gallery_detail_page");
  }
  if(current_url.indexOf('Tower_Gallery.html') >= 0){
    $('body').addClass('galleries_page');
    $('#virtual_tourCover').slideDown(animation_speed/2, function(){
      InitializeVirtualRoomScroller();
    });
    $('.galleries_list').slideDown(animation_speed/2, function(){
      $('.galleries_list_item.not_current').slideDown(animation_speed/2);
    });
    $('.galleries_list_item').removeClass('hidden').addClass('not_current');
  }
  if(current_url.indexOf('Availability.html') >= 0){
    $('#middle').slideUp(animation_speed/2);
    $('#booking_box').slideDown(animation_speed/2);
    sliding_panels_init();
  }
  oloc= window.location.protocol.toString()+'//'+window.location.host.toString()+''+window.location.pathname.toString();
  loc = window.location.protocol.toString()+'//'+window.location.host.toString()+''+window.location.hash.substr(1);
  //$('.addthis_button').attr('addthis:url', loc);
  $('.addthis_toolbox a[href]').each(function () {
    $(this).attr('href', $(this).attr('href').replace(escape(oloc).replace(/\//g, '%2F'), escape(loc).replace(/\//g, '%2F')));
  });
  $('.addthis_toolbox a:not([href])').each(function () {
    $(this).attr('addthis:url', loc);
  });
  addthis.update(addthis.links, 'url', loc);
}

function InitializeVirtualRoomScroller(){
  var $virtual_room_scroller = $('#virtual_tour');
  var virtual_room_scroller_width = $virtual_room_scroller.find('img').width();
  var image_to_scroll = $virtual_room_scroller.html();

  $('#virtual_tour_thumbnails a').unbind('click');
  $('a#virtual_tour_prev, a#virtual_tour_next').unbind('mouseover').unbind('mouseout');

  if($virtual_room_scroller.is(':visible')){
    $virtual_room_scroller.html(image_to_scroll+image_to_scroll+image_to_scroll);
    $virtual_room_scroller[0].scrollLeft = virtual_room_scroller_width;
  }

  $('#virtual_tour_thumbnails a').click(function(){
    if($(this).hasClass('active'))
      return false;
    var new_image_src = $(this).find('img').attr('src').replace('_small','');
    $virtual_room_scroller.find('img').fadeOut("slow", function(){
      $(this).attr('src', new_image_src).fadeIn("slow", function(){
        virtual_room_scroller_width = $virtual_room_scroller.find('img').width();
        $virtual_room_scroller.scrollTo({left:virtual_room_scroller_width}, {speed: 750, axis: 'x', easing: 'linear'});
      });
    });
    $(this).parent().find('a').removeClass('active');
    $(this).addClass('active');
    return false;
  });

  $('a#virtual_tour_next').mouseover(function(){
    $virtual_room_scroller.scrollTo('+='+(virtual_room_scroller_width*2)+'px', {speed: 30000, axis: 'x', easing: 'linear'});
  }).click(function(){
    return false;
  });
  $('a#virtual_tour_prev').mouseover(function(){
    $virtual_room_scroller.scrollTo('-='+(virtual_room_scroller_width*2)+'px', {speed: 30000, axis: 'x', easing: 'linear'});
  }).click(function(){
    return false;
  });
  $('a#virtual_tour_prev, a#virtual_tour_next').mouseout(function(){
    $virtual_room_scroller.stop();
    $virtual_room_scroller.stop();
    $virtual_room_scroller.stop();
  });

  $virtual_room_scroller.scroll(function(){
    if($virtual_room_scroller[0].scrollLeft == 0){
      $virtual_room_scroller[0].scrollLeft = virtual_room_scroller_width;
      $virtual_room_scroller.stop();
      $virtual_room_scroller.scrollTo('-='+virtual_room_scroller_width+'px', {speed: 15000, axis: 'x', easing: 'linear'});
    }
    if($virtual_room_scroller[0].scrollLeft == $virtual_room_scroller[0].scrollWidth - virtual_room_scroller_width){
      $virtual_room_scroller[0].scrollLeft = virtual_room_scroller_width;
      $virtual_room_scroller.stop();
      $virtual_room_scroller.scrollTo('+='+virtual_room_scroller_width+'px', {speed: 15000, axis: 'x', easing: 'linear'});
    }
  });
}

$(document).ready(function(){
  sliding_panels_init();
});
var heading_bounced = false;
function sliding_panels_init(){
  $("#sliding_panels .sliding_panelHeader").mouseover(function(){
    $(this).addClass('active').next(".sliding_panelContent").slideDown("slow").siblings(".sliding_panelContent").slideUp("slow", function(){
      setTimeout("bounce_heading()", 100);
    });
    $(this).siblings().removeClass('active');
  }).click(function(){
    url = $(this).attr('href');
    if(url == '#')
      return false;
  });
  $("#sliding_panels .sliding_panelContent").click(function(){
    url = $(this).attr('href');
    if(url == '#')
      return false;
  });
  if($("#sliding_panels .sliding_panelHeader:first").length > 0)
    $("#sliding_panels .sliding_panelHeader:first").trigger("mouseover");
  else
    setTimeout("bounce_heading()", 100);
}

function bounce_heading(){
  if(!heading_bounced){
    $("#banner_image h1")
      //.effect("bounce", {direction: "up", distance: 120, times: 3 }, 600);
      .animate({'top': '80px', 'easing': 'swing'}, 300, function(){
        $(this).animate({'top': '30px', 'easing': 'swing'}, 150, function(){
          $(this).animate({'top': '70px', 'easing': 'swing'}, 150, function(){
            $(this).animate({'top': '55px', 'easing': 'swing'}, 250, function(){
              $(this).animate({'top': '70px', 'easing': 'swing'}, 150);
            });
          });
        });
      });
    heading_bounced = true;
  }
}

