// JavaScript Document
var current_total_price = 0.00;
var requires_delivery = true;
var current_delivery_price = 0.00;
var current_hire_description = 'Six Month Hire';
var current_price = false;
    $(document).ready(function(){ 
        $('ul.sf-menu').superfish({ 
			delay:         500,
            animation: {height:'show'},   // slide-down effect without fade-in 
        	speed:         'fast',
			dropShadows:   false,
			autoArrows:    false
		});
		
		//Adjust the position of the button...
		var top_pos = $('.total .btn').position();
		$('#flip-fwd-btn').css({top:top_pos.top+40});
		
		// Cycle plugin for gallery
		//$('ul.gallery').cycle();
		
			
		$('.ajax_prod_link').click(function() {
			
			var url = $(this).attr('alt');
			//$('#ajax-preview-frame').empty();
			$('#loading-frame').show();
			$('#ajax-preview-frame').load(url,new Date().getTime(),function() {
				$('#loading-frame').hide();		
			});
			
			return false;									
		});
		
		
		
		var front = true;
								
		$('#flip-fwd-btn').bind('click',function(){
			$('#flip1').flip({
				direction:'rl',
				speed:400,
				color:'#f0f8fb',
				content:$('#flip2'),
				onBefore: function() {
					$('#flip-fwd-btn').css({display:'none'});
					$('#flip-back-btn').css({display:'none'});
				},
				onEnd: function() {
					if(front == false) {
						$('#flip-fwd-btn').css({display:'none'});
						$('#flip-back-btn').css({display:'block'});
					} else {
						$('#flip-fwd-btn').css({display:'block'});
						$('#flip-back-btn').css({display:'none'});
						
						//update the radios to show the correct value...
						var radios = $('input:radio[name=hire-period]');
						radios.each(function() {
							r = $(this);
							if (r.val() == current_price) {
								 r.attr('checked',true);
							}
						});
						
					}
					//reapply the handlers
					$('[name=hire-period]').click(function(){handle_pricing();});
					$('#delivery').click(function(){handle_pricing();});
					_re_apply_ajax_forms()
				}
			});
			
			
			front = !front;
			
			return false;
		});
		
		$('#flip-back-btn').bind('click',function(){
			$('#flip1').revertFlip();
			front = !front;
			return false;
		});
		
		if($.browser.msie){
			$('a.tooltip').tooltip({
				track: true, 
				delay: 0, 
				showURL: false,
				showBody: " - ", 
				extraClass: "",
				fixPNG: true, 
				top: -35, 
				left: 5
			});
		} 
		else{
			$('a.tooltip').tooltip({
				track: true, 
				delay: 0, 
				showURL: false,
				showBody: " - ", 
				extraClass: "",
				fade: 150,
				fixPNG: true, 
				top: -35, 
				left: 5
			});
		} 
		
		handle_pricing();
		
		$('[name=hire-period]').click(function(){handle_pricing();});
		$('#delivery').click(function(){handle_pricing();});
		
    }); 
	
	
	function handle_pricing() {
		
		var radios = $('input:radio[name=hire-period]');
		var delivery_price = false;
		var total_price = 0.00;
		radios.each(function() {
			r = $(this);
			if (r.attr('checked')) {
				current_price =r.val();
			}
		});
		
		//break apart the two parts of the value...
		if (current_price) {
			var parts = current_price.split('|');
			
			if ($('#delivery').attr('checked')) {
				delivery_price = $('#hidden_price').val();
				requires_delivery = true;
				total_price = parseFloat(parts[1]) + parseFloat(delivery_price);
				$('#current_delivery_price').empty();
				$('#current_delivery_price').append('$' + parseFloat(delivery_price).toFixed(2));
				$('#current_delivery_description').empty();
				$('#current_delivery_description').append('Delivery');
				$('#delivery_required').val('Yes');
				$('#delivery_price').val('$' + parseFloat(delivery_price).toFixed(2));
			} else {
				delivery_price = 0;
				requires_delivery = false;
				total_price = parseFloat(parts[1]);
				$('#current_delivery_price').empty();
				$('#current_delivery_price').append('');
				$('#current_delivery_description').empty();
				$('#current_delivery_description').append('No Delivery');
				$('#delivery_required').val('No');
				$('#delivery_price').val('0.00');
			}
			current_total_price = total_price;
		}
		
		$('#total_1_display').empty();
		$('#total_1_display').append(pretty_total(total_price));
		
		$('#current_total_price').empty();
		$('#current_total_price').append(pretty_total(total_price));
		
		$('#current_hire_description').empty();
		$('#current_hire_description').append(parts[0]);
		
		$('#current_hire_price').empty();
		$('#current_hire_price').append('$' + parseFloat(parts[1]).toFixed(2));
		
		//update the hidden fields on the form...
		$('#hire_description').val(parts[0]);
		$('#hire_price').val('$' + parseFloat(parts[1]).toFixed(2));
		
		$('#product_title').val($('#product_title_field').text());
		
		
	}
	
	function pretty_total(tot) {
		var fullparts = tot.toFixed(2).split('.');
		return '$'+fullparts[0]+'<span class="sup">.'+fullparts[1]+'</span>';
	}