/// <reference path="../jquery-1.3.2.min-vsdoc.js" />

//PLUGIN FUNCTIONS
//Menu functions
$.fn.trinityMenu = function(options) {
    //settings
    var settings = $.extend({
        selectorExtension: '.GROUP',
        hideNode: [],
        removeSiblingNode: []
    }, options);

    //global
    var curarr;

    //init
    var $t = $(this);

    //settings implementation
    if ($.isArray(settings.removeSiblingNode)) {
        curarr = settings.removeSiblingNode;
        for (var i in curarr) {
            $t.find(settings.selectorExtension + curarr[i]).siblings("ul").remove();
        }
    }
    if ($.isArray(settings.hideNode)) {
        curarr = settings.hideNode;
        for (var i in curarr) {
            $t.find(settings.selectorExtension + curarr[i]).hide();
        }
    }

    //hiding and showing sub categories
    $t.find('ul').hide();
    $t.find('li:not(li:has(ul)):not(li ul li) a').css({ 'background-image': 'url()' });
    var cssbg = $t.find('a#active').css('background-image');
    if (cssbg != undefined) {
        $t.find('li:has(ul) a#active:not(li ul li a#active)').css({ 'background-image': cssbg.replace('_closed', '_open') }).siblings('ul').show();
    }
    cssbg = $t.find('li ul li a#active').parents('ul:first').show().siblings('a').css('background-image');
    if (cssbg != undefined) {
        $t.find('li ul li a#active').parents('ul:first').show().siblings('a').css({ 'background-image': cssbg.replace('_closed', '_open') });
    }
}

//Splash functions
$.fn.splash = function(options) {
    //settings
    var settings = $.extend({
        splashThis: '#splashpanel',
        closeButton: '.closebutton'
    }, options);

    //init
    var panel = $(settings.splashThis);

    //global
    var st; //scrollTop
    var wh; //window height
    var ww; //window width
    var ph; //panel height
    var pw; //panel width
    var py; //new y pos
    var px; //new x pos

    //event
    this.click(function() {
        st = $('body, html').scrollTop();
        wh = $(window).height();
        ww = $(window).width();

        panel.css({
            'position': 'absolute',
            'z-index': '1001'
        });

        ph = panel.outerHeight();
        pw = panel.outerWidth();
        py = Math.floor((st + (wh / 2)) - (ph / 2)) + 'px';
        px = Math.floor((ww / 2) - (pw / 2)) + 'px';

        //overlay
        $('<div></div>').css({
            'position': 'absolute',
            'background': '#000',
            'z-index': '1000',
            'top': '0',
            'left': '0',
            'width': $('body').outerWidth() + 'px',
            'height': $('body').outerHeight() + 'px'
        }).attr('id', 'overlay').appendTo('body').fadeTo(0, 0.5);

        //showing panel
        panel.css({
            'top': py,
            'left': px
        }).show();

        //close button event
        $(panel.find(settings.closeButton)).click(function() {
            panel.hide();
            $("#overlay").remove();
        });

        return false;
    });

    return this;
}

//Image functions
$.fn.smallSlideshow = function(options) {
    //settings
    var settings = $.extend({
        animationspeed: 200,
        fadespeed: 200,
        showitems: 5,
        gotourl: false,
        bigimagelink: '.slideshowbigimagelink',
        bigimage: '.slideshowbigimage',
        backbutton: '.slideshowback',
        forwardbutton: '.slideshowforward'
    }, options);

    //init settings
    var bigimagelink = $(settings.bigimagelink);
    var bigimage = $(settings.bigimage);
    var back = $(settings.backbutton);
    var forward = $(settings.forwardbutton);

    //init lightbox
    bigimagelink.lightBox();
    
    //global
    var currentpos;
    var nextpos;
    var backbg = back.css('background-image');
    var forwardbg = forward.css('background-image');

    //selectors
    var obj = this;
    var items = $(this).children('li');
    var imglink = items.children('a');

    //math
    var itemWidth = parseInt(items.filter(':first').outerWidth()) + parseInt(items.filter(':first').css('margin-right'));
    var slideWidth = items.length * itemWidth;
    var maxSlidePos = ((settings.showitems * itemWidth) - slideWidth);

    //init buttons
    back.css('background-image', 'none');
    forward.css('background-image', 'none');

    //if thumbnail image should de displayed in a big image container
    if (!settings.gotourl) {
        imglink.filter(':first').fadeTo(settings.fadespeed, 0.5);
        imglink.click(function() {
            bigimagelink.attr('href', $(this).attr('href').replace('Width=254', 'Width=900').replace('Height=244', 'Height=900')); //.replace('&Crop=5', '')
            bigimagelink.lightBox();
            bigimage.attr('src', $(this).attr('href'));
            imglink.fadeTo(settings.fadespeed, 1.0);
            $(this).fadeTo(settings.fadespeed, 0.5);
            return false;
        });
    }

    //if thare are more images than settings.showitems
    if (settings.showitems < items.length) {
        //button background
        forward.css('background-image', forwardbg);
        back.css('background-image', backbg);
        //clickenvents
        forward.click(function() {
            currentpos = parseInt(obj.css('left').replace('px', ''));
            nextpos = (currentpos - itemWidth);
            if (nextpos >= maxSlidePos) {
                nextpos += 'px';
                obj.animate({ 'left': nextpos }, settings.animationspeed);
            }
            return false;
        });
        back.click(function() {
            currentpos = parseInt(obj.css('left').replace('px', ''));
            if (currentpos < 0) {
                nextpos = (currentpos + itemWidth) + 'px';
                obj.animate({ 'left': nextpos }, settings.animationspeed);
            }
            return false;
        });
    }
    else {

    }

    return this;
}
