document.write('<script type="text/javascript" src="js/ui/flash.min.js"></script>');
$(document).ready(function() {
   if ($('#homeBanner').size()) {
		// home banner
		$('#homeBanner').banner();

		// marquee text
		$('#marquee').marquee();
	}

	// print version
    $('#print').show().click(function(){
        switchToPrint();
        return false;
    });
    
    // add icones to document links
	var ext = ['pdf', 'doc', 'docx'];

	for (var i = 0; i < ext.length; i++)
	{
		$('#content a[href$=".'+ ext[i] +'"]').addClass(ext[i]).attr('target', '_blank');
	}

    // externals links
    $('#main a[rel=ext]').attr('target', '_blank');

    // search input
    $('input#searchInput').focus(function () {
        if ($(this).val() == 'Paieška' || $(this).val() == 'Search') $(this).val('');
    });
    $('input#searchInput').blur(function () {
        if ($(this).val() == '') {
        	var lang = $('body').attr('class');

        	if (lang == 'lt') {
        		$(this).val('Paieška');
        	} else {
        		$(this).val('Search');
        	}
        }
    });

    // sitemap list
    if ($('#sitemapList').size()) { makeSitemap(); }

    // funds filter
    if ($('input#FilterTypeProgram').size() && $('input#FilterTypeFund').size()) {
        filterType();
        $('input[type="radio"].typeRadio').bind('click', filterType);
    }

    // show / hide graph
    $('#graph').show();
    $('#showGraph').show().click(function() {
        if ($(this).hasClass('show')) {
            $('#graph').show();
            $(this).html(hideGraph).removeClass('show');
        } else {
            $('#graph').hide();
            $(this).html(showGraph).addClass('show');
        }
        return false;
    });

    // show / hide values
    $('#unitPrice').hide();
    $('#showValues').parent('p').show();
    $('#showValues').click(function() {
        if ($(this).hasClass('showing')) {
            $('#unitPrice').hide();
            $(this).html(showValues).removeClass('showing');
        } else {
            $('#unitPrice').show();
            $(this).html(hideValues).addClass('showing');
        }
        return false;
    });

    // expand / collapse
    $('dl.accord dt').each(function(){
        if (!$(this).hasClass('single')) {
            $(this).addClass('expand');
        }
    });

    $('dl.accord dd').hide();

    $('dl.accord dt.expand').click(function(){
        $(this).toggleClass('collapse');
        $(this).next('dd').toggle();
	});

    $("#FilterProgramId").change(function(){
        if ($(this).val() == '6')
        {
            $(this).parent().next().removeClass('hidden');
        }
        else
        {
            $(this).parent().next().addClass('hidden');
        }
    }).trigger('change');
    $("#fundsForm").submit(function(){
        var valid = true;
        var total = 100;
        if (!$(".funds-list").hasClass('hidden') && $(".funds-list").length > 0)
        {
            total = 0;
            $(".funds-list input").each(function(){
                v    = $(this).val();
                vInt = parseInt(v);
                if (v.trim() != '' && isNaN(vInt))
                {
                    valid = false;
                }
                else if (vInt < 10)
                {
                    valid = false;
                }
                else if (!isNaN(vInt))
                {
                    total = total + vInt;
                }

            });
        }
        if (!valid || total != 100)
        {
            alert('Bendra procentų investuojamų į kryptis suma turi būti 100');
            return false;
        }
        else
        {
            return true;
        }
    });
});

function switchToPrint() {

	var lang = $('body').attr('class');

    var btnPrint = new Array();
    	btnPrint['lt'] = 'Spausdinti';
    	btnPrint['en'] = 'Print';

    var btnClose = new Array();
    	btnClose['lt'] = 'Uždaryti';
    	btnClose['en'] = 'Close';

	$('link[rel*=stylesheet]').each(function() {
		if (this.media == 'print') {
			var clone = this.cloneNode(false);
			clone.media = 'screen';
			clone.title = 'printVersion';
			$('head')[0].insertBefore(clone, this);
			this.disabled = false;
		} else if (this.media != 'all') {
			this.disabled = true;
		}
	});

	$('body').prepend('<div id="printButtons"><button onclick="window.print();">' + btnPrint[lang] + '</button>&nbsp;<button id="printClose">' + btnClose[lang] + '</button></div>');
	$('#printClose').bind('click', switchToDefault);

	//var url = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
	//$('<p>URL: '+url+'</p>').insertBefore("#logo");

	return false;
}

function switchToDefault() {
	$('link[rel*=stylesheet]').each(function() {
		this.disabled = false;
		if (this.title == 'printVersion') {
			this.disabled = true;
            $('head')[0].removeChild(this);
		}
	});
	$('#printButtons').remove();
}

function makeSitemap() {
        $('#sitemapList li ul').hide();
		$('#sitemapList li:has(ul)').each(function(){
			$(this).prepend('<span class="expand"></span>');
		});

		$('span.expand').bind('click', function(){
			if ($(this).hasClass('show')) {
				$('ul:first', this.parentNode).hide();
				$(this).removeClass('show');
			} else {
				$('ul:first', this.parentNode).show();
				$(this).addClass('show');
			}
		});

		var lang = $('body').attr('class');

		var expand = new Array();
			expand['lt'] = 'Išskleisti viską';
			expand['en'] = 'Expand all';

		var collapse = new Array();
			collapse['lt'] = 'Suskleisti viską';
			collapse['en'] = 'Collapse all';

		var expandCollapse = '<a id="expandCollapse" href="">'+expand[lang]+'</a>';

		$('p#additional').prepend(expandCollapse);

		$('a#expandCollapse').click(function(){
			if ($(this).hasClass('collapse')) {
				$('span.expand').each(function(){
					$('ul:first', this.parentNode).hide();
					$(this).removeClass('show');
				});
				$(this).removeClass('collapse');
				$(this).html(expand[lang]);
			} else {
				$('span.expand').each(function(){
					$('ul:first', this.parentNode).show();
					$(this).addClass('show');
				});
				$(this).addClass('collapse');
				$(this).html(collapse[lang]);
			}
			return false;
		});
}

function filterType() {
    var selected = $('input[type="radio"].typeRadio:checked').val();
    if (selected == 'program') {
        $('div#fundDiv').hide();
        $('div#programDiv').show();
        $('select#FilterProgramId').attr('disabled', '');
        $('select#FilterFundId').attr('disabled', 'disabled');
    } else if (selected == 'fund') {
        $('div#programDiv').hide();
        $('div#fundDiv').show();
        $('select#FilterFundId').attr('disabled', '');
        $('select#FilterProgramId').attr('disabled', 'disabled');
    }
}

// banner plugin
(function($){
    $.fn.banner = function(options) {
        var defaults = {
            delaySpeed: 10000
        };

        var options = $.extend(defaults, options);

        var clickableSize = 0,
            randomNumber  = -1,
            currentSlide  = 0,
            prevSlide     = null,
            interval      = null,
            slides        = $('#banners div'),
            areas         = $('#homeBanner div.areas'),
            idArray       = new Array();

        return this.each(function() {

            if (slides.size() > 0) {
                slides.hide();

                slides.each(function(e) {
                    var ids = $(this).attr('id').split('-');
                    idArray[e] = ids[1];
                });

                areas.each(function(e) {
                    var id = $(this).attr('id').split('-');
                    idArray = idArray.toString();
                    if (idArray.indexOf(id[1]) > -1) {
                        $(this).addClass('clickable');
                        $(this).bind("click",{index:clickableSize},function(event){
                            currentSlide = event.data.index;
                            gotoSlide(currentSlide);
                        });
                        clickableSize++;
                    }
                });

                var areasClickable = $('#homeBanner div.clickable');

                nextSlide();

            }

            function nextSlide () {
                if (clickableSize > 1) {
                    if (randomNumber != -1)
                    {
                    	randomNumber = Math.floor(Math.random()*clickableSize);
                    	while (randomNumber == currentSlide) {
                    	    randomNumber = Math.floor(Math.random()*clickableSize);
                    	}
                    } else {
                        randomNumber = 0;
                    }
                	
                } else {
                    randomNumber = 0;
                }

            	currentSlide = randomNumber;
            	gotoSlide(currentSlide);
            }

            function gotoSlide(slideNum){
                if (slideNum != prevSlide) {
                    if (prevSlide != null) {
                        $(slides[prevSlide]).stop().hide();
                        $(areasClickable[prevSlide]).removeClass('active');
                    }

                    $(areasClickable[slideNum]).addClass('active');
                    $(slides[slideNum]).stop().show();
                    prevSlide = currentSlide;

                    if (clickableSize > 1) {
                        if (interval != null){
                           clearInterval(interval);
                        }
                        interval = setInterval(nextSlide, options.delaySpeed);
                    }
                }
            }

        });
    };
})(jQuery);

// marquee plugin
(function($){
    $.fn.marquee = function(options) {
        var defaults = {
            pixelSpeed: 4,
            delaySpeed: 100
        };

        var options = $.extend(defaults, options);

        var marquee    = $('#marqueeText'),
            width      = marquee.width(),
            interval   = null,
            pixelSpeed = options.pixelSpeed;

        return this.each(function() {
            marquee.hover(function() {
                pixelSpeed = 0;
            }, function() {
                pixelSpeed = options.pixelSpeed;
            });
            function marqueeText() {
                if (parseInt(marquee.css('left')) >= width*(-1)) {
                    marquee.css('left', parseInt(marquee.css('left')) - pixelSpeed + 'px');
                } else {
                    marquee.css('left', '942px');
                }

                if (interval != null) {
                    clearInterval(interval);
                }

                interval = setInterval(marqueeText, options.delaySpeed);
            };
            marqueeText();
        });
    };
})(jQuery);


// Disable form elements in block

(function($){
    $.fn.disable = function(options){
        var defaults={
            objects: 'all',
            action: 'disable'
        }

        var options = $.extend(defaults, options);

        return this.each(function(){
            switch(options['action']){
                case 'disable':
                    $('input[type="text"],input[type="checkbox"], input[type="radio"],select, textarea ',this).attr('disabled','disabled');
                    break;
                case 'enable':
                    $('input[type="text"],input[type="checkbox"], input[type="radio"],select, textarea',this).removeAttr('disabled');
            }

        });
    }
})(jQuery);
