$(document).ready(function() {

	/* */
	$('.change').focus(function() {
		if(this.value == this.defaultValue) this.value = '';
	});
	$('.change').blur(function() {
		if(this.value == '') this.value = this.defaultValue;
	});


    $(".login").click(function() {
		$('.login-popup').toggle();
    });

    $(".watchlist").click(function() {
		$('.watchlist-popup').toggle();
    });

    $(".close_watchlist").click(function(){
		$('.watchlist-popup').hide();
	})

	$(".close_follow").click(function(){
		$('.follow-popup').hide();
	})

    $(".email").click(function() {
		$('.email-popup').toggle();
		$('.email_close').css('display', 'block');
    });

    $(".email_close").click(function() {
		$('.email-popup').hide();
		$('.email_close').css('display', 'none');
    });


    $(".follow").click(function() {
		$(this).next().toggle();
    });


    // tabs
    $("ul.nav").tabs();
    $("ul.nav li:last-child").addClass('last-tab');
    $("ul.nav li:first-child").addClass('first-tab');
    $("ul.nav li.right").removeClass('last-tab').prev().addClass('last-tab');
    //$('ul.ui-tabs-nav li a').click(function(){$(this).parent().removeClass('prev').siblings().removeClass('prev').end().prev().addClass('prev');})();

    // tabs external links
    $('ul.nav a.external').click(function(){
        top.location = $(this).attr('rel');
    });

    //search box focus
    $('.edit').focus(function() { $(this).css("color","#404040"); });
    $('.edit').blur(function() { $(this).css("color","#999999"); });

    /* Zebra tables */
	$("table.alternate").find("tr:odd").addClass("odd");
});

/* Typsy */
if(typeof($.fn.tipsy) != 'undefined') {
    $(function() {
        $('a.remove').tipsy({gravity: 's'});
        $('a.print-pdf').tipsy({gravity: 's'});
        $('a.email').tipsy({gravity: 's'});
    });
}

/* Hover for IE6 */
sfHover = function() {
	var sfEls = document.getElementById("main_menu").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover = function() {
			this.className +=" hover";
		}
		sfEls[i].onmouseout = function() {
			this.className=this.className.replace(new RegExp(" hover\\b"), "");
		}
	}
}
if(window.attachEvent) window.attachEvent("onload", sfHover);

/* Company details on hover */
var currentID;
var hideDelay = 600;
var hideTimer = null;
var briefAjax = null;

$(document).ready(function() {
	$('#companyPopupContainer, #ui-datepicker-div').live('mouseover', function() {
		if (hideTimer)
		  clearTimeout(hideTimer);
	});

	// One instance that's reused to show info for the current company
	var container = $('<div id="companyPopupContainer">'
		+ '<table width="" border="0" cellspacing="0" cellpadding="0" align="center" class="companyPopupPopup">'
		+ '<tr>'
		+ '   <td class="corner topLeft"></td>'
		+ '   <td class="top"></td>'
		+ '   <td class="corner topRight"></td>'
		+ '</tr>'
		+ '<tr>'
		+ '   <td class="companyBoxLeft">&nbsp;</td>'
		+ '   <td style="background-color: #FFFFFF;"><div id="companyPopupContent"></div></td>'
		+ '   <td class="companyBoxRight">&nbsp;</td>'
		+ '</tr>'
		+ '<tr>'
		+ '   <td class="corner bottomLeft">&nbsp;</td>'
		+ '   <td class="bottom">&nbsp;</td>'
		+ '   <td class="corner bottomRight"></td>'
		+ '</tr>'
		+ '</table>'
		+ '</div>');
	$('body').append(container);

	$('.companyPopupTrigger').live('mouseover', function() {
		// format of 'rel' tag: companyid,companyguid
		var settings = $(this).attr('rel').split(',');
		var companyID = settings[0];
		currentID = settings[1];

		// If no guid in url rel tag, don't popup blank
		if (currentID == '')
		  return;

		if (hideTimer)
		  clearTimeout(hideTimer);

		var pos = $(this).offset();
		var width = $(this).width();
		container.css({
		  left: (pos.left + width) + 'px',
		  top: pos.top - 120 + 'px'
		});

		// Preloader image and company title
		var preloader = $('<img>').attr({
			src: '/img/ajax-loader.gif',
			style: 'display: block; margin: 36px auto'
		});
		$('#companyPopupContent')
			.html('')
			.append('<h3 style="width:180px">' + $(this).text() + '</h3>')
			.append(preloader);

		// Read data
		if(briefAjax) briefAjax.abort();
		briefAjax = $.ajax({
			url: '/swift/companyBrief/' + companyID,
			success: function(data) {
				briefAjax = null;
				$('#companyPopupContent').html(data);
			}
		});
		$('.youtube').addClass('background');
		container.show();
	});

	$('.companyPopupTrigger').live('mouseout', function() {
		if (hideTimer)
		  clearTimeout(hideTimer);
		hideTimer = setTimeout(function()
		{
		  container.css('display', 'none');
		}, hideDelay);
		//$(this).closest('li').find('.youtube').removeClass('background');
	});

	// Allow mouse over of details without hiding details
	$('#companyPopupContainer').mouseover(function() {
		if (hideTimer)
		  clearTimeout(hideTimer);
	});

	// Hide after mouseout
	$('#companyPopupContainer').mouseout(function() {
		if (hideTimer)
		  clearTimeout(hideTimer);
		hideTimer = setTimeout(function()
		{
		  container.css('display', 'none');
		}, hideDelay);
	});
	//company details on hover end
});

