// $cmignore
TopUp.images_path = "/images/top_up/";

var controllers = {
	wireOverlays : function () {
		$('.fullscreen').each(function (index) {
			$(this).attr('href', $(this).attr('href') + '?contentonly=true');
		});
		
		$('.fullscreen').not('.table').click(function () {
			TopUp.display($(this).attr('href'), {
					shaded: 1,
					effect: 'fade',
					overlayClose: 1,
					width: '677',
					height: '550',
					type:'iframe'
				}
			);
			return false;
		});
	},
	wireMisc : function () {
		$('#recommendations li:last-child').addClass('last-child');
		//$('.tab .content .with-image').supersleight({shim: '/images/bg/transparent.gif'});
	},
	wireTabs : function () {
		var selectTab = function (tab) {

			if (typeof(tab) == 'undefined' || !(tab instanceof jQuery) || !tab.hasClass('tab')) {
				tab = $('.tab:first');
			}
			
			if  (!tab.hasClass('current')) {
				$('.tab, #tab-navigation li').removeClass('current');
				tab.addClass('current');
			
				var tabId = tab.attr('id');
				var tabButton = $('#tab-navigation li a[href=#' + tabId + ']');
			
				tabButton.parent().addClass('current');
			
				dcsMultiTrack('DCS.dcsuri', ('/products/' + tabId + '/'), 'WT.ti', (tabButton.text() + ' Tab'));
			}
		};
	
		$('#tabs')
			.addClass('dynamic')
			.prepend('<ol id="tab-navigation"></ol>');
	
		$('.tab').each(function (i) {
			if (!$(this).attr('id')) $(this).attr('id', ('tab-' + i));
		
			$('#tab-navigation')
				.append('<li><a>' + $(this).find('h2').text() + '</a></li>')
				.find(':last-child a')
					.attr('href', '#' + $(this).attr('id'))
					.click(function (event) {
						event.preventDefault();
						// window.location.hash = this.hash;
						selectTab($($(this).attr('href')));
					})
				.end();
			$(this).find('h2').remove();
			
			//"Be the first to review this product" Functionality.
			$(".review > p > a.em").bind("click", function(event){
			    event.preventDefault();
    			selectTab($($(this).attr('href')));
    		});
			
			$(".review > div > a").bind("click", function(event){
			    event.preventDefault();
    			selectTab($($(this).attr('href')));
    		});
			
		});
	
		selectTab($(document.location.hash));
		
		// TODO this guy below is causing selecTab to execute twice on click
		/*$.history.init(function (hash) {
			selectTab($('#' + hash));
		});*/
		
		// for older browsers...
		$('#tab-navigation li:first-child').addClass('first-child');
		$('#tab-navigation li:last-child').addClass('last-child');
		$('table tr:nth-child(odd)').addClass('nth-child-odd');
	},
	wireLinks : function () {
		$('.print > a, a.print').click(function (event) {
			window.print();
			return false;
		});
	},
	wireImageRollovers : function () {
		var image = $('#product-img > img');
		image.data('primary', image.attr('src'));
		
		$('#thumbs li a').click(
			function () {
				image.attr('src', $(this).attr('href'));
				return false;
			}
		);
	},
	wireSwatchRollovers : function () {
		var timer = null;
		$('<div id="color-zoom"><img /><p></p></div>').appendTo('body');
		$('.swatches img').each(function () {
			var description = $(this).attr('title');
			$(this)
				.mousemove (function (event) {
						var color = $(this);
						$('#color-zoom')
							.css('top', event.pageY - $('#color-zoom').height() - 20 + 'px')
							.css('left', event.pageX - $('#color-zoom').width() - 20 + 'px')
				})
				.mouseenter(function () {
					clearTimeout(timer);
					var color = $(this);
					$('#color-zoom')
						.show()
						.find('img').attr('src', color.attr('src')).end()
						.find('p').text(color.attr('title')).end();
				})
				.mouseout (function (event) {
					hideRollover = function () { $('#color-zoom').hide(); }
					timer = setTimeout('hideRollover()', 150);
				});
		});
	},
	wirePopupTable : function () {
		$('.content table').clone().appendTo('body').wrap('<div id="msds-table"></div>');
		
		$('.fullscreen.table').click(function () {
			TopUp.defaultPreset({
				layout: "dashboard",
				modal: 1,
				shaded: 1,
				effect: 'appear',
				resizable: 0
			});
			
			TopUp.display('#msds-table', {type : 'dom'});
			
			return false;
		});
	}
}

$(document).ready(function () {

	for (controller in controllers) {
		controllers[controller]();
	}
});
// $/cmignore

