
	
		 function getCookie(c_name) {
			if (document.cookie.length>0)
			  {
			  c_start=document.cookie.indexOf(c_name + "=");
			  if (c_start!=-1)
				{
				c_start=c_start + c_name.length+1;
				c_end=document.cookie.indexOf(";",c_start);
				if (c_end==-1) c_end=document.cookie.length;
				return unescape(document.cookie.substring(c_start,c_end));
				}
			  }
			return "";
		}

		function setCookie( name, value, expires, path, domain, secure ) {
			path = "/"
			// set time, it's in milliseconds
			var today = new Date();
			today.setTime( today.getTime() );
			
			/*
			if the expires variable is set, make the correct
			expires time, the current script below will set
			it for x number of days, to make it for hours,
			delete * 24, for minutes, delete * 60 * 24
			*/
			if ( expires )
			{
			expires = expires * 1000 * 60 * 60 * 24;
			}
			var expires_date = new Date( today.getTime() + (expires) );
			
			document.cookie = name + "=" +escape( value ) +
			( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
			( ( path ) ? ";path=" + path : "" ) +
			( ( domain ) ? ";domain=" + domain : "" ) +
			( ( secure ) ? ";secure" : "" );
		}
		
		function check_layout() {
			layout = getCookie('layout');
			return layout
		}
		
		function add_widget(title,content,footer,datasource,column,draggy) {
			zarf = ""
			zarf = portal.add(new Xilinus.Widget().setTitle(title).setContent(content).setFooter(footer).setDs(datasource), column, datasource, draggy);			
			onUpdate();
			//alert(zarf)
			con_ref='content_'+zarf
			var gCallCount = 0;
			new PeriodicalExecuter(function(pe) {
  			if (++gCallCount > 10)
				//alert("STOP");
				pe.stop();
			  else
				layoutChange();
				dataChange();
			}, 1);
			
		}
		
		function get_dataSource(ds,col) {
			title = ""
			content = ""
			pfooter = ""
			if(ds==0) {
				draggy = false
			}
			else {
				draggy = true
			}
			//alert(draggy)
			url = "getdata.php?id="+ds
			new Ajax.Request(url, {
			  onSuccess: function(transport) {
				 //alert(transport)
				 tdata = transport.responseText
				 data = tdata.split("|")
				 //alert("DATA:" + data)
				 title = data[0]
				 //alert("TITLE: "+title)
				 content = data[1]
				 pfooter = data[2]
				 add_widget(title,content,pfooter,ds,col,draggy)
			  }
			});
		}
		
		function get_defaults() {
			url = "getdefaults.php"
			new Ajax.Request(url, {
			  onSuccess: function(transport) {
				 //alert(transport)
				 tdata = transport.responseText
				 data = tdata.split("|")
				 for (i=0;i<data.length;i++) {
					final_data = data[i].split(',')
					get_dataSource(final_data[0],final_data[1])
				 
				 }
			  }
			});
		}


		 function onOverWidget(portal, widget) {
		   widget.getElement().insertBefore($('control_buttons'), widget.getElement().firstChild);
		   $('control_buttons').show(); 
		 } 
	   
		 function onOutWidget(portal, widget) {
		   $('control_buttons').hide();      
		 } 
	   
		 function removeWidget(element) {
		   var widget = $(element).up(".widget").widget;
		 
		  // if (confirm('Are sure to remove this widget?')) { 
			 document.body.appendChild($('control_buttons').hide())
			 
			 Effect.Fade(widget.getElement(), { afterFinish: function() { 
				portal.remove(widget);
				
			 }});
			 
			 
			 portal._updateColumnsHeight();
			 onUpdate()
		   //}
		 }
		 
		 function getwidgetid(element) {
		   var widget = $(element).up(".widget").widget;
		   barf = widget._getId();
		   //alert(barf);
		   
	
		 }
		 
		 function serialize(element) {
			 var ser=portal.serialize();
			 alert(ser);
		 }
																																							   
		function onChange() {
			//alert("CHANGE");
		  
		}
		
		function layoutChange() {
			portal.updateLayout();
		}
		
		function dataChange() {
			portal.fixem()
			layoutChange()
			//Effect.Fade($(widget))
			//alert("BYE")
		}
		
		function update_widgets() {
			//alert("time to make the donuts");
			blah = portal.getem();
			for (z=0;z<blah.length;z++) {
				zxobj = blah[z]
				//alert(zxobj)
				con_holder = 'content_'+blah[z]
				//$(con_holder).innerHTML = "TEST"
				datastore = 'ds_'+blah[z]
				//alert(datastore)
				data1 = $(datastore).innerHTML
				//Effect.Fade($(zxobj))
				var update_string = ""
				update_string = "refresh_data.php?id="+data1
				updater = new Ajax.Updater($(con_holder), update_string, {
					 method: 'get', evalScripts: false, onComplete: function() {
						var gCallCount = 0;
						new PeriodicalExecuter(function(ze) {
						if (++gCallCount > 10)
							//alert("STOP");
							ze.stop();
						  else
							dataChange();
						}, 1);
				}
				});
				
			}
		}
		
		
		function onUpdate() {
			narf = portal.breakem();
		   	var final_string = ""
			//alert(narf);
		   	for (i=0;i<narf.length;i++) {
				//alert(narf[i])
				temp = narf[i].split('|')
				work_string = ""
				final_string = final_string + "COLUMN:"+i+"|"
				for (z=0;z<temp.length-1;z++) {
					//alert(temp[z])	
					work_string = work_string + temp[z] + ":"
					booger='content_'+temp[z]
					booger_data = $(booger).innerHTML
					datastore='ds_'+temp[z]
					data1 = $(datastore).innerHTML
					work_string = work_string + data1 + "|"
				}
				final_string = final_string + work_string +"^"
			}
			//alert(final_string)
			setCookie('layout',final_string,365);

		}
		
		 function init() {             
		   portal = new Xilinus.Portal("#page div", {onOverWidget: onOverWidget, onOutWidget: onOutWidget, onChange: onChange, onUpdate: onUpdate, removeEffect: Effect.Fade}); 
		   // Fake widgets
		   var layit
		   layit = ""
		   		   
		   layit = check_layout();
		   
		   if(layit != "") {
			    cols = layit.split("^")
			  	for (z=0;z<cols.length-1;z++) {
					//alert("Column "+(z+1)+" Widgets")
					wids = cols[z].split('|')
					if(z==0) {
						get_dataSource(0,0)	
					}
					for (i=1;i<wids.length-1;i++) {
						
						ds_work = wids[i].split(":")
						datasource = ds_work[1]
						get_dataSource(datasource,z)
					}
				}
		   }
		   else {
				get_defaults() 
		   }
		   
		   
		   // Add controls buttons
		    portal.addWidgetControls("control_buttons");
			
			var gCallCount = 0;
			new PeriodicalExecuter(function(pe) {
  			if (++gCallCount > 10) {
				//alert("STOP");
				pe.stop();
			}
			else {
				layoutChange();
				//$('bbad').innerHTML = $('google_ads_div_bigboard').innerHTML
				//Move.element('bbad_hidden', 'bbad', 'copy');
				
			}
			}, 1);

			//new PeriodicalExecuter(layoutChange, 1);
			new PeriodicalExecuter(update_widgets,600);
			new ProtoFish('navmenu', '200', 'hover', false, false, true);	
				
		 }
		 
		function get_options(sorted) {
		   	holder = new Array()
			blah = portal.getem();
			if(!sorted) {
				sorted = getCookie('widget_sort')
			}
			if(!sorted) {
				sorted = "C"
			}
			setCookie('widget_sort',sorted,360)
			for (z=0;z<blah.length;z++) {
				datastore='ds_'+blah[z]
				//alert(datastore)
				data1 = $(datastore).innerHTML
				holder[z] = data1
			}
			var update_string = ""
			update_string = "getwidgets.php?exists="+holder+"&sorted="+sorted
			Modalbox.show(update_string, {title: "indycentric - add widgets to the big board", width: 800, height: 500}); return false;
	 	 }
		 
		 function hide_options() {
		   	 Effect.Fade($('options'))
		 }
		 
		 function add_new(ds,row_id) {
			 Effect.Fade($(row_id))
			 get_dataSource(ds,0)
			 
		 }
		 
		 function rated(event,user) {
			 event_memo = event.memo
			 id = event.memo.identity
			 rated = event.memo.rated
			 url = 'savestar.php?id='+id+'&vote='+rated+'&user='+user
			  new Ajax.Request(url, {
				parameters: event.memo,
				on200: function(xhr) {
				  //alert("YUP")
				  larf = xhr.responseText
				  //alert(larf)
				}
			  });

		 }
