/*

		TELLEMACHUS.COM
		VERSION 1.0

		GLOBAL ONLOAD FUNCTIONS
		
*/

$(document).ready(function() {
	$("a[href*=.pdf]").click(function(){
	window.open(this.href);
	return false;
});
	$(".btnVideo").colorbox({inline:true, href:"#videoEmbed"});
	$("#enquiry_form").validate({
   		errorElement: "em"
	})
	$("#callback_form").validate({
   		errorElement: "em"
	})
	if ($('#strap img').length > 1) initStrap();
	if ($('#product-images').length > 0) initSlideshow();
	if ($('#product-details').length > 0) createProductTabs();
	if ($('.cameraVideo').length > 0) addVideo();
	if ($('.contactMap').length > 0) addMap();
	$("a[href*=.pdf]").parent().addClass("pdf");
	$("a[href*=.zip]").parent().addClass("zip");		
	$("a[href*=.doc]").parent().addClass("doc");
	$("a[href*=.ppt]").parent().addClass("ppt");		
	$("a[href*=.jpg]").parent().addClass("jpg");		
	$("a[href*=.xls]").parent().addClass("xls");
	$("a[href*=.avi], a[href*=.mpg]").parent().addClass("video");
});
function initStrap() {
	numberOfPhotos = $('#strap img').length;
	$('#strap img').each(function(i) {
		$(this).css('z-index',((numberOfPhotos - i) + 2) % numberOfPhotos);
	});
	setTimeout(function() { rotateStrap($('#strap img').length - 1); }, 3000)
}

function rotateStrap(currentPhoto) {
	var numberOfPhotos = $('#strap img').length;
	currentPhoto = currentPhoto % numberOfPhotos;
	$('#strap img').eq(currentPhoto).fadeOut(1000,function() {
		$('#strap img').each(function(i) {
			$(this).css('z-index',((numberOfPhotos - i) + currentPhoto) % numberOfPhotos);
		});
		$(this).show();
		setTimeout(function() {rotateStrap(++currentPhoto);}, 3000);
	});
}

function initSlideshow() {
	var $images = $('#product-images');
	
	$images.find('a').css('cursor','default').hover(function() {
		$('#product-images img:first').attr('src', $(this).attr('href'));
	}).click(function(){
		return false;
	});
}

function createProductTabs() {
	var maxHeight = 0;
	var $tabs = $('<ul id="product-tabs" />');
	var $details = $('#product-details');
	
	$details.find('h2').each(function() {
		$this = $(this);
		$('<li />')
			.append($('<a class="txtWhite noUnderline" />').html($this.text()).attr('href', '#' + $this.parent().attr('id')))
			.appendTo($tabs);
		maxHeight = ($this.parent().height() > maxHeight) ? $this.parent().height() : maxHeight;
		$this.remove();
	});
	$details
		.addClass('bgGradient tabbed')
		 //.css('height', maxHeight + 'px')
		.children()
		.filter(':first')
		.addClass('current')
		.end()
		.filter(':not(:first)')
		.hide();
	$tabs
		.find('li:first-child a')
		.removeClass('txtWhite')
		.addClass('txtOrange current')
		.end()
		.find('li:last-child')
		.addClass('last-child')
		.end()
		.insertBefore($details)
		.find('a')
		.click(function() {
			$this = $(this);
			$current = $('#product-tabs .current');
			$this
				.removeClass('txtWhite')
				.addClass('current txtOrange');
			$current
				.removeClass('current txtOrange')
				.addClass('txtWhite');
			$($current.attr('href')).fadeOut('fast', function() {
				$($this.attr('href')).fadeIn('fast');
			});
			return false;
		});
}

function addVideo() {
	var flashvars = {};
	var params = {};
	
	$('.cameraVideo')
		.append('<div id="video-container" />')
		.find('#video-container');
	params.wmode = 'transparent';
	swfobject.embedSWF('/flash/camera-lens.swf','video-container','526','295','8.0.0','',flashvars,params);
}
function addMap() {
	var flashvars = {};
	var params = {};
	
	$('.contactMap')
		.append('<div id="map-container" />')
		.find('#map-container');
	params.wmode = 'transparent';
	swfobject.embedSWF('/flash/contact-map.swf','map-container','650','295','8.0.0','',flashvars,params);
}

