/*Object model for NCM Medialibrary component*/
function MEDIA(id,type,parent,tpl) {
    NCMComponent.apply(this,arguments);

    this.m_albumtype = "";           //Type of medialibrary (0 - gallery on the top, 1 - gallery on the left, 2 - without preview zone)
    this.fields = new collection();  // Collection of media components (images, webcams, videos)
    this.m_maxN = 16;                // Number of images in gallery zone
    this.m_reinit = false;           // Need update size of medialibrary?
    this.sz = new Array(2);          // Size of preview zone
    this.m_curList = 1;              // Current list in gallery zone.
    this.m_curMediaId = -1;          // ID of current media object in preview
}

MEDIA.prototype = new NCMComponent;

Object.extend(MEDIA.prototype, {
    addMedia: function(id,type) {
        var obj = null;
        switch(type) {
            case 0:       //Photo
                obj = new NCMImage(id);
                break;
            case 1:       //WebCam
                obj = new NCMWebCam(id);
                break;
            case 2:       //Video
                obj = new NCMVideo(id);
                break;
            case 3:       //Flash
                obj = new NCMFlash(id);
                break;
        }
        if(obj!=null)
            this.fields.add(""+id,obj);
    },

    getMedia: function(id) {
        return this.fields.get(""+id);
    },

    setAlbumType: function(val) {
        this.m_albumtype = val;
    },

    getAlbumType: function() {
        return this.m_albumtype;
    },

    getListsNumber: function() {
        var res = (this.fields.size()-(this.fields.size()%this.m_maxN))/this.m_maxN;
        if(this.fields.size()%this.m_maxN>0) res++;
        return res;
    },

    setGallerySize: function(size) {
        this.m_maxN = size;
    },

    reInit: function() {
        return this.m_reinit;
    },

    setAlbumSize: function() {
        var alb=$("photos"+this.m_id);
        if(alb!=null) {
            var MAXWIDTH_LAT = parseInt(this.getTextResource("MAXWIDTH_LAT"));
            var GALWIDTH_LAT = parseInt(this.getTextResource("GALWIDTH_LAT"));
            var GALWIDTH_NOLAT = parseInt(this.getTextResource("GALWIDTH_NOLAT"));
            var PADDING = parseInt(this.getTextResource("PADDING"));
            var MINGALHEIGHT = parseInt(this.getTextResource("MINGALHEIGHT"));
            var MAX_IMGWIDTH=parseInt(this.getTextResource("MAX_IMGWIDTH"));

            var ww=alb.getDimensions().width;
            var mThumb=$("listphotos"+this.m_id);
            if(mThumb!=null) {
                if(this.isGalleryLeft(mThumb))
                    mThumb.style.width = (ww<MAXWIDTH_LAT?GALWIDTH_LAT:GALWIDTH_NOLAT)+"px";
                else
                    mThumb.style.width = ww + "px";
            }

            var mPrev=$("previewphoto"+this.m_id);
            if(mPrev!=null) {
                var w = this.isGalleryRight(mPrev)?ww-(ww<MAXWIDTH_LAT?GALWIDTH_LAT:GALWIDTH_NOLAT)-PADDING:ww;
                mPrev.style.width=w-15 + "px";

                w = this.isGalleryRight(mPrev)?w-w%24:(w-w%24)-24;
                if(w>MAX_IMGWIDTH) w=MAX_IMGWIDTH;
                var h=w*2/3;

                if($("previmgcont"+this.m_id)!=null)
                    $("previmgcont"+this.m_id).style.height=(h+8)+"px";

                this.sz[0]=w-4;
                this.sz[1]=h;
            }

            if($("listnavcont"+this.m_id)!=null)
                $("listnavcont"+this.m_id).style.width=ww+"px";

            return true;
        }
    },

    movePreviousList: function() {
        var curPos=this.fields.getIndex(""+this.m_curMediaId);
        if(curPos<1) return;
        this.setList(--curPos);
    },

    moveNextList: function() {
        var curPos=this.fields.getIndex(""+this.m_curMediaId);
        var total=this.fields.size();
        if(curPos==total-1) return;
        this.setList(++curPos);
    },

    setList: function(idlist) {
        //idlist - is index of the image in the gallery
        idlist++;
        var totalgr=this.getListsNumber();
        var idgroup=this.getListIndex(idlist);

        for(var i=1;i<=totalgr;i++) {
            var myUl=$("gallery"+this.m_id+"_"+i);
            if(myUl!=null) {
                var isLeft=this.isGalleryLeft(myUl);
                myUl.className = this.getTextResource("gallery_class")+(i==idgroup?"":this.getTextResource("gallery_sufix_hidden"))+(isLeft?this.getTextResource("gallery_sufix_left"):"");
            }
        }
        if(this.fields.size() != 0) {
            var idMed = this.fields.item(idlist-1).getId();
            this.setPreviewMedia(idMed);
        }
    },

    setPreviewMedia: function(id) {
        if(this.m_curMediaId==id) return;
        var myThumb=$("thumb"+id);
        if(myThumb!=null) {
            var parent=myThumb.parentNode;
            var myPrev=null;
            if(parent!=null) {
                var listId = parent.getAttribute("id");
                listId=listId.replace(/gallery/ig,"");
                var pos=listId.indexOf("_");
                listId=listId.substr(pos+1);

                if(this.m_reinit) {
                    if(!this.setAlbumSize())
                        return;
                    else
                        this.m_reinit = false;
                }

                myPrev=$("previmgcont"+this.m_id);
                if(myPrev!=null) {
                    var strObj = this.getMedia(id).getCode();
                    if(strObj.indexOf("OBJECT")!=-1) {
                        strObj=strObj.replace(/WIDTH="168"/ig,'WIDTH="'+this.sz[0]+'"');
                        strObj=strObj.replace(/HEIGHT="112"/ig,'HEIGHT="'+this.sz[1]+'"');
                    }
                    Element.update(myPrev,strObj);

                    var child=myPrev.firstChild;
                    if(child!=null) {
                        if(child.nodeName.toUpperCase()=="OBJECT") {
                            child.style.width=this.sz[0]+"px";
                            child.style.height=this.sz[1]+"px";
                            child.style.left=(NCMPage.findPosX(myPrev)+(!this.isGalleryRight(myPrev)?16:0))+"px";
                        } else {
                            var hh=0,h0=0,w0=0;
                            if (this.getMedia(id) instanceof NCMImage || this.getMedia(id) instanceof NCMFlash) {
                                h0=this.getMedia(id).getHeight();
                                w0=this.getMedia(id).getWidth();
                                hh=h0;
                            } else
                                hh=this.sz[1];

                            if(hh>this.sz[1]) hh=this.sz[1];
                            child.style.height=hh+"px";

                            if(w0>h0)
                                child.style.width=hh*3/2+"px";
                            child.style.top=((this.sz[1]-hh)-(this.sz[1]-hh)%2)/2+"px";
                        }
                        child.style.textAlign="center";
                    }
                }

                if($("previewcap"+this.m_id)!=null) {
                    var foot = this.getMedia(id).getFooter();
                    Element.update("previewcap"+this.m_id,foot);
                    if(trim(foot)=="")
                        Element.hide("previewcap"+this.m_id);
                    else
                        Element.show("previewcap"+this.m_id);
                }

                this.m_curMediaId = id;
                this.m_curList = eval(listId);

                this.refreshListNavigation();
            }
        }
        this.setSelected(id);
    },

    loadPhoto: function() {
        this.openMediaInWindow(this.m_curMediaId);
    },

    sendPhoto: function() {
        var photo=$("photoimg"+this.m_curMediaId);
        if(photo!=null) {
            var url=photo.getAttribute("src");
            window.open('postcard.jsp?IMG='+url,'SEND POSTCARD','height=512,width=768,status=yes,toolbar=no,menubar=no,location=no');
        }
    },

    openMediaInWindow: function(id) {
        var photo=$("photoimg"+id);
        if(photo!=null) {
            var strObj = this.getMedia(id).getCode();
            if (strObj.indexOf("OBJECT")==-1 && strObj.indexOf("object")==-1) {
                var url=this.getMedia(id).getDownload();
                if(url=="") url=photo.getAttribute("src");
                openWindow(url);
            } else {
                strObj=strObj.replace(/WIDTH="168"/ig,'WIDTH="408"');
                strObj=strObj.replace(/HEIGHT="112"/ig,'HEIGHT="272"');
                strObj=strObj.replace(/width=168/ig,'width=408');
                strObj=strObj.replace(/height=112/ig,'height=272');

                var generator=window.open('');
                generator.document.write('<html><head><title>'+this.getMedia(id).getFooter()+'</title>');
                generator.document.write('</head><body>');
                generator.document.write(strObj);
                generator.document.write('</body></html>');
                generator.document.close();
            }
        }
    },

    isGalleryLeft: function(elem) {
        var isLeft=-1;
        var elemclass=elem.className;
        if(elemclass!=null)
            isLeft=elemclass.indexOf(this.getTextResource("gallery_sufix_left"));
        return (isLeft>=0);
    },

    isGalleryRight: function(elem) {
        var isRight=-1;
        var elemclass=elem.className;
        if(elemclass!=null)
            isRight=elemclass.indexOf(this.getTextResource("gallery_sufix_right"));
        return (isRight>=0);
    },

    refreshListNavigation: function () {
        var total=this.fields.size();
        var curPos=this.fields.getIndex(""+this.m_curMediaId);

        var lstNav=$("listnav"+this.m_id);
        if(lstNav!=null) {
            var m_html=this.formListPages(curPos,total);

            var oldHTML=lstNav.innerHTML.toLowerCase();
            var pos1=oldHTML.indexOf("<li",0);
            pos1 = oldHTML.indexOf("<li",pos1+"<li".length);
            var text1="";
            if(pos1>=0) text1=lstNav.innerHTML.substr(0,pos1);
            var pos2=oldHTML.lastIndexOf("<li");
            var text2="";
            if(pos2>=0) text2=lstNav.innerHTML.substr(pos2);

            lstNav.innerHTML=text1+m_html+text2;

            if(this.m_parent.getBrowser()=="Internet Explorer")
                lstNav.style.marginBottom = "0px";
        }
    },

    formListPages: function(curPos,total) {
        var arPg=NCMPage.formNavListPagesAdv(curPos,total,3);

        var m_html="";
        for(var j=0;j<arPg.length;j++){
            if(arPg[j]!=curPos.toString() && arPg[j]!="")
                m_html+="<li style=\"background-image:none;\"><a href=\"noscript.html\" onclick=\"ncmMngr.getComponent("+this.m_id+").setList("+arPg[j]+"); return false;\" onkeypress=\"ncmMngr.getComponent("+this.m_id+").setList("+arPg[j]+"); return false;\">"+(parseInt(arPg[j])+1)+"</a></li>\n";
            else
                m_html+="<li style=\"background-image:none;\">"+(arPg[j]!=""?"<em>":"")+(arPg[j]!=""?""+(parseInt(arPg[j])+1):"...")+(arPg[j]!=""?"</em>":"")+"</li>\n";
        }
        return m_html;
    },

    setSelected: function(id) {
        var myThumb=$("thumb"+id);
        if(myThumb!=null) {
            var isLeft=this.isGalleryLeft(myThumb);
            var actClass = this.getTextResource("thumb_class")+this.getTextResource("thumb_sufix_active")+(isLeft?this.getTextResource("gallery_sufix_left"):"");
            var normClass = this.getTextResource("thumb_class")+(isLeft?this.getTextResource("gallery_sufix_left"):"");

            var actEls = document.getElementsByClassName(actClass,myThumb.parentNode);
            if(actEls.length) actEls[0].className=normClass;
            myThumb.className=actClass;
        }
    },

    getListIndex: function(index) {
        var res = (index-(index%this.m_maxN))/this.m_maxN;
        if(index%this.m_maxN>0) res++;
        return res;
    }
});

var NCMMedia = Class.create();

NCMMedia.prototype = {
    initialize: function(id) {
        this.m_id = id;
        this.m_foot = "";
        this.m_code = "";
        this.m_h = 0;
        this.m_w = 0;
        this.m_download = "";
    },

    getId: function() {
        return this.m_id;
    },

    getFooter: function() {
        return this.m_foot;
    },

    getCode: function() {
        return this.m_code;
    },

    getHeight: function() {
        return this.m_h;
    },

    getWidth: function() {
        return this.m_w;
    },

    getDownload: function() {
        return this.m_download;
    },

    initMedia: function() {
    }
}

var NCMImage = Class.create();
Object.extend(NCMImage.prototype,NCMMedia.prototype);
Object.extend(NCMImage.prototype,{
    initMedia: function(strFoot,srcCode,h,w,downloadUrl) {
        this.m_foot = strFoot;
        this.m_code = srcCode;
        this.m_h = h;
        this.m_w = w;
        if(typeof(downloadUrl)!="undefined")
            this.m_download = downloadUrl;
    },

    initImage: function(strFoot,srcCode,h,w,downloadUrl) {
        this.initMedia(strFoot,srcCode,h,w,downloadUrl);
    }
});

var NCMWebCam = Class.create();
Object.extend(NCMWebCam.prototype,NCMMedia.prototype);
Object.extend(NCMWebCam.prototype, {
    initMedia: function(strFoot,srcCode,downloadUrl) {
        this.m_foot = strFoot;
        this.m_code = srcCode;
        if(typeof(downloadUrl)!="undefined")
            this.m_download = downloadUrl;
    },

    initWebcam : function(strFoot,srcCode,downloadUrl) {
        this.initMedia(strFoot,srcCode,downloadUrl);
    }
});

var NCMVideo = Class.create();
Object.extend(NCMVideo.prototype,NCMMedia.prototype);
Object.extend(NCMVideo.prototype, {
    initMedia: function(strFoot,srcCode,downloadUrl) {
        this.m_foot = strFoot;
        this.m_code = srcCode;
        if(typeof(downloadUrl)!="undefined")
            this.m_download = downloadUrl;
    },

    initVideo: function(strFoot,srcCode,downloadUrl) {
        this.initMedia(strFoot,srcCode,downloadUrl);
    }
});

var NCMFlash = Class.create();
Object.extend(NCMFlash.prototype,NCMMedia.prototype);
Object.extend(NCMFlash.prototype,{
    initMedia: function(strFoot,srcCode,h,w,downloadUrl) {
        this.m_foot = strFoot;
        this.m_code = srcCode;
        this.m_h = h;
        this.m_w = w;
        if(typeof(downloadUrl)!="undefined")
            this.m_download = downloadUrl;
    },

    initFlash: function(strFoot,srcCode,h,w,downloadUrl) {
        this.initMedia(strFoot,srcCode,h,w,downloadUrl);
    }
});
/*end object model for NCM Medialibrary component*/
