
$(document).ready(function(){
    $('div#tickets .optionTitle h2').click(function(){
        $('div.subOptionGroup', $(this).parents('.optionGroup')).toggleClass('open');	
        //$('ul.li_of_options', $(this).parents('.optionGroup')).slideToggle();	
    });
    $('input:radio#pkg_basic').click(function() {
        $('div.optionGroup input:radio').val(profOptions);
        var basic_cost = $('div#exportpage div#e2w-export input#basic_cost').val();
	if (basic_cost)
            basic_cost = parseInt(basic_cost);
        else
            basic_cost = 100;
        calculateCostAndChangeText(basic_cost);
    });
    $('input:radio#pkg_high').click(function() {
        $('div.optionGroup input:radio').val(hiEndOptions);
        var basic_cost = $('div#exportpage div#e2w-export input#basic_cost').val();
	if (basic_cost)
            basic_cost = parseInt(basic_cost);
        else
            basic_cost = 100;
	calculateCostAndChangeText(basic_cost);
    });
    $('div.optionGroup input:radio').click(function(){
	    if (!pageCount) {
		pageCount = 1;
		$('#id_ticket-no_pages').val('1');
	    }
        thisGroup = $('div.optionGroup :radio[name=' + this.name + ']')
        chargeSelected = optionPriceDict[this.id];
        var costToSet = $($(this).next()).next().text();
        if(costToSet.replace(/^\s+/,"")) {
	    for (i = 0; i < thisGroup.length; i++) {
		charge = optionPriceDict[thisGroup[i].id] - chargeSelected;
		price = ((charge == 0) ? 'same price'  : ((charge < 0) ? ('-$' + charge*(-1)) : ('+$' + charge)));
		$($(thisGroup[i]).next()).next().text(price);
	    }
	    $($(this).next()).next().text('included');
	    operatorAndCost = costToSet.split('$', 2);
	    op = operatorAndCost[0];
	    price = operatorAndCost[1];
	    toAdd = (typeof(price) == 'undefined') ? 0 : ((op == '+') ? parseInt(price) : parseInt(price) * (-1));
	    cost += parseInt(toAdd);
            costToDisplay = Math.floor(cost + ((pageCount - 1)*cost)/2);
	    $("span#total_price").text('$'+costToDisplay);
	}
    });
    $('div#contactPage input#contact-ip').live("click", function(){
		$("textarea.contact_note").addClass("a_loader");
        $form = $(this).parent();
        var values = {};
	$form.children().children().children('input').each(function(){
	    values[$(this).attr('name')] = $(this).val();
	});
        var textArea = $form.children().children().children('textarea');
	values[textArea.attr('name')] = textArea.val();
	$.ajax({
		type: 'POST',
		url: $form.attr('action'),
		data: values,
		success: function(html) {                   
			$("div#contactPage span").remove();
			$("div#contactPage div.widgetTop").remove();
		    $('#contactPage').prepend(html);
		    var dbw = $("div#contactPage").width();
		    re_position(dbw);
		}
	    });
	return false;
	});	
    $("a.closeMe").live("click", function(){
        $("div#contactPage").hide();
        $("div.overlay").fadeOut("slow");	
    });
});


function calculateCostAndChangeText(basic_cost) {
    toChange = 0;
    cost = basic_cost;
    selectedOfAll = $('div.optionGroup input:radio[checked]');
    if (!pageCount) {
	pageCount = 1;
	$('#id_ticket-no_pages').val('1');
    }
    for (i = 0; i < selectedOfAll.length; i++) {
	toChange = 0;
        thisDOM = selectedOfAll[i];
        chargeSelected = optionPriceDict[thisDOM.id];
        thisGroup = $('div.optionGroup :radio[name=' + thisDOM.name + ']');
        for (j = 0; j < thisGroup.length; j++) {
            if (optionPriceDict[thisGroup[j].id] - chargeSelected) {
                toChange = 1;
		break;
	    }
	}
        if (toChange) {
	    for (j = 0; j < thisGroup.length; j++) {
		charge = optionPriceDict[thisGroup[j].id] - chargeSelected;
		price = ((charge == 0) ? 'same price'  : ((charge < 0) ? ('-$' + charge*(-1)) : ('+$' + charge)));
		$($(thisGroup[j]).next()).next().text(price);
	    }
	    $($(thisDOM).next()).next().text('included');
	}
	cost += chargeSelected;
    }
    costToDisplay = Math.floor(cost + ((pageCount - 1)*cost)/2);
    $("span#total_price").text('$' + costToDisplay);
}

function isValidEntry(event) {
    if (event.keyCode != 8 && (event.keyCode > 57 || event.keyCode < 48)) {
	if (oldVal) 
	    $('#id_ticket-no_pages').val(oldVal);
	else 
	    $('#id_ticket-no_pages').val('1');
    }
    pageCount = parseInt($('#id_ticket-no_pages').val());
    if (pageCount) {
	costToDisplay = Math.floor(cost + ((pageCount - 1)*cost)/2);
	$("span#total_price").text('$'+costToDisplay);
    }
}

function recordVal() {
    oldVal = $('#id_ticket-no_pages').val();
}

function show_db(url){
    if(!$.browser.msie){
	mtop = window.pageYOffset + 120;
	$("div#contactPage").css("top",mtop+"px");
    }
    $.ajax({
	url: url,
	cache: false,
	success: function(html){
		$("div.overlay").fadeIn("fast");
		$("div#contactPage").fadeIn(600);
		$("div#contactPage div.widgetTop").remove();
		$("div#contactPage").prepend(html);
		var dbw = $("div#contactPage").width();
                re_position(dbw);
	    }
    });	
};
   
function re_position(dbw){
    var sh = $(document).height();
    var sw = $(document).width();
    var mlv = (sw-dbw)/2;
    $("div.overlay").css("width",sw+"px");
    $("div.overlay").css("height",sh+"px");
    $("div#contactPage").css("left",mlv+"px");
}  
