var acc_animating = false;
var $langover = false;
var $menushown = false;
$(document).ready(function(e){
  jQuery(window).resize(function() { DoDesingReset() });
  DoDesingReset();

  $('#transpBox').animate({'margin-top': '50px'}, 900);

  $("a.videobox").fancybox({
    'padding': 2,
    'autoScale': false,
    'autoDimensions': true,
    'overlayShow': true,
    'overlayOpacity': '0.8',
    'overlayColor': '#000',
    'width': 994,
    'height': 510,
    'scrolling': 'no',
    'onStart': function(){
        $('#contentVideoInnerPlayer').css('visibility', 'hidden');
        stopFLVPlayer();
    },
    'onClosed': function(){
        $('#contentVideoInnerPlayer').css('visibility', 'visible');
        startFLVPlayer();
    }
  });

  $("a.startpage").fancybox({
    'padding': 2,
    'autoScale': false,
    'autoDimensions': true,
    'overlayShow': true,
    'overlayOpacity': '0.8',
    'overlayColor': '#000',
    'width': 390,
    'height': 565,
    'scrolling': 'no',
    'onStart': function(){
      if (!$startpage) {
        $('#contentVideoInnerPlayer').css('visibility', 'hidden');
        stopFLVPlayer();
      }
    },
    'onClosed': function(){
      if (!$startpage) {
        $('#contentVideoInnerPlayer').css('visibility', 'visible');
        startFLVPlayer();
      }
    }
  });

  $("#acc").accordion({
      navigation: "false",
      autoHeight: "true",
      change: function(event, ui){
         ui.oldHeader.css('background-image','url("/images/btn_down.jpg")');
         ui.newHeader.css('background-image','url("/images/btn_up.jpg")');
         acc_animating = false;
      },
      changestart: function(){
          acc_animating = true;
      }
  });

  $('.addContentTop').mousemove(function(e) {
    //Sidebar Offset, Top value
    var s_top = parseInt($('.addContentTop').offset().top);
    //content height
    var mheight = parseInt($('#contentTopInner').height());
    //Calculate the top value
    var top_value = Math.round(( (s_top - (e.pageY - 20)) / 100) * mheight / 2)
    //stop point
    var stop_point = parseInt($('.btnDown').offset().top);
    //Animate the #inner div by chaging the top value
    if (((top_value > ((s_top + mheight - stop_point)*-1)) && !acc_animating) && ((top_value < 0)  && !acc_animating)) {
      if (top_value > -12) {
          top_value = 5;
      }
      if (top_value < (((s_top + mheight - stop_point)*-1) + 20)) {
          top_value = ((s_top + mheight - stop_point)*-1) + 1;
      }
      $('#contentTopInner').animate({top: top_value}, { queue:false, duration:300});
    }
  });

  $('.addContentBottom').mousemove(function(e) {
    var s_top = parseInt($('.addContentBottom').offset().top);
    var mheight = parseInt($('#contentBottomInner').height());
    var top_value = Math.round(( (s_top - (e.pageY - 20)) / 100) * mheight / 2)
    var stop_point_bottom = parseInt($('.addContentBottom').height() + s_top - 8);
    if (((top_value > ((s_top + mheight - stop_point_bottom)*-1)) && !acc_animating) && ((top_value < 0)  && !acc_animating)) {
      if (top_value > -15) {
          top_value = 0;
      }
      if (top_value < (((s_top + mheight - stop_point_bottom)*-1) + 23)) {
          top_value = ((s_top + mheight - stop_point_bottom)*-1) + 8;
      }
      $('#contentBottomInner').animate({top: top_value}, { queue:false, duration:300});
    }
  });

  $("#language_menu").mouseover(function(){
          $("#language_box").show();
          $("#language_menu").css("color","#ffffff");
          $("#language_menu").css("cursor","pointer");
  });
  $("#language_menu").mouseout(function(){
         if ($langover == false) {
              $("#language_box").hide();
              $("#language_menu").css("color","#a9a9a9");
         }
  });
  $("#language_box").mouseover(function(){
            $langover = true;
            $("#language_box").show();
            $("#language_menu").css("color","#ffffff");
    });
    $("#language_box").mouseout(function(){
            $langover = false;
            $("#language_box").hide();
            $("#language_menu").css("color","#a9a9a9");
    });

    $("#menu_1").hover(
      function () {
        setTimeout(function(){
            hideAll();
            $('#menubox').show();
            $('#menubox_1').show();
        }, 1);
      },
      function () {
        setTimeout(function(){if(!$menushown){hideAll();}}, 1);
      }
    );
    $("#menu_2").hover(
      function () {
        setTimeout(function(){
            hideAll();
            $('#menubox').show();
            $('#menubox_2').show();
        }, 1);
      },
      function () {
        setTimeout(function(){if(!$menushown){hideAll();}}, 1);
      }
    );
    $("#menu_3").hover(
      function () {
        setTimeout(function(){
            hideAll();
            $('#menubox').show();
            $('#menubox_3').show();
        }, 1);
      },
      function () {
        setTimeout(function(){if(!$menushown){hideAll();}}, 1);
      }
    );

    $('#menubox').mouseenter(function(){
      $menushown = true;
    });
    $('#menubox').mouseleave(function(){
      $menushown = false;
      hideAll();
    });

});

function hideAll(){
  $('#menubox').hide();
  $('#menubox_1').hide();
  $('#menubox_2').hide();
  $('#menubox_3').hide();
}

function DoDesingReset() {
  var maxWidth = jQuery(window).width();
  if(maxWidth > jQuery("#wrap").width()) {
    maxWidth = (maxWidth-jQuery("#wrap").width());
  }
  else{
    maxWidth = 0;
  }
  jQuery("#language_box").css('left',((maxWidth/2)+jQuery("#wrap").width()-(jQuery("#language_box").width())));
  jQuery("#language_box").css('opacity',0.9);
}

function stopFLVPlayer(){
  swfobject.removeSWF('contentVideoInnerPlayer');
  //var swf = document.getElementById('contentVideoInner');
  //swf.sendEvent('STOP');
  //console.log(swf.IsPlaying());
}

function startFLVPlayer(){
  $('#contentVideoInner').html('<div id="contentVideoInnerPlayer"></div>');
  var attributes = { id: "videoPlayer", name: "videoPlayer" };
  if (short_tag) {
    swfobject.embedSWF("/player_"+short_tag+"/videoPlayer_"+short_tag+".swf", "contentVideoInnerPlayer", "674", "410", "9.0.0", false, flashvars, attributes);
  }
}
