$.preload([ 'artists', 'news', 'contact', 'mailinglist', 'releases', 'merchandise', 'basket', 'mediaplayer', 'submissions', 'facebook', 'donate' ], {
    base:'widgets/img/',
    ext:'_widget_down.gif'
});

jQuery(document).ready(function(){

//jQuery("select, input:checkbox, input:radio, input:file").uniform();
//jQuery("select, input:checkbox, input:radio, input:file").uniform();


//jQuery(function(){ $("select").uniform(); });



//jQuery.get("widgets/widget-header.php", {title: "Messum's Noticeboard"}, function(data){ var newWidget = data; });
/* hide widgets at startup
jQuery(".content").each(function(){
	jQuery(this).hide();
});
*/

//jQuery("#news_li .content_container").hide();
//jQuery("#contact_li .content_container").hide();
//jQuery("#mailinglist_li .content_container").hide();
//jQuery("#releases_li .content_container").hide();
//jQuery("#artists_li .content_container").hide();
//jQuery("#merchandise_li .content_container").hide();
//jQuery("#basket_li .content_container").hide();
//jQuery("#mediaplayer_li .content_container").hide();
//jQuery("#submissions_li .content_container").hide();


//close widget
//jQuery("#dashboard_container .widget .header .header_buttons #close").live("click", function(){jQuery(this).parent().parent().parent().parent().remove();});

// minimize widget
jQuery("#dashboard_container .widget .header .header_buttons .min").toggle(
 function(){
	
	var thiswidget = jQuery(this).parent().parent().parent().parent();

   	jQuery(this).attr('src','widgets/img/'+thiswidget.attr('id')+'_down.gif');
	jQuery("#"+thiswidget.attr('id')+" .content_container").show(200);

}, function() { // same here

	var thiswidget = jQuery(this).parent().parent().parent().parent();

   	jQuery(this).attr('src','widgets/img/'+thiswidget.attr('id')+'_up.gif');
	jQuery("#"+thiswidget.attr('id')+" .content_container").hide();

});






//jQuery("ul#widget_col1").sortable({connectWith: ["#widget_col2", "#widget_col3"], placeholder: 'widget-placeholder',forcePlaceholderSize: true, revert: true, scroll: false, handle: jQuery(".widget .header"), update: function(){getOrder();}});

//jQuery("ul#widget_col2").sortable({connectWith: ["#widget_col1", "#widget_col3"], placeholder: 'widget-placeholder',forcePlaceholderSize: true, revert: true, scroll: false, handle: jQuery(".widget .header"), update: function(){getOrder();}});

//jQuery("ul#widget_col3").sortable({connectWith: ["#widget_col1", "#widget_col2"], placeholder: 'widget-placeholder',forcePlaceholderSize: true,revert: true, scroll: false, handle: jQuery(".widget .header"), update: function(){getOrder();}});


//noticeboard 
/*
jQuery("#noticeboard_widget #messages").load("widgets/noticeboard/get_notices.php");


jQuery("#noticeboard_widget textarea").keyup(function(){
	
	if(jQuery(this).val != ""){
		jQuery("#noticeboard_widget #post_button").removeClass("disabled");
	}
	else{
		jQuery("#noticeboard_widget #post_button").addClass("disabled");
	}
});
jQuery("#noticeboard_widget #post_button").live("click", function(){
	if(jQuery("#noticeboard_widget textarea").val() != ""){
		var notice = jQuery("#noticeboard_widget textarea").val();
		jQuery("#noticeboard_widget #post_button").addClass("disabled");
		jQuery("#noticeboard_widget textarea").val("");
		jQuery.get("widgets/noticeboard/add_notice.php", {notice: notice}, function(){
			jQuery("#noticeboard_widget #messages").load("widgets/noticeboard/get_notices.php");
		});
	}
});
*/
//addcustomer
jQuery("#addcustomer_toggle_address").live("click", function(){
	jQuery("#addcustomer #addcustomer_address").toggle()
	if(jQuery("#addcustomer #addcustomer_address").is(':visible')){
		jQuery("#addcustomer_toggle_address").attr({src:"img/li_arrow_down.gif"});	
	}
	else jQuery("#addcustomer_toggle_address").attr({src:"img/li_arrow_right.gif"});
});




jQuery("#addcustomer_submit").live("click", function(){
	
	jQuery("#addcustomer #feedback").html("<img src=\"img/ajax-loader-grey.gif\" alt=\"loading\" />");
	
	//get form variables
	
	var addcustomer_title = jQuery("#addcustomer_title").val();
	var addcustomer_first_name = jQuery("#addcustomer_first_name").val();
	var addcustomer_last_name = jQuery("#addcustomer_last_name").val();
	var addcustomer_suffix = jQuery("#addcustomer_suffix").val();
	var addcustomer_email = jQuery("#addcustomer_email").val();
	var addcustomer_phone = jQuery("#addcustomer_phone").val();
	var addcustomer_line1 = jQuery("#addcustomer_line1").val();
	var addcustomer_line2 = jQuery("#addcustomer_line2").val();
	var addcustomer_city = jQuery("#addcustomer_city").val();
	var addcustomer_county = jQuery("#addcustomer_county").val();
	var addcustomer_postcode = jQuery("#addcustomer_postcode").val();
	var addcustomer_country = jQuery("#addcustomer_country").val();
	var addcustomer_definitions = jQuery("#addcustomer_definitions").val();
	
	//get values from multiple select
	var addcustomer_definitions = "";
	jQuery("#addcustomer_definitions :selected").each(function(i, selected){
	    addcustomer_definitions += jQuery(selected).val() + ",";
	});

	//check first name, last name and email address are filled
	
	if(addcustomer_first_name != "" && addcustomer_last_name != "" && addcustomer_email != ""){
		jQuery.post("widgets/addcustomer/add_customer.php", {addcustomer_title : addcustomer_title, addcustomer_first_name:addcustomer_first_name, addcustomer_last_name:addcustomer_last_name, addcustomer_suffix:addcustomer_suffix, addcustomer_email:addcustomer_email, addcustomer_phone:addcustomer_phone, addcustomer_line1:addcustomer_line1, addcustomer_line2:addcustomer_line2, addcustomer_city:addcustomer_city, addcustomer_county:addcustomer_county, addcustomer_postcode:addcustomer_postcode, addcustomer_country:addcustomer_country, addcustomer_definitions:addcustomer_definitions}, function(data){
			jQuery("#addcustomer #feedback").html(data);
		});
	}

	
	
	else{
		jQuery("#addcustomer #feedback").html("First name, last name and email address fields must be filled out.");
	}

});


//notes

//jQuery("form#notes_form").autosave();



	/////////////////////////////////////////////////////////////////
	////////////									 ///////////////
	///////////  saves widget locations to array    ////////////////
	//////////								       ////////////////
	///////////////////////////////////////////////////////////////

// function that writes the list order to a cookie


function getOrder() {
	// save custom order to cookie
	
	var col1_widgets = jQuery("#widget_col1").sortable("toArray");
	var col2_widgets = jQuery("#widget_col2").sortable("toArray");
	var col3_widgets = jQuery("#widget_col3").sortable("toArray");
	
	jQuery.cookie("col1_cookie", col1_widgets, {path: "/" });
	jQuery.cookie("col2_cookie", col2_widgets, {path: "/" });
	jQuery.cookie("col3_cookie", col3_widgets, {path: "/" });
	
	var active_widgets = col1_widgets + "," + col2_widgets + "," + col3_widgets;
		
	jQuery(".addwidget").each(function(){
		if(active_widgets.search(jQuery(this).val()) != -1){
			jQuery(this).hide();
			
		}
	});
}

function loadWidget(col, name){
	var directory = "widgets/";
	var filename = name.replace("_li", "") + ".php";
	jQuery.get(directory + filename, function(data){
		var html = "<li id=\"" + name + "\">" + data + "</li>";
		jQuery("#widget_col" + col).prepend(html);
	});
}

jQuery("#widget_select").change(function(){ getOrder(); loadWidget(1, jQuery(this).val()); });

/*
function loadColumns(){
	//make array from data stored in column cookies
	var col1_cookie_array = jQuery.cookie("col1_cookie").split(",");
	var col2_cookie_array = jQuery.cookie("col2_cookie").split(",");
	var col3_cookie_array = jQuery.cookie("col3_cookie").split(",");
	
	//iterate over arrays and load widhets into each column
	jQuery.each(col1_cookie_array, function(){loadWidget(1, this);});
	jQuery.each(col2_cookie_array, function(){loadWidget(2, this);});
	jQuery.each(col3_cookie_array, function(){loadWidget(3, this);});
}*/



});//document end





