var C1 = {
	Rotator : {
		repeat: {
			enabled : true,
			times: -1
		},
		delay : 5000,
		fade_time : 1000,
		link_text : '',
		selector : '',
		skel_selector : 'div#c1_marquee_skel',
		images : [],
		start_idx : 0,
		current_idx : 0,
		max_idx : 0,
	
		/**
		 * Initialize function, starts all our
		 * basic initialize routines, does NOT
		 * need to be wrapped in a document.ready
		 * call as it does that itself.
		 * 
		 * @author dstaley
		 */
		
		Initialize : function(options){
			C1.Rotator.selector = options.selector;
			C1.Rotator.link_text = options.link_text;
			//process the images found
			jQuery(this.selector+" > img").each(function(i,item){
				var img = new C1_Image(jQuery(item), options);
				C1.Rotator.images.push(img);
				jQuery(item).remove();
			});
			C1.Rotator.max_idx = C1.Rotator.images.length;
			//now lets initialize a few things
			C1.Rotator.start_rotator();
		},
		
		start_rotator : function(){
			//if they want to start at a particular index.
			if(C1.Rotator.start_idx != C1.Rotator.current_idx){
				C1.Rotator.current_idx = C1.Rotator.start_idx-1;
			}
			C1.Rotator.move_rotator();
			setInterval("C1.Rotator.move_rotator();", this.delay);
		},
		
		move_rotator : function(){
			var curr = C1.Rotator.current_idx;
			//we are at the end, start it over
			if(curr == C1.Rotator.max_idx){
				C1.Rotator.current_idx = 0;
			}
			var holder = jQuery(C1.Rotator.selector);
			var skel = C1.Rotator.process_skeleton(C1.Rotator.images[C1.Rotator.current_idx]);
			holder.html(skel.html());
			jQuery("img", holder).hide().fadeIn(this.fade_time);
			C1.Rotator.current_idx++;
		},
		
		process_skeleton : function(img){
			try{
				var skel = jQuery(C1.Rotator.skel_selector).clone();
				//process all the info..
				jQuery("img", skel).attr("src", img.src).attr("alt", img.alt);
				if(img.show_details){
					jQuery("span.details", skel).html(img.details);
					if(img.show_link){
						jQuery("span.link", skel).html(get_hyperlink(C1.Rotator.link_text));
					}else{
						jQuery("span.link", skel).html("").hide();
					}
				}else{
					jQuery("span.details", skel).html("").hide();
				}
				skel.hide();
				return skel;
			}
			catch(e){}
		}
	},
	UI : {
		setCurrentPage : function(name){
			jQuery("a#nav_"+name).addClass("current");
		},
		Init : function(){
			jQuery(document).ready(function(){
				jQuery(".tabbed").tabs();
			});
		}
	},
	Admin : {
		selectors : {
			editables : '.editable',
			editables_select : 'select.editable-list-select',
			editables_ul : 'ul.editable-list-ul',
			dialog : 'div#dialog',
			alert_dialog : 'div#notify',
			dialog_content : '#editable_content',
			inline_popup : '.inline-popup'
		},
		urls : {
			//will be set at runtime.
			save : ''
		},
		popup : {
			width: 1000,
			height: 600
		},
		current_type : null,
		current_section : '',
		current_elm : null,
		current_dialog_opener : null,
		Init : function(){
			jQuery(document).ready(function(){
				//setup the basic editable content
				jQuery(C1.Admin.selectors.editables).click(function(){
					var $this = jQuery(this);
					var val = $this.text();
					C1.Admin.current_elm = $this;
					C1.Admin.current_type = "text";
					C1.Admin.current_section = $this.attr("rel");
					//spawn our dialog, with the editable content
					C1.Admin.makeEditable($this, val);
					//prevent default behavior
					return false;
				});
				//setup the select editables
				jQuery(C1.Admin.selectors.editables_select).click(function(){
					var $this = jQuery(this);
					var val = '';
					C1.Admin.current_elm = $this;
					C1.Admin.current_type = "select";
					C1.Admin.current_section = $this.attr("rel");
					//now get all the options
					jQuery("option", $this).each(function(i,item){
						if(i > 0){
							val += "\n";
						}
						val += jQuery(item).text();
					});
					
					//spawn our dialog, with the editable content
					C1.Admin.makeEditable($this, val);
					//prevent default behavior
					return false;
				});
				//setup the list editables
				jQuery(C1.Admin.selectors.editables_ul).click(function(){
					var $this = jQuery(this);
					var val = '';
					C1.Admin.current_type = "ul";
					C1.Admin.current_elm = $this;
					C1.Admin.current_section = $this.attr("rel");
					//get all the children li's
					jQuery("li", $this).each(function(i,item){
						if(i > 0){
							val += "\n";
						}
						val += jQuery(item).text();
					});
					
					//spawn our dialog, with the editable content
					C1.Admin.makeEditable($this, val);
					//prevent default behavior
					return false;
				});
				
				//create the dialog object for our editables
				jQuery(C1.Admin.selectors.dialog).dialog({
					bgiframe: true,
					autoOpen: false,
					width: 450,
					modal: true,
					buttons: {
						'Cancel Changes': function() {
							$(C1.Admin.current_dialog_opener).removeClass("active");
							$(this).dialog('close');
						},
						'Save Changes': function() {
							//TODO
							C1.Admin.saveChanges();
							$(C1.Admin.current_dialog_opener).removeClass("active");
							$(this).dialog('close');
						}
					}
				});
				//create the dialog for our alerts
				jQuery(C1.Admin.selectors.alert_dialog).dialog({
					bgiframe: true,
					autoOpen: false,
					width: 300,
					buttons: {
						'Ok': function() {
							$(this).dialog('close');
						}
					}
				});
			});
		},
		makeEditable : function(elm, value){
			//highlight the currently selected one
			jQuery(C1.Admin.selectors.editables).removeClass("active");
			elm.addClass("active");
			C1.Admin.current_dialog_opener = elm;
			jQuery(C1.Admin.selectors.dialog_content).val(value);
			jQuery(C1.Admin.selectors.dialog).dialog('open');
		},
		//called when they have save.
		saveChanges : function(){
			//get our text value
			if(C1.Admin.current_section != null){
				var _text = jQuery(C1.Admin.selectors.dialog_content).val();
				var vars = {
					content : _text,
					block : C1.Admin.current_section
				};
				jQuery.postJSON(C1.Admin.urls.save, vars, function(data){
					if(data.status){
						//clear out the content field
						jQuery(C1.Admin.selectors.dialog_content).val('');
						C1.Admin.current_section = null;
					}
					//close the current dialog
					jQuery(C1.Admin.selectors.dialog).dialog('close');
					//open the alert dialog
					jQuery(C1.Admin.selectors.alert_dialog).dialog('title',data.title).html(data.message).dialog('open');
					if(data.content != null){
						switch(C1.Admin.current_type){
						case "text":
							jQuery(C1.Admin.current_elm).html(data.content);
							break;
						case "select":
							var lines = data.content.split("\n");
							var content = '';
							jQuery(lines).each(function(i,item){
								content += '<option>'+item+'</option>';
							});
							jQuery(C1.Admin.current_elm).html(content);
							break;
						case "ul":
							var lines = data.content.split("\n");
							var content = '';
							jQuery(lines).each(function(i,item){
								content += '<li>'+item+'</li>';
							});
							jQuery(C1.Admin.current_elm).html(content);
							break;
						
						}
					}
				});
			}
		},
		//called when they hit cancel button
		cancelChanges : function(){
			
		}
	}
};

C1.UI.Init();

//simple post version for jQuery json calls
jQuery.postJSON = function(url, data, callback) {
	jQuery.post(url, data, callback, "json");
};


/**
 * A simple object for each image
 * 
 * @author dstaley
 * @param instance
 * @param options
 * @return C1_Image
 */

function C1_Image(instance, options){
	this.attributes = jQuery(instance).attr('rel').split("::");
	this.name = get_attribute(0, this.attributes);
	this.details = get_attribute(1, this.attributes);
	this.link = get_attribute(2, this.attributes);
	this.show_link = (options.show_link != null) ? options.show_link : false;
	this.show_details = (options.show_details != null) ? options.show_details : false;
	this.src = jQuery(instance).attr("src");
	this.alt = jQuery(instance).attr("alt");
	return this;
}

function get_hyperlink(text){
	return '<a href="'+this.link+'">'+text+'</a>';
}

function get_attribute(idx, attrs){
	var cnt = attrs.length-1;
	if(idx <= cnt){
		return attrs[idx];
	}
	return null;
}
