function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() {
            if (oldonload) {
                oldonload();
            }
            func();
        }
    }
}

function feedback(type, message) {
    var cls = 'success-msg';
    switch (type) {
        case('WARN'):
            cls = 'warning-msg';
            break;
        case('ERROR'):
            cls = 'error-msg';
            break;
        case('INFO'):
            cls = 'info-msg';
            break;
    }
    var feedbackDiv = document.getElementById('feedback-messages');
    if (feedbackDiv) {
        var html = '<ul class="feedback-messages">';
        html += '<li class="' + cls + '">' + message;
        html += '</li>';
        html += '</ul>';
        feedbackDiv.innerHTML = html;
    }
}

function clearFeedback() {
    var feedbackDiv = document.getElementById('feedback-messages');
    if (feedbackDiv) {
        feedbackDiv.innerHTML = '';
    }
}

function openWindow(location){
    var extWin = window.open( location, "", 'left=20,top=20,width=1000,height=600,toolbar=1, scrollbars=1, resizable=1');
}
(function($) {
  $.fn.vCenter = function(options) {
    var pos = {
      sTop : function() {
        return window.pageYOffset
        || document.documentElement && document.documentElement.scrollTop
        ||  document.body.scrollTop;
      },
      wHeight : function() {
        return window.innerHeight
        || document.documentElement && document.documentElement.clientHeight
        || document.body.clientHeight;
      }
    };
    return this.each(function(index) {
      if (index == 0) {
        var $this = $(this);
        var elHeight = $this.height();
        var elTop = pos.sTop() -200 + (pos.wHeight() / 2) - (elHeight / 2);
        $this.css({
          position: 'relative',
          marginTop: '0',
          top: elTop
        });
      }
    });
  };
})(jQuery);

function processCompareResponse(response, contextPath, showComparePanel, showTime) {
    var removeProdGifUrl = contextPath + "/mantiki/images/design/topBanner/topBanner_compare_checkbox.gif";
    var countStr = "0 items";
    var sb = "<ul>\n";

    var errorMsg = "";
    var styles = null;
    
    if (response == null || response['styles'] == null) {
           sb = sb + "<li> No items to compare </li>";
    } else {
        errorMsg = response['error'];
        styles = response['styles'];
               
        for (i = 0; i < styles.length; i++) {
            sb = sb + "<li>";
            sb = sb + "<span><a href='" + contextPath + "/product/" + styles[i].styleCode + ".html'>";
            sb = sb + styles[i].description;
            sb = sb + "</a></span>";
            sb = sb + "<a href=\"javascript:removeFromCompare('" + styles[i].styleCode + "','" + contextPath + "',"+ showTime +")\"><img src='" +
                 removeProdGifUrl + " ' border='0' alt='Remove item' title='Remove item'/></a>";
            sb = sb + ("</li>\n");
        }
        if ( styles.length  == 1 ){
           countStr = styles.length + " item";
        } else {
           countStr = styles.length + " items";
        }
    }
    
    sb = sb + ("</ul>\n");


    $("#compareList").html(sb);
    $('#compareCount').html(countStr);

    if (errorMsg != '' && errorMsg != null){
        alert(errorMsg);
    }
    if (showComparePanel) {
        // close main compare basket
        hideCompare();
        
        $("#comparePanelClone").remove();
        var tmpCompare = $("#comparePanel").clone();
        $(tmpCompare).vCenter();
        $(tmpCompare).attr("id", "comparePanelClone");

         tmpCompare.insertAfter($("#comparePanel"));

         showCompareClone();
         $("#comparePanelClone .comparePanel").css({"display":"block"});
         $("#comparePanelClone  a.toggleCompare").click(removeCompareClone);
         
         GLOBAL_COMPARE_CALLER = "CLONE";

         if ( typeof  COMPARE_CLONE_TIMER_HANDLE != "undefined"  && COMPARE_CLONE_TIMER_HANDLE !=''){
             clearTimeout(COMPARE_CLONE_TIMER_HANDLE);
         }

        COMPARE_CLONE_TIMER_HANDLE = setTimeout(removeCompareClone, showTime);
    }
}

function removeCompareClone(){
      $("#comparePanelClone").remove();
}

function addToCompare(styleCode, contextPath,  showTime) {
    var showComparePanel = true;
    var callbackProxy = function(response) {
        processCompareResponse(response, contextPath, showComparePanel, showTime);
    }

    var callMetaData = { callback:callbackProxy };
    CompareService.add(styleCode, callMetaData);
}

function removeFromCompare(styleCode, contextPath, showTime) {
    var showComparePanel = false;

    if ( typeof  GLOBAL_COMPARE_CALLER != "undefined" && GLOBAL_COMPARE_CALLER == "CLONE") {
         showComparePanel = true;
     }        

    var callbackProxy = function(response) {
        processCompareResponse(response, contextPath, showComparePanel, showTime);
    }

    var callMetaData = { callback:callbackProxy };
    CompareService.remove(styleCode, callMetaData);
}

function clearCompare(contextPath, showTime) {
    var showComparePanel = false;

    if ( typeof  GLOBAL_COMPARE_CALLER != "undefined" && GLOBAL_COMPARE_CALLER == "CLONE") {
        showComparePanel = true;
    }
    
    var callbackProxy = function(response) {
        processCompareResponse(response, contextPath, showComparePanel, showTime);
    }

    var callMetaData = { callback:callbackProxy };
    CompareService.clear(callMetaData);
}

function back() {
        window.history.back();       
}