// Common namespace
if(typeof UNMAX == 'undefined')
    UNMAX = {}

UNMAX.context ='';

UNMAX.setContext = function(path) {
    UNMAX.context = path;
}

UNMAX.inArray = function(a,b) {
    if(b.indexOf) return b.indexOf(a);
    
    for(var d=0,f=b.length;d<f;d++)
        if(b[d]===a)return d;
    
    return-1
}

UNMAX.parseCoupon = function() {
    $('header').insert('<div id="couponTooltip">Copier et ouvrir le site</div>');

    var lastSiteClicked;
    var clipboard=null;
    var divToolTip=$("couponTooltip");

    $$(".coupon").each(function(element){
        var selecteur=element.id;
        var data = selecteur.split("-");
        var couponId=data[0];
        var siteId=data[1];
        
        var divYesVote = $$("#"+selecteur + " .inner .voting .yesVote")[0];
        var divNoVote = $$("#"+selecteur + " .inner .voting .noVote")[0];

        if(divYesVote) {
            divYesVote.onclick = function(){
                new Ajax.Request(UNMAX.context+'/ajax.jsp?command=voteyes&cid='+couponId, {
                    onSuccess: function(response) {
                        Dialog.alert("Merci de votre contribution<br/><br/>", {
                            windowParameters: {className: "spread"},
                            width:200,
                            height:70,
                            okLabel: "Fermer",
                            ok:function(win) {
                                window.location = window.location;
                            }
                        });
                    }
                });
            }
        }

        if(divNoVote) {
            divNoVote.onclick = function(){
                new Ajax.Request(UNMAX.context+'/ajax.jsp?command=voteno&cid='+couponId, {
                    onSuccess: function(response) {
                        Dialog.alert("Merci de votre contribution<br/><br/>", {
                            windowParameters: {className: "spread"},
                            width:200,
                            height:70,
                            okLabel: "Fermer",
                            ok:function(win) {
                                window.location = window.location;
                            }
                        });
                    }
                });
            }
        }

        var divComment=$("comments_"+couponId);
        var closeComment=$$("#commentActions_"+couponId+" .closeComments a")[0];
        var writeComment=$$("#commentActions_"+couponId+" .writeComment a")[0];
        var addComment=$$("#commentActions_"+couponId+" .addComment a")[0];

        if(writeComment) {
            writeComment.onclick = function() {
                Dialog.confirm($('comment_box').innerHTML, {
                    className:"spread",
                    width:400,
                    okLabel: "Soumettre",
                    cancelLabel: "Annuler",
                    onOk:function(win){
                        new Ajax.Request(UNMAX.context+'/ajax.jsp?command=postcomment&cid='+couponId+"&text="+$F('comment_text'), {
                            onSuccess: function(response) {
                                window.location = window.location;
                            }
                        });
                        return false;
                    }
                });
            }
        }

        if(addComment) {
            addComment.onclick = function() {
                Dialog.confirm($('comment_box').innerHTML, {
                    className:"spread",
                    width:400,
                    okLabel: "Soumettre",
                    cancelLabel: "Annuler",
                    onOk:function(win){
                        new Ajax.Request(UNMAX.context+'/ajax.jsp?command=postcomment&cid='+couponId+"&text="+$F('comment_text'), {
                            onSuccess: function(response) {
                                window.location = window.location;
                            }
                        });
                        return false;
                    }
                });
            }
        }
        
        if(divComment) {
            divComment.setStyle({height:"auto"});
            divComment.hide();
        }
        
        Event.observe(element, 'mouseover', function(event) {
                       
            var toggleComment=$$("#commentActions_"+couponId+" .toggleComments a")[0];

            var divCode = $$("#"+couponId+"_p strong")[0];
            
            Event.observe(divCode,"mouseover",function(event) {
                if(clipboard!= null) clipboard.destroy();
                clipboard=new ZeroClipboard.Client;
                var j=divCode.positionedOffset();
                lastSiteClicked != couponId ? divToolTip.innerHTML = "Copier et ouvrir le site" : divToolTip.innerHTML = "Copier le code";
                divToolTip.setStyle({left:j[0]+divCode.getWidth()+"px",top:j[1]+2+"px",visibility:"visible"}).show();
                clipboard.setText(divCode.innerHTML);
                clipboard.setHandCursor(true);
                clipboard.glue(divCode);

                clipboard.addEventListener("onComplete",function() {
                    divCode.setStyle({backgroundColor:"#FEBF01",borderColor:"#DBA100"});
                    divToolTip.innerHTML = ("Copié !");
                    if(lastSiteClicked!=couponId) {
                        lastSiteClicked=couponId;

                        if(!window.open(UNMAX.context+"/out/"+couponId,"merchantWindow")) {
                            window.location=UNMAX.context+"/out/"+couponId;
                        }
                    }
                });

                clipboard.addEventListener("mouseout",function() {
                    divToolTip.hide();
                    clipboard.destroy()
                });

                
            });

            if(toggleComment) toggleComment.onclick = function(j) {
                j.preventDefault();
                
                Effect.BlindDown(divComment);
                toggleComment.setStyle({display:"none"});
                closeComment.setStyle({display:"inline"});
                writeComment.setStyle({display:"inline"});
            }

            if(closeComment) closeComment.onclick = function(j) {
                j.preventDefault();

                Effect.BlindUp(divComment);
                toggleComment.setStyle({display:"inline"});
                closeComment.setStyle({display:"none"});
                writeComment.setStyle({display:"none"});
            }

            Event.stopObserving(element,"mouseover");
        })

    });
}

var Slide = Class.create({
    initialize: function(fireLinkId, elementToSlideID, elementDefaultClassName){
        this.elementToSlide = $(elementToSlideID);
        this.elementClassName = elementDefaultClassName;
        this.fireLink = $(fireLinkId);
        this.fireLink.observe('click', this.play.bind(this));
    },

    play: function(){
        this.openedItems = document.getElementsByClassName(this.elementClassName + ' opened');
        this.openedItem = this.openedItems[0];
        if(this.elementToSlide.className == this.elementClassName){
            Effect.BlindDown(this.elementToSlide);
            this.elementToSlide.className = this.elementClassName + ' opened';
            try{
                this.openedItem.hide();
                this.openedItem.className = this.elementClassName;
            }catch(err){}

        }else if(this.elementToSlide.className == this.elementClassName + ' opened'){
            Effect.BlindUp(this.elementToSlide) ;
            this.elementToSlide.className = this.elementClassName;
        }
    }


});

UNMAX.parseArticle = function(article) {
    var divYesVote = $("idLike");
    if(divYesVote) {
        divYesVote.onclick = function(){
            new Ajax.Request(UNMAX.context+'/ajax.jsp?command=ilike&id='+article, {
                onSuccess: function(response) {
                    Dialog.alert("Merci de votre contribution<br/><br/>", {
                        windowParameters: {className: "spread"},
                        width:200,
                        height:70,
                        okLabel: "Fermer",
                        ok:function(win) {
                            window.location = window.location;
                        }
                    });
                }
            });
        }
    }

    var divWriteBox = $("idWriteBox");

    if(divWriteBox) {
        divWriteBox.onclick = function() {
                Dialog.confirm($('comment_box').innerHTML, {
                    className:"spread",
                    width:400,
                    okLabel: "Soumettre",
                    cancelLabel: "Annuler",
                    onOk:function(win){
                        new Ajax.Request(UNMAX.context+'/ajax.jsp?command=postopinion&id='+article+"&text="+$F('comment_text'), {
                            onSuccess: function(response) {
                                window.location = window.location;
                            }
                        });
                        return false;
                    }
                });
            }
    }
}
