  /*
   @author Jasna
   */

if ("undefined" === typeof HICONV) {
	HICONV = {};
}

if (! HICONV.Constants) {
   HICONV.Constants = function() {
      var beginnigUrl = 'http://www.hiconversion.com:80'; // edit here
      var serverUrl = beginnigUrl + ''; // edit here
      var dwrServlet = '/dwr'; // normally no need to change
      var getLoggedOutLocation = 'http://www.hiconversion.com/ajax/webSite/login.htm';
      return {
         getDwrUrl: function() {
            return serverUrl + dwrServlet;
         },
         getServerUrl: function() {
            return serverUrl;
         },
         getLoggedOutLocation: function() {
            return getLoggedOutLocation;
         },
         getHomeLocation: function() {
            return serverUrl + '/ajax/webSite/experimentsHome.jsp';
         }
      };
   };
}

var	Constants = HICONV.Constants();

function htmltoString (element) {
    
//////////        if (element.outerHTML) {
//////////            return element.outerHTML;
//////////        } else {
//////////            var parent = element.parentNode;
//////////            var el = document.createElement(parent.tagName);
//////////            el.appendChild(element);
//////////            var shtml = el.innerHTML;
//////////            parent.appendChild(element);
//////////            return shtml;
//////////        }
//////////        
//////////        function getOuterHTML(object) {
//////////    var element;
//////////    if (!object) return null;
//////////    element = document.createElement("div");
//////////    element.appendChild(object.cloneNode(true));
//////////    return element.innerHTML;
//////////    }

var variationContent = '';
var variationContentAfter = '';
if (!element) {
    return '';
}
if (element.tagName) {
    variationContent = variationContent + '<' + element.tagName.toLowerCase() + ' ';
    variationContentAfter = '</' + element.tagName.toLowerCase() + '>';
}
if (element.id) {
    variationContent = variationContent + ' id="' + element.id + '" ';
} 
if (element.className) {
    variationContent = variationContent + ' className="' + element.className + '" ';
} 
if (element.getAttribute('class')) {
    variationContent = variationContent + ' class="' + element.getAttribute('class') + '" ';
} 

//if (element.nodeType) {
//    variationContent = variationContent + ' nodeType="' + element.nodeType + '" ';
//} 
if (element.style) {
    if (element.style.cssText) {
        variationContent = variationContent + ' style="' + element.style.cssText + '" ';
    }
} 
if (element.nodeValue) {
    variationContent = variationContent + ' nodeValue="' + element.nodeValue + '" ';
} 
if (element.baseURI) {
    variationContent = variationContent + ' baseURI="' + element.baseURI + '" ';
} 
if (element.title) {
    variationContent = variationContent + ' title="' + element.title + '" ';
} 
if (element.tabIndex) {
    variationContent = variationContent + ' tabIndex="' + element.tabIndex + '" ';
} 
if (element.src) {
    variationContent = variationContent + ' src="' + element.src + '" ';
} 
if (element.name) {
    variationContent = variationContent + ' name="' + element.name + '" ';
} 
if (element.alt) {
    variationContent = variationContent + ' alt="' + element.alt + '" ';
} 
if (element.href) {
    variationContent = variationContent + ' href="' + element.href + '" ';
} 
var att = element.attributes;
for (var i = 0; i < att.length; i++) {
    if ((att[i].name == 'align') || (att[i].name == 'valign')) {
        variationContent = variationContent + ' '+att[i].name+'="' +att[i].value + '" ';
    }
}

if (element.style) {
    if (element.style.length > 0) {
        var styleBegin = ' style="';
        var styleEnd = '"';
        var stylevariationContent = '';
        for(var i = 0; i < element.style.length; i++) {
        if (i != 0) {
            stylevariationContent = stylevariationContent + ';';
            }
        }
    }
} 
if (element.attributes) {
    if (element.attributes.length > 0) {
        for(var i = 0; i < element.attributes.length; i++) {
            ///if (element.attributes[i].nodeName == "hiconversion_id") {
            //    variationContent = variationContent + ' hiconversion_id="' + element.attributes[i].nodeValue + '" ';
            //}
            if (element.attributes[i].nodeName == "class") {
                variationContent = variationContent + ' class="' + element.attributes[i].nodeValue + '" ';
            }
        }
       // variationContent = variationContent + styleBegin + stylevariationContent + styleEnd;
    }
} 
variationContent = variationContent + ' >';
if (element.innerHTML) {
    variationContent = variationContent + element.innerHTML;
} 
variationContent = variationContent + variationContentAfter;
variationContent = variationContent.replace(/,hiconversionEvent_/g, '');
return variationContent;
}

function stringToHtml (variationContent, myDoc) {

    var element = null;
    var tagName = '';
    var tagName1 = variationContent.substring(1, variationContent.indexOf(' '));
    var tagName2 = variationContent.substring(1, variationContent.indexOf('>'));
    if (tagName1.length < 2) {
        tagName = tagName2;
    } else if (tagName2.length < 2) {
        tagName = tagName1;
    } else if (tagName2.length < tagName1.length) {
        tagName = tagName2;
    } else {
        tagName = tagName1;
    }
    var tagList = ["p", "div", "span", "table", "tr", "td", "img", "a", "center", "font", "ul", "li", "h1", "h2", "h3", "h4", "h5", "h6", 
                    "b", "big", "dd", "dl", "dt", "em", "link", "ol", "th", "tt", "u"];
    for (var i = 0; i < tagList.length; i++) {
        if ((tagList[i] == tagName1.toLowerCase()) || (tagList[i] == tagName2.toLowerCase()))
        {
            tagName = tagList[i];
            break;
        }
    }
    if (myDoc) {
        var htmlTag = myDoc.createElement(tagName);
    }
    else {
        var htmlTag = document.createElement(tagName);
    }
/////////
    var variationInnerHTML = '';
    if (variationContent.indexOf('>') < variationContent.lastIndexOf('<')) {
        variationInnerHTML = variationContent.substring(variationContent.indexOf('>') + 1, variationContent.lastIndexOf('<'));
    } 
    var variationContent2End = variationContent.indexOf('>') - 1;
    
    
    var pairs;
    
    var firstPart = variationContent.substring(0, variationContent.indexOf('>') + 1);
    firstPart = firstPart.replace(' ', '');
    if (firstPart == '<' + tagName + '>') {
        pairs = [];
    } else {
        if (variationContent.substring(variationContent.indexOf('>') - 3, variationContent.indexOf('>') - 1) == '" ') {
            variationContent2End = variationContent.indexOf('>') - 3;
        }
        
        var variationContent2 = variationContent.substring(tagName.length + 2, variationContent2End);
        var beginning = variationContent2.substring(0, 1);
        if (beginning == ' ') {
            variationContent2 = variationContent2.substring(1, variationContent2.length);
        }
        variationContent2 = variationContent2.replace(/"/g, '');
        if (variationContent2.indexOf('  ') == -1) {
            pairs = variationContent2.split(' ');
        } else {
            pairs = variationContent2.split('  ');
        }
    }
    for (var i = 0; i < pairs.length; i++) {
        try {
            if (pairs[i].substring(0, pairs[i].indexOf('=')) == 'innerHTML') {
                htmlTag.innerHTML = pairs[i].substring(pairs[i].indexOf('=') + 1, pairs[i].length);
            } else {
                var variationName = pairs[i].substring(0, pairs[i].indexOf('='));
                var variationValue = pairs[i].substring(pairs[i].indexOf('=') + 1, pairs[i].length);
                if (variationName.toLowerCase() == 'class') {
                    htmlTag.setAttribute('class', variationValue);
                }
                if (variationName.toLowerCase() == 'classname') {
                    htmlTag.className = variationValue;
                }
//                if (variationName.toLowerCase() == 'class') {
//                    htmlTag.class = variationValue;
//                }
                if ((variationName.toLowerCase() == 'href') && (htmlTag.nodeName.toLowerCase == 'a')) 
                {
                    htmlTag.href = variationValue;
                }
                if ((variationName.toLowerCase() != 'style')){
                    htmlTag.setAttribute(variationName, variationValue);
                }
                if (variationName.toLowerCase() == 'style') {
                    htmlTag.style.cssText = variationValue;
                }
                
            }
        } catch (e) {
        //    e.
        }
    }
    if (variationInnerHTML != '') {
        try {
            htmlTag.innerHTML = variationInnerHTML;
        } catch (e) {
        //    e.
        }
    }
    return htmlTag;
////////
}

function hiconversion_update_navigation (_experiment, sections) {

    var imageProperties = null;
    var imageSections = null;
    var imagePublish = null;
    var renderPlace = document.body;

    imageProperties = document.getElementById('imgInitialize');
    imageSections = document.getElementById('imgSections');
    imagePublish = document.getElementById('imgPublish');

    var myTooltip = new YAHOO_adst.widget.Tooltip("myTooltip", { 
        context: imageProperties, 
        text:"Step completed!",
        container: renderPlace,
        showDelay:500 } );
    
    var info = 0;
    if (sections.length == 0) {
        var myTooltip1 = new YAHOO_adst.widget.Tooltip("myTooltip", { 
        context:imageSections, 
        text:"No sections created!",
        container: renderPlace,
        showDelay:500 } );
    } else {
        for (var i = 0; i < sections.length; i++) {
            if (sections[i].variations.length == 1) {
                var myTooltip2 = new YAHOO_adst.widget.Tooltip("myTooltip", { 
                context:imageSections, 
                text:"No variations created!<br/>All sections must have at least one variation other then baseline.",
                container: renderPlace,
                showDelay:500 } );
                //imageSections.src = completedIco;
                info = 1;
                break;
            }
        }
    }
    if (info == 0) {
        var myTooltip2 = new YAHOO_adst.widget.Tooltip("myTooltip", { 
        context:imageSections, 
        text:"Step completed!",
        container: renderPlace,
        showDelay:500 } );
        imageSections.className = "hiconversion_setupNavigation2";
    }
    
    if ((_experiment.experimentStatus != 'TEST_CREATED') && (_experiment.experimentStatus != 'IMPLEMENTATION_CREATED')) {
        var myTooltip3 = new YAHOO_adst.widget.Tooltip("myTooltip", { 
        context:imagePublish, 
        text:"Incompleted!",
        container: renderPlace,
        showDelay:500 } );
    } else {
        var myTooltip3 = new YAHOO_adst.widget.Tooltip("myTooltip", { 
        context:imagePublish, 
        text:"Step completed!",
        container: renderPlace,
        showDelay:500 } );
        imagePublish.className = "hiconversion_setupNavigation2";
    }
}
