﻿//DRAG-N-DROP MODULE FUNCTIONS -------------------------------------------------------------------------------
//called after adding a new module to reset the elements of the page -- also add database update here based upon current serialization
function document_reset() {
		$('a.closeEl').bind('click', toggleContent);
		$('div.groupWrapper').Sortable(
			{
				accept: 'groupItem',
				helperclass: 'sortHelper',
				activeclass : 	'sortableactive',
				hoverclass : 	'sortablehover',
				handle: 'div.itemHeader',
				tolerance: 'pointer',
				onChange : function(ser)
				{
				//serialize, show the sort order using the serialize funtion - doug
				serialize('sort1');
				},
				onStart : function()
				{
					$.iAutoscroller.start(this, document.getElementsByTagName('body'));
				},
				onStop : function()
				{
					$.iAutoscroller.stop();
				}
			}
		);
		//serialize, show the sort order using the serialize funtion - doug
		serialize('sort1');
	}

//called after document is loaded to set drag functionality of all modules loaded onto the page
$(document).ready(
	function () {
		$('a.closeEl').bind('click', toggleContent);
		$('div.groupWrapper').Sortable(
			{
				accept: 'groupItem',
				helperclass: 'sortHelper',
				activeclass : 	'sortableactive',
				hoverclass : 	'sortablehover',
				handle: 'div.itemHeader',
				tolerance: 'pointer',
				onChange : function(ser)
				{
				//serialize, show the sort order using the serialize funtion - doug
				serialize('sort1');
				},
				onStart : function()
				{
					$.iAutoscroller.start(this, document.getElementsByTagName('body'));
				},
				onStop : function()
				{
					$.iAutoscroller.stop();
				}
			}
		);
	}
);

//hides, shows the contents of a module
var toggleContent = function(e)
{
	var targetContent = $('div.itemContent', this.parentNode.parentNode);
	if (targetContent.css('display') == 'none') {
		targetContent.slideDown(300);
		$(this).html('[-]');
	} else {
		targetContent.slideUp(300);
		$(this).html('[+]');
	}
	return false;
};

//returns the order of the modules
function serialize(s)
{
	serial = $.SortSerialize(s);
	var sort_order;
	//sort through the hash and add the order to the database here
	var x = serial.hash.split("&");
	var sort_order = "";
	for (var i=0; i<x.length; i++)
	{
		var y = x[i].split("=");
		if (i==0){
		    sort_order += y[1];
		}else{
		    sort_order += ',' + y[1];
		}
		    
		
	}
	//SORT_ORDER is a string of each module name, separated by (|).  These should be id's, not names, once stored in a database
	update_module_order(sort_order)
	//alert(sort_order);
};


//called to add a module to the main grid
//will have to use httpobject to call function to database to fill module with appropriate data and elements
function addElement(element_name, element_nice_name, module_link) {
  //create the new groupItem tag within the sort1 tag
  var ni = document.getElementById('sort1');
  var newdiv = document.createElement('div');
  var divIdName = element_name;
  newdiv.setAttribute('class', 'groupItem');
  newdiv.setAttribute('id',divIdName);
  ni.appendChild(newdiv);
  document.getElementById(element_name).className = 'groupItem'; 
  
  //create the new itemHeader tag within the tag made above
  var ni_header = document.getElementById(element_name);
  var newitemheader = document.createElement('div');
  newitemheader.setAttribute('class', 'itemHeader');
  newitemheader.setAttribute('id',divIdName + '1');
  newitemheader.innerHTML = element_name + ' Module <a href="#" class="closeEl">[-]</a>';
  ni_header.appendChild(newitemheader);
  document.getElementById(divIdName + '1').className = 'itemHeader';
  
  //create the new itemContent tag within the tag made above
  var ni_content = document.getElementById(element_name);
  var newitemContent = document.createElement('div');
  newitemContent.setAttribute('class', 'itemContent');
  newitemContent.setAttribute('id',divIdName + '2');
  //var on_click = element_name + "|" + element_nice_name + "|" + module_link ;
  //added by doug
  if (divIdName == "EventInformation"){
    //fill the innerhtml with a httpobject request to aj/mod_rsvp.aspx?Mode=preview or edit
    newitemContent.innerHTML = '<a href="/clients/aj/aj_eventinfo_edit.aspx" title="Event Information Edit" rel="gb_page_center[800, 480]">&nbsp;&nbsp;Click here</a> to add/edit Event Information.<br><br><a href="#" onclick=removeElement("' + element_name + '","' + module_link + '");>remove me</a></div>';
  }else if(divIdName == "PhotoAlbum"){
    newitemContent.innerHTML = '<a href="/clients/aj/aj_eventinfo_edit.aspx" title="Event Information Edit" rel="gb_page_center[800, 480]">&nbsp;&nbsp;Click here</a> to add/edit Event Information.<br><br><a href="#" onclick=removeElement("' + element_name + '","' + module_link + '");>remove me</a></div>';
  }else{
    newitemContent.innerHTML = 'This is the content. <div><a href="#" onclick=removeElement("' + element_name + '","' + module_link + '");>remove me</a></div>';
  
  }
  //doug
  
  
  
  
  ni_content.appendChild(newitemContent);
  document.getElementById(divIdName + '2').className = 'itemContent';
  
  //document.getElementById(module_link).setAttribute('onclick', '');
  //document.getElementById(module_link).style.color = 'grey';
  //document.getElementById(module_link).style.textdecoration = 'none';
  
  document.getElementById(module_link).style.visibility = 'hidden';
  //reset the document and serialize
  document_reset(); 
  
  
  
 
 

}

function removeElement(divNum, module_link) {
  var d = document.getElementById('sort1');
  var olddiv = document.getElementById(divNum);
  d.removeChild(olddiv);
  //document.getElementById(module_link).setAttribute('onclick', on_click);
  document.getElementById(module_link).style.visibility = 'visible';
  document.getElementById(module_link).style.color = '#ba5915';
  
  //serialize, show the sort order using the serialize funtion - doug
  serialize('sort1');
}
/*  /DRAG-DROP Modules   */





//sets the URLS for textual column, sms send, and email_video send

var url = "/clients/aj/aj_module_order.aspx"; // The server-side script


//when a new movie is clicked, this splits the returned title and text from server2.php, and writes them to the page

function handleHttpResponse() {



  if (http.readyState == 4) {


	//thise function is what needs to happen to page elements after the aspx file is called
    // Split the comma delimited response into an array



    //results = http.responseText.split("^^");



    //document.getElementById('title_text_change').innerHTML = results[0];



    //document.getElementById('body_text_change').innerHTML = results[1];
	
	//document.getElementById('uniquename1').style.display = "none";

	//document.getElementById('uniquename2').style.display = "none";

	//document.getElementById('video_sent').style.display = "block";



  } 



}







//sets the http object to allow http requests without reloading the page

function getHTTPObject() {



  var xmlhttp;



  /*@cc_on



  @if (@_jscript_version >= 5)



    try {



      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");



    } catch (e) {



      try {



        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");



      } catch (E) {



        xmlhttp = false;



      }



    }



  @else



  xmlhttp = false;



  @end @*/



  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {



    try {



      xmlhttp = new XMLHttpRequest();



    } catch (e) {



      xmlhttp = false;



    }



  }



  return xmlhttp;



}



// Creates the HTTP Object

var http = getHTTPObject(); 



//This is where the magic happens..here, it calls the aspx page, passing the serialization results through to the page

function update_module_order(sort_order) {



  	//var id = document.forms['smsform'].elements['id'].value;   //document.getElementById('id').value;

	//var name = document.forms['smsform'].elements['name'].value;   //var name = document.getElementById('name').value;

	//var smsto = document.forms['smsform'].elements['smsto'].value;   //var smsto = document.getElementById('smsto').value;

  	http.open("GET", url + "?sort_order=" + escape(sort_order), true);

	http.onreadystatechange = handleHttpResponse;

	http.send(null);



 }







var http = getHTTPObject(); 