﻿
Mortenson.Projects = function()
{
};

var Projects = Mortenson.Projects;
    
Mortenson.Projects.SubmitProductSearchFilterChange = function(ddl)
{
    var industry;
    var location;
    var size;
    var contextObj = {varddl: ddl};
    industry = document.getElementById('ddlIndustry').value;
    location = document.getElementById('ddlLocation').value;
    size = document.getElementById('ddlSize').value;
    Mortenson.AjaxAPI.MortensonAjax.ProjectFilterDropdownRetrieve(industry, location, size, Projects.SubmitProductSearchFilterChange_callback, contextObj);
};
Mortenson.Projects.SubmitProductSearchFilterChange_callback = function(res)
{
    if (res.error)
        alert(res.error.Message);
    else
    {
        var industry;
        var location;
        var size;
        
        var x = res.value.documentElement;
        var ddl = res.context.varddl;
        
        for (var i = 0, child; child = x.childNodes[i]; i++)
        {
            if (child.nodeName == 'DDLType')
            {
                switch(child.attributes[0].value)   //DDLType
                {
                    case 'Industry':
                        if (ddl == null || ddl.id != "ddlIndustry" || ddl.value == 0)
                            Projects.RebuildDDL(document.getElementById("ddlIndustry"), child);                        
                    break;
                    case 'Location':
                        if (ddl == null || ddl.id != "ddlLocation" || ddl.value == 0)
                            Projects.RebuildDDL(document.getElementById("ddlLocation"), child);                     
                    break;
                    case 'Size':
                        if (ddl == null || ddl.id != "ddlSize" || ddl.value == 0)
                            Projects.RebuildDDL(document.getElementById("ddlSize"), child);  
                    break;
                }
            }
        }
    }
};

Mortenson.Projects.RebuildDDL = function(ddl, child)
{
    var selectedVal = ddl.value;
    ddl.length = 0;
    Projects.BuildFilterDDL(ddl, child.childNodes);    
    ddl.value = selectedVal;
};

Mortenson.Projects.BuildFilterDDL = function(ddl, nodes)
{
    for (var i = 0; i < nodes.length; i++)
    {
        if (nodes[i].nodeName == 'option')
        {
            Projects.AddOption(ddl, nodes[i].childNodes[0].nodeValue, nodes[i].attributes[0].value);
        }
    }
};

Mortenson.Projects.ProjectSearchClear = function()
{
    document.getElementById("ddlIndustry").value = 0;
    document.getElementById("ddlLocation").value = 0;
    document.getElementById("ddlSize").value = 0;
//    Projects.ProjectSearchSubmit();
    Projects.SubmitProductSearchFilterChange(null);
    Projects.ProjectSearchSubmit();
};

Mortenson.Projects.AddOption = function(ddl, text, value)
{
    var optn = document.createElement("OPTION");
    optn.text = text;
    optn.value = value;
    ddl.options.add(optn);
};

Mortenson.Projects.ProjectSearchSubmit = function()
{
    var industry;
    var location;
    var size;
    industry = document.getElementById('ddlIndustry').value;
    location = document.getElementById('ddlLocation').value;
    size = document.getElementById('ddlSize').value;
    Mortenson.AjaxAPI.MortensonAjax.ProjectSearch(industry, location, size, 1, Projects.ProjectSearchSubmit_callback);
};
Mortenson.Projects.ProjectSearchSubmit_callback = function(res)
{
    if (res.error)
        alert(res.error.Message);
    else
    {
        window.location.hash = "filter";
        var d = Ext.DomQuery.selectNode('Projects', res.value);
        Ext.get('divSearchContent').update(d.firstChild.nodeValue);

        var j = Ext.DomQuery.selectNode('JSArray', res.value);
        var ar = j.firstChild.nodeValue.split(",");
        OWProjects = ar;
        RegenSifrHeaders();
    }   
};

Mortenson.Projects.ShowModal = function(pjID, posID)
{
    _currentArrPosition = posID;
    
    //if we have the div on the page use it, otherwise go get it
    if (document.getElementById('ProjectOverlay' + pjID))
    {
        Projects.ShowHideModal('ProjectOverlay' + pjID);
    }
    else
    {
        var contextObj = {varpjID: pjID}
        //Go get it from the server
        Mortenson.AjaxAPI.MortensonAjax.ProjectModalDivRetrieve(pjID, Projects.ProjectModalDivRetrieve_callback, contextObj);
    }
};
Mortenson.Projects.ProjectModalDivRetrieve_callback = function(res)
{
    if (res.error)
        alert(res.error.Message);
    else
    {
        var d = Ext.DomQuery.selectNode('data', res.value);
        
        var newDiv = new Ext.Element(document.createElement('div'));
        newDiv.update(d.firstChild.nodeValue);
        var parentProjectDiv = Ext.get('modalProjects');
        parentProjectDiv.appendChild(newDiv);
        
        Projects.ShowHideModal('ProjectOverlay' + res.context.varpjID);
    }    
};


Mortenson.Projects.ShowModalVDC = function(pjID, asID, posID, filetype)
{
    _currentArrPosition = posID;
    
    //if we have the div on the page use it, otherwise go get it
    if (document.getElementById('ProjectOverlay' + pjID))
    {
        Projects.DisplayVDCImage(pjID, asID, posID)
        Projects.ShowHideModal('ProjectOverlay' + pjID);
		Projects.VDCVideoSetup(pjID, asID, posID, filetype);
    }
    else
    {
        var contextObj = {varpjID: pjID, asID: asID, varposID: posID, vartype: filetype};
        //Go get it from the server
        Mortenson.AjaxAPI.MortensonAjax.ProjectModalDivRetrieve(pjID, Projects.ProjectModalDivRetrieveVDC_callback, contextObj);
    }
};
Mortenson.Projects.ProjectModalDivRetrieveVDC_callback = function(res)
{
    if (res.error)
        alert(res.error.Message);
    else
    {
        var d = Ext.DomQuery.selectNode('data', res.value);
        
        var newDiv = new Ext.Element(document.createElement('div'));
        newDiv.update(d.firstChild.nodeValue);
        var parentProjectDiv = Ext.get('modalProjects');
        parentProjectDiv.appendChild(newDiv);
        
        Projects.DisplayVDCImage(res.context.varpjID, res.context.asID, res.context.varposID)
        
        Projects.ShowHideModal('ProjectOverlay' + res.context.varpjID);
		
		Projects.VDCVideoSetup(res.context.varpjID, res.context.asID, res.context.varposID, res.context.vartype);
    }    
};

Mortenson.Projects.DisplayVDCImage = function(projID, asID, posID)
{
        // hide all the popup images
        $('.VDCAssetContainer').css('display', 'none');
		$('.VDCEventAsset').css('display', 'none');
        // display the specified container and larger image
        var projCtr = "#AssetContainer" + '_' + projID + '_' + asID;
        $(projCtr).css('display', 'block'); 
        var projDiv = "#EventAsset" + '_' + projID + '_' + asID + '_' + posID;
        $(projDiv).css('display', 'block'); 
};

Mortenson.Projects.VDCVideoSetup = function(projID, asID, posID, filetype)
{
	if (filetype == '.swf' || filetype == '.flv' || filetype == '.wmv')
	{
		var assetDiv = '#EventAsset_' + projID + '_' + asID + '_' + posID;
		var videoSetup = $(assetDiv).attr('VideoSetup');
		if (videoSetup != null)
		{
			eval(videoSetup);
		}
	}
};

Mortenson.Projects.ShowHideModal = function(ref)
{
    //div exists
    if (document.getElementById(ref).style.display == "block")
    {
        document.getElementById(ref).style.display = "none";
        document.getElementById('modalSubmission').style.display = "none";
		
		// close all open video containers  -- this is needed so IE stops playing video when popup is closed.
		Mortenson.Projects.RemoveVDCObjectTags();
		
    }else
    {
        document.getElementById(ref).style.top = getScrollY();
        document.getElementById(ref).style.display = "block";
        document.getElementById('modalSubmission').style.display = "block";
        window.onscroll = function () { document.getElementById('modalSubmission').style.top = getScrollY();};
    }
};

Mortenson.Projects.FlipAsset = function(d, isNext)
{
    var nextDiv = '';
    
    var a = getFormElementsArray(d, '1');
    
    //loop through the assets, turn them off.  when we find the "on" one, then turn on the next one
    for (i = 0; i < a.length; i++)
    {
        if (document.getElementById(a[i]).style.display == "block")
        {
            //asset is on.  turn off and show next one
            document.getElementById(a[i]).style.display = "none";
            
            if (isNext == 1)
            {
                if (document.getElementById(a[i+1]))
                    nextDiv = a[i+1];
                else
                    nextDiv = a[0]; //set to 1st
            }
            else
            {
                if (document.getElementById(a[i-1]))
                    nextDiv = a[i-1];
                else
                    nextDiv = a[a.length - 1]; //set to last            
            }            
        }
    }
    
    document.getElementById(nextDiv).style.display = "block";
};

Mortenson.Projects.FlipVDCAsset = function(d, isNext, eventID)
{
    var nextDiv = '';
    var videoSetup = '';
    var a = getFormElementsArray(d, eventID);
    
    //loop through the assets, turn them off.  when we find the "on" one, then turn on the next one
    for (i = 0; i < a.length; i++)
    {
        if (document.getElementById(a[i]).style.display == "block")
        {
            // asset is on.  if necessary, remove <object > tag, then turn off and show next one
			Mortenson.Projects.RemoveVDCObjectTags();
			document.getElementById(a[i]).style.display = "none";
			nextDiv = Mortenson.Projects.FindNextVDCLargeAsset(a, isNext, i);		
        }
    }
    
	videoSetup = $('#' + nextDiv).attr('VideoSetup');
	if (videoSetup != null)
	{
		eval(videoSetup);
	}
    document.getElementById(nextDiv).style.display = "block";
};


Mortenson.Projects.RemoveObjectTags = function ()
{
	$('#VideoPlayerFrame object').each(function()
		{
			var id = $(this).attr('id');
			swfobject.removeSWF(id);
			$('#VideoPlayerFrame').append('<div id="vcontainer">&nbsp;</div>');
		}
	);
}


Mortenson.Projects.RemoveVDCObjectTags = function ()
{
	$('.VDCEventAsset object').each(function()
		{
			var id = $(this).attr('id');
			var p = $(this).parent();
			swfobject.removeSWF(id);
			$(p).prepend('<div id="' + id + '" class="VDCVideoBox">&nbsp;</div>');
		}
	);
}

Mortenson.Projects.FindNextVDCLargeAsset = function (assetArray, isNext, current)
{
	var nextID = "";
	var i = 0;
	if (isNext)
	{
		// step forward
		i = current + 1;
		while (i > -1)
		{
			if (document.getElementById(assetArray[i]))
			{
				var curr = document.getElementById(assetArray[i]);
				if (!curr.getAttribute('emptyDiv'))
				{
					nextID = curr.getAttribute('id');
					break;
				}
				else
				{
					i++;
				}
			}
			else
			{
				if (i > (assetArray.length - 1))
				{
					i = 0;
				}
				else
				{
					i++;
				}
			}
		}
	}
	else 
	{
		// step backward
		i = current - 1;
		while (i > -10)
		{
			if (document.getElementById(assetArray[i]))
			{
				var curr = document.getElementById(assetArray[i]);
				if (!curr.getAttribute('emptyDiv'))
				{
					nextID = curr.getAttribute('id');
					break;
				}
				else
				{
					i--;
				}
			}
			else
			{
				if (i < 0)
				{
					i = assetArray.length;
				}
				else
				{
					i--;
				}
			}
		}
	}
	return nextID;
};

/*  Not needed right now  */
/*
Mortenson.Projects.JumpToVDCAsset = function(d, jumpTo, eventID)
{
    var jumpToDiv = '';
    var videoSetup = '';
	
	var jumpTarget = jumpTo-1;
	
    var a = getFormElementsArray(d, eventID);
    
    //loop through the assets, turn them off.  
    for (i = 0; i < a.length; i++)
    {
		if (i == jumpTarget)
		{
			jumpToDiv = document.getElementById(a[i]);
		}
        else if (document.getElementById(a[i]).style.display == "block")
        {
            //asset is on.  turn off 
            document.getElementById(a[i]).style.display = "none";            
        }
    }
	// show the next asset
	videoSetup = jumpToDiv.getAttribute('VideoSetup');
	if (videoSetup != null)
	{
		eval(videoSetup);
	}
    jumpToDiv.style.display = "block";
};
*/

Mortenson.Projects.MoveToProject = function(c, isNext)
{
    var nextPJ;

    if(isNext == 1)
    {
        if (_currentArrPosition == currentProjectArray.length - 1)
        {
            nextPJ = currentProjectArray[0]; //set to 1st
            _currentArrPosition = 0;
        }
        else
        {
            nextPJ = currentProjectArray[_currentArrPosition + 1];
            _currentArrPosition = _currentArrPosition + 1;
        }
    }
    else
    {
        if (_currentArrPosition == 0)
        {
            nextPJ = currentProjectArray[currentProjectArray.length - 1]; //set to last
            _currentArrPosition = currentProjectArray.length - 1;
        }
        else
        {
            nextPJ = currentProjectArray[_currentArrPosition - 1];
            _currentArrPosition = _currentArrPosition - 1;
        }
    }            

    
    document.getElementById('ProjectOverlay' + c).style.display = "none";
    Projects.ShowModal(nextPJ, _currentArrPosition);
};

