/*
 * Custom Javascript Library
 * Author D. Rogaczewski, C. Berg
 * Requires: jQuery v1.3+
*/

var premiums = new Array(30.0, 52.50, 80.0, 112.50, 150.0, 192.5, 240.0, 287.5, 335.0);

$(document).ready(function()
{
	if($('.tabs').length > 0){
		initSidebarTabs();
	}
	
	if($('.faq-list').length > 0){
		$('.faq-list').accordion();
	}
	
	if($('#rabatt').length > 0){
		$('select').selectToUISlider({
            sliderOptions: {
              change: function(event, ui) {
                  updateForm();
              }
            }
        });
	}
		
	// Modell Dialog			
	if($('#modell-dialog').length > 0){
		$('#modell-dialog').dialog({
			autoOpen: false,
			width: 445,
			resizable: false,
			buttons: {
				"Abbrechen": function() { 
					$(this).dialog("close"); 
				} 
			}
		});
	}
	
	
	// Modell Dialog Link
	$('#modell-trigger').click(function(){
		$('#modell-dialog').dialog('open');
		$(".ui-dialog-titlebar").hide();
		return false;
	});

    /**
     * Replaces the contents of the currently shown car information with the
     * selected one and recalculates the form values.
     */
    $('#modell-list a:modell-link').click(function(){
        var carParts = $('#modell > span');

        for(i = 0; i < carParts.length; ++i)
        {
            replaceBy = '';
            carPart = $(carParts[i]);
            cssClass = carPart.attr('class');
            selector = '.' + cssClass;

            if(cssClass == 'modell-desc')
            {
                carPart = carPart.children('span');               
                replaceBy = $(this).children(selector).children('span').html();
            }
            else
            {
                replaceBy = $(this).children(selector).html();
            }

            carPart.html(replaceBy);
        }

        $('#modell-dialog').dialog('close');
        updateForm();
    });

    // Initial calculation of default values
	if($('#modell-calculator').length > 0) updateForm();
	
	
	// Video Dialog
	if($('#video-dialog').length > 0){
		$('#video-dialog').dialog({
			autoOpen: false,
			width: 688,
			resizable: false,
			overlay: { opacity: 0.1, background: 'white'},
			modal: true,
			buttons: {
				"Schliessen": function() { 
					$(this).dialog("close"); 
				} 
			}
		});
	}
	
	// Video Dialog Link
	$('#video-trigger').click(function(){
		$('#video-dialog').dialog('open');
		$(".ui-dialog-titlebar").hide();
		return false;
	});
	
	
	if($('#video-dialog2').length > 0){
		$('#video-dialog2').dialog({
			autoOpen: false,
			width: 720,
			resizable: false,
			overlay: { opacity: 0.1, background: 'white'},
			modal: true,
			buttons: {
				"Schliessen": function() { 
					$(this).dialog("close"); 
				} 
			}
		});
	}
	$('#video-trigger2').click(function(){
		$('#video-dialog2').dialog('open');
		$(".ui-dialog-titlebar").hide();
		return false;
	});
	
});

/**
 * Recalculate the form values if any of the user editable fields (discount
 * level, selected car) changes.
 */
function updateForm()
{
    discountSelector = document.getElementById('rabatt');

    carPrice = parseInt($('#modell span.modell-price > strong').text().replace(/\./, ''));
    discount = carPrice * discountSelector.value / 100;

    $('#inp-result').val(discount).formatCurrency({region: 'de'});
    $('#inp-bonus').val(premiums[discountSelector.selectedIndex]).formatCurrency({region: 'de'});
    $('#inp-basis').val(carPrice - discount).formatCurrency({region: 'de'});
    $('#inp-time').val(carPrice / 1000 + ' Monate');
}


/* init Tabs
==================================================================================================== */
function initSidebarTabs()
{
	if($('.tabs').length > 0)
	{
		$('.tabs').each(function(){
			
			t = $(this);
		
			$(this).find(".tabs-content").hide(); //Hide all content
			$(this).find("ul.tabs-nav li:first").addClass("selected").show(); //Activate first tab
			
			$(this).find(".tabs-content:first").show(); //Show first tab conten
		
			//On Click Event
			$(this).find("ul.tabs-nav li a").click(function()
			{
				$(this).parent().parent().find('li').removeClass("selected"); //Remove any "active" class
				$(this).parent().addClass("selected"); //Add "active" class to selected tab
				
				$(this).parent().parent().parent().parent().find(".tabs-content").hide(); //Hide all tab content

				var activeTab = $(this).attr("href");
				$(activeTab).fadeIn();
				return false;
			});
		});
	}
}

