$(document).ready( function() {

    breadcrumb();
    tabs();
    forms();

    var hostname = $(window.location).attr('hostname');
    $('a').each(function() {
	var href = $(this).attr('href');
	if( href.indexOf('http://') >= 0 )
	{
	    if((href.split('/')[2] != hostname)){
		$(this).attr('target','_blank');
		$(this).addClass('external');
	    }
	}
    });

    function tabs()
    {
	var current_tab;
	function openTab(tab)
	{
	    if( tab == current_tab ) return;
	    current_tab = tab;

	    $('.tab').removeClass('active');
	    $(tab).addClass('active');
	    //$('.tabContent').hide();
	    //$(tab).children('.tabContent').show();
	
	    $('#emptyTabContent').html(""); // Do not use hide - flickering on small resolutions
	    $('#emptyTabContent').css('opacity', 0.1);
	    $('#emptyTabContent').html($(tab).children('.tabContent').html());
	    $('#emptyTabContent').animate({opacity: 1}, 200);
	}

	openTab($('.tab:first'));
	$('.tab').click( function(event) {
	    openTab(this);
	});

	$('table tr:odd').addClass('odd');
	$('div.tab div.tabContent').hide();
	$('div.tab, div.tab h4').addClass('jsEnabled');
    }

    function breadcrumb() {
	$('#breadCrumb ul li a').addClass('jsEnabled');
	$('#breadCrumb ul li a').mouseenter( function(event)
	{
	    $(this).parent().next().addClass('highlight');
	});
	$('#breadCrumb ul li a').mouseleave( function(event)
	{
	    $(this).parent().next().removeClass('highlight');
	});
    }

    function forms()
    {
	var input_value = new Object;
	$('input.tip').focus( function(event) {
	    var var_name = $(this).attr("name");
	    var reset = false;
	    var val = $(this).val();
	    eval('if(input_value.'+var_name+' == null || input_value.'+var_name+' == val) { input_value.'+var_name+' = val; reset = true;}');

	    if( reset )
		$(this).val('');
	});
	$('input.tip').blur( function(event) {
	    var var_name = $(this).attr("name");
	    eval('var var_value = input_value.'+var_name);
	    if($(this).val() == '')
	    {
		$(this).val(var_value);
	    }
	});
    }


    $('a.image').fancybox({
	'titleShow'	: true,
	'transitionIn'	: 'elastic',
	'transitionOut'	: 'elastic',
	'overlayShow'	: true,
	'titlePosition' : 'over'
    });

    

    $('#submenu li.close ul').hide();
    $('#submenu li.close > a').click(function(event)
    {
	if( !$(this).parent('li').hasClass('open') )
	{
	    event.preventDefault();
	    event.stopPropagation();

	    $('#submenu li.open ul').hide();
	    $('#submenu li.open').addClass('close');
	    $('#submenu li.open').removeClass('open');
	    $('#submenu li').removeClass('active');

	    $(this).parent().children('ul').show();
	    $(this).parent().addClass('open');
	    $(this).parent().removeClass('close');
	    
	}
    });
    
    
    
});
