var allBlocks = new Array();
var allBlocksCount = 0;

function GetCookie(sName) {
	var aCookie = document.cookie.split("; ");
	for (var i = 0; i < aCookie.length; i++) {
		var aCrumb = aCookie[i].split("=");
		if (sName == aCrumb[0])return aCrumb[1]
	}
	return null
}

function SetCookie(name, value, domain){
    var today = new Date();
    var expires = 1000 * 3600 * 24 * 365;

    document.cookie = name+'='+value+((expires) ? ';expires=' + new Date(today.getTime() + expires).toGMTString() : '') +';path= /'+((domain) ? ';domain=' + domain : '');
}

function DeleteCookie(name, path, domain){
    if(GetCookie(name)){
        SetCookie(name, '', -30, path, domain);
    }
}


var blocksShown = new Array();
var leftContentArray = new Array();
var rightContentArray = new Array();

var currentPortal;
var save_layout = "";

var blocksIdSting = "";
save_layout = function() {
	currentPortal = Ext.select(".x-portal").first();
	var firstColumn = currentPortal.select('.x-portal-column').first();
	var lastColumn = currentPortal.select('.x-portal-column').last();
	blocksIdSting = "";
	firstColumn.select('div.x-portlet').each(function(e) {
		blocksIdSting = blocksIdSting + e.dom.id + ",";
   	}, mainPortal);
	lastColumn.select('div.x-portlet').each(function(e) {
		blocksIdSting = blocksIdSting + e.dom.id + ",";
	}, mainPortal);
	DeleteCookie("BlockShownArray");
	SetCookie("BlockShownArray", blocksIdSting);
}


function getAllBlocks(){
	var allBlocksList = GetCookie("allBlocks", null);
		if (allBlocksList == null)
			allBlocks = listOfAllBlocks();
		else
		{
			allBlocksCount = 0;
			var tempAllBlocks = allBlocksList.split("/");
			tempAllBlocks = removeGivenValueFromArray(tempAllBlocks, '');
			for (var tempAllBlocksCount = 0; tempAllBlocksCount < tempAllBlocks.length; tempAllBlocksCount++) {
				allBlocks[allBlocksCount] = tempAllBlocks[tempAllBlocksCount].split(",");
				allBlocks[allBlocksCount] = removeGivenValueFromArray(allBlocks[allBlocksCount], '');
				allBlocksCount++;
			}
		}
}

function reArrangeAllBlocks(dragElementId, dragElementCurrentSide) {
	var generatedString = GetCookie("BlockShownArray");

	//fetch dragElementId in generatedString
	var previousIds = generatedString.substring(0,generatedString.indexOf(dragElementId)-1);
	var draggedBehindId = previousIds.substring(previousIds.lastIndexOf(",")+1,previousIds.length);

	var array1 = new Array();
	var array2 = new Array();
	
	var array3 = new Array();

	if (draggedBehindId == "") {
		for (var x = 0; x < allBlocks.length; x++) {
			if (allBlocks[x][0] == dragElementId) {
				array2 = allBlocks.splice(x, 1);
				array2[0][1] = dragElementCurrentSide;
			}
		}
		allBlocks = array2.concat(allBlocks);
	}
	else {
		for (var totalCount = 0; totalCount < allBlocks.length; totalCount++) {
			if (allBlocks[totalCount][0] == draggedBehindId)
			{
				array1 = allBlocks.slice(0, totalCount + 1);
				array3 = allBlocks.slice(totalCount + 1, allBlocks.length);
			}
		}

		for (var array1Count = 0; array1Count < array1.length; array1Count++) {
			if (array1[array1Count][0] == dragElementId) {
				array1.splice(array1Count, 1);
			}
		}
		for (var array3Count = 0; array3Count < array3.length; array3Count++) {
			if (array3[array3Count][0] == dragElementId) {
				array3.splice(array3Count, 1);
			}
		}
		for (var y = 0; y < allBlocks.length; y++) {
			if (allBlocks[y][0] == dragElementId)
			{
				array2 = allBlocks.splice(y, 1);
				array2[0][1] = dragElementCurrentSide;
			}
		}
		allBlocks = array1.concat(array2).concat(array3);
	}

	for (var z = 0; z < allBlocks.length; z++) {
		allBlocks[z][2] = "/";
	}
	DeleteCookie("allBlocks");
	SetCookie("allBlocks", allBlocks);
}

function listOfAllBlocks() {
	allBlocksCount = 0
	allBlocks = new Array();
	for (var blockOrder = 0; blockOrder < incidentsOrdering.length; blockOrder++) {
		if (incidentsOrdering[blockOrder].trim() == "FROM_STATION_UPDATES") {
			allBlocks[allBlocksCount] = new Array(3);
			allBlocks[allBlocksCount][0] = "generalUpdatesFromStationUpdates";
			allBlocks[allBlocksCount][1] = "left";
			allBlocks[allBlocksCount][2] = "/";
			allBlocksCount++
		}
		if (incidentsOrdering[blockOrder].trim() == "FROM_THIS_STATION_UG") {
			allBlocks[allBlocksCount] = new Array(3);
			allBlocks[allBlocksCount][0] = "generalUpdatesFromStationUndergroundUpdate";
			allBlocks[allBlocksCount][1] = "left";
			allBlocks[allBlocksCount][2] = "/";
			allBlocksCount++
		}

		if (incidentsOrdering[blockOrder].trim() == "ENGINEERING_WORKS") {
			allBlocks[allBlocksCount] = new Array(3);
			allBlocks[allBlocksCount][0] = "generalUpdatesEngineeringWorkUpdate";
			allBlocks[allBlocksCount][1] = "left";
			allBlocks[allBlocksCount][2] = "/";
			allBlocksCount++
		}

		if (incidentsOrdering[blockOrder].trim() == "LINE_PROBLEM")
		{
			allBlocks[allBlocksCount] = new Array(3);
			allBlocks[allBlocksCount][0] = "generalUpdatesLineUpdate";
			allBlocks[allBlocksCount][1] = "left";
			allBlocks[allBlocksCount][2] = "/";
			allBlocksCount++
		}

		if (incidentsOrdering[blockOrder].trim() == "CATERING_ALTERATIONS") {
			allBlocks[allBlocksCount] = new Array(3);
			allBlocks[allBlocksCount][0] = "generalUpdatesCateringAlteration";
			allBlocks[allBlocksCount][1] = "left";
			allBlocks[allBlocksCount][2] = "/";
			allBlocksCount++
		}

		if (incidentsOrdering[blockOrder].trim() == "TRAIN_CANCELLATIONS") {
			allBlocks[allBlocksCount] = new Array(3);
			allBlocks[allBlocksCount][0] = "generalUpdatesTrainCancellation";
			allBlocks[allBlocksCount][1] = "left";
			allBlocks[allBlocksCount][2] = "/";
			allBlocksCount++
		}

		if (incidentsOrdering[blockOrder].trim() == "OTHER_TRAIN_ALTERATIONS") {
			allBlocks[allBlocksCount] = new Array(3);
			allBlocks[allBlocksCount][0] = "generalUpdatesOtherTrainAlteration";
			allBlocks[allBlocksCount][1] = "left";
			allBlocks[allBlocksCount][2] = "/";
			allBlocksCount++
		}

		if (incidentsOrdering[blockOrder].trim() == "ALTERED_FORMATIONS")
		{
			allBlocks[allBlocksCount] = new Array(3);
			allBlocks[allBlocksCount][0] = "generalUpdatesAlteredFormations";
			allBlocks[allBlocksCount][1] = "left";
			allBlocks[allBlocksCount][2] = "/";
			allBlocksCount++
		}


		if (incidentsOrdering[blockOrder].trim() == "SERVICE_PROBLEM") {
			allBlocks[allBlocksCount] = new Array(3);
			allBlocks[allBlocksCount][0] = "generalUpdatesServiceUpdate";
			allBlocks[allBlocksCount][1] = "left";
			allBlocks[allBlocksCount][2] = "/";
			allBlocksCount++
		}

		if (incidentsOrdering[blockOrder].trim() == "UNDERGROUND_PROBLEM") {
			allBlocks[allBlocksCount] = new Array(3);
			allBlocks[allBlocksCount][0] = "generalUpdatesUndergroundUpdate";
			allBlocks[allBlocksCount][1] = "left";
			allBlocks[allBlocksCount][2] = "/";
			allBlocksCount++
		}


		if (incidentsOrdering[blockOrder].trim() == "OTHER_INCIDENT") {
			allBlocks[allBlocksCount] = new Array(3);
			allBlocks[allBlocksCount][0] = "generalUpdatesGeneralUpdate";
			allBlocks[allBlocksCount][1] = "left";
			allBlocks[allBlocksCount][2] = "/";
			allBlocksCount++
		}

		if (incidentsOrdering[blockOrder].trim() == "TO_THIS_STATION_UG") {
			allBlocks[allBlocksCount] = new Array(3);
			allBlocks[allBlocksCount][0] = "generalUpdatesToStationUndergroundUpdate";
			allBlocks[allBlocksCount][1] = "left";
			allBlocks[allBlocksCount][2] = "/";
			allBlocksCount++
		}

		if (incidentsOrdering[blockOrder].trim() == "STATION_UPDATES") {
			allBlocks[allBlocksCount] = new Array(3);
			allBlocks[allBlocksCount][0] = "generalUpdatesStationUpdates";
			allBlocks[allBlocksCount][1] = "left";
			allBlocks[allBlocksCount][2] = "/";
			allBlocksCount++
		}

		if (incidentsOrdering[blockOrder].trim() == "TO_STATION_UPDATES") {
			allBlocks[allBlocksCount] = new Array(3);
			allBlocks[allBlocksCount][0] = "generalUpdatesToStationUpdates";
			allBlocks[allBlocksCount][1] = "left";
			allBlocks[allBlocksCount][2] = "/";
			allBlocksCount++
		}
	}
			allBlocks[allBlocksCount] = new Array(3);
			allBlocks[allBlocksCount][0] = "liveDepartures";
			allBlocks[allBlocksCount][1] = "right";
			allBlocks[allBlocksCount][2] = "/";
			allBlocksCount++

			allBlocks[allBlocksCount] = new Array(3);
			allBlocks[allBlocksCount][0] = "liveArrivals";
			allBlocks[allBlocksCount][1] = "right";
			allBlocks[allBlocksCount][2] = "/";
			allBlocksCount++

			allBlocks[allBlocksCount] = new Array(3);
			allBlocks[allBlocksCount][0] = "currentRouteCO2Emission";
			allBlocks[allBlocksCount][1] = "right";
			allBlocks[allBlocksCount][2] = "/";
			allBlocksCount++

	        DeleteCookie("allBlocks");
			SetCookie("allBlocks", allBlocks);
			return allBlocks
}

function removeGivenValueFromArray(receivedArray, value) {
	var newArray = new Array();
	var newArrayCount = 0;
	for (var i = 0; i < receivedArray.length; i++) {if (receivedArray[i] != value)newArray[newArrayCount++] = receivedArray[i]}
	return newArray
}

function generateHeader(commonName, cls) {
	var portalHeader = new Ext.BoxComponent({applyTo:"portletDivHeading" + commonName, baseCls:cls});
	return portalHeader
}

function generateText(commonName, cls) {
	var poratlBody = new Ext.BoxComponent({applyTo:"portletDivBody" + commonName,style:'cursor:default', baseCls:cls});
	return poratlBody
}

function generatePortlet(commonName, side) {
	var generatedPortletDiv = new Ext.ux.Portlet({
		applyTo:commonName,
		id:commonName,
		autoHeight:true,
		title:'',
		footer : false,
		forceLayout : true,
		frame:false,
		baseCls:"mainSection",
		collapsible:false
	});
	return generatedPortletDiv
}


function jcheckSlideIn(divId, direction, duration, options) {
	Ext.get(divId).slideIn(direction, options);
	if(mainPortal!= null)
	mainPortal.fireEvent("expandCollapse", mainPortal, duration);
}

function jcheckSlideOut(divId, direction, duration, options) {
	Ext.get(divId).slideOut(direction, options);
	if(mainPortal!= null)
		mainPortal.fireEvent("expandCollapse", mainPortal, duration);
}

var minMainPortalHeight = 0;
function setMainPortalHeight() {
	if(Ext.isIE6 || Ext.isOpera)
	{
		var setToHeight = Ext.select("div.x-column-inner").first().dom.clientHeight;
		if(Ext.isIE6)
		{
			if (setToHeight < (minHeightForPotal))
				Ext.select("div.x-column-inner").first().setHeight(minHeightForPotal, {})
		}
		Ext.get("overlay").dom.style.height = (setToHeight < (minHeightForPotal) ? minHeightForPotal : setToHeight)+300;
	}
}

function addBlock(portalSide, blockId) {
	if (portalSide == 'leftSidePortal')
	{
		var leftPortlet = generatePortlet(blockId, "left");
		leftSidePortal.add(leftPortlet);
	}
	else {
		var rightPortlet = generatePortlet(blockId, "right");
		rightSidePortal.add(rightPortlet);
	}
}

function drawPortalLayout() {
	leftSidePortal = new Ext.ux.PortalColumn({
		stateful:true,
		scope:this,
		applyTo:'leftSection',
		collapsible:true,
		columnWidth:Ext.isIE6 ? .49 :.50,
		style:Ext.isIE6?"height:"+minHeightForPotal+"px;padding:0;padding-top:10px;padding-left:4px":"min-height:"+minHeightForPotal+"px;height:auto",
		extraCls: 'x-column contentLeft' ,
		forceLayout : true,
		autoHeight:true
	});
	rightSidePortal = new Ext.ux.PortalColumn({
		stateful:true,
		scope:this,
		applyTo:'rightSection',
		style:Ext.isIE6?"height:"+minHeightForPotal+"px;padding:0;padding-top:10px;padding-left:4px":"min-height:"+minHeightForPotal+"px;height:auto",
		collapsible:true,
		columnWidth:Ext.isIE6 ? .49 :.50,
		extraCls: 'x-column contentRight',
		forceLayout : true,
		autoHeight:true
	});

	for (var i = 0; i < leftContentArray.length; i++) {
		if (document.getElementById(leftContentArray[i]) != null) {
			addBlock('leftSidePortal', leftContentArray[i])
		}
	}
	for (var j = 0; j < rightContentArray.length; j++) {
		if (document.getElementById(rightContentArray[j]) != null) {
			addBlock('rightSidePortal', rightContentArray[j])
		}
	}

	mainPortal = new Ext.ux.Portal({
		border:false,
		applyTo:'mainDataDiv',
		autoScroll :false,
		items:[leftSidePortal,rightSidePortal],
		style:Ext.isIE6?"width:1000px;height:"+minHeightForPotal+"px;":"width:1000px;min-height:"+minHeightForPotal+"px;height:auto",
		header : false,
		monitorResize:false,
		width:1000,
		baseCls:'mainDataSection',
		listeners:{"expandCollapse":function(portal, timeout) {
			window.setTimeout("setMainPortalHeight();", timeout + 100);
		},
		render: function(){
		Ext.get("mainDataDivBody").setHeight = function(height, animate) {
				height = this.adjustHeight(height);
				if (!animate || !A) {
					return this;
				} else {
					this.anim({height:{to:height}}, this.preanim(arguments, 1));
				}
				return this
			}
		}}
	});

	removeGivenValueFromArray(blocksShown, '');

	jcheckContainerPanel = new Ext.Panel({
		border:false,
		applyTo:"jcheckContainerDiv",
		autoScroll :false,
		header:false,
		width:1000,
		autoHeight:true,
		baseCls:'jcheckDataContainer',
		items:[mainPortal]
	})

	setMainPortalHeight();

	save_layout.call(mainPortal);

	if (Ext.isIE) {
		Ext.select('div.mainSection-header').each(function(e) {
			var eWidth = e.dom.parentNode.clientWidth;
			e.dom.style.width = eWidth;
		});
	}
}

