﻿/// <reference path="jquery-1.3.2-vsdoc2.js" />

var Ratio = {};

Ratio.DropDownLinks = {
    init: function() {
        jQuery('.dropdown-links').change(function() {
            var url = jQuery("option:selected", this).val();
            if (url != '') {
                window.location = url;
            }
        });
    }
};

Ratio.ResearchTabs = {
    init: function() {
        if (jQuery('.tabs')) {
            this.setBehavior();
            var location = document.location.toString();
            if (location.match('#')) {
                //alert(window.location.hash);
                var anchor = window.location.hash.replace("#link-", ""); //split('#')[1];
                jQuery('.tabs ul li a[href="#' + anchor + '"]').trigger('click');
            }

        }
    },
    setBehavior: function() {
        jQuery('.tabs ul li').click(function(e) {
            Ratio.ResearchTabs.showTab(this);
        });
        jQuery('.tabs ul li a').click(function(e) {
            Ratio.ResearchTabs.showTab(jQuery(this).parent());
            return false;
        });
    },
    showTab: function(sender) {
        if (jQuery(sender).attr('class') != 'selected') {
            jQuery('.tabs ul li').attr('class', '');
            jQuery(sender).attr('class', 'selected');
            jQuery('.tabs div').attr('class', '');
            jQuery('.tab-images li').attr('class', '');
            var href = jQuery(sender).find('a').attr('href');
            jQuery(href).attr('class', 'active');
            jQuery(href + '-image').attr('class', 'active');
        }
    }
};

Ratio.StartAnim = {
    slide: null,
    timer: null,
    init: function() {
        if (jQuery('#start-splash'))
            this.StartAnimation();
    },
    StartAnimation: function() {
        var inst = this;
        // Set first slide
        this.slide = jQuery('#start-splash div.active');
        inst.animateSlide();
    },
    animateSlide: function() {
    var inst = this;
        // Reset position and opacity
        jQuery(inst.slide).find('img:not(.anim-pic)').animate({ opacity: 0, left: '+=20px' }, 0);
        jQuery(inst.slide).find('.anim-pic').animate({ opacity: 0 }, 0);
        jQuery(inst.slide).find('div a').animate({ opacity: 0, left: '+=20px' }, 0);
        // Set active class to current slide
        jQuery(inst.slide).attr('class', 'active');
        // Start animation
        jQuery(inst.slide).find('.anim-hs').animate({ opacity: 1, left: '-=20px' }, 500,
            function() {
                jQuery(inst.slide).find('.anim-hb').animate({ opacity: 1, left: '-=20px' }, 500,
                    function() {
                        jQuery(inst.slide).find('div a').animate({ opacity: 1, left: '-=20px' }, 500,
                        // Remove filter attribute to fix jagged text in ie
                        function() { if (jQuery.browser.msie) this.style.removeAttribute('filter'); });
                        jQuery(inst.slide).children('.anim-pic').animate({ opacity: 1 }, 1500,
                            function() {
                                // Pause and fade out
                                jQuery(inst.slide).find('img').animate({ dummy: 1 }, 4000).animate({ opacity: 0 }, 1000);
                                jQuery(inst.slide).find('a').animate({ dummy: 1 }, 4000).animate({ opacity: 0 }, 1000,
                                function() {
                                    // Remove active class
                                    jQuery(inst.slide).attr('class', '');
                                    // Find the next slide
                                    inst.slide = jQuery(inst.slide).next();
                                    if (!inst.slide.length)
                                        inst.slide = jQuery('#start-splash div').eq(0);
                                    // Prepare the new slide to be displayed
                                    jQuery(inst.slide).attr('class', 'pending');
                                    // Wait and call animation again
                                    setTimeout(function() { inst.animateSlide() }, 200);
                                })
                            });
                    });
            });
    }
};

jQuery(document).ready(function() {
    Ratio.DropDownLinks.init();
    Ratio.ResearchTabs.init();
    Ratio.StartAnim.init();
});
