$(document).ready(function() {
	
	$('select').each(function()	{
	
		var selectclass = $(this).attr("class");
		if (selectclass != "") {
	
			if(!$(this).parent().hasClass('enhanced')) {
				targetselect = $(this);
				targetselect.hide();
		
				// set our target as the parent and mark as such
				var target = targetselect.parent();
				target.addClass('enhanced');
		
				// prep the target for our new markup
				target.append('<dl class="dropdown '+selectclass+'"><dt><a class="dropdown_toggle" href="#"></a></dt><dd><div class="options '+selectclass+'"><ul></ul></div></dd></dl>');
				target.find('.dropdown').css('zIndex',z);
				z--;
		
				// we don't want to see it yet
				target.find('.options').hide();
		
				// parse all options within the select and set indices
				var i = 0;
				targetselect.find('option').each(function()	{
					// add the option
					target.find('.options ul').append('<li><a href="#"><span class="value">' + $(this).text() + '</span><span class="hidden index">' + i + '</span></a></li>');
		
					// check to see if this is what the default should be
					if($(this).attr('selected') == true) {
		  				targetselect.parent().find('a.dropdown_toggle').append('<span></span>').find('span').text($(this).text());
		  				targetselect.parent().find('a.dropdown_toggle').append('<span class="hidden selindex"></span>').find('span.selindex').text($(this).val());
					}
					i++;
					if (i==minItemsForScroll) target.find('.options').addClass("biggie");
				});
			}
		}
	});
	
	// let's hook our links, ya?
	$('a.dropdown_toggle').live('click', function()	{
		var theseOptions = $(this).parent().parent().find('.options');
		if(theseOptions.css('display')=='block') {
			$('.activedropdown').removeClass('activedropdown');
			theseOptions.hide();
		} else {
			theseOptions.parent().parent().addClass('activedropdown');
			theseOptions.show();
		}
		return false;
	});

	// bind to clicking a new option value
	$('.options a').live('click', function(e) {
		$('.options').hide();

		var enhanced = $(this).parent().parent().parent().parent().parent().parent();
		var realselect = enhanced.find('select');

		// set the proper index
		realselect[0].selectedIndex = $(this).find('span.index').text();

		// update the pseudo selected element

		enhanced.find('.dropdown_toggle').empty().append('<span></span>').find('span').text($(this).find('span.value').text());
		enhanced.find('.dropdown_toggle').append('<span class="hidden selindex"></span>').find('span.selindex').text($(this).find('span.index').text());
		e.preventDefault();


		for (var i=0; i<ticketDropdowns.length; i++) {
			
			if (realselect.attr("id") == ticketDropdowns[i]) {
				updateCart();
				break;
			}
			
		}
		return false;
	});
	
	
	function updateCart() {
	
	    try {
		    //grabbing the id of the total field
		    var totalCartElementId = "#"+totalField;

		    var dropdownValuesArray;
    		
		    //checking if total input field is in the document
		    if ($(totalCartElementId).length) {
    			
			    // creating an array for dropdown values			
			    dropdownValuesArray = new Array();
    			
			    // for every dropdown push its value into the array
			    for (var a=0;a<ticketDropdowns.length; a++) {
				    var currentDropdownValue = $("#"+ticketDropdowns[a]).val();
				    dropdownValuesArray.push(currentDropdownValue);
			    }
    			
			    // here goes your setTotal calculations
			    var totalResult = 0;
    				
			    // for every dropdown there's a price, we multiply each dropdown value by corresponding price and sum it all up
			    for (var a=0; a<dropdownValuesArray.length; a++) {
			        totalResult += parseInt(dropdownValuesArray[a])*parseFloat(ticketPrices[a]);
			    }
    				
			    // we set the value of the total input field
			    $(totalCartElementId).val(parseFloat(totalResult).toFixed(2));
                setCartTotal(totalResult);
    	    }
	    }
	    catch(e) {
	    }

    }
        
    function setCartTotal(val){
        var total = parseFloat(val).toFixed(2);
        var totalEl = $("#"+totalElement);
        if($(totalEl).length > 0){
            totalEl.html("&euro; "+total);
        }
    }

    updateCart();
  	showLessTickets();
});



function showMoreTickets(){
    $(".ticketboxrow_mandatory").each( function(ind,el){
        $(el).show();
    });
    $(".ticketboxrow_optional").each( function(ind,el){
        $(el).show();
    });
    $(".ticketboxrow_show_less").each( function(ind,el){
        $(el).show();
    });
    $(".ticketboxrow_show_more").each( function(ind,el){
        $(el).hide();
    });
}

function showLessTickets(){
    $(".ticketboxrow_mandatory").each( function(ind,el){
        $(el).show();
    });
    $(".ticketboxrow_optional").each( function(ind,el){
        $(el).hide();
    });
    $(".ticketboxrow_show_less").each( function(ind,el){
        $(el).hide();
    });
    $(".ticketboxrow_show_more").each( function(ind,el){
        $(el).show();
    });
}
