/**
 * Effects required in the site template fusion-bikes3.
 */
function NewsEffects(){
    this.selectedNews = null;
};

NewsEffects.prototype.initNews = function(firstNodeId){
    var firstNode = document.getElementById(firstNodeId);
//    alert("FirstNode:"+firstNode);
    if(firstNode!=null){
        newsEffects.selectedNews = firstNode;
    }
}
NewsEffects.prototype.toggleContent = function(node) {
//    alert("toggle:"+node.getAttribute("id")+" and replace:"+newsEffects.selectedNews.getAttribute("id"));
    var contentNode = newsEffects.getContentNode(node);
    if(contentNode==null) {
//        alert("Node: "+contentNode+" not found!");
        return;
    }
    if (newsEffects.selectedNews != null) {
        var oldContentNode = newsEffects.getContentNode(newsEffects.selectedNews);
        // if the old node is the same with the new one, do nothing:
        if(oldContentNode.getAttribute("id")==contentNode.getAttribute("id")){
//            alert("It was the same node:"+node.getAttribute("id"));
            return;
        }
        newsEffects.hide(oldContentNode);
        newsEffects.show(contentNode);
    } else {
        newsEffects.show(contentNode);
    }
    newsEffects.selectedNews = node;
}
NewsEffects.prototype.getContentNode = function(node) {
    var contentId = node.getAttribute("id") + "_content";
//    alert("ToFind:"+contentId);
    var contentNode = null;
    if (contentId != null) {
        contentNode = document.getElementById(contentId);
//        alert("Found:"+contentNode.getAttribute("id"));
    }
    return contentNode;
}
NewsEffects.prototype.hide = function(contentNode){
//    alert("hide:"+contentNode.getAttribute("id"));
    var contentStyle = contentNode.getAttribute("style");
    if(contentStyle!=null) {
        // internet explorer hack.
        if(contentStyle.display) {
//            alert("There's a style Object");
            contentStyle.display = "none";
            return;
        }
        var displayStyle = newsEffects.getStyleProperty(contentStyle,"display");
        if(displayStyle!=null) {
            newsEffects.setStyleProperty(contentNode,"display","none");// or call remove if the trick will not work.
        } else {
            newsEffects.addStyleProperty(contentNode,"display","none");
        }
    } else {
        contentNode.setAttribute("style","display:none;");
    }
}
NewsEffects.prototype.show = function(contentNode){
//    alert("show:"+contentNode.getAttribute("id"));
    var contentStyle = contentNode.getAttribute("style");
    if(contentStyle!=null) {
        // internet explorer hack.
        if(contentStyle.display) {
//            alert("There's a style Object");
            contentStyle.display = "block";
            return;
        }
        var displayStyle = newsEffects.getStyleProperty(contentStyle,"display");
        if(displayStyle!=null) {
            newsEffects.setStyleProperty(contentNode,"display","block");// or call remove if the trick will not work.
        } else {
            newsEffects.addStyleProperty(contentNode,"display","block");
        }
    } else {
        contentNode.setAttribute("style","display:block;");
    }
}
//////////////////////////////////////////////////////////////////////////////////////////
// todo: the list of functions below are general and should be migrated to some util libs
//////////////////////////////////////////////////////////////////////////////////////////

//if (!String.endsWithChar) {
    String.prototype.endsWithChar = function (theChar) {
        if (this.charAt(this.length) == theChar)
            return true;
        else
            return false;
    }
//}
//if(!String.split) {
    // incredible but IE doesn't have this method.
//    alert("there's no split method");

//}
NewsEffects.prototype.getStyleProperty = function(styleString,toFind){
    // take all properties
    var styleProps = styleString.split(";");
    if(styleProps!=null) {
        for(var i=0;i<styleProps.length;i++) {
            var prop = styleProps[i];
            if(prop=null) {
                var propParts = prop.split(":");
                if(propParts[0]==toFind) {
                    // return the value for the display property display.
                    return propParts[1];
                }
            }
        }
    }
}
// this method assumes that the style property to add does not exist.
NewsEffects.prototype.addStyleProperty = function(node,property,value){
    var contentStyle = node.getAttribute("style");
    if(contentStyle.length!=0&&(contentStyle.charAt(contentStyle.length-1)!=';')) {
        contentStyle+=";";
    }
    contentStyle+=property+":"+value+";";
    node.setAttribute("style",contentStyle);
}

// this method assumes that the style property exists.
NewsEffects.prototype.setStyleProperty = function(node,property,value){
    // take all properties
    var props = styleString.split(";");
    var newProps = new Array();
    if(props!=null) {
        for(var i=0;i<props.length;i++) {
            var prop = props[i];
            if(prop!=null) {
                var propParts = prop.split(":");
                if(propParts[0]==property) {
                    newProps[newProps.length]=property+":"+value;
                } else {
                    newProps[newProps.length]=prop;
                }
            }
        }
    }
    var newStyleString = newProps.join(";");
    if(!newStyleString.endsWithChar(";")) {
         newStyleString+=";";
    }
    node.setAttribute("style",newStyleString);
}
// this function assumes that the style property to be removed exists.
NewsEffects.prototype.removeStyleProperty = function(node,property){
    // take all properties
    var props = styleString.split(";");
    var newProps = new Array();
    if(props!=null) {
        for(var i=0;i<props.length;i++) {
            var prop = props[i];
            if(prop!=null) {
                var propParts = prop.split(":");
                if(propParts[0]==property) {
                    // ignore
                } else {
                    newProps[newProps.length]=prop;
                }
            }
        }
    }
    var newStyleString = newProps.join(";");
    if(!newStyleString.endsWithChar(";")) {
         newStyleString+=";";
    }
    node.setAttribute("style",newStyleString);
}
// global instances.
var newsEffects = new NewsEffects();