/* script JS */

var base_url = window.location.host;

/* CUFON */
Cufon.replace('#language, #menu, #footer h3',{
	hover:true,
	fontFamily: 'Banda Regular'
});
Cufon.replace('#heading h2, .helvetica',{
	fontFamily: 'HelveticaNeueLT Com 57 Cn'
});
Cufon.replace('#heading h2.intro, #heading h3, #main h3:not(.testimonies), #bottom h3, #bottom h4:not(.bottom), table thead, .button, button, #bottom label, .sample h4, #contact_success',{
	fontFamily: 'HelveticaNeueLT Com 67 MdCn'
});
Cufon.replace('#heading li span.caps',{
	fontFamily: 'HelveticaNeueLT Com 67 MdCn'
});
Cufon.replace('#heading li span:not(.caps)',{
	fontFamily: 'HelveticaNeueLT Com 55 Roman'
});
Cufon.replace('.toopixel',{
	hover: true,
	fontFamily: 'HelveticaNeueLT Com 55 Roman'
});
Cufon.replace('span.aka',{
	fontFamily: 'akaDora'
});

/* // CUFON */

$(document).ready(function(){
	// logo hover
	$('#logo').hover(function() {
		$('span',this).stop(true,true).fadeOut(250);
	}, function() {
		$('span',this).stop(true,true).fadeIn(250);
	});
	
//	alert('Document is ready.')

	// heading menu hover
	$('#heading li').hover(function(){
		if(!$('a',this).hasClass('active')) {
			$('span.icon',this).stop(true,true).animate({
				backgroundColor: '#FFF'
			},250);
			$('span',this).css('color','#c80001');
			$('span.caps',this).css('color','#FFF');
			Cufon.refresh();
		}
	},function(){
		if(!$('a',this).hasClass('active')) {
			$('span.icon',this).stop(true,true).animate({
				backgroundColor: '#7D7D7D'
			},250);
			$('span',this).css('color','#a1a1a1');
			Cufon.refresh();
		}
	});
	
	// bubble hover if not ie
	if(!$.browser.msie) {
		$('.bubble').hover(function() {
			$(this).stop(true,true).animate({
				backgroundColor: '#c80001',
				opacity: 1
			},250);
		}, function() {
			$(this).stop(true,true).animate({
				backgroundColor: '#000',
				opacity: .9
			},250);
		});
	}
	
	// button hover
	$('.button, button').hover(function() {
		$(this).stop(true,true).animate({
			backgroundColor: '#c80001'
		},250);
	}, function() {
		$(this).stop(true,true).animate({
			backgroundColor: '#000'
		},250);
	});
	
	// rotate testimonials
	testimonials();
	
	// tooltip
	tooltips();
	
	// create sliders
	if($('.slider').length > 0) {
		$('.slider').each(function() {
			// get input id
			var pid = $('input[type=text]',$(this).parent()).attr('id');
			var min = 0;
			var max = 100;
			var disabled = false;
			// total length can go to 1500
			switch (pid) {
				case 'total_length':
					max = 1500;
				break;
				case 'dvds_needed':
					max = 50;
					disabled = true;
				break;
				case 'aditional_dvd':
					max = 50;
				break;
				case 'total_minutes':
					max = 1500;
				break;
				case 'reels_no':
					max = 1500;
				break;
				default:
					max = 100;
					disabled = false;
				break;
			}
			
			$(this).slider({
				range: 'min',
				min : min,
				max : max,
				animate: true,
				disabled: disabled,
				slide: function( event, ui ) {
					if (!event.originalEvent) return;
					var p = $(ui.handle).parent().parent();
					$('input[type=text]',p).val(ui.value);
				},
				change: function(event,ui) {
					if (!event.originalEvent) return;
					var p = $(ui.handle).parent().parent();
					$('input[type=text]',p).change();
				}
			});
		});
		
		// set option disabled true for additional dvds slider
		$('.slider',$('#aditional_dvd').parent()).slider('option', 'disabled', true).slider('option', 'value', 0);
	}
	// value change on cost inputs run estimate
	$('input[type=text]','#cost').change(function(){
		$('.slider',$(this).parent()).slider( "option", "value", $(this).val());
		estimate();
	});
	// check on avi recalculate
	$('#avi_check').click(function() {
		estimate();
	});
	
	// reset estimate
	$('.reset_estimate').click(function(){
		$('input[type=text]','#cost').each(function(){
			$(this).val(0);
			$('.slider',$(this).parent()).slider( "option", "value", 0);
		});
		// uncheck avi_check
		$('#avi_check').attr('checked',false);	
		estimate();
		return false;
	});
	
	// init map
	if($('#map_canvas').length > 0) {
		initializeMap(46.521216,6.633146,'map_canvas');
	}
	
	// Open all external links and PDF in new window
	$("a[href^='http:']:not([href*='" + base_url + "']), a[href$='.pdf']:not([href*='" + base_url + "']), a[href$='.pdf']").live('click', function() {
		$(this).attr('target','_blank');
	});
});

function estimate() {
	var pricePerDvd = 26;
	var pricePerFilmMeter = 0.9;
	var pricePerVideoHour = 42;
	var pricePerReel = 8.5;
	var pricePerAdditionalDVD = 15;
	var pricePerAvi = 17.5;
	
	// calculate total time 
	var total_time = Math.round(0.24150945*$('#total_length').val(), 2);
	// set total time to total span
	$('.total_time').text(total_time);
	
	var total_price = 0;
	// calculate price per reel
	total_price = total_price + ($('#reels_no').val()*pricePerReel);
	// calculate price per film meter
	total_price = total_price + ($('#total_length').val()*pricePerFilmMeter);
	
	// calculate number of dvds needed
	var filmcontrib = 0.24150945*$('#total_length').val()/90;
	var videocontrib = $('#total_minutes').val()/90
	var dvd_count = Math.ceil(filmcontrib+videocontrib);

	// if dvd_count > 0 unlock additional dvds slider
	if(dvd_count > 0) {
		$('#aditional_dvd').attr('readonly',false);
		$('.slider',$('#aditional_dvd').parent()).slider('option', 'disabled', false);
	} else {
		$('#aditional_dvd').attr('readonly',true).val(0);
		$('.slider',$('#aditional_dvd').parent()).slider('option', 'disabled', true).slider('option', 'value', 0);
	}
	
	// set dvd needed 
	$('#dvds_needed').val(dvd_count);
	var total_dvds = parseFloat(dvd_count)+parseFloat($('#aditional_dvd').val());
	$('.dvds_needed').text(total_dvds);

	if(!$('#avi_check').is(':checked')) {	
		// add needed dvds to price
		total_price = total_price + ($('#dvds_needed').val()*pricePerDvd);
		// add extra dvd to price
		total_price = total_price + ($('#aditional_dvd').val()*pricePerAdditionalDVD);
	} else {
		// whole total time
		var tTime =  parseFloat($('#total_minutes').val()) + parseFloat(total_time);
		if(tTime == 0) {
			total_price = total_price + pricePerAvi;
		} else {
			total_price = total_price + Math.ceil(tTime/60)*pricePerAvi;
		}
	}
	// calculate video price
	if($('#total_minutes').val() > 0) {
		total_price = total_price + Math.ceil($('#total_minutes').val()/60)*pricePerVideoHour
	}
	
	// set total cost
	$('.total_cost').text(total_price.toFixed(2));
	Cufon.refresh();
	
	
}

function testimonials() {
	// check if testimonials list is present
	if($('#testimonials li').length < 2) {
		return false;
	}
	
	// set interval
	var ti = setInterval(function(){
		// count testimonials
		var total_items = $('#testimonials li').length;
		// check active index
		var current = $('#testimonials li').index($('#testimonials li.active'));
		// check if current item is not last
		var next_item = 0;
		if(current < total_items - 1) {
			next_item = current + 1;
		} else {
			next_item = 0;
		}
		// remove current active
		$('#testimonials li.active').stop(true,true).fadeOut(500,function(){
			$(this).removeClass('active');
			// add class active to next
			$('#testimonials li:eq('+next_item+')').fadeIn(250).addClass('active');
		});
		
	
	}, 5000)

}

function tooltips() {
	$('td .info').hover(function(){
		var of = $(this).offset();
		// get tooltip vertical coordinate
		var top = of.top - 30;
		var left = of.left - ($('.tooltip',$(this).parent()).width()/2) + 240;
		
		$('.tooltip',$(this).parent()).hide().css({
			left:left,
			top:top
		}).fadeIn(250);
		
	},function(){
		$('.tooltip',$(this).parent()).css({
			left: -9999
		});
	});
}


var map;
var markersArray = [];
function initializeMap(Mlat,Mlong,div_id) {
	var latlng = new google.maps.LatLng(Mlat,Mlong);
	var myOptions = {
		zoom: 16,
		center: latlng,
		mapTypeId: google.maps.MapTypeId.ROADMAP,
		disableDefaultUI: true
	};
	map = new google.maps.Map(document.getElementById(div_id),
		myOptions);
	placeMarker(latlng);
}
function placeMarker(location) {
	if (markersArray) {
		for (i in markersArray) {
			markersArray[i].setMap(null);
		}
	}
	var marker = new google.maps.Marker({
		position: location,
		map: map
	});
	markersArray.push(marker);
	map.setCenter(location);
}

/* end */

