$(document).ready(function()
{

    $(".alternate tr:even").addClass("alt");

    $('a.dynamic-bt').wrapInner('<span class="button"></span>');
    $('.darkblue-heading').wrapInner('<span />');

    // Put the link text inside a span
    $('.grey-content2').append('<div class="grey-content2-footer"></div>');

    $('.grey-content').append( '<div class="grey-content-footer"></div>');

    // Add to tables with one row of white
    $('.no-alt').after('<div class="grey-content3-footer"><span></span></div>');

    // transparent png fix for IE 6
    if (document.all && /MSIE (5\.5|6)/.test(navigator.userAgent) &&
      document.styleSheets && document.styleSheets[0] && document.styleSheets[0].addRule)
     {         
       // document.styleSheets[0].addRule('.transparent', 'behavior: url(' + oc.constants.base_url + 'css/iepngfix.htc)');
     }     

     if ($('.fancyRegister').length !== 0)
     {
        $('.fancyRegister').fancybox({'type':'iframe','width':482,'height':284,'padding':10});
     }

    //clear default value on focus
    var clearFocusFN = function () {
            if ($(this).val() == $(this).attr('rel')) {
                    $(this).val("");
            }
    };
    var fillBlurFN = function () {
            if ($(this).val() == "") {
                    $(this).val($(this).attr('rel'));
            }
    };

    //with class nondefault
    $('.nondefault').focus(clearFocusFN);
    $('.nondefault').blur(fillBlurFN);

    simple_tooltip("a.tooltips","tooltip",false);
    simple_tooltip("a.tooltipssupport","tooltip",false);

      // login dropdown
    if($('.dropdown').length)
    {
        // TT: fixing the login box behaviour
        // via http://rob-bell.net/2009/02/handling-clicks-outside-a-specified-area-in-jquery/
        $(document).click(function(){
            $('.dropdown').hide();
            $('.dropdown').slideUp('fast').removeClass('open');
        });

        $('.login-show').click(function(e){
            e.stopPropagation();
        });

        $('.dropdown').click(function(e){
            e.stopPropagation();
        });

      $('.login-show').click(
        function() {
          $('.dropdown').show();
          $('.dropdown').slideDown('fast').addClass('open');
        });
       $('.login-link').click(
        function () {
            //hide its submenu
            $('.dropdown').hide();
            $('.dropdown').slideUp('fast').removeClass('open');

      });
      $('#text-pass').focus(function(){
          $('#text-pass').hide();
          $('#top-pass').show();
          $('#top-pass').focus();
      });
      $('#top-pass').blur(function(){
          if($(this).val()=='Password' || ($(this).val()=='')){
            $('#top-pass').hide();
            $('#text-pass').show();
          }
      });
        
    }

    $('#msg-tooltip-link').hover(function() {
                $('#msg-tooltip').show();
            },
        function() {
            $('#msg-tooltip').hide();
        });
  
    /*
       // button roll over effect
       $('.dynamic-bt,a.button,.bt-go,.bt-search,.video-small,.video-med').hover(function(){
            $(this).css('opacity','0.8')},
        function(){
            $(this).css('opacity','1')});
    */

    /*
	// button roll over effect
    $('.dynamic-bt,a.button,.bt-go,.bt-search,.video-small,.video-med').hover(function(){$(this).css('opacity','0.8')}, function(){$(this).css('opacity','1')});
    */


    if($( '#followScroll' ).length)
    {
        $( '#followScroll' ).scrollFollow(
        {
            speed: 500,
            offset: 28,
            delay: 50
        });
        
        // On each scroll down/up adjust the style position of the box
        // if the box needs to move down or up (css issue)
        var el = $('#followScroll');
        var elpos_original = el.offset().top;

        $(window).scroll(function(){
            var windowpos = $(window).scrollTop();
            //window.alert(windowpos + ' - original pos: ' + elpos_original);
            if(windowpos > elpos_original) {
                $('#followScroll').css({'position' : 'absolute'});
            } else if (windowpos <= elpos_original) {
                $('#followScroll').css({'position' : 'relative'});
            }
        });
    }

    // USERVOICE FEEDBACK WIDGET TAB
    var uservoiceOptions = {
      key: 'officecavalry',
      host: 'officecavalry.uservoice.com',
      forum: '94703',
      showTab: true,
      /* optional */
      alignment: 'left',
      background_color:'#E70286',
      text_color: 'white',
      hover_color: '#A2BCCB',
      lang: 'en'
    };
    if (typeof UserVoice != "undefined")
    {
        UserVoice.Tab.show(uservoiceOptions);
    }
    
});

function simple_tooltip(target_items, name,withtip){
$(target_items).each(function(i){

		$("body").append("<div class='"+name+"' id='"+name+i+"'><div class='tip-mouth sprite'></div><div class='"+name+"-title'>"+$(this).attr('title')+"</div><div class='"+name+"-text'>"+$(this).html()+"</div></div>");
		var my_tooltip = $("#"+name+i);

		if($(this).attr("title") != "undefined" ){

            $(this).removeAttr("title").mouseover(function(){
                   obj_pos = getPos($(this),my_tooltip);
                   my_tooltip.css({display:"block", top:obj_pos.top_pos,left:obj_pos.left_pos}).fadeIn(400);
            }).mouseout(function(){
                    my_tooltip.css({display:"none"});
            });
            if(withtip){
                $('.withtip').eq(i).focus(function(){
                    obj_pos = getPos($('.tooltips').eq(i),my_tooltip);
					if($('.tooltips:eq('+i+')').is(':visible')){
						my_tooltip.css({display:"block", top:top_pos,left:left_pos}).fadeIn(400);
					}
                }).keyup(function(){
                    my_tooltip.fadeOut(1000);
                }).blur(function(){
                    my_tooltip.fadeOut(1000);
                });
            }
		}

	});
}

function getPos(tip_obj,tooltip){

            top_pos =  tip_obj.offset().top;
            left_pos = tip_obj.offset().left;

            top_pos  -= tooltip.outerHeight() + 17;
            left_pos += tip_obj.outerWidth()+5;

            // adjust Y
            height = tooltip.outerHeight() + tip_obj.outerHeight();
            top_pos += height;

            // adjust X
            width = tooltip.outerWidth() + tip_obj.outerWidth();
            //left_pos += width;
            obj_pos = {'top_pos':top_pos,'left_pos':left_pos};
            return obj_pos;
}



