﻿if (typeof (window.ceemeagain) == "undefined") window.ceemeagain = new Object();
if (typeof (ceemeagain.photoalbum) == "undefined") {
    ceemeagain.photoalbum = {
        itemArr: new Array(),
        currentPhoto: 0,
        catid: 0,
        interval: null,
        viewallcomment: false,
        comment: {
            itemArr: new Array(),
            showcomment: function(photoid) {
                var html = "";
                for (var i = 0; i < this.itemArr.length; i++) {
                    if (this.itemArr[i].photoid != photoid) continue;
                    html += '<div class="item">';
                    html += '<div class="avatar">' + this.itemArr[i].avatar + '</div>';
                    html += '<div class="cm">';
                    html += '<div class="name">';
                    html += '<span>' + this.itemArr[i].commentby + '</span> posted comment on ' + this.itemArr[i].createdate + '</div>';
                    html += '<div class="detail">' + this.itemArr[i].comment + '</div>';
                    if ($.trim(this.itemArr[i].thumb).length > 0)
                        html += '<div class="photothumb" style="background: url(' + this.itemArr[i].thumb + ') center center no-repeat;cursor:pointer;" onclick="javascript:ceemeagain.photoalbum.selectPhotoId(' + this.itemArr[i].photoid + ');"></div>';
                    if (this.itemArr[i].candelete)
                        html += '<div class="command"><a href="javascript:ceemeagain.photoalbum.deletecomment(' + this.itemArr[i].id + ');">Delete this comment?</a></div>';
                    html += '</div>';
                    html += '</div>';
                    html += '<div class="clear">';
                    html += '</div>';
                }
                $(".photocomment .comment").html(html);
                $('.avatar48x48,.avatar75x75,.avatar154x115').unbind("mouseover").mouseover(function() {
                    $('.social-link', this).show();
                }).unbind("mouseout").mouseout(function() {
                    $('.social-link', this).hide();
                });
            },
            viewallcomment: function() {
                ceemeagain.photoalbum.viewallcomment = true;
                var html = "";
                for (var i = 0; i < this.itemArr.length; i++) {
                    html += '<div class="item">';
                    //html += '<div class="avatar48x48" style="background: url(' + this.itemArr[i].avatar + ') center center no-repeat;"></div>';
                    html += '<div class="avatar">' + this.itemArr[i].avatar + '</div>';
                    html += '<div class="cm">';
                    html += '<div class="name">';
                    html += '<span>' + this.itemArr[i].commentby + '</span> posted comment on ' + this.itemArr[i].createdate + '</div>';
                    html += '<div class="detail">' + this.itemArr[i].comment + '</div>';
                    if ($.trim(this.itemArr[i].thumb).length > 0)
                        html += '<div class="photothumb" style="background: url(' + this.itemArr[i].thumb + ') center center no-repeat;cursor:pointer;" onclick="javascript:ceemeagain.photoalbum.selectPhotoId(' + this.itemArr[i].photoid + ');"></div>';
                    if (this.itemArr[i].candelete)
                        html += '<div class="command"><a href="javascript:ceemeagain.photoalbum.deletecomment(' + this.itemArr[i].id + ');">Delete this comment?</a></div>';
                    html += '</div>';
                    html += '</div>';
                    html += '<div class="clear">';
                    html += '</div>';
                }
                $(".photocomment .comment").html(html);
                $('.avatar48x48,.avatar75x75,.avatar154x115').unbind("mouseover").mouseover(function() {
                    $('.social-link', this).show();
                }).unbind("mouseout").mouseout(function() {
                    $('.social-link', this).hide();
                });
            }
        },
        deletecomment: function(commentid) {
            var $this = this;
            if (confirm("Do you want to delete this comment?")) {
                setTimeout(function() {
                    $(".photocomment .comment").unblock().block({ message: $("<div>loading...</div>").css({ border: "10px solid #EEE", padding: "5px" }) });
                    $.post("/Photo/DeleteComment", { commentid: commentid, cache: false }, function(data) {
                        if (data.iserror) {
                            alert(data.message);
                        } else {
                            $this.comment.itemArr = eval(data.data);
                            if ($this.viewallcomment) {
                                $this.comment.viewallcomment();
                            } else {
                                $this.comment.showcomment($this.itemArr[$this.currentPhoto - 1].id);
                            }

                        }
                        $(".photocomment .comment").unblock();
                    }, "json");
                }, 100);
            }
        },
        postcomment: function(the) {
            //check login
            var _checkLogin = function() {
                var isLogin = $("#cmdSignOut").length == 1;
                if (!isLogin) { $('#dialogLogin').dialog('open'); }
                return isLogin;
            }
            if (!_checkLogin()) {
                alert("Ooops! You're not logged in.");
                return;
            }
            var form = null;
            for (var el = $(the); (el != null) ? el[0].tagName.toLowerCase() != "form" : false; form = el = el.parent());
            var comment = $.trim(form.find("textarea").val());

            if (comment.length == 0) {
                alert("Ooops! Please enter your message.");
                return;
            }

            var $this = this;
            setTimeout(function() {
                $(".commentform").unblock().block({ message: $("<div>loading...</div>").css({ border: "10px solid #EEE", padding: "5px" }) });
                $.post("/Photo/PostComment", { albumid: $this.catid
                                           , photoid: ($this.viewallcomment ? null : $this.itemArr[$this.currentPhoto - 1].id)
                                           , comment: comment
                                           , cache: false
                }, function(data) {
                    if (data.iserror) {
                        alert(data.message);
                    } else {
                        form.find("textarea").val('');
                        ceemeagain.photoalbum.comment.itemArr = eval(data.data);
                        if ($this.viewallcomment) {
                            ceemeagain.photoalbum.comment.viewallcomment();
                        } else {
                            ceemeagain.photoalbum.comment.showcomment($this.itemArr[$this.currentPhoto - 1].id);
                        }

                    }
                    $(".commentform").unblock();
                }, "json");
            }, 100);
            return false;
        },
        reset: function(the) {
            var form = null;
            for (var el = $(the); (el != null) ? el[0].tagName.toLowerCase() != "form" : false; form = el = el.parent());
            form.find("textarea").val('');
        },
        next: function() {
            if (this.currentPhoto + 1 > this.itemArr.length)
                this.selectPhoto(1);
            else this.selectPhoto(this.currentPhoto + 1);
        },
        prev: function() {
            if (this.currentPhoto - 1 <= 0)
                this.selectPhoto(this.itemArr.length);
            else this.selectPhoto(this.currentPhoto - 1);
        },
        applyHtml: function() {
            $(".list .sc_menu_wrapper .sc_menu").html('');

            var str = "";
            for (var i = 0; i < this.itemArr.length; i++) {
                var title = $("<div></div>").text(this.itemArr[i].title).text();
                str += '<a title="' + title + '" href="#' + (i + 1) + '">';
                str += '<img src="' + this.itemArr[i].photo + '" alt="' + title + '" title="' + title + '" /></a>';
            }
            $(".list .sc_menu_wrapper .sc_menu").html(str);
        },
        selectPhotoId: function(id) {
            for (var i = 1; i <= this.itemArr.length; i++) {
                if (this.itemArr[i - 1].id === id) {
                    this.selectPhoto(i);
                    return;
                }
            }
            this.selectPhoto(1);
        },
        selectPhoto: function(index) {
            this.viewallcomment = false;
            if (index < 1) index = 1;
            if (index > this.itemArr.length) index = this.itemArr.length;
            if (index == 0) return;
            if (this.currentPhoto == index) return;
            this.currentPhoto = parseInt(index);
            $(".sc_menu a").removeClass('selected').eq(index - 1).addClass('selected');
            var html = '<a href="' + this.itemArr[index - 1].fullsize + '" target="_new"><img src="' + this.itemArr[index - 1].photo2 + '" alt="' + encodeURIComponent(this.itemArr[index - 1].title) + '" title="' + encodeURIComponent(this.itemArr[index - 1].title) + '" /></a>';
            $("#photocontainer .largeimage .rating").html(this.itemArr[index - 1].votehtml.html);
            var photoid = this.itemArr[index - 1].id;
            $("#photocontainer .largeimage .rating .bgrating").mousemove(function(ev) {
                var $this = $(this);

                var pos = parseInt(ev.pageX) - parseInt($this.offset().left);
                var w = pos;
                var rating = 0;
                if (w >= 0 && w <= 19) {
                    w = 19;
                    rating = 1;
                }
                else if (w >= 20 && w <= 36) {
                    w = 36;
                    rating = 2;
                }
                else if (w >= 37 && w <= 55) {
                    w = 55;
                    rating = 3;
                }
                else if (w >= 56 && w <= 72) {
                    w = 72;
                    rating = 4;
                }
                else if (w >= 73 && w <= 92) {
                    w = 92;
                    rating = 5;
                }
                $(".bevotingrating").css("width", w + 'px');
                $(".bevotingrating").unbind('click').click(function() {
                    $.post("/Photo/VotePhotoItem", { id: photoid, point: rating, cache: false },
                    function(data) {
                        if (data.iserror) {
                            alert(data.message);
                        } else {
                            alert("Thank you for your vote +" + rating + "!\nYou can vote this photo again tomorrow :)");
                            ceemeagain.photoalbum.itemArr[index - 1].votehtml = data.data;
                            $("#photocontainer .largeimage .rating").html(data.data.html);
                        }
                    }, "json");
                });
            })
            .mouseout(function() {
                $(".bevotingrating").css("width", "0%");
            });

            $("#photocontainer .largeimage .image").html(html);
            $("#photocontainer .largeimage .name").text(this.itemArr[index - 1].title);
            $("#photocontainer .largeimage .info").text(this.itemArr[index - 1].view + ' views, ' + this.itemArr[index - 1].comment + ' comments');
            ceemeagain.photoalbum.comment.showcomment(this.itemArr[index - 1].id);
            $.post("/Photo/ViewPhoto/", { albumid: this.catid, photoid: this.itemArr[index - 1].id }, function(data)
            { });
            window.location.hash = '#' + index;
        },
        initAlbum: function() {
            this.applyHtml();
            if (this.interval != null) clearInterval(this.interval);
            this.interval = setInterval(function() { ceemeagain.photoalbum.checkHash(); }, 1000);
            makeScrollable("div.sc_menu_wrapper", "div.sc_menu");
        },
        checkHash: function() {
            var hash = window.location.hash;
            var myregexp = /#([0-9]+)$/;
            var match = myregexp.exec(hash);
            if (match != null) {
                this.selectPhoto(parseInt(match[1]));
            } else {
                this.selectPhoto(this.currentPhoto);
            }
        }
    };
}