﻿$(document).ready(function() {
    $('.next, .previous').click(function() { scrollList($(this)) });
    setUpbanner('bannerBig');
    setUpbanner('bannerSmall');
    
    if($('.accordian-header').length > 0 ) {
        $('.article a.btn.grey.hide').each(function() {
            accordianClose($(this));
        });
        $(".article a.btn.grey.hide").live("click", function() {
            accordianClose($(this));
        });
        $(".article a.btn.grey.show").live("click", function() {
            accordianOpen($(this));
        });
    }
});
function setUpbanner(galleryId) {
    gallerySetup(galleryId);
    gallerySwitch(galleryId);

    $('#' + galleryId + ' ul li a').click(function() {
        applyClass($(this), galleryId);
        gallerySelect($(this), galleryId);
        if (galleryId == "bannerBig") {
            clearTimeout(galleryLoopBig);
            galleryLoopBig = setTimeout(function() { gallerySwitch(galleryId) }, 5000);
        }
        else {
            clearTimeout(galleryLoopSmall);
            galleryLoopSmall = setTimeout(function() { gallerySwitch(galleryId) }, 5000);
        }
    });
}
function applyClass(element,galleryId) {
    $('#' + galleryId + ' ul li').removeClass('selected');
    $(element).parent().addClass('selected');
}
function gallerySetup(galleryId) {
    $('#' + galleryId + ' ul li a').each(function() {
        var href = $(this).attr('href');
        $(this).attr('rel',href).removeAttr('href');
    });
}
function gallerySelect(element, galleryId) {
    var url = $(element).attr('rel');
    //Siva: To accomodate CR to allown banners without links, now adding a click event and style to disable hyperlinks.
    if (url != "#") {
        $('#' + galleryId + '>a').attr('href', url);
        $('#' + galleryId + '>a img').attr('src', $(element).children('img').attr('src'));
        $('#' + galleryId + '>a').unbind('click');
        $('#' + galleryId + '>a').removeAttr("style");
    }
    else {
        $('#' + galleryId + '>a').attr('href', url);
        $('#' + galleryId + '>a').click(function() {
            return false;
        });
        $('#' + galleryId + '>a').attr("style", "cursor:default;");
        $('#' + galleryId + '>a img').attr('src', $(element).children('img').attr('src'));
    }
}
var countBig = 0;
var galleryLoopBig;
var countSmall = 0;
var galleryLoopSmall;


function gallerySwitch(galleryId) {
    if (galleryId == "bannerBig") {
        var itemCount = $('#' + galleryId + ' ul li').length;
        if (countBig == itemCount) {
            galleryReset(galleryId);
        } else {
            applyClass($($('#' + galleryId + ' ul li')[countBig]).children('a'), galleryId);
            gallerySelect($($('#' + galleryId + ' ul li')[countBig]).children('a'), galleryId);
            countBig++;
            galleryLoopBig = setTimeout(function() { gallerySwitch(galleryId) }, 5000);
        }
    }
    else {
        var itemCount = $('#' + galleryId + ' ul li').length;
        if (countSmall == itemCount) {
            galleryReset(galleryId);
        } else {
            applyClass($($('#' + galleryId + ' ul li')[countSmall]).children('a'), galleryId);
            gallerySelect($($('#' + galleryId + ' ul li')[countSmall]).children('a'), galleryId);
            countSmall++;
            galleryLoopSmall = setTimeout(function() { gallerySwitch(galleryId) }, 5000);
        }
    }
}
function galleryReset(galleryId) {
    if (galleryId == "bannerBig") {
        countBig = 0;
        applyClass($($('#' + galleryId + ' ul li')[countBig]).children('a'), galleryId);
        gallerySelect($($('#' + galleryId + ' ul li')[countBig]).children('a'), galleryId);
        countBig++;
        galleryLoopBig = setTimeout(function() { gallerySwitch(galleryId) }, 5000);
    }
    else {
        countSmall = 0;
        applyClass($($('#' + galleryId + ' ul li')[countSmall]).children('a'), galleryId);
        gallerySelect($($('#' + galleryId + ' ul li')[countSmall]).children('a'), galleryId);
        countSmall++;
        galleryLoopSmall = setTimeout(function() { gallerySwitch(galleryId) }, 5000);
    }
}

var currentStep = 0;
function scrollList(element) {
    var getList = $(element).siblings('ul');
    var childAmount = $(getList).children().length - 1;
    var stepSize = $(getList).children(':first').height();
    var maxSize = stepSize * childAmount;
    if($(element).attr('class') == "next")  {
        if(currentStep == "-"+maxSize) {
            return;
        } else {
            $(getList).children('li').animate({ top: '-='+stepSize });
            currentStep = currentStep - stepSize;
        }
    } else if($(element).attr('class') == "previous") { 
        if(currentStep == 0) {
            return;
        } else {
            $(getList).children('li').animate({ top: '+='+stepSize });
            currentStep = currentStep + stepSize;
        }
    }
}

function textMaxLength(obj, maxLength, evt) {
    var charCode = (evt.which) ? evt.which : event.keyCode
    var max = maxLength - 0;
    var text = obj.value;
    if (text.length > max) {
        var ignoreKeys = [8, 46, 37, 38, 39, 40, 35, 36];
        for (i = 0; i < ignoreKeys.length; i++) {
            if (charCode == ignoreKeys[i]) {
                return true;
            }
        }
        return false;
    }
    else {
        return true;
    }
}
function textCounter(maxlimit, textBox, div) {
    var text = textBox.value;
    if (text.length > maxlimit) // if too long...trim it!
    {
        textBox.value = text.substring(0, maxlimit);
        //document.getElementById(div).innerHTML = "max " + maxlimit + " characters <span>(0 remaining)</span>";
        document.getElementById(div).innerHTML = "<span>(0 remaining)</span>";
    }
    else if (text.length < maxlimit) {
        var textLength = text.length;
        var charsLeft = maxlimit - textLength;
        //document.getElementById(div).innerHTML = "max " + maxlimit + " characters <span>(" + charsLeft + " remaining)</span>";
        document.getElementById(div).innerHTML = "<span>(" + charsLeft + " remaining)</span>";
    }
}

//ACCORDIAN FUNCTIONALITY
function accordianClose(link) {
    var content = $(link).parent('p').next('div.accordian-content');
    $(link).removeClass('hide').addClass('show');
    $(content).animate({ height: 0 }, 300).css('padding-bottom','0');
}
function accordianOpen(link) {
    var content = $(link).parent('p').next('div.accordian-content');
    var height = 0;
    $(content).children().each(function() {
        var margin = $(this).css('margin-bottom');
        margin = parseFloat(margin.replace("px", ""));
        height += $(this).height();
        height += margin;
    });
    $(link).removeClass('show').addClass('hide');
    $(content).animate({ height: height }, 300).css('padding-bottom','7px');
}
