$(document).ready(function() {

var navEnabled = 1;
$('div.abox').hide();
$('div.abox.current').fadeIn();
if ($('div#whats-new').size() != 0) {
	var sideHeight = $('div#whats-new').height();
	$('#ad-products-nav').animate({height: sideHeight});
}



/** Adjust CSS for TR when interacting **/
/****************************************/
$('.ad-products-table tr').click(function() {
	$('tr').removeClass('tr-select');
	$(this).addClass('tr-select');
	$('div.qtip-contentWrapper').css('border', '')
	$('div.qtip-contentWrapper').css('background', '')
	$('div.qtip-contentWrapper').css('padding', '0px 0px 10px 0px')
	$('div.qtip-title').css('background', '')
	$('div.qtip-content').css('background', '')
	$('div.qtip').css('-moz-border-radius', '10px')
});

$('.ad-products-table tr').mouseover(function() { $(this).css('background-color', '#F6F6F6') });
$('.ad-products-table tr').mouseout(function() { $(this).css('background-color', '') });
/****************************************/



/******** QTip Styling Variables ********/
/****************************************/
var adPosition = { adjust: { x: 300 }, corner: { tooltip: 'rightMiddle', target: 'rightMiddle' } }
var adStyle = { width: 680, border: { color: '#FFFFFF' }, name: 'light', tip: 'leftMiddle' }
var adShow = { when: 'click', solo: true, effect: { type: 'fade', length: 350 } }
var adHide = { when: false, effect: { type: 'fade', length: 350 } }
/****************************************/



/***************** QTip *****************/
/****************************************/
$('.ad-products-table tr').each(function() {

	if ($(this).parents('#specifications-by-site-location').size() != 0) { return false; }
	
	$(this).attr('tooltip', 'popout'+$(this).parent().children().index($(this)))
	
	tablename = $(this).parent().parent().attr('tablename');
	tooltip = $(this).attr('tooltip');
	pdf_link = $(this).children('td.ap-btn').children('div.save-pdf').children('a').attr('href');
	img_link = '<a href="'+pdf_link+'"><img src="/img/_adsamples/'+tablename+'_'+tooltip+'.jpg" /></a>';

	$(this).qtip({
		content: {
			text: img_link,
			title: {
				text: '',
				button: 'X'
			}
		},
		api: { onShow: function() { $('tr.tr-select').addClass('tr-selected') }, beforeHide: function() { $('tr').removeClass('tr-selected') } },
		show: adShow, hide: adHide, position: adPosition, style: adStyle
	})

})
/****************************************/



/************ Right Sidebar *************/
/****************************************/
$('#ad-products-nav ul#apn a').click(function() {
	if (navEnabled == 1) {
		
		var current = $(this).parent().attr("class");
		$('div.qtip').fadeOut();
		$('tr').removeClass('tr-selected');
		$('tr').removeClass('tr-select');
		navEnabled = 0;
		$('#ad-products-nav li.active').removeClass('active');
		$(this).parent().addClass('active');
		
		sideHeight = $('div#'+current).height();
		if(sideHeight > 184) { 
			$('div.current').fadeOut( 500, function() { $('div.abox').removeClass("current"); $('#ad-products-nav').animate({height: sideHeight}, function() { $('div#'+current).addClass("current").fadeIn( 500, function() { navEnabled = 1; } ); } ); } );
		}
		else { 
			$('div.current').fadeOut( 500, function() { $('div.abox').removeClass("current"); $('#ad-products-nav').animate({height: 184}, function() { $('div#'+current).addClass("current").fadeIn( 500, function() { navEnabled = 1; } ); } ); } );
		}
		
	}
})

$('.goto-unit.po-custom a').click(function() {
	if (navEnabled == 1) {
		
		$('div.qtip').fadeOut();
		$('tr').removeClass('tr-selected');
		$('tr').removeClass('tr-select');
		navEnabled = 0;
		$('#ad-products-nav li.active').removeClass('active');
		$('li.specifications-by-ad-unit').addClass('active');
		
		sideHeight = $('div#specifications-by-ad-unit').height();
		if(sideHeight > 184) { 
			$('div.current').fadeOut( 500, function() { $('div.abox').removeClass("current"); $('#ad-products-nav').animate({height: sideHeight}, function() { $('div#specifications-by-ad-unit').addClass("current").fadeIn( 500, function() { navEnabled = 1; } ); } ); } );
		}
		else { 
			$('div.current').fadeOut( 500, function() { $('div.abox').removeClass("current"); $('#ad-products-nav').animate({height: 184}, function() { $('div#specifications-by-ad-unit').addClass("current").fadeIn( 500, function() { navEnabled = 1; } ); } ); } );
		}
		
	}
})
/****************************************/

});
