﻿/* Product Navigation Scripts */
var scrollObj;
var scrollMenu;

scrollDistance = 100;
scrollSpeed = 5;
scrollTimer = 25;
scrollMouseFlag = false;
scrollFlag = false;
scrollDirection = 0;
scrollStop = 0;
scrollMenuSize = 0;
scrollWidth = 500;
scrollMenu = 0;

function initializeScrollNew(Menu, SelectedProduct, ProductCount) {
    // Force safari to calculate page dimensions
    var ignoreMe = document.body.offsetWidth;
    var scrollWidth = 100;
    var leftPos = 0;
    var rightPos = 4 * scrollWidth;
    var menu = Menu ? Menu : 0;
    var menuWidth = ProductCount ? ProductCount * scrollWidth : 0;
    var scrollPosition = SelectedProduct ? (SelectedProduct - 1) * scrollWidth : 0;

    if (document.getElementById) {
        // look for scroll position cookie
        var cookie = getCookie('MenuScroll');
        if (cookie) {
            var values = cookie.split(':');
            if (parseInt(values[0]) == menu) {
                leftPos = parseInt(values[1]);
            }
        }
            
        if (scrollPosition >= leftPos && scrollPosition <= (leftPos + rightPos)) {
            scrollPosition = leftPos;
        }
                   
        scrollObj = document.getElementById('ProductWindow');
        scrollMenu = menu;
        scrollObj.scrollLeft = scrollPosition;
        scrollMenuSize = menuWidth;

        // Initialize Buttons
        scrollButtons(scrollPosition);
        scrollCookie(scrollPosition);
    }
}

function beginScroll(Direction) {
    if (scrollObj) {
        if (!scrollFlag) {
            scrollDirection = Direction;
            scrollMouseFlag = true;
            scrollFlag = true;
            scrollHorizontal();
        }
    }
}

function cancelScroll() {
    scrollMouseFlag = false;
}

function scrollHorizontal() {
    if (scrollObj) {
        posLeft = parseInt(scrollObj.scrollLeft);
        if (!scrollStop) {
            scrollStop = posLeft + (scrollDirection * scrollDistance);
            if (scrollStop <= 0) scrollStop = 0;
            if (scrollStop >= scrollObj.scrollWidth - scrollObj.clientWidth) scrollStop = scrollObj.scrollWidth - scrollObj.clientWidth;
        }

        posLeft += scrollSpeed * scrollDirection;
        if (scrollDirection == -1 && posLeft < scrollStop) posLeft = scrollStop;
        if (scrollDirection == 1 && posLeft > scrollStop) posLeft = scrollStop;
        scrollObj.scrollLeft = posLeft;

        if (posLeft != scrollStop) {
            scrollTimeout = setTimeout('scrollHorizontal();', scrollTimer);
        } else {
            scrollStop = 0;
            if (scrollMouseFlag == true) {
                if (posLeft > 0 && posLeft < (scrollObj.scrollWidth - scrollObj.clientWidth)) {
                    scrollTimeout = setTimeout('scrollHorizontal();', scrollTimer);
                } else {
                    scrollFlag = false;
                    scrollCookie(posLeft);
                    scrollButtons(posLeft);
                }
            } else {
                scrollFlag = false;
                scrollCookie(posLeft);
                scrollButtons(posLeft);
            }
        }
    }
}

function scrollButtons(scrollPosition) {
    var leftButton = document.getElementById('NavButtonLeft');
    var rightButton = document.getElementById('NavButtonRight');

    // Set Left Button
    if (scrollPosition > 0) {
        leftButton.style.display = '';
    } else {
        leftButton.className = '';
        leftButton.style.display = 'none';
    }

    // Set Right Button
    if (scrollPosition < (scrollMenuSize - scrollWidth)) {
        rightButton.style.display = '';
    } else {
        rightButton.className = '';
        rightButton.style.display = 'none';
    }
}

function scrollHover(navButton, navType) {
    navButton.className = (navType == 1) ? 'NavHover' : '';
}

function scrollCookie(posLeft) {
    setCookie('MenuScroll', scrollMenu + ':' + posLeft, null, '/', null, false);
}

//function PositionScroll(leftPosition) {
//    PL = document.getElementById('ProductWindow');
//    PL.scrollLeft = leftPosition;
//}

// Open Image Viewer
function OpenImageViewer(Product) {
    var height = (screen.height - 570) / 2;
    var width = (screen.width - 670) / 2;
    window.open(Product, 'ImageViewer', 'location=no, menubar=no, resizable=no, scrollbars=no, status=no, titlebar=no, toobar=no, directories=no, height=570, width=670, top=' + height + ', left=' + width);
}

// Set Eula Cookie and redirect to download document
function ViewDownload() {
    document.cookie = 'AgreedToEula=1';
    window.open(window.location);
}

// Comic Book Viewer
function OpenCB(iVol) {
    window.open('ComicBookViewer.aspx?Book=' + iVol, '_blank', 'location=no,scrollbars=no,menubar=no,height=590,width=435');
}

/* Search Scripts */
var searchText = '';
var searchPrompt = ''

function toggleSearch(searchField, type) {
    var textField = document.getElementById('SearchTerm');
    var textWindow = document.getElementById('SearchWindow');

    if (searchPrompt == '') searchPrompt = textField.value;
    
    if (type == 1) {
        textField.value = searchText;
        setCursorAtEnd(textField);
    } else {
        if (textField.value.length > 0) {
            searchText = textField.value;
        } else {
            textField.value = searchPrompt;
            searchText = '';
        }
    }

    var style = (type == 0) ? 'Inactive' : 'Active';

    if (textField) textField.className = style;
    if (textWindow) textWindow.className = style;
}

function submitSearch() {
    var textField = document.getElementById('SearchTerm');
    if (searchPrompt == '') searchPrompt = textField.value;

    if (textField) {
        if (textField.value == searchPrompt) textField.value = '';
    }
    return true;
//        return (textField.value == searchPrompt || textField.value.length <= 0)
//            ? false : true;
//    }
}

function setCursorAtEnd(oTextbox) {
    if (oTextbox.createTextRange) {
        var r = (oTextbox.createTextRange());
        r.moveStart('character', (oTextbox.value.length));
        r.collapse();
        r.select();
    }
}

/* Cookie Functions 

name - name of the cookie
value - value of the cookie
[expires] - expiration date of the cookie
(defaults to end of current session)
[path] - path for which the cookie is valid
(defaults to path of calling document)
[domain] - domain for which the cookie is valid
(defaults to domain of calling document)
[secure] - Boolean value indicating if the cookie transmission requires
a secure transmission
* an argument defaults when it is assigned null as a placeholder
* a null placeholder is not required for trailing omitted arguments
*/

function setCookie(name, value, expires, path, domain, secure) {
    var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
    document.cookie = curCookie;
}


/*
name - name of the desired cookie
return string containing value of specified cookie or null
if cookie does not exist
*/

function getCookie(name) {
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1) {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    } else
        begin += 2;
    var end = document.cookie.indexOf(";", begin);
    if (end == -1)
        end = dc.length;
    return unescape(dc.substring(begin + prefix.length, end));
}


/*
name - name of the cookie
[path] - path of the cookie (must be same as path used to create cookie)
[domain] - domain of the cookie (must be same as domain used to
create cookie)
path and domain default if assigned null or omitted if no explicit
argument proceeds
*/

function deleteCookie(name, path, domain) {
    if (getCookie(name)) {
        document.cookie = name + "=" +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}

// date - any instance of the Date object
// * hand all instances of the Date object to this function for "repairs"

function fixDate(date) {
    var base = new Date(0);
    var skew = base.getTime();
    if (skew > 0)
        date.setTime(date.getTime() - skew);
}

/* Locator validation functions */
function CheckProductLine(source, args) {
}
