﻿/// <reference path="jquery-1.3.2-vsdoc.js" />

/**
* Contains all global functions for Skidor.com
* Dependencies:
* - jquery.cookie.js
* - jquery.hoverIntent.js
*/

$(function() {

    // Search "watermark"
    $('.searchtext').val($('.searchtext').attr('title'));

    $('.searchtext').focus(function() {
        // See if the input field is the default
        if ($(this).val() == $(this).attr('title')) {
            $(this).val('');
        }
    })
	.blur(function() {
	    if ($(this).val() == '') {
	        $(this).val($(this).attr('title'));
	    }
	});

    $('.glow, div.xform input.button').glowbuttons();
    $('.glowalternative').css({ cssFloat: 'none' });

    // Drop-down menu
    $('div#nav li ul').hide();

    $('div#nav li').hoverIntent(
		function() {
		    if ($('ul', this).is(':hidden')) {
		        if ($.browser.msie) {
		            $('div#nav li ul').hide(); // Ensure all are hidden for IEs slow reactions
		        }
		        $('ul', this).slideDown('medium');
		    }
		},
		function() {
		    if ($.browser.msie) { // Just hide the menu for IE, which gets messed up due to transparent bg bug which remains in IE8 too
		        $('ul', this).hide();
		    }
		    else {
		        $('ul', this).fadeOut();
		    }
		}
	);

    // Add support for IE hovering
    if ($.browser.msie) {
        $('input.searchbutton').hoverClass('over');
        $('ul#toolbar li').hoverClass('highlight');
        $('div#nav li').hoverClass('over');
    }

    // Link nudge
    $('div#rightbar div#sports ul li a').hoverIntent(
        function() { //mouse in
            $(this).animate({ paddingRight: '15px' }, 300);
        },
        function() { //mouse out
            $(this).animate({ paddingRight: 0 }, 400);
        }
    );

    // Backlink
    $('a.backlink').click(function() {
        history.go(-1);
        return false;
    });

    // Login boxes
    $('.login-open').click(function() {
        $(this).placeBottomLeft('.LoginBox').show('fast');
        $('.LoginBoxTextBox').eq(0).focus();
        return false;
    });

    $('.login-close').click(function() {
        $('.LoginBox').hide('fast');
        return false;
    });

    // Font-sizing
    $('li#textsize').show();

    var fontSizeCookieName = 'fontsize';
    var originalFontSize = $('html').css('font-size');

    // If exists load saved value, otherwise store it
    if ($.cookie(fontSizeCookieName)) {
        var $getSize = $.cookie(fontSizeCookieName);
        $("html").css({ fontSize: $getSize + ($getSize.indexOf('px') != -1 ? '' : 'px') }); // IE fix for double "pxpx" error
    }
    else {
        $.cookie(fontSizeCookieName, originalFontSize, { expires: 7, path: '/' });
    }

    $('li#textsize').click(function() {
        // Increase the font size 
        var currentFontSize = $('html').css('font-size');
        var currentFontSizeNum = parseFloat(currentFontSize, 10);
        var newFontSize = currentFontSizeNum * 1.2;

        if (newFontSize < 30) { // We won't go beyond 30px;
            $('html').css({ fontSize: newFontSize });
            $.cookie(fontSizeCookieName, newFontSize, { expires: 7, path: '/' });
        }
        else {
            $('html').css({ fontSize: originalFontSize });
            $.cookie(fontSizeCookieName, originalFontSize, { expires: 7, path: '/' });
        }
        return false;
    });

    $(function() {
        $('div.small-news-list marquee').marquee('pointer').mouseover(function() {
            $(this).trigger('stop');
        }).mouseout(function() {
            $(this).trigger('start');
        }).mousemove(function(event) {
            if ($(this).data('drag') == true) {
                this.scrollLeft = $(this).data('scrollX') + ($(this).data('x') - event.clientX);
            }
        }).mousedown(function(event) {
            $(this).data('drag', true).data('x', event.clientX).data('scrollX', this.scrollLeft);
        }).mouseup(function() {
            $(this).data('drag', false);
        });
    });
});

$(function() {

    // IE chokes on fading PNGs...
    if ($.browser.msie) return;

    $('ul#toolbar li')
            .removeClass('highlight')
            .find('a.hova')
            .append('<span class="hover" />').each(function() {
                var $span = $('> span.hover', this).css('opacity', 0);
                $(this).hover(function() {
                    // on hover
                    $span.stop().fadeTo(500, 1);
                }, function() {
                    // off hover
                    $span.stop().fadeTo(500, 0);
                });
            });

});
$.fn.hoverClass = function(c) {
    return this.each(function() {
        $(this).hover(
            function() { $(this).addClass(c); },
            function() { $(this).removeClass(c); }
        );
    });
};
$.fn.placeBottomLeft = function(el) {

    $(el).css({
        "right": ($(window).width() - $(this).offset().left - $(this).width()) + "px",
        "top": $(this).offset().top + $(this).height() + "px"
    });

    return $(el);
}

function toggleLoginBox(loginButtonID, loginBoxDivID, userNameTextBoxID, passwordTextBoxID) {
    $('.login-open').placeBottomLeft('.LoginBox').show();
}

$(function() {

    var sportsMenuItems = $('div#nav ul#sports-menu li');
    var lastBlock = $('a:first', sportsMenuItems);
    var minWidth = 59;

    $('a', sportsMenuItems).each(function() {
        // Add a div based on the title
        $(this).append('<div><span>' + $(this).attr('title') + '</span></div>');
        // Store the original width
        jQuery.data(this, 'orgWidth', { width: $(this).width() });
        $(this).css('width', minWidth + 'px');
    });

    $('a', sportsMenuItems).hoverIntent(
        function() {
            $(lastBlock).animate({ width: minWidth + 'px' }, { queue: false, duration: 300 });
            var orgWidth = jQuery.data(this, 'orgWidth').width;
            $(this).animate({ width: orgWidth + 'px' }, { queue: false, duration: 300 });
            lastBlock = this;
        },
        function() {
            var orgWidth = jQuery.data(lastBlock, 'orgWidth').width;
            $(lastBlock).animate({ width: minWidth + 'px' }, { queue: false, duration: 300 });
        }
    );

});

$(document).ready(function() {

    // Using each as an 'if' and containing stuff inside a private namespace.
    $('.inner-sponsors').each(function() {

        var $list = $(this);

        var viewData = window[this.id + '_ViewData'];
        var total = viewData.length;
        var interval = 4000;
        var limit = 3;

        // No need to rotate anything unless there are more than 3 items
        if (total <= limit)
            return;

        // Grab the height of the first item
        var height = 70; // $list.find('> a:first').height();

        // Set the limit to the total amount if there are less items than the defined limit
        if (total <= limit) {
            limit = total;
        }

        var currentItem = limit - 1; // Set the last item as the current

        //Empty the list
        $list.empty();

        // Append the initially visible items
        $.each(window[this.id + '_ViewData'], function(i) {
            if (i >= limit)
                return;

            var $link = $('<a></a>').attr('href', this.Link)
            var $img = $('<img></img>').attr('src', this.ImageUrl);

            $link.html($img);
            $list.append($link);
        });

        // 2. effect        
        function spy() {

            var toInsert = viewData[currentItem];

            var $link = $('<a></a>').attr('href', toInsert.Link)
            var $img = $('<img></img>').attr('src', toInsert.ImageUrl);
            $link.html($img);

            // insert a new item with opacity and height of zero
            var $insert = $link.css({
                height: 0,
                opacity: 0,
                display: 'none'
            }).prependTo($list);

            // fade the LAST item out
            $list.find('> a:last').animate({ opacity: 0 }, 1000, function() {

                // increase the height of the NEW first item
                $insert.animate({ height: height }, 1000).animate({ opacity: 1 }, 1000);

                $(this).prev().addClass('last');

                // AND at the same time - decrease the height of the LAST item
                $(this).animate({ height: 0 }, 1000, function() {
                    // finally fade the first item in (and we can remove the last)
                    $(this).remove();
                });
            });

            currentItem++;
            if (currentItem >= total) {
                currentItem = 0;
            }

            // trigger the effect again in 4 seconds
            setTimeout(spy, interval);
        }

        spy();
    });
});


