﻿var lastTouched = " ";
var currentStudy = 0;
var autoChangeTimer = 0;
var t;

$(document).ready(function () {
    ToggleAutoChangeTimer();
    GoogleExclude();
    $('a.galleryTag').lightBox();

    //-- Change the highlighted case study thumbnail --//
    $('.csThumbnail').hover(function () {
        var id = $(this).attr('id');
        currentStudy = parseInt(id = id.replace("csThumb-", ""));
        $(this).parent().children().removeClass('selected');
        $(this).addClass('selected');
        $(this).parent().parent().find('.csScreenshot div.screenshotNav img').attr({ src: "/images/vizioz/dot.png" });
        $(this).parent().parent().find('.csScreenshot div.screenshotNav img#csDot-' + id.replace("csThumb-", "")).attr({ src: "/images/vizioz/dot-selected.png" });
        ToggleAutoChangeTimer();


    }, function () {
        ToggleAutoChangeTimer();
    });

    $('.csScreenshot').hover(function () {
        ToggleAutoChangeTimer();
    }, function () {
        ToggleAutoChangeTimer();
    });

    //-- Change homepage case study images --//
    $('div.csShow div.csFooter div.csThumbnail, div.csPage div.csFooter div.csThumbnail').hover(function () {
        $('.csImg img').attr({ src: $(this).find('img').attr('src').replace("Small", "Medium"), title: $(this).find('img').attr('title'), alt: $(this).find('img').attr('title') + " Thumbnail" });
        $('.csTitleText').text($(this).find('img').attr('title'));
        $('.csSummary').html(studySummary[$(this).attr('id').replace("csThumb-", "")]);
    });

    //-- Change group case study images --//
    $('div.csGroup div.csFooter div.csThumbnail').hover(function () {
        $('#' + $(this).attr('id').replace("csThumb", "csFav")).parent().find('li').removeClass('selected');
        $('#' + $(this).attr('id').replace("csThumb", "csFav")).addClass('selected');
    }, function () {
        $(this).parent().children().removeClass('selected');
        $('#' + $(this).attr('id').replace("csThumb", "csFav")).parent().find('li').removeClass('selected');
    });

    $('div.csGroup div.csBody ul li').hover(function () {
        $('#' + $(this).attr('id').replace("csFav", "csThumb")).addClass('selected');
    }, function () {
        $('#' + $(this).attr('id').replace("csFav", "csThumb")).parent().children().removeClass('selected');
    });

    if (isTouchDevice()) {
        document.addEventListener("touchstart", touchEventHandler, false);
    }

});

function isTouchDevice() {
    var el = document.createElement('div');
    el.setAttribute('ongesturestart', 'return;');
    if (typeof el.ongesturestart == "function") {
        return true;
    } else {
        return false
    }
}

function touchEventHandler(event) {
    var numTouch = event.touches.length;
    if (numTouch > 0) {
        var touchObj = event.touches[0];

        var target = touchObj.target;

        if (findClass(target.parentNode.parentNode, 'csThumbnail')) {
            if (lastTouched != target.parentNode.parentNode.id) {
                event.preventDefault();
                $(target.parentNode.parentNode).parent().children().removeClass('selected');
                $(target.parentNode.parentNode).addClass('selected');
                $(target.parentNode.parentNode.parentNode.parentNode).find('div.csBody div.csScreenshot div.screenshotNav img').attr({ src: "/images/vizioz/dot.png" });
                $(target.parentNode.parentNode.parentNode.parentNode).find('div.csBody div.csScreenshot div.screenshotNav img#csDot-' + $(target.parentNode.parentNode).attr('id').replace("csThumb-", "")).attr({ src: "/images/vizioz/dot-selected.png" });

                $('.csImg img').attr({ src: $(target.parentNode.parentNode).find('img').attr('src').replace("Small", "Medium"), title: $(target.parentNode.parentNode).find('img').attr('title'), alt: $(target.parentNode.parentNode).find('img').attr('title') + " Thumbnail" });
                $('.csTitleText').text($(target.parentNode.parentNode).find('img').attr('title'));
                $('.csSummary').html(studySummary[$(target.parentNode.parentNode).attr('id').replace("csThumb-", "")]);
                lastTouched = $(target.parentNode.parentNode).attr('id');
            }
        }
    }
}

function findClass(obj, className) {
    var foundit = false;
    var classList = obj.className.split(/\s+/);
    for (i = 0; i < classList.length; i++) {
        if (classList[i] === className) {
            foundit = true;
        };
    }
    return foundit;
}

function HasCaseStudy() {
    switch (window.location.pathname) {
        case '/case-studies': return 0; break;
        default: return 1;
    }
}

function ChangeStudy(studyObj) {
    studyObj.parent().children().removeClass('selected');
    studyObj.addClass('selected');
    studyObj.parent().parent().find('.csScreenshot div.screenshotNav img').attr({ src: "/images/vizioz/dot.png" });
    studyObj.parent().parent().find('.csScreenshot div.screenshotNav img#csDot-' + studyObj.attr('id').replace("csThumb-", "")).attr({ src: "/images/vizioz/dot-selected.png" });

    //-- Change the highlighted case study thumbnail --//

        $('.csImg img').attr({ src: studyObj.find('img').attr('src').replace("Small", "Homethumb"), title: studyObj.find('img').attr('title'), alt: studyObj.find('img').attr('title') + " Thumbnail" });
        $('.csTitleText').text(studyObj.find('img').attr('title'));
        $('.csImg a').attr({href: studyObj.find('a').attr('href')});

        if ($('.csSummary').length != 0) {
            $('.csSummary').html(studySummary[studyObj.attr('id').replace("csThumb-", "")]);
        }

    //-- Change group case study images --//
        $('#' + studyObj.attr('id').replace("csThumb", "csFav")).parent().find('li').removeClass('selected');
        $('#' + studyObj.attr('id').replace("csThumb", "csFav")).addClass('selected');


    //-- Change homepage case study images --//
        $('#' + studyObj.attr('id').replace("csFav", "csThumb")).addClass('selected');

}

function ToggleAutoChangeTimer() {
    if (!autoChangeTimer && HasCaseStudy()) {
        autoChangeTimer = 1;
        AutoChange();
    }
    else {
        autoChangeTimer = 0;
        clearTimeout(t);
    }
}

function AutoChange() {
    var obj = $("#csThumb-" + currentStudy);


    // If obj is empty then our index is to high, there are no more studies, so reset to the first study
    if (obj.length == 0 ) {
        currentStudy = 1;
        var obj = $("#csThumb-" + currentStudy);
    }

    if (obj.length != 0) {
        ChangeStudy(obj);
        t = setTimeout("AutoChange()", 5000);
        currentStudy += 1;
    }
}

function GetCookie(nameOfCookie) {
    if (document.cookie.length > 0) {
        begin = document.cookie.indexOf(nameOfCookie);
        if (begin != -1) {
            begin += nameOfCookie.length + 1;
            end = document.cookie.indexOf(";", begin);
            if (end == -1) end = document.cookie.length;
            return unescape(document.cookie.substring(begin, end));
        }
        return null;
    }
}

function DeleteCookie() {
    ClearCookie('__utmv', window.location.hostname, '/');
    window.location.reload();
}

function GoogleExclude() {
    if (GetCookie("__utmv") != null) {
        var excludehtml = "<a href=\"javascript:DeleteCookie();\"  style=\"position: absolute; top: 0pt; left: 0pt; border: 0pt none; width: 149px; height: 149px; background: url('/images/generic/ninja_mode.png') no-repeat scroll 0% 0% transparent;\"/>"
        $("body").append(excludehtml);
    }
}

function ClearCookie(name, domain, path) {
    try {
        domain = domain.replace('www', '');
        function Get_Cookie(check_name) {
            // first we'll split this cookie up into name/value pairs
            // note: document.cookie only returns name=value, not the other components
            var a_all_cookies = document.cookie.split(';');
            var a_temp_cookie = '';
            var cookie_name = '';
            var cookie_value = '';
            var b_cookie_found = false; // set boolean t/f default f

            for (i = 0; i < a_all_cookies.length; i++) {
                // now we'll split apart each name=value pair
                a_temp_cookie = a_all_cookies[i].split('=');

                // and trim left/right whitespace while we're at it
                cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

                // if the extracted name matches passed check_name
                if (cookie_name == check_name) {
                    b_cookie_found = true;
                    // we need to handle case where cookie has no value but exists (no = sign, that is):
                    if (a_temp_cookie.length > 1) {
                        cookie_value = unescape(a_temp_cookie[1].replace(/^\s+|\s+$/g, ''));
                    }
                    // note that in cases where cookie is initialized but no value, null is returned
                    return cookie_value;
                    break;
                }
                a_temp_cookie = null;
                cookie_name = '';
            }
            if (!b_cookie_found) {
                return null;
            }
        }
        if (Get_Cookie(name)) {
            var domain = domain || document.domain;
            var path = path || "/";
            document.cookie = name + "=; expires=" + new Date + "; domain=" + domain + "; path=" + path;
        }
    }
    catch (err) {
    }
};
