﻿/// <reference path="../js/jquery-1.3.2.min.js" />
/// <reference path="../js/jquery.watermark.js" />
/// <reference path="../js/jquery.utils.js" />
/// <reference path="../js/jquery.cs_twitter.js" />
/// <reference path="../js/google_cse.js" />

tabcontrol = function(controlid) {
    this.control = $(controlid);

    this.init();
};

tabcontrol.prototype = {
    options: {
        rotationTime: 5000
    },
    currentIndex: -1,
    tabsHeader: null,
    tabsContent: null,
    rotationId: null,
    init: function() {
        var me = this;

        this.control.children('.tab-content-shell:gt(0)').hide();
        var header = $(this.control.children('#headers'));
        var ul = $(header.children('ul'));
        this.tabsHeader = $(ul.children('li'));
        this.tabsHeader.each(function(i) {
            this.index = i;
            $(this).click(function() {
                me.changeIndex(this.index);
            });

            $(this).mouseenter(function() {
                me.enterTab();
            });

            $(this).mouseleave(function() {
                me.leaveTab();
            });
        });
        this.tabsContent = $(this.control.children('.tab-content-shell'));
        this.changeIndex(0);

        this.rotationId = setTimeout(function() {
            me.rotate();
        }, this.options.rotationTime);
    },
    changeIndex: function(index) {

        if (this.currentIndex == index)
            return;

        if (this.currentIndex >= 0) {
            $(this.tabsContent[this.currentIndex]).hide();
            $(this.tabsHeader[this.currentIndex]).removeClass("selected");
        }

        $(this.tabsContent[index]).show();
        $(this.tabsHeader[index]).addClass("selected");

        this.currentIndex = index;

    },
    rotate: function() {
        var me = this;

        if ((this.currentIndex + 1) == this.tabsHeader.length) {
            this.changeIndex(0);
        }
        else {
            this.changeIndex(this.currentIndex + 1);
        }

        this.rotationId = setTimeout(function() {
            me.rotate();
        }, this.options.rotationTime);
    },
    enterTab: function() {
        clearTimeout(this.rotationId);
    },
    leaveTab: function() {
        var me = this;
        this.rotationId = setTimeout(function() {
            me.rotate();
        }, this.options.rotationTime);
    }
};

function roundTabCorner(ctrl) {
    var node = $(ctrl);

    node.each(function() {
        var node = $(this);

        var parent = node.parent();

        node.remove();

        var newcontainer = $('<div class="tab-content-shell" style="position:relative"></div>');

        newcontainer.append('<div style="background-image: url(images/tab_top_left.png); width: 7px; height: 6px; position: absolute; top: 0px; left: 0px"></div>');
        newcontainer.append('<div style="background-image: url(images/tab_top_right.jpg); width: 5px; height: 6px; position: absolute; top: 0px; right: 0px"></div>');
        newcontainer.append(node);
        newcontainer.append('<div style="background-image: url(images/tab_bottom_left.png); width: 7px; height: 7px; position: absolute; bottom: 0px; left: 0px"></div>');
        newcontainer.append('<div style="background-image: url(images/tab_bottom_right.jpg); width: 5px; height: 7px; position: absolute; bottom: 0px; right: 0px"></div>');

        parent.append(newcontainer);
    });
}

function twitterDataFormat(obj) {
    return obj.Message + " POSTED " + obj.CreationTime.format("dd.mm.yyyy hh:MM TT");
}

function twitterOnEnd(obj, status, arg1, arg2) {
    if (status != 'success') {
        alert(arg1);
        alert(arg2);
    }
}


$(document).ready(function() {
    roundTabCorner('.tab-content');

    var control = new tabcontrol('#newtabs');

    $("#twitter").twitterCached({ondatarowbound: twitterDataFormat, count:2});
});