// foo-bar-128 -> 128
function extract_id_from_string(str) {
    var last_dash_pos = str.lastIndexOf("-");
    return str.substr(last_dash_pos+1);
}

function fade_background() {
    setTimeout("$('#middle-bg').fadeOut(30000)",2000);
}

function fade_splash() {
    $('#splash-img').fadeIn(8000);
}

function observe_copy_billing_to_delivery() {
	$('#copy-billing-to-delivery').bind('click', function(e) {
		$("#customer_delivery_name").val($("#customer_name").val());
		$("#customer_delivery_street").val($("#customer_street").val());
		$("#customer_delivery_zip").val($("#customer_zip").val());
		$("#customer_delivery_city").val($("#customer_city").val());
		$("#customer_delivery_country").val($("#customer_country").val());
		return false;
	});
}

function observe_discolure_links() {
	// ► ▼
	$("a.content-disclosure-link").click(function(e) {
		var current_id 			= extract_id_from_string(this.id);
		var current_link		= $("#content-disclosure-link-"+current_id);
		var current_extended	= $("#extended-"+current_id);
		
		if(current_link.text()==="► …") {
			current_link.text("▼");
			current_extended.show();
		}
		else {
			current_link.text("► …");
			current_extended.hide();
		}
		return false;
	});
}

function init_facebox() {
    $('a.special').facebox({
      loading_image : '/images/facebox/loading.gif',
      close_image   : '/images/facebox/closelabel.gif'
    })
}

function observe_special_links() {
    //jQuery('a.special').facebox()
    
    $('a.special').click(function() {
        alert('click');
        $(this).attr("href", "");
        jQuery.facebox({ ajax: 'http://google.com' })
    })

    //$('a.special').each(function() {
    //    //$(this).attr('rel', 'facebox');
    //    $(this).facebox();
    //    // alert($(this).attr('rel'));
    //    // jQuery('a[rel*=facebox]').facebox()
    //    //jQuery($(this)).facebox();
    //});
    
    //jQuery('a.special').facebox()
}

$(document).ready(function () {
    init_facebox();
    //observe_special_links();
    fade_background();
    fade_splash();
	observe_copy_billing_to_delivery()
	observe_discolure_links();
});

