﻿var live_domains = ['www.mctdairies.com', 'mctdairies.com'];

addLoadEvent(checkForPoundAnchors);
addLoadEvent(wireImagesForMouseOver);
addLoadEvent(setInitialGMapLocation);
addLoadEvent(wireHomepageRotate);
addLoadEvent(checkForDev);
addLoadEvent(wireTablesEvenOdd);
/*
var tickerULs;
var ticker;
var tickerULLeft = 10;
var tickerTimerDuration = 25;
*/
var homenavc;
var homepageImageCount = 6;
var homepageImageIdx = 1;
var homepageImageLoader;
var homepageImageCurOpacity = 1;
var homepageImageRotationDuration = 5000;

//tickerULLeft = 100;

//DEV only, disables empty hyperlinks and puts up a message box if clicked
function checkForPoundAnchors() {
    //Grab all hyperlinks
    var AS = document.getElementsByTagName('a');
    for (var i = 0; i < AS.length; i++) {
        //If they don't have a hyperlink set or its just a pound
        if (!AS[i].href || (AS[i].href == '#' || AS[i].href.indexOf('#') == AS[i].href.length - 1) || AS[i].href == '') {
            //See if they already have on onclick handler wired
            if (typeof AS[i].onclick != 'function') {
                //If not, wire an alert message in
                //If the link is text-only send a message about where it will eventually go
                if (AS[i].innerHTML && AS[i].innerHTML.length > 0 && AS[i].innerHTML.indexOf('<') < 0) {
                    eval("AS[i].onclick = function() { sendPoungMsg('" + AS[i].innerHTML + "'); return false; };");
                }else{
                    AS[i].onclick = function() { sendPoungMsg(null); return false; };
                }
            }
        }
    }
}
function sendPoungMsg(msg) {
    if(msg){
        alert('This link doesn\'t go anywhere yet but will eventually go to:\n\n' + msg);
    }else{
        alert('This link doesn\'t go anywhere yet.');
    }
}

//Generic function called on every page load, looks for all images and hyperlinks with a class name of "overswap".
//Assumes images are named <xyz>-(off|over).(png|jpg|gif) so:
//text-off.png and text-over.png
function wireImagesForMouseOver() {
    //Grab all images
    var images = document.getElementsByTagName('img');
    //Make sure we've got something
    if (images && images.length > 0) {
        //Loop through all images
        for (var i = 0; i < images.length; i++) {
            //If the image has a class of "overswap"
            if (images[i].className && images[i].className.indexOf('overswap') >= 0) {
                //On mouseover replace the "-off.xyz" with "-over.xyz" and switch back on mouseout
                images[i].onmouseover = function() { this.src = this.src.replace(/\-off\.(png|jpg|gif)/, '-over.$1'); };
                images[i].onmouseout = function() { this.src = this.src.replace(/\-over\.(png|jpg|gif)/, '-off.$1'); };
            }
        }
    }
    //Same as above but handles mouseover of hyperlinks with images inside
    //Grab all of the hyperlinks
    var as = document.getElementsByTagName('a');
    //Make sure we've got something
    if (as && as.length > 0) {
        //Loop through all hyperlinks
        for (var i = 0; i < as.length; i++) {
            //If the hyperlink has a class of "overswap" and contains an image
            if (as[i].className && as[i].className.indexOf('overswap') >= 0 && as[i].getElementsByTagName('img').length == 1) {
                //On mouseover replace the image's "-off.xyz" with "-over.xyz" and switch back on mouseout
                as[i].onmouseover = function() { var img = this.getElementsByTagName('img')[0]; img.src = img.src.replace(/\-off\.(png|jpg|gif)/, '-over.$1'); };
                as[i].onmouseout = function() { var img = this.getElementsByTagName('img')[0]; img.src = img.src.replace(/\-over\.(png|jpg|gif)/, '-off.$1'); };
            }
        }
    }
}

function setInitialGMapLocation() {
    var gmap = document.getElementById('gmap-location');
    if (!gmap) return;
    var links = document.getElementsByTagName('a');
    for (var i = 0; i < links.length; i++) {
        if (links[i].className && links[i].className == 'gmap-location') {
            setGMapLocation(links[i]);
            //gmap.src = links[i].href;
            return;
        }
    }
}
function setGMapLocation(obj) {
    var divs = document.getElementsByTagName('div');
    if (!divs || divs.length == 0) return;
    for (var i = 0; i < divs.length; i++) {
        if (divs[i].className && divs[i].className.indexOf('address-phone') >= 0) {
            divs[i].className = divs[i].className.replace(/selected/g, '');
        }
    }
    obj.parentNode.className += ' selected';
    document.getElementById('gmap-location').src = obj.href + '&output=embed';
}
function wireHomepageRotate() {
    //See if we're on the homepage
    homenavc = document.getElementById('home-navc');
    if (!homenavc) return;
    
    //See if we've hidden the background using CSS (iPhone)
    if (getStyle('home-navc', 'background-image') == 'none') {
        return;
    }
    setTimeout(homepageRoate, homepageImageRotationDuration);
}
function homepageRoate() {
    //Increment the current index
    homepageImageIdx++;
    //If we're out of bounds reset it to the first item
    if (homepageImageIdx > homepageImageCount) {
        homepageImageIdx = 1;
    }
    //Create a loader image
    homepageImageLoader = new Image();
    //Set the source
    homepageImageLoader.src = '/images/home-main-image-' + homepageImageIdx + '.jpg'
    //When the image finishes loading call the function that swaps the main with the loader
    homepageImageLoader.onload = homepageImageLoader_onload;
}
function homepageImageLoader_onload() {
    //Opacity filter, won't work because this is a background image
    /*
    homepageImageCurOpacity -= 0.1;
    if (homepageImageCurOpacity > 0) {
        homenavc.style.opacity = homepageImageCurOpacity;
        homenavc.style.filter = 'alpha(opacity=' + (homepageImageCurOpacity * 100) + ')';
        setTimeout(homepageImageLoader_onload, 100);
        return;
    }
    */
    //homenavc.style.opacity
    //Set the background image to the loader's image
    homenavc.style.backgroundImage = 'url(' + homepageImageLoader.src + ')';
    //Set a timeout to rotate the next one in
    setTimeout(homepageRoate, homepageImageRotationDuration);
}
function checkForDev() {
    if (!live_domains || live_domains.length <= 0) return;
    for (var i = 0; i < live_domains.length; i++) {
        if (live_domains[i] == window.location.host) return;
    }
    if (readCookie('no-show-dev') == 'true') return;

    var div = document.createElement('div');
    div.id = 'dev-site';
    div.innerHTML = 'Warning: You are on the development site, click <a href="http://' + live_domains[0] + '" target="_blank" style="color:#FFFFFF;text-decoration:underline !IMPORTANT;font-style:italic;">here</a> to visit the live site';
    setCss(div, 'position:absolute;top:0;left:0;z-index:1000;width:100%;height:35px;text-align:center;color:#FFFFFF;font-family:Arial, Sans-Serif;font-size:20px;padding-top:5px;background-repeat:repeat;background-image:url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGQAAABkCAYAAABw4pVUAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAKdJREFUeNrs0QENADAIBLFn%2Fj0hDWwspCfhWpN09E3PAiACAkRAgAgIEAEBIiACAkRAgAgIEAEBIiACAkRAgAgIEAEBIiACAkRAgAgIEAEBIiACAkRAgAgIEAEBIiACAkRAgAgIEAEBIiACAkRAgAgIEAEBIiACAkRAgAgIEAEBIiACAkRAgAgIEAEBIiACAkRAgAgIEAEBIiBABERAgAgIEAG53gowAMIDAoY02mC6AAAAAElFTkSuQmCC);');
    var div_close = document.createElement('div');
    div_close.id = 'dev-site-close';
    setCss(div_close, 'float:right;margin-right:20px;cursor:pointer;');
    div_close.innerHTML = String.fromCharCode(9746);
    div_close.onclick = function() { document.getElementById('dev-site').style.display = 'none'; if (document.getElementById('dev-site-check').checked) { createCookie('no-show-dev', 'true', 1); } };
    var div_check = document.createElement('div');
    div_check.id = 'dev-site-check-cont';
    setCss(div_check, 'float:right;margin-right:30px;font-size:16px;padding-top:4px;');
    div_check.innerHTML = '<span>Do not show again</span><input type="checkbox" id="dev-site-check" />';
    div.appendChild(div_close);
    div.appendChild(div_check);
    document.getElementsByTagName('body')[0].appendChild(div);
}
function setCss(obj, css) {
    //Most browsers
    obj.setAttribute('style', css);
    //IE7
    if (obj.style.setAttribute) {
        obj.style.setAttribute('cssText', css);
    }
}
function wireTablesEvenOdd() {
    var dls = document.getElementsByTagName('dl');
    if (!dls || dls.length <= 0) return;
    var items;
    for (var i = 0; i < dls.length; i++) {
        if (dls[i].className && dls[i].className.indexOf('even-odd') >= 0) {
            items = dls[i].getElementsByTagName('dt');
            for (var j = 0; j < items.length; j++) {
                items[j].className = (items[j].className ? items[j].className : '') + (j % 2 ? ' even' : ' odd');
            }
            items = dls[i].getElementsByTagName('dd');
            for (var j = 0; j < items.length; j++) {
                items[j].className = (items[j].className ? items[j].className : '') + (j % 2 ? ' even' : ' odd');
            }
        }
    }
    /*
    var tables = document.getElementsByTagName('table');
    if (!tables || tables.length <= 0) return;
    var rows;
    for (var i = 0; i < tables.length; i++) {
        if (tables[i].className && tables[i].className.indexOf('even-odd') >= 0) {
            rows = tables[i].getElementsByTagName('tr');
            for (var j = 0; j < rows.length; j++) {
                if (rows[j].getElementsByTagName('th').length <= 0) {
                    rows[j].className = (rows[j].className ? rows[j].className : '') + (j % 2 ? ' odd' : ' even');
                }
            }
        }
    }
    */
}
