// Uretech Default JS include.
/*************************************
 * Written by Halogen Graphics
 * Copyright© Halogen Graphics 2010
 *
 * For more information please contact
 * us at studio@halogengraphics.co.za.
 *************************************/

function MM_openBrWindow(theURL,winName,features) { //v2.0
    window.open(theURL,winName,features);
}

function ChangeIFrameSrc(iframeId, src) {
	var iFrame = document.getElementById(iframeId);
	iFrame.src = src;	
}

function getVar(name)
{
    var get_string = document.location.search;
    var return_value = '';
         
    do
    { //This loop is made to catch all instances of any get variable.
    	var name_index = get_string.indexOf(name + '=');
            
        if(name_index != -1)
        {
            get_string = get_string.substr(name_index + name.length + 1, get_string.length - name_index);
              
            var end_of_value = get_string.indexOf('&');
            if(end_of_value != -1)                
                value = get_string.substr(0, end_of_value);
            else                
                var value = get_string;
                
            if(return_value == '' || value == '')
                 return_value += value;
            else
                 return_value += ', ' + value;
        }
    } while(name_index != -1)
            
    //Restores all the blank spaces.
    var space = return_value.indexOf('+');
    while(space != -1)
    { 
        return_value = return_value.substr(0, space) + ' ' +
        return_value.substr(space + 1, return_value.length);
							 
        space = return_value.indexOf('+');
    }
          
    return(return_value);        
}

function returnDocument() {
    var file_name = document.location.href;
    var end = (file_name.indexOf("?") == -1) ? file_name.length : file_name.indexOf("?");
    return file_name.substring(file_name.lastIndexOf("/")+1, end);
}

// Switch content and content header...
function switchContent(contentObject)
{	
    // Pull data from JSON object
    //var headerFilename = contentObject.headerFilename;
    //var contentFilename = contentObject.contentFilename;
    var contentType = contentObject.type;
    var contentPage = contentObject.page;
	
    if (contentPage == null || contentPage.lengh == 0)
        contentPage = "index.html";
	
    if (contentPage == returnDocument())
    {
        // Get DOM objects
        var headerImage = document.getElementById('contentHeaderImage');
        var contentFrame = document.getElementById('sideFrame');

        if (contentType == 'sales')
        {
            window.open('sales_temp.html','salesWindow','height=400,width=650');
        } else {
            // header image
            headerImage.src = 'images/content_headers/' + contentType + '_header.png';
            // frame content
            contentFrame.src = contentType + '.html';
        }

    } else {
        window.location.href = contentPage + "?ct=" + contentType;
    }

    CloseOpenMenus();
}

function LoadUrlMenuLink(obj, url, target) {
    obj.target = target;
    obj.href = url;
}

function checkGetVar()
{
    if (getVar('ct') != null && getVar('ct').length > 0)
    {
        switchContent({type: getVar('ct'), page: 'index.html'});
    }
}

/**
 * Fade an element up. Sets an elements visibility to true.
 */
function FadeUp(dom, increment, interval) {

    if (increment == null) {
        increment = 0.05;
    }
    if (interval == null) {
        interval = 10;
    }
    
    var opacity = 0;
    var ieOpacity = 0;

    var fader = function() {

        opacity += increment;
        ieOpacity = opacity * 100;

        if (opacity < 1) {
            dom.style.visibility = 'visible';
            dom.style.opacity = opacity;
            dom.style.filter = 'alpha(opacity=' + ieOpacity + ')';
        }
        if (opacity >= 1) {
            clearInterval(faderInterval);
        }
    }

    var faderInterval = setInterval(fader, interval);
}

/**
 * Fade an element down. Sets the elements visibility to false.
 */
function FadeDown(dom, increment, interval) {
    if (increment == null) {
        increment = 0.05;
    }
    if (interval == null) {
        interval = 10;
    }

    var opacity = 1;
    var ieOpacity = 100;

    var fader = function() {

        opacity -= increment;
        ieOpacity = opacity * 100;

        if (opacity < 1) {
            //dom.style.visibility = 'visible';
            dom.style.opacity = opacity;
            dom.style.filter = 'alpha(opacity=' + ieOpacity + ')';
        }
        if (opacity <= 0) {
            clearInterval(faderInterval);
            dom.style.visibility = 'hidden';
        }
    }

    var faderInterval = setInterval(fader, interval);
}

function AddMenus() {
    var profileMenu = document.createElement('div');
    var systemMenu = document.createElement('div');
    profileMenu.id = 'profileMenu';
    profileMenu.className = 'profileMenu';
    profileMenu.innerHTML = '<a href="javascript:switchContent({type: \'profile\', page:\'index.html\'})"><img src="images/dropdowns/profile_off.png" name="profileButton" width="200" height="16" vspace="3" border="0" id="profileButton" onmouseover="MM_swapImage(\'profileButton\',\'\',\'images/dropdowns/profile_on.png\',1)" onmouseout="MM_swapImgRestore()" onclick="HideMenu(\'profileMenu\')" /></a><a href="http://sites.google.com/a/ngage.co.za/mediazone/uretech" target="_blank"><img src="images/dropdowns/news_off.png" name="newsButton" width="200" height="16" vspace="3" border="0" id="newsButton" onmouseover="MM_swapImage(\'newsButton\',\'\',\'images/dropdowns/news_on.png\',1)" onmouseout="MM_swapImgRestore()" onclick="HideMenu(\'profileMenu\')" /></a><a href="javascript:switchContent({type: \'vm\', page: \'index.html\'})"><img src="images/dropdowns/VM_off.png" name="VMButton" width="200" height="16" vspace="3" border="0" id="VMButton" onmouseover="MM_swapImage(\'VMButton\',\'\',\'images/dropdowns/VM_on.png\',1)" onmouseout="MM_swapImgRestore()" /></a><a href="javascript:switchContent({type: \'bee\', page: \'index.html\'})"><img src="images/dropdowns/bbbee_off.png" name="bbbeeButton" width="200" height="16" vspace="3" border="0" id="bbbeeButton" onmouseover="MM_swapImage(\'bbbeeButton\',\'\',\'images/dropdowns/bbbee_on.png\',1)" onmouseout="MM_swapImgRestore()" /></a><a href="javascript:switchContent({type: \'csi\', page: \'index.html\'})"><img src="images/dropdowns/csi_off.png" name="csiButton" width="200" height="16" vspace="3" border="0" id="csiButton" onmouseover="MM_swapImage(\'csiButton\',\'\',\'images/dropdowns/csi_on.png\',1)" onmouseout="MM_swapImgRestore()" /></a><a href="javascript:switchContent({type:\'quality\', page:\'index.html\'})"><img src="images/dropdowns/quality_off.png" name="qualityButton" width="200" height="16" vspace="3" border="0" id="qualityButton" onmouseover="MM_swapImage(\'qualityButton\',\'\',\'images/dropdowns/quality_on.png\',1)" onmouseout="MM_swapImgRestore()" /></a><a href="javascript:switchContent({type:\'safety\', page:\'index.html\'})"><img src="images/dropdowns/safety_off.png" name="safetyButton" width="200" height="16" vspace="3" border="0" id="safetyButton" onmouseover="MM_swapImage(\'safetyButton\',\'\',\'images/dropdowns/safety_on.png\',1)" onmouseout="MM_swapImgRestore()" /></a>';
    document.body.appendChild(profileMenu);
/*    systemMenu.id = 'systemMenu';
    systemMenu.className = 'systemMenu';
    systemMenu.innerHTML = '<a href="javascript:switchContent({type:\'quality\', page:\'index.html\'})"><img src="images/dropdowns/quality_off.png" name="qualityButton" width="150" height="16" vspace="3" border="0" id="qualityButton" onmouseover="MM_swapImage(\'qualityButton\',\'\',\'images/dropdowns/quality_on.png\',1)" onmouseout="MM_swapImgRestore()" /></a><a href="javascript:switchContent({type:\'safety\', page:\'index.html\'})"><img src="images/dropdowns/safety_off.png" name="safetyButton" width="150" height="16" vspace="3" border="0" id="safetyButton" onmouseover="MM_swapImage(\'safetyButton\',\'\',\'images/dropdowns/safety_on.png\',1)" onmouseout="MM_swapImgRestore()" /></a>';
    document.body.appendChild(systemMenu);*/

    var resultsDiv = document.createElement('div');
    resultsDiv.id = 'resultsDiv';
    resultsDiv.className = 'resultsDiv';
    document.body.appendChild(resultsDiv);
}

/**
 * Find the position of an element on the page.
 */
function FindElementPosition(elementID) {
    var eLeft = 0;
    var eRight = 0;

    var element = document.getElementById(elementID);

    if (element.offsetParent) {
        do {
            eLeft += element.offsetLeft;
            eRight += element.offsetTop;
        } while (element = element.offsetParent);
    }
    return [eLeft, eRight];
}

function MoveMenu(menuID, buttonID) {
    var menu = document.getElementById(menuID);
    var buttonPos = FindElementPosition(buttonID);

    menu.style.left = (buttonPos[0] + 14) + 'px';
    menu.style.top = (buttonPos[1] + 29) + 'px';
}

function MoveMenus() {
    MoveMenu('profileMenu', 'profileImage');
    MoveMenu('systemMenu', 'systemImage');
}

function CloseOpenMenus() {
    var profileMenu = document.getElementById('profileMenu');
    var systemMenu = document.getElementById('systemMenu');

    if (profileMenu.style.visibility == 'visible') {
        FadeDown(profileMenu);
    }
/*    if (systemMenu.style.visibility == 'visible') {
        FadeDown(systemMenu);
    }*/
}

function ShowMenu(menuID, buttonID) {
    var menu = document.getElementById(menuID);
    MoveMenu(menuID,buttonID);
    FadeUp(menu);
}

function HideMenu(menuID) {
    var menu = document.getElementById(menuID);
    FadeDown(menu);
}

function ToggleMenu(menuID) {
    CloseOpenMenus();
    
    var menu = document.getElementById(menuID);

    if (menuID == 'profileMenu') {
        if (menu.style.visibility != 'visible') {
            MoveMenu('profileMenu','profileImage');
            FadeUp(menu);
        } else {
            FadeDown(menu);
        }
    }
    if (menuID == 'systemMenu') {
        if (menu.style.visibility != 'visible') {
            MoveMenu('systemMenu','systemImage');
            FadeUp(menu);
        } else {
            FadeDown(menu);
        }
    }
}

function WebSearch() {
    var searchTextDOM = document.getElementById('searchText');
    var resultsDiv = document.getElementById('resultsDiv');

    var ajax = new AJAX();
    ajax.AsynchronousGet('search.php?st=' + searchTextDOM.value);

    ajax.OnRequestComplete = function(response) {
        resultsDiv.innerHTML = response;
        FadeUp(resultsDiv);
    }
}

function CloseSearch() {
    var resultsDiv = document.getElementById('resultsDiv');
    FadeDown(resultsDiv);
}
