﻿$(document).ready(function() {
    // dropdowns
    // dropdowns
    $('select').each(function(dropdownNum) {
        var selectBox = $(this);
        $(this).before('<div id="dropdown' + dropdownNum + '" class="dropdown"><div class="textbox"></div><div class="arrow"></div>');
        var selectedIndex = "";
        if (selectBox.val().length > 0)
            selectedIndex = selectBox.val();

        $(this).children().each(function(itemNum) {
            if ($(this).val() == selectedIndex) {
                $('#dropdown' + dropdownNum+' .textbox').append('<span class="activeVal">' + $(this).text() + '</span><div class="closed"><div class="top"></div><div class="middle"><ul><li class="first">' + $(this).text() + '</li>');
                return;
            }
        });
        $(this).children().each(function(itemNum) {
            $('#dropdown' + dropdownNum + ' .middle ul').append('<li>' + $(this).text() + '</li>');
        });
        $(this).addClass('init');

         $('#dropdown' + dropdownNum + ' .closed').append('<div class="bottom"></div>');

        /* open dropdown */
        $('#dropdown' + dropdownNum+' .arrow').bind('click', function(event) {
           	$('.dropdown div.closed').hide();
            var theDropdown = $('#dropdown' + dropdownNum + ' div.closed');
            var winHeight = $(document).height();
            var viewportHeight = window.innerHeight ? window.innerHeight : $(window).height();
            
            $('#dropdown' + dropdownNum + ' div').show();
            if(theDropdown.offset().top+theDropdown.height()>winHeight) {
            	theDropdown.css({'top':-theDropdown.height()+25+"px"});
            }
            (jQuery.Event(event)).stopPropagation();

            $(document).bind('click', function() {
                $('.dropdown div.closed').hide();
                $(document).unbind('click');
            });

            $(selectBox).focus();
        });

        $('#dropdown' + dropdownNum).bind('mouseover', function() {
            !$('#dropdown' + dropdownNum).hasClass('over')
            	$('#dropdown' + dropdownNum).addClass('over');
        });
        $('#dropdown' + dropdownNum).bind('mouseout', function() {
            $('#dropdown' + dropdownNum).removeClass('over');
        });

        $('#dropdown' + dropdownNum + ' li').each(function() {
            $(this).bind('click', function() {
                $('#dropdown' + dropdownNum + ' .activeVal').text($(this).text());
                $('#dropdown' + dropdownNum + ' li.first').text($(this).text());
				$('#dropdown' + dropdownNum+' div.closed').hide();
                $('#dropdown' + dropdownNum + ' li').each(function() {
                	$('#dropdown' + dropdownNum + ' li').removeClass('active');
                	if ($(this).text() == $(selectBox).val() && !$(this).hasClass('first')) {
                        $(this).addClass('active');
                        return false;
                    }
                    $(selectBox).val($(this).text());
                });
                $(selectBox).val($(this).text());
                $(selectBox).focus()
            });
            $(this).bind('mouseenter mouseleave', function() {
                if(!$(this).hasClass('first')){
                	$('#dropdown' + dropdownNum + ' li').removeClass('over');
                	$(this).addClass('over');
                }
            });

        });

        /* bind to the original dropdown */
        $(this).bind('focus', function() {
            $('#dropdown' + dropdownNum).addClass('over');
        });
        $(this).bind('keyup', function(e) {
        	if(e.which == 13) {
        		$('.dropdown div.closed').hide();
        		$(this).blur();
        	}
        	else if(e.which == 38 || e.which == 40) {
            	if($('#dropdown' + dropdownNum + ' .activeVal').text() != $(this).children("option:selected").text()) {
            		var activeValue = $(this).children("option:selected").text();
               		$('#dropdown' + dropdownNum + ' .activeVal').text($(this).children("option:selected").text());
               		$('#dropdown' + dropdownNum + ' li.first').text($(this).children("option:selected").text());
                	$('#dropdown' + dropdownNum + ' li').removeClass('over');
	                $('#dropdown' + dropdownNum + ' li').each(function() {
                    	if ($(this).text() == activeValue) {
                    		$(this).addClass('over');
                    	}
                	});
                }
             }
        });
        $(this).bind('change', function(e) {
            $('#dropdown' + dropdownNum + ' .activeVal').text($(this).children("option:selected").text());
            $('#dropdown' + dropdownNum + ' li.first').text($(this).children("option:selected").text());
        });
        $(this).bind('blur', function() {
        	$('#dropdown' + dropdownNum).removeClass('over');
        });
        /* end bindings */
    });

   // expanding text
    var expandLinkText = $('.moreLink').text();
    $('.moreLink').bind('click', function(event) {
        if ($('.expanded').is(":visible")) {
        	$(this).removeClass('open');
            $(this).text(expandLinkText);
        } else {
        	$(this).addClass('open');
            $(this).text(' Collapse');
        }
        $('.expanded').toggle();
        event.preventDefault();
    });	

    var startPos = $('.menu li.active').is(":visible") ? $('.menu li.active').offset().top - $('.menu').offset().top : 0;
    var animation = false;
    var offSet = 0;

    // menu
    $('.menu').append('<div class="indicator"></div>');
    $('.menu li.active').css({ 'background': 'none' });
    $('.menu .indicator').stop().animate({ 'top': startPos + 'px' }, 500);


    $('.menu a').bind('mouseover', function() {
        if ($(this).parents('ul').hasClass('subMenu'))
            offSet = $(this).parents('ul').parent('li').offset().top - $('.menu').offset().top;
        else
            offSet = $(this).parent('li').offset().top - $('.menu').offset().top;
        $('.menu .indicator').stop().animate({ 'top': offSet + 'px' }, 300);
    });
    $('.menu a').bind('mouseout', function() {
        $('.menu .indicator').stop().animate({ 'top': startPos + 'px' }, 300);
    });
    $('.menu .languages a').unbind('mouseover');

    // Downloads page
    /* show registration fields on click */
    $('a#registerDownloads').bind('click', function(event) {
        $(this).remove();
        $('.registerDownloadsForm').slideDown(400);
        event.preventDefault();
    });

    /* erase text on focus, re-add if needed */
    $('.registerDownloadsForm input.text, .loginDownloads input.text, .newsletter input.text, .registerLogin input.text, .unsubscribe input.text').bind('focus', function(event) {
        if (!$(this).hasClass('changed'))
            var originalVal = $(this).val();

        $(this).bind('blur', function(event) {
            if ($(this).val() == '') {
                $(this).val(originalVal);
                $(this).removeClass('changed');
            }
        });

        $(this).bind('change', function(event) {
            if (!$(this).hasClass('changed'))
                $(this).addClass('changed');
        });

        if ($(this).val() == originalVal)
            $(this).val('');

        event.preventDefault(originalVal);
    });
});
