// Scrolls the gallery thumbnail container to a given position. Called from RJS.

jQuery(document).ready(function($) {

  // Stickyfloat
  $('.subscribe a.newsletter').click(function() {
    if (parseInt($('#newsletter_tab').css('left')) == 0) {
      $('#newsletter_tab').stop(true, true).animate({left: -1*(parseInt($('#newsletter_tab').css('width')))});
    } else {
      hide_subscribe_tab()
      $('#newsletter_tab').stop(true, true).animate({left: '0px'});
    }
    return false;
  });

  $('.subscribe a.contact').click(function() {
    if (parseInt($('#contact_tab').css('left')) == 0) {
      $('#contact_tab').stop(true, true).animate({left: -1*(parseInt($('#contact_tab').css('width')))});
    } else {
      hide_subscribe_tab()
      $('#contact_tab').stop(true, true).animate({left: '0px'});
    }
    return false;
  });
  //$('#subscribe_tab, .subscribe_tab_content').stickyfloat({
  //  offsetY: 250
  //});

  // Hide subscribe tab if the page narrower then the wrapper
  $(window).resize(function(){
    if ($(window).width() < 1020) {
      $('#subscribe_tab').stop().animate({left: -35}, 100);
    } else {
      $('#subscribe_tab').stop().animate({left: 0}, 100);
    }
  }).resize();

  // Ombrello network
  $('.network').mouseover(function() {
    $('#footer_popup').fadeIn('fast');
  });

  $('#network_menu').mouseleave(function() {
    $('#footer_popup').fadeOut('fast');
  });

  $("#right_box_tabs").tabs("#right_box_panes > div", {
    rotate: true
  }).slideshow({
    autoplay: true,
    interval: 4000
  });

  $("#right_box_tabs a").click(function() {
    $("#right_box_tabs").data("slideshow").stop();
  });


  // Google search Stylish select
  $("#cse-search-box select").sSelect();

  // INIT TOPMENU
  $('#nav li.active > a, #nav li.active > ul').addClass('active');
  $('#nav li.active').removeClass('active');

  $('#nav > ul > .parent_node > a').click(function() {
    $('#nav > ul > .parent_node > ul').hide();
    $('#nav a.active').removeClass('active');
    $(this).parent().children('ul > li > a').addClass('active');
    $(this).parent().children('ul').show();
    return false;
  });

  $('#nav .submenu .parent_node:has(.groups)').hover(function() {
    $(this).parent().children('ul.active').hide();
    $(this).children('ul').show();
  }, function() {
    $(this).children('ul').hide();
  });

  $('#nav > ul > li').mouseleave(function() {
//    $('ul.active').show().siblings('a').addClass('active');
  });


  $('a[rel*=facebox]').facebox();
  /*
  $('#facebox_termekajanlo').click(function() {
      jQuery.ajax({
        type: "GET",
        url: '/galleries/2/popup',
        dataType: "script"
      });
  });*/

  if(!!window.interstitialTimeout){
    show_interstitial();
    setTimeout(function() { hide_interstitial(); }, interstitialTimeout);
  }

  function set_jcarousel(carousel) {
      jQuery('.jcarousel-control a').bind('click', function() {
          carousel.scroll(jQuery.jcarousel.intval(jQuery(this).text()));
          return false;
      });

      // Pause autoscrolling if the user moves with the cursor over the clip.
      carousel.clip.hover(function() {
          carousel.stopAuto();
      }, function() {
          carousel.startAuto();
      });
      jQuery('.jcarousel-control a').hover(function() {
          carousel.stopAuto();
      }, function() {
          carousel.startAuto();
      });

      // animate the related links
      jQuery('.jcarousel-item table tr td ul li a').bind('mouseover', function() {
        $(this).animate({marginLeft: "10px"}, 200 );
        return false;
      });
      jQuery('.jcarousel-item table tr td ul li a').bind('mouseout', function() {
        $(this).animate({marginLeft: "0"}, 200 );
        return false;
      });

  };

  function set_active_control(carousel) {
    var a = $('.jcarousel-list').css("left");
    a = parseInt(a);
    a = a / 666;
    a = Math.abs(a)
    jQuery('.jcarousel-control a').removeClass('active');
    jQuery('.jcarousel-control a.item_number_' + a).addClass('active');
  };

  if ( $("#ajanlo_doboz_nagy") ){
    $("#ajanlo_doboz_nagy").jcarousel({
        animation: 700,
        auto: 5,
        wrap: "both",
        scroll: 1,
        initCallback: set_jcarousel,
        itemLoadCallback: {onAfterAnimation: set_active_control},
        buttonNextHTML: null,
        buttonPrevHTML: null
    });
  };

  $('.stickyfloat').css('height', parseInt($(document).height()-$('#footer2011').height()-200) + 'px');

})

/* ===============================================================
 * Functions
 *
 */

function setWidth(id,value){
  $(id).width = value;
}

function scrollTo(id,position) {
  if (position == 1){position = 0;};
  $(id).scrollable().setPage(position);
}

function setImageNumber(position, gallery_id) {
  if ( $("#galery-img-number-" + gallery_id) ){
    $("#galery-img-number-" + gallery_id).innerHTML = position + ". kép";
  };
}

// facebook close window and redirect
function fb_close(){
  if ( window.opener ){
    window.opener.location.href = '/';
    window.opener.window.location.href = '/';
    window.close();
  }
  else{
    document.location.href = '/';
  }
}

// facebook close window and redirect
function fb_close_and_redirect(page){
  if ( window.opener ){
    window.opener.location.href = page;
    window.opener.window.location.href = page;
    window.close();
  }
  else{
    document.location.href = page;
  }
}

// Gogu guess submit
function gogu_guess_submit() {
    if ($('#gogu_guess').val() == '') {
        alert('Valamit kéne tippelni!');
    } else {
        $('#gogu_form').submit();
    }

}

function gogu_giveup() {
    window.location.href='/gogus/'+$('#gogu_id').val()+"/giveup";
}


function PopChat() {
  ChatWindow = window.open(
    'http://aegondirekt.hu/chat/index.php?chat_from_ombrello=1',
    'AEGONdirektChat',
    'status=1,toolbar=0,titlebar=0,location=0,menubar=0,directories=0,resizable=1,scrollbars=1,width=700,height=500,top=100,left=100'
  );
  ChatWindow.focus();
}


function lakas_form() {
    $('.lakas_form').css('display','block');
    $('.kisallat_form').css('display','none');
}

function kisallat_form() {
    $('.lakas_form').css('display','none');
    $('.kisallat_form').css('display','block');
}



function show_interstitial() {
  //Get the screen height and width
  var maskHeight = $(document).height();
  var maskWidth = $(window).width();

  //Set height and width to mask to fill up the whole screen
  $('#interstitial-mask').css({'width':maskWidth,'height':maskHeight});

  //transition effect
  $('#interstitial-mask').fadeIn(1000);
  $('#interstitial-mask').fadeTo("slow",0.7);

  center_interstitial();

  //transition effect
  $('#interstitial-box').fadeIn(1000);

}
function hide_interstitial() {
  // $('#interstitial-box').fadeOut(500);
  // $('#interstitial-mask').fadeOut(500);
  $('#interstitial-box').hide();
  $('#interstitial-mask').hide();
}
function center_interstitial() {
  //Get the window height and width
  var winH = $(window).height();
  var winW = $(window).width();

  var Btop = winH/2-$('#interstitial-box').height()/2;
  var Bright = winW/2-$('#interstitial-box').width()/2;

  if ( Btop < 0 ) { Btop = 0 }
  if ( Bright < 15 ) { Bright = 15 }

  //Set the popup window to center
  $('#interstitial-box').css('top', Btop );
  $('#interstitial-box').css('right', Bright);
}

function hide_subscribe_tab() {
  $('.subscribe_tab_content').each(function() {
    $(this).stop(true, true).animate({left: -1*(parseInt($(this).css('width')))});
  });
}

