(function($){
    function op(num) {
        num = num || 0;
        var op = "-=";
        if (num < 0) {
            op = "+=";
        }
        return [op,Math.abs(num),"px"].join("");
    }

    function Single(){
        var self = this;
        self.data = {
            init: false
        };

        /*
         * navigate 
         */
        self.nav = function() {
            var nav = $("#nav_list"),
                ul = $("#nav_list ul"),
                left = $("#nav_arrow1"),
                right = $("#nav_arrow2"),
                speed = 400,
                num = 4;

            left.click(function() {
                var item = ul.find("li:visible:last").hide();
                ul.prepend(item);
                item.animate({"width":"show","opacity":"show"},speed);
            });

            right.click(function() {
                var item = ul.find("li:visible:first");
                item.animate({"width":"hide","opacity":"hide"},speed-200,function() {
                    ul.append(item);
                    item.show();
                });
            });
        };

        /*
         * home page video
         */
        self.video = function() {
            var links = $("#nav_list ul li a"),
                items = $("div.video_item"),
                speed = 800;

            links.click(function() {
                if ($("#video").is(":hidden")){
                    $("#page").fadeOut(speed,function() {
                        $("#video").fadeIn(speed);
                    });
                }
                var index = links.index(this);
                items.fadeOut(speed).delay(speed-100).eq(index).fadeIn(speed);
            });
        };


        /*
         * main gallery 
         */
        self.gallery = function(elem) {
            dom = $(elem);
            var gallery = dom.find("div.gallery"),
                gallery_left = dom.find("div.gallery_left"),
                gallery_right = dom.find("div.gallery_right"),
                gallery_show = dom.find("div.gallery_middle_show ul"),
                gallery_nail = dom.find("div.gallery_middle_list"),
                gallery_nail_link = gallery_nail.find("li a"),
                speed = 400,
                lock = false;

            var index = dom.data("index");
            if (index === null) {
               index = 0;
               dom.data('index',0);
            }

            gallery_left.click(function() {
                gallery_nail.find("li:hidden:last").animate({"width":"show","opaticy":"show"},speed);
            });

            gallery_right.click(function() {
                if (gallery_nail.find("li:visible").length > 9) {
                    gallery_nail.find("li:visible:first").animate({"width":"hide","opacity":"hide"},speed);
                }
            });

            gallery_nail_link.click(function() {
                var seq = gallery_nail_link.index(this);
                self.slide(seq,elem);
            });
        };

        /*
         * main gallery sliding
         */
        self.slide = function(index,elem) {
            dom = $(elem);
            var lists = dom.find("div.gallery_middle_list ul li");
            if (index < 0 || index === lists.length) {
                return ;
            } 
            var list = dom.find("div.gallery_middle_show ul"),
                list_width = 642,
                text = dom.find("div.gallery_text > ul"),
                text_width = 750,
                speed = 400;
            var offset = index - dom.data('index');
            dom.data("index",index); 
            list.animate({"left": op(offset*list_width)},speed);
            text.animate({"left": op(offset*text_width)},speed);
        };

        /*
         * main gallery toggle
         */
        self.gallery_map = function() {
            var video = $("#video"),
                item = video.find("div.video_item");
            item.each(function(i) {
                self.gallery(item[i]);
            });
        };

        
        /*
         * blog and page in colorbox
         */
        self.cbox = function() {
            /*
            $("a.gallery_boxes").colorbox({
                    iframe:true,
                    width: "690px",
                    height:"370px",
                    onComplete: function() {
                        $("#cboxTopLeft, #cboxTopCenter, #cboxTopRight").show();
                        $("#colorbox").css({"top":"218px"});
                    },
                    onClosed: function() {
                        $("#cboxTopLeft, #cboxTopCenter, #cboxTopRight").hide();
                    },
                    opacity:"0.57"
                });
            */
//            $("#menu_blog").colorbox({iframe:true,width: "895px",height:"690px",opacity:"0.57"});
//            $("#menu_about").colorbox({iframe:true,width: "895px",height:"500px",opacity:"0.57"});
            $('a.menu_item_link').colorbox({iframe:true,width: "895px",height:"690px",opacity:"0.57"});
        };

        /*
         * video play iframe lightbox
         */
        self.lightbox = function() {
            $("#videobox").show();
            $('#videobox_overlay').animate({opacity:0.57},0);
            $('#videobox_main').show();
            $('#videobox_close').click(function(){
                $('#videobox').hide();
                $("a.gallery_play").show();
                $("#videobox_iframe").attr('src','#');
            });
        };

        
        /*
         * video play box and button
         */
        self.videobox = function() {
            $("a.gallery_boxes,a.gallery_play").click(function() {
                self.lightbox();
                $("a.gallery_play").hide();
                $("#videobox_iframe").attr("src",$(this).attr('href'));
                return false;
            });
        };

        /*
         * social menu effects
         */
        self.menu_social = function() {
            var btn = $("#menu_socialbtn"),
                list = $("#menu_social"),
				hide_menu;
            btn.hover(function() {
                list.fadeIn();
                clearTimeout(hide_menu);				
            },function(){
			    hide_menu = setTimeout(function(){
				    list.fadeOut();
				},2000);
			});
            list.hover(function() {
                $(this).fadeIn();
				clearTimeout(hide_menu);
            },function() {
                $(this).fadeOut();
            });
        };

        /*
         * RSS Email button
         */
        self.rssmail = function(){            
            var btn = $("#rss_button"),
                box = $(".rss_box");
            btn.hover(function() {
                box.fadeIn();
            });
            box.hover(function() {
                $(this).fadeIn();
            },function() {
                $(this).fadeOut();
            });
        };

        self.init = (function() {
            self.nav();
            self.video();
            self.cbox();
            self.videobox();
            self.gallery_map();
            self.menu_social();
            self.rssmail();
            $("#nav_list li a").eq(0).trigger('click');
        })();

        return self;
    }

    $(document).ready(function(){
        window.Single = new Single();
    });

})(jQuery);
