﻿
$(document).ready(function(){
  //var originalMarginTop = $('.topdd').css('margin-top');
  var originalMarginTop = '-79px';
  var topSlideDownSpeed = 500;
  var topSlideUpSpeed = 300;
  
  $('.topdd .login-button').click(function(){
    if ($('.topdd').css('margin-top') == originalMarginTop) {
      $('.topdd').animate({
        marginTop:0
      }, topSlideDownSpeed);
      $('.topdd .username').focus();
    } else {
      $('.topdd').animate({
        marginTop:originalMarginTop
      }, topSlideUpSpeed);
    }
  });
  
  $('*').click(function(event){
    var e = $(this);
    var t = $('.topdd:not(:animated)');
    var mt = t.css('margin-top');
    
    if (mt != originalMarginTop && t.length == 1 && e.parents('.topdd').length == 0 && !e.hasClass('topdd') && this == event.target) {
      $('.topdd').removeClass('topddnohide').css('margin-top', 0);
      $('.topdd').animate({
        marginTop:originalMarginTop
      }, topSlideUpSpeed);
    }
  });
  
  //-- Top Navigation Menu --//
  $('.topnav h3, .topnav li').removeAttr('title');
  
  $('.topnav h3 a').hover(function(){
    if (!$(this).parents('li').hasClass('current')){
      $(this).stop().animate({
        height:25,
        paddingTop:25
      }, 800, 'easeOutBounce');
    }
    $(this).addClass('on').attr('hover','true');
    $(this).parents('li').addClass('on');
    $('.topnav h3 a[hover!="true"][class="on"]').each(function(){
      $(this).removeClass('on');
      $(this).parents('li').find('.subnav').hide(0);
      $(this).parents('li').find('*[hover="true"]').removeAttr('hover');
    });
    $(this).parents('li').find('.subnav').hide(0);
    $(this).parents('li').find('.subnav:first').show(0);
  },function(){
    $(this).removeAttr('hover');
    hoverClose($(this).parents('li'));
  }).click(function(){
    $(this).parents('li').addClass('current');
  });
  
  $('.subnav').hover(function(){
    $(this).attr('hover','true');
  },function(){
    $(this).removeAttr('hover');
    hoverClose($(this).parent());
  });
  
  $('.subnav h4').find('a').hover(function(){
    $(this).attr('hover','true');
    $(this).parents('.subnav:first').find('.subnav').hide(0);
    $(this).parents('h4').siblings('.subnav:first').show(0);
  },function(){
    $(this).removeAttr('hover');
    hoverClose($(this).parents('li'));
  });
  
  function hoverClose($p){
    setTimeout(function(){
      if ($p.find('*[hover="true"]').length==0){
        $p.find('.subnav').hide(0);
        $li = $p.hasClass('subnav') ? $p.parents('li') : $p;
        if ($li.find('.subnav[hover="true"]').length==0){
          $li.find('h3 a').removeClass('on');
          $li.removeClass('on');
          if (!$li.hasClass('current')){
            $li.find('h3 a').stop().animate({
              height:50,
              paddingTop:0
            }, 100, 'linear');
          }
        }
      }
    },100);
  }
  
  //-- info ball modal event --//
  $('.modalball').each(function(){
    var span = '<span class="modalball click-extension"';
    
    if ($(this).attr('modal')!=null && $(this).attr('modal')!='')
      span += ' modal="' + $(this).attr('modal') + '"';
      
    if ($(this).attr('youtube')!=null && $(this).attr('youtube')!='')
      span += ' youtube="' + $(this).attr('youtube') + '"';
      
    span += '></span>';
    
    $(this).after(span);
  });
  
  $('.modalball, .modallaunch').click(function(){
    var target = '#' + $(this).attr('modal');
    
    if ($(target).length == 1){
      var hasyt = $(this).attr('youtube')!=null && $(this).attr('youtube')!='';
      
      if (hasyt){
        var ytid = target.substr(1) + 'Object';
      
        $(target).find('.npContent').html('<object width="470" height="335">\n' + 
        '<param name="movie" value="http://www.youtube.com/v/' + $(this).attr('youtube') + '&hl=en&rel=0&autoplay=1&loop=0&showsearch=0&showinfo=0&iv_load_policy=3&ap=%2526fmt%3D18" />' + 
        '<param name="allowFullScreen" value="false" />' + 
        '<param name="allowscriptaccess" value="always" />' + 
        '<param name="wmode" value="transparent" />' + 
        '<embed width="470" height="335" ' + 
            'src="http://www.youtube.com/v/' + $(this).attr('youtube') + '&hl=en&rel=0&autoplay=1&loop=0&showsearch=0&showinfo=0&iv_load_policy=3&ap=%2526fmt%3D18" ' + 
            'type="application/x-shockwave-flash" ' + 
            'allowscriptaccess="always" ' + 
            'allowfullscreen="false" ' + 
            'wmode="transparent" />' + 
        '</object>');
        
        //$(target).find('.npContent').html('<div id="' + ytid + '"></div>');
        //var params = { allowScriptAccess: "always" };
        //var atts = { id: 'myytvideo' };
        //swfobject.embedSWF("http://www.youtube.com/v/" + $(this).attr('youtube') + "&hl=en&rel=0&autoplay=1&loop=0&showsearch=0&showinfo=0&iv_load_policy=3&enablejsapi=1",
        //                 ytid, "470", "335", "8", null, null, params, atts);
                         
        $(this).npModal({
          containerTarget: target,
          enableOverlayClose: true,
          height:'420px',
          closeCallback:function(){
            if (hasyt){
              $(target).find('.npContent').html('');
            }
          }
        });
      } else {
        $(this).npModal({
          containerTarget: target,
          enableOverlayClose: true
        });
      }
    }
  });
  
  //-- video modal --//
  $('.sidebar .vtestimonial .play').css('opacity', .7).hover(function(){
    $(this).stop(true, true).animate({opacity:1},200);
  },function(){
    $(this).stop(true, true).animate({opacity:.7},800);
  });
  
  //-- update singup sidebar --//
  $('.sidebar .updates .email').Watermark('email address');
  
  //-- expandable sidebar --//
  $('.sidebar .expandable .slidetoggle').addClass('down').text('click to read more');
  $('.sidebar .expandable .slidetoggle').click(function(){
    $(this).siblings('ul').find('li:not(.alwayson)').slideToggle();
    
    if ($(this).hasClass('down'))
      $(this).removeClass('down').addClass('up').text('click to collapse this text');
    else
      $(this).removeClass('up').addClass('down').text('click to read more');
  });
  
  //-- slide containers --//
  $('.slide-container .more, .slide-container .title').click(function(){
    var hide = $(this).parents('.slide-container').find('.hide');
    var more = $(this).parents('.slide-container').find('.more');
    
    hide.slideToggle();
    
    if (more.hasClass('down'))
      more.removeClass('down').addClass('up').text('click to collapse this text');
    else
      more.removeClass('up').addClass('down').text('click to read more');
  });
  
  $('.slide-container .more').text('click to read more');
});

//-- sIFR --//
if(typeof sIFR == "function"){
  sIFR.bHideBrowserText = true;
  sIFR.setup();
};
