var InformationDot = function(obj) { this._initialize(obj) };
InformationDot.prototype = {
    
    queue: new Array(),
    delay: 0,
    speed: 500,
    hs: window.hs,
    parentWindowId: "",
    createdElements: new Array(),
    startIndex: 0,
    status: new Array(),
    currentIndex: 0,

   _initialize: function(opt) {
        this.queue = opt.animaticonz;
        this.delay = opt.delay | 0;
        this.speed = opt.speed | 500;
        // MODES:
        //  1 - Standard
        //  2 - Dots pulsate instead of open
        //  3 - Same as 2, and text appears in a specified area
        this.mode = opt.mode;
        this.parentWindow = opt.parentWindow;
        this._createElements();
        this._run();
    },
    
    _createElements: function() {
        var self = this;
        for(var i = 0; i < this.queue.length; i++) {
            var element = document.createElement("a");
            element.id = (self.queue[i].altId != undefined && self.queue[i].altId != "")?self.queue[i].altId:"Tag"+i;
            element.title = "Klikk for detaljer";
            element.href = self.queue[i].zoomImage;
            element.style.left = self.queue[i].xPos+"px";
            element.style.top = self.queue[i].yPos+"px";
            element.onclick = function(){
                //jQuery('#Content div.frontimg a').stop().css('opacity','1');
                return hs.expand(this, frontimgtags);
            };
            this.createdElements.push(element.id);
            this.status.push(false);
            this.parentWindow.append(element);
            this._setObservers(i, element.id);
            if(self.mode != 1 && jQuery.browser.msie && jQuery.browser.version >= 9) {
                this._pulseInfoDot(i);
            }else if(self.mode != 1 && !jQuery.browser.msie){
                this._pulseInfoDot(i);
            }
        }
        //console.log(this.queue);
        
    },
    
    _setObservers: function(i, elementID) {
        var self = this;
        if(self.mode == 1 || self.mode == 2) {
            this.hs.registerOverlay({
                thumbnailId: 'Tag'+i,
                html: "<p class='frontimgtag textbox'>"+self.queue[i].html+"</p>",
                offsetX: self.queue[i].offsetX,
                offsetY: self.queue[i].offsetY,
                position: 'top right',
                hideOnMouseOut: false,
                opacity: 0.8
            });
        }else{
            var tag = jQuery('#TagXTextbox').clone().appendTo('#Content div.funksjonsdemoimg');
            
            tag.attr('id', 'Tag' + i + 'Textbox')
                .css('display', 'none')
                .children('h6')
                    .html(self.queue[i].title)
                .siblings('div')
                    .html(self.queue[i].html)
                .siblings('img')
                    .attr('src', self.queue[i].tipImage)
                    .attr('alt', self.queue[i].tipAlt)
                    .attr('title', self.queue[i].tipTitle)
            ;
            
            var prev = (elementID > 0) ? true : false;
            var next = (elementID < (self.queue.length-1)) ? true : false;
            //this._updateNav(prev, next, tag, elementID);
        }
       // this._updateNav();
    },
    
    _updateNav: function(prev, next, tag, elementID) {
        var $contentBoxes = jQuery("div.funksjonsdemoimg div.textbox");
        var $prevBtns = jQuery("div.funksjonsdemoimg a.back");
        var $nextBtns = jQuery("div.funksjonsdemoimg a.next");
        currentIndex = 0;
        var total = $contentBoxes.size()-2;
        var self = this;
        $contentBoxes.hide().filter("#Tag0Textbox").show();
        $nextBtns.click(function(){
            if(window.isAnimating == 0) {
                //console.log(currentIndex);
                self._closeInfoDot(currentIndex);
                currentIndex++;
                if(currentIndex > total)
                    currentIndex = 0;
                $contentBoxes.hide();
                $contentBoxes.filter("#Tag"+currentIndex+"Textbox").show();
                self._openInfoDot(currentIndex);
            }    
        });
        $prevBtns.click(function(){
            if(window.isAnimating == 0) {
                self._closeInfoDot(currentIndex);
                currentIndex--;
                if(currentIndex < 0)
                    currentIndex = total;
                $contentBoxes.hide();
                $contentBoxes.filter("#Tag"+currentIndex+"Textbox").show();
                self._openInfoDot(currentIndex);
            }
        });
    },
    
    jumpTo: function(i) {
        var $contentBoxes = jQuery("div.funksjonsdemoimg div.textbox");
        $contentBoxes.hide();
        $contentBoxes.filter("#Tag"+i+"Textbox").show();
        this._openInfoDot(i);
        currentIndex = i;
        //console.log(i);
    },
    
    _run: function() {
        var self = this;
        if(self.mode == 1) {
            var totalAnimations = this.queue.length;
            var delay = (this.status[self.startIndex] == false)?this.queue[self.startIndex].opened:this.queue[self.startIndex].closed;
            var timer = setTimeout(function() {
                
                if(self.status[self.startIndex] == false) {
                    self._openInfoDot(self.startIndex);
                    self.status[self.startIndex] = true;
                }
                else {
                    self._closeInfoDot(self.startIndex);
                    self.startIndex++;
                }
                //console.log(self.startIndex);
                //console.log(self.queue[self.startIndex]);
                //console.log(totalAnimations);
                if(self.startIndex == totalAnimations)
                    clearTimeout(timer);
                else {
                    
                    self._run();
                }
                
            },delay)
        }else if(self.mode == 3){
            var tag = jQuery("#Tag" + self.startIndex + "Textbox");
            self._openInfoDot(self.startIndex);
            jQuery("#Content div.funksjonsdemoimg div.textbox").hide();
            tag.show();
            this._updateNav(false, true, jQuery("#Tag" + self.startIndex + "Textbox"), "Tag" + self.startIndex);
        }
    },
    
    _pulseInfoDot: function(i) {
        jQuery(document.getElementById(this.createdElements[i])).pulse({
            opacity: [0.3,1]
        }, {
            duration: 1000, // duration of EACH individual animation
            times: 99999, // Will go three times through the pulse array [0,1]
            easing: 'linear', // easing function for each individual animation
            complete: function() {
                
            }
        });
    },
    
    _openInfoDot: function(i) {
        this.hs.expand(document.getElementById(this.createdElements[i]), window.frontimgtags);
    },
    
    _closeInfoDot: function(i) {
        this.hs.close(document.getElementById(this.createdElements[i]));
    }
};
