$(function() {
	$('#sliderwrap').tabs({
		show: function(event, ui) {

            var lastOpenedPanel = $(this).data("lastOpenedPanel");

            if (!$(this).data("topPositionTab")) {
                $(this).data("topPositionTab", $(ui.panel).position().top)
            }         

            //Dont use the builtin fx effects. This will fade in/out both tabs, we dont want that
            //Fadein the new tab yourself            
            $(ui.panel).hide().fadeIn(500);

			sliderscolor = $(ui.panel).children('.bgcolor').html();

			$('#sliders').animate({ backgroundColor: sliderscolor }, "500");

            if (lastOpenedPanel) {

                // 1. Show the previous opened tab by removing the jQuery UI class
                // 2. Make the tab temporary position:absolute so the two tabs will overlap
                // 3. Set topposition so they will overlap if you go from tab 1 to tab 0
                // 4. Remove position:absolute after animation
                lastOpenedPanel
                    .toggleClass("ui-tabs-hide")
//                    .css("position", "absolute")
//                    .css("top", $(this).data("topPositionTab") + "px")
                    .fadeOut(500, function() {
                        $(this)
                        .css("position", "");
                    });
            }

            //Saving the last tab has been opened
            $(this).data("lastOpenedPanel", $(ui.panel));

        }

    });		

	$('#sliderwrap').tabs( 'rotate' , 6000, 'true' );

	$('.inpage-tabs').tabs();

	$('#askfraser-question').click( function(e) {
		e.preventDefault();
		$('body').append('<div id="popup"><div id="popup-spacer"></div><div id="popup-contents"><iframe src="/?page_id=129&ajax" border="0" height="100%" width="100%" frameBorder="0"></div></div>');

		$('#popup').live('click', function(e) {
			e.preventDefault();
			$(this).remove();
		});
	});
	
	$('input[type="text"]').val( $(this).prev('label').html() );

	$('form.unify input[type="text"]').each( function() {
		itemlabel = $(this).prev('label').html();
		$(this).val( itemlabel );
	});
	
	$('form.unify input[type="text"]').click( function() {
		if ( $(this).val() == $(this).prev('label').html() ) {
			$(this).val('');
		}
	});
	$('form.unify input[type="text"]').blur( function() {
		if ( $(this).val() == '' ) {
			$(this).val( $(this).prev('label').html() );
		}
	});
	
	if ($('#map_canvas').length) { 
		initialize();
	}
	
	if ( $('html.ielt8 .table, html.ie8 .table').length > 0 ) { 
		$('.table-row:odd').addClass('odd');
	}
	
	$('.testimonials a').live('click', function(e) {
		e.preventDefault();

		testimonialwrap = $('.testimonialwrap');
		testimonialwrap.parent().css('height', testimonialwrap.parent().height() );
		window.t = $(this).attr('href');
		testimonialwrap.fadeToggle(function() { 
//			testimonialwrap.load( $(this).attr('href') + '?ajax', function() {
			testimonialwrap.load( window.t + '&ajax', function() {
				heightchange = $('.testimonials .testimonialwrap').outerHeight()  + 50;
				$('.testimonials').animate({
						height: heightchange
					}, 300, function() {
						$('.testimonials .testimonialwrap').fadeToggle();
					});
			});
		});
	});	
	
});
