﻿// JScript File
[].indexOf || (Array.prototype.indexOf = function(v,n){
  n = (n==null)?0:n; var m = this.length;
  for(var i = n; i < m; i++)
    if(this[i] == v)
       return i;
  return -1;
});


var currFeature = null;
var currDeptidx = null;


function selectFeature(dept, deptindex)
{
    var cs_feature_id = dept + '_' + deptindex;
    var li_feature_id = 'li_' + dept + '_' + deptindex;
    var cs_featureslists = document.getElementsByTagName("dl");
    $('dl li a').each(function(){
        $(this).removeClass("active");
        if($(this).parent().attr("id") == li_feature_id)
        {
            $(this).addClass("active");
        }
    
    });
    $('.cs_feature_buttons a').each(function()
    {
        $(this).attr("rel", cs_feature_id);
    });
//    for(var i=0;i<cs_featureslists.length;i++)
//    {
//        var lis = cs_featureslists[i].getElementsByTagName("li");
//        for(var j=0;j<lis.length;j++)
//        {
//            var as = lis[j].getElementsByTagName("a");
//            if(as.length > 0){
//                removeClassName(as[0],'active');
//                if(lis[j].getAttribute("id") && lis[j].getAttribute("id") == li_feature_id)
//                {
//                    addClassName(as[0],'active');
//                }
//            }
//        }
//    }
    var cs_features = document.getElementById("cs_features");
    if(cs_features)
    {
        var $from;
        var $to;
        
        var divs = cs_features.getElementsByTagName("div");
        for(var i=0;i<divs.length;i++)
        {
            
            if(divs[i].className && divs[i].className.split(" ").indexOf('cs_feature') > -1)
            {
                if(divs[i].style.display == 'block')
                    $from = divs[i];
                if(divs[i].getAttribute("id") && divs[i].getAttribute("id") == cs_feature_id)
                    $to = divs[i];
                
                if($from && $to && $from != $to)
                    animFade($from, $to, "normal");                    
            }
        }
    }    
    return false;

}

function selectNextFeature(source)
{
    
    var featureid = $(source).attr("rel");
    
    _csfeatures = $(".cs_feature");
    var thisIndex = 0;
    var count = 0;
    $(_csfeatures).each(function(){
        if($(this).attr("id") == featureid)
        {
            thisIndex = count;            
        }
        count++;
        
    });
    
    var nextIndex = 0;
    if(thisIndex == _csfeatures.length)
        nextIndex = 0;
    else
        nextIndex = (++thisIndex) % _csfeatures.length;
    
    nextFeature = _csfeatures[nextIndex];
    
    var id = $(nextFeature).attr("id");
    _parts = id.split('_');
    
    if(_parts.length < 2) return true;
    
    var retVal = selectFeature(_parts[0], _parts[1]);
    
    return retVal;

}

function animFade($from, $to, speed)
{    
    var spd = speed || "normal";
    
    $($from).fadeOut(spd, function()
    {
        
        
    });
    $($to).fadeIn(spd, function(){
        
    }); 
    $($from).css("display", "none").css("opacity","0");
    $($to).css("display", "block").css("opacity","1");   
    
    //$($from).css("display","none").css("opacity","0");
    //$($to).css("display","block").css("opacity","1");
    
}

function addClassName(elem, className)
{
    var currClass = elem.className || "";
    var classes = currClass.split(" ");
    classes[classes.length] = className;
    elem.className = classes.join(' ');
}

function removeClassName(elem, className)
{
    var currClass = elem.className || "";
    if(currClass == "")
        return;
    
    var classes = currClass.split(' ');
    var newClasses = [];
    for(var i=0;i<classes.length;i++)
    {
        if(classes[i] != className)
        {
            newClasses[newClasses.length] = classes[i];
        }
    }
    elem.className = newClasses.join(' ');
}

function selectFeatureImage(dept, deptidx, imageidx)
{
    var cs_feature = $('#'+dept + '_' + deptidx); //document.getElementById(dept+'_' + deptidx);
    if(cs_feature)
    {
        var imgs = $('#'+dept + '_' + deptidx + ' img') //.getElementsByTagName("img");
        var $from, $to;
        for(var i=0;i<imgs.length;i++)
        {            
            if(imgs[i].style.display != 'none')
            {
                
                $from = imgs[i];  
            }
            if(imgs[i].id && imgs[i].id == 'img_' + dept + '_' + deptidx + '_' + imageidx)
            {
                
                $to = imgs[i];
                
//                imgs[i].style.display = 'none';
//                imgs[i].style.opacity = '0';                
//                
//                if(imgs[i].id == 'img_' + dept + '_' + deptidx + '_' + imageidx)
//                {
//                    imgs[i].style.display = 'block';
//                    imgs[i].style.opacity = '1';
//                    //imgs[i].style.filter = "alpha(opacity=100)";                    
//                }
            }                        
        }
        if($from && $to && $from != $to)
        {
            animFade($from, $to, "normal");
            
        }
        selectImageButton(dept, deptidx, imageidx);
    }
    return false;
}

function selectImageButton(dept, deptidx, imageidx)
{
    var cs_feature = $('#' + dept + '_' + deptidx);
    if(cs_feature)
    {   
        var buttons = $(cs_feature).find("div[class*=csbutton]");
        
        $(buttons).each(function()
        {
            $(this).find("a").removeClass("active");            
        });
        $(buttons[imageidx-1]).find("a").addClass("active");
    }
}


(function($)
{
    jQuery.fn.featureContent = function(options){        
            
        var settings = jQuery.extend({}, jQuery.fn.featureContent.defaults, options);
        
        return this.each(function()
        {
            var _fc = this;
            var _fp = null;
            var _fd = null;
            var hideTimer = null;
            
            init(_fc);
            function init(content)
            {
                _fp = $(content).parent();
                // store the original panel display value
                var origDisp = _fp.css("display");
                // make invisible and set display to block
                // we can now get heights
                _fp.css("visibility","hidden").css("display", "block");            
                
                // find the feature detail div
                _fd = $(content).find(".cs_feature_detail");
                // make invisible,set height to auto
                _fd.css("visibility", "hidden").css("height", "auto");
                // store computed height in local attr. set height to 0, 
                // make visible, 
                
                var $height = _fd.height();
                _fd.attr("csheight", $height).css("height", "0").css("visibility", "visible");                
                var _fn = _fp.find(".cs_feature_nav");
                var $nHeight = _fn.height();                
                _fd.css("bottom", $nHeight);
                
                // restore original display value and make visible
                _fp.css("display", origDisp).css("visibility", "visible");    
            };
            
            // default show/hide animations
            var doAnimateShow = function()
            {
                $(_fd).stop().animate({"height": $(_fd).attr("csheight")}, settings.showSpeed);
            };
            var doAnimateHide = function()
            {
                $(_fd).stop().animate({"height": 0}, settings.hideSpeed);
            };
            
            
            // bind events to triggers
            $(settings.trigger).each(function()
            {
                clearTimeout(hideTimer);
                $(this).bind(settings.eventNameShow, doAnimateShow);
            });            
            $(settings.trigger).each(function()
            {            
                $(this).bind(settings.eventNameHide, function(e) {
                    hideTimer = setTimeout(function(){
                        doAnimateHide();
                    }, settings.hideDelay);
                
                });
                
            });
            
        });    
    };
    
    jQuery.fn.featureContent.defaults = 
    {
        name: "featureContent",
        eventNameShow: "mouseenter",
        eventNameHide: "mouseleave",
        showSpeed: "fast",
        hideSpeed: "slow",
        hideDelay: 200,
        trigger : ".cs_featurecontent"
    };

})(jQuery);

$(document).ready(function()
{
    $(".cs_featurecontent").featureContent( { trigger: ".hoverButton" });
});



