/****************************************
 * sortPanels is used to move the panels and store their positions in a cookie.
 * It is called via a section's extra_script page element.
 * 
 * The syntax is "panel2-677|panel3-677|panel4-677 which is the panelID-sectionID
 * 
 */

var sortPanelCookieName = "panel_order_section";

$('document').ready(function(){
	// If this is a section front, then initialize
	if (templateType == 'section-front') {
		// Find the panels and add a class defined by the sectionID
		var panelIDlist = $('#panel2, #panel3, #panel4, #panel5, #panel6, #panel7');
		jQuery.each(panelIDlist, function(){
			var divHtml = $(this).html();
			if ((divHtml.match('div')) || ($(this).attr('id') == 'panel6')) {
				$(this).addClass('paneldiv');
				/**
				 * This is a kludge to catch the panel6 in IE. Currently, 
				 * IE does not pass this check. Maybe this is due to an HTML error
				 * in the marketplace panel sub components?
				 */
				if ($(this).attr('id')== 'panel6') {
				////	alert('panel6 being handled')
				}
			}
		});

		//define function for click on up arrow
			$('li.panelMoveUp a').click(
				function() {
	        		var curr = $(this).parents('div.paneldiv').attr('id');
	       			var prev = $("#"+curr).prev().attr('id');
	        		
	        		// swap                        
	        		$("#"+prev+" script").remove();           
	        		$("#"+prev).insertAfter("#"+curr);
	        		setFirstLast();
	        		setPanelOrder();
				}
			);
		//define function for click on down arrow
			$('li.panelMoveDown a').click(
				function() {
	        		var curr = $(this).parents('div.paneldiv').attr('id');
	        		var next = $("#"+curr).next().attr('id');
	        		        		
	        		// Swap
	        		$("#"+next+" script").remove();           
	        		$("#"+next).insertBefore("#"+curr);
	        		setFirstLast();
	        		setPanelOrder();
				}
			);
		//look for cookie and set the order of the panels
	    getPanelOrder();
	    //remove up arrow on first panel and down arrow on last panel
		setFirstLast();
	
	} // END if (section-front)
});

function setFirstLast(){
	alertTest('running setFirstLast()');
	$('div.paneldiv').removeClass('first').removeClass('last');
    $('div.paneldiv:first').addClass('first');
    $('div.paneldiv:last').addClass('last');
	}

/***********
 * Use this to store the cookie. This way the cookie can store panel locations
 * on mulitple pages identified by their section number
 */
function setPanelOrder(){
//	var panelDivJQ = $('div.paneldiv');
	var panelList = ''; // Use this to store the updated cookie

	// If there is an existing cookie, save it's contents correctly
	var panelOrderCookie = $.cookie(sortPanelCookieName);
	if (panelOrderCookie) {
		var panelIDandSection = panelOrderCookie.split("|");
	
		// Go through the existing list of panels and save the ones that are not this section
		for (i = 0; i < (panelIDandSection.length); i++) { //loop through the array of panelIDandSection
			var panelIDs = panelIDandSection[i].split("-"); // Get the Panel ID and it's section #
			// If the stored panel does not belong to this section, then save it w/o changing it
			if (panelIDs[1] != sectionID) {
				alertTest(panelIDs[1] + '!=' +  sectionID);
				panelList = panelList + panelIDs[0]+'-' + panelIDs[1] + '|';
			}
		}
	} // END if panelOrderCookie exists

	// Go through the list of panels on this page and save their settings
	jQuery.each($('div.paneldiv'), function(){
		panelList = panelList + $(this).attr('id')+'-' + sectionID + '|';
	});	
	// Strip the last pipe from the string
	panelListLength = panelList.length-1;
	panelList = panelList.substr(0,panelListLength);
	$.cookie(sortPanelCookieName, panelList, { expires: 365 });
}
function getPanelOrder(){
	var panelOrderCookie = $.cookie(sortPanelCookieName);
	if (!panelOrderCookie) {
		alertTest('No Cookie Found');
		return; //if no cookie is set, do nothing
	}
	var panelIDandSection = panelOrderCookie.split("|");
	for (i = 0; i < (panelIDandSection.length); i++) { //loop through the array of panelIDandSection
		var panelIDs = panelIDandSection[i].split("-"); // Get the Panel ID and it's section #
		if (panelIDs[1] == sectionID) {
			var curr = panelIDs[0];
	       	var next = panelIDandSection[i+1].split("-");
			// Set the correct order
			$("#"+next[0]+" script").remove();
	        $("#"+next[0]).insertAfter("#"+curr);
		}
	}
}


