﻿//This code was created by the fine folks at Switch On The Code - http://blog.paranoidferret.com
//This code can be used for any purpose

function animate(elementID, newLeft, newTop, newWidth, newHeight, time, callback) {
    document.getElementById(elementID).style.display = "block";
    var el = document.getElementById(elementID);
    if (el == null)
        return;

    var cLeft = parseInt(el.style.left);
    var cTop = parseInt(el.style.top);
    var cWidth = parseInt(el.style.width);
    var cHeight = parseInt(el.style.height);

    var totalFrames = 1;
    if (time > 0)
        totalFrames = time / 40;

    var fLeft = newLeft - cLeft;
    if (fLeft != 0)
        fLeft /= totalFrames;

    var fTop = newTop - cTop;
    if (fTop != 0)
        fTop /= totalFrames;

    var fWidth = newWidth - cWidth;
    if (fWidth != 0)
        fWidth /= totalFrames;

    var fHeight = newHeight - cHeight;
    if (fHeight != 0)
        fHeight /= totalFrames;



    doFrame(elementID, cLeft, newLeft, fLeft, cTop, newTop, fTop, cWidth, newWidth, fWidth, cHeight, newHeight, fHeight, callback);
}

function doFrame(eID, cLeft, nLeft, fLeft, cTop, nTop, fTop, cWidth, nWidth, fWidth, cHeight, nHeight, fHeight, callback) {
    var el = document.getElementById(eID);
    if (el == null)
        return;

    cLeft = moveSingleVal(cLeft, nLeft, fLeft);
    cTop = moveSingleVal(cTop, nTop, fTop);
    cWidth = moveSingleVal(cWidth, nWidth, fWidth);
    cHeight = moveSingleVal(cHeight, nHeight, fHeight);

    el.style.left = Math.round(cLeft) + 'px';
    el.style.top = Math.round(cTop) + 'px';
    el.style.width = Math.round(cWidth) + 'px';
    el.style.height = Math.round(cHeight) + 'px';

    if (cLeft == nLeft && cTop == nTop && cHeight == nHeight && cWidth == nWidth) {
        if (callback != null)
            callback();
        return;
    }
    setTimeout('doFrame("' + eID + '",' + cLeft + ',' + nLeft + ',' + fLeft + ',' + cTop + ',' + nTop + ',' + fTop + ',' + cWidth + ',' + nWidth + ',' + fWidth + ',' + cHeight + ',' + nHeight + ',' + fHeight + ',' + callback + ')', 40);
}

function moveSingleVal(currentVal, finalVal, frameAmt) {

    if (frameAmt == 0 || currentVal == finalVal)
        return finalVal;

    currentVal += frameAmt;
    if ((frameAmt > 0 && currentVal >= finalVal) || (frameAmt < 0 && currentVal <= finalVal)) {
        return finalVal;
    }
    return currentVal;
}

var id, previd, flagmenu

function DataCheck(id) {
    try {
        document.searchfrm.q.value = document.searchfrm.q.value.replace(/(\r\n|\n|\r)/gm, "");
        if (document.searchfrm.q.value.length > 100) {
            document.searchfrm.q.value = document.searchfrm.q.value.substring(0, 100);
        };

    } catch (e) {

    }
    if (document.searchfrm.q.value != "") {
        searchValue = document.searchfrm.q.value;
        if (id == "web" || id == "web2") document.searchfrm.action = 'WebsearchResults.aspx?q=' + searchValue;
        if (id == "images" || id == "images2") document.searchfrm.action = 'ImagesSearchResultsJS.aspx?q=' + searchValue;
        if (id == "video" || id == "video2") document.searchfrm.action = 'VideoSearchResults.aspx?q=' + searchValue;
        if (id == "twitter" || id == "twitter2") document.searchfrm.action = 'TwitterSearchResults.aspx?q=' + searchValue;
        if (id == "shopping" || id == "shopping2") document.searchfrm.action = 'ShoppingSearchResults.aspx?q=' + searchValue;
        document.searchfrm.submit();
        return true;
    }

    else {
        return false;
    }
}


function slide(elementId, headerElement, moreText, fewerText) {
    var element = document.getElementById(elementId);
    cookiestatus = checkcookie("menustatus")
    if (cookiestatus == null || cookiestatus == true) {
        element.down = false;
        element.up = true;
    }
    else {
        element.up = false;
        element.down = true;
    }

    if (element.down == true) {
        animate(elementId, 0, 30, 147, 0, 150, null);
        element.up = true;
        element.down = false;
        headerElement.innerHTML = '<span class="sicon moreicon"><img src="images/spacer.gif" width="22" height="22" alt="More" /></span>' + moreText;
    } else {
        animate(elementId, 0, 30, 147, 30, 150, null);
        element.down = true;
        element.up = false;
        headerElement.innerHTML = '<span class="sicon fewericon"><img src="images/spacer.gif" width="22" height="22" alt="Fewer" /></span>' + fewerText;
    }
    createCookie("menustatus", element.up)
}

function switchLanguage() {
    var cookie = readCookie('search_localized');
    if (cookie == null || cookie == "" || cookie == "true") {
        createCookie('search_localized', false, 30);
    }

    else {
        createCookie('search_localized', true, 30);
    }

    window.location.reload();
}

function createCookie(name, value, days) {
    //days="3650";
    //	domain=";domain=.start.searchcomplete.com";
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();
    }
    else var expires = "";

    document.cookie = name + "=" + value + expires;
}

function readCookie(cookieName) {
    var theCookie = "" + document.cookie;
    var ind = theCookie.indexOf(cookieName);
    if (ind == -1 || cookieName == "") return "";
    var ind1 = theCookie.indexOf(';', ind);
    if (ind1 == -1) ind1 = theCookie.length;
    return unescape(theCookie.substring(ind + cookieName.length + 1, ind1));
}

function checkcookie(name) {
    var x = readCookie(name);
    if (x == "true" || x == null || x == "") {
        return true;
    }
    else if (x != "") {
        return false;
    }
}

function mouseOverShoppingImage(img, url) {
    /*document.getElementById("bigShoppingDiv").style.display = '';
    document.getElementById("bigShoppingImg").src = img.src;
    document.getElementById("bigShoppingHref").innerHTML = img.alt;
    document.getElementById("bigShoppingHref").href = url;*/
}

function showShoppingDiv() {
    //document.getElementById("bigShoppingDiv").style.display = '';
}

function AddScript(src) {
    var script = document.createElement('script');
    script.src = src;
    script.type = 'text/javascript';
    script.charset = 'utf-8'; // Safari bug;
    script = document.getElementsByTagName('head')[0].appendChild(script);
}

