jQuery(document).ready(function($) {
    SetDefaultFieldValues();
    HandleNavigationDropDowns();
    HandleScrollerEvents();
});

var that = null;
var nogo = false;

function HandleNavigationDropDowns() {
    $('#mainNavigationDropDown li a').not('#mainNavigationDropDown ul.level1 li a').mouseover(function() { if (that != $(this).closest('li')[0] && that != null) { $(that).removeClass('showsub'); } that = $(this).closest('li')[0]; $(that).addClass('showsub'); });
    $('#contentWrapper').mouseover(function() { $(that).removeClass('showsub'); }); 
}

function HandleScrollerEvents() {
    //Set Apporiate Width
    $('.text.scroller').each(function(index) {
        var that = $(this).children('.scrollerWrapper');
        $(that).children('.content').css('width', $(that).children('.content').children('.scrollerItem').width() * $(that).children('.content').children('.scrollerItem').size());
        if ($(that).children('.content').children('.scrollerItem').size() < 5) {
            $(that).children('.content').css('width', '1000px');
            $(this).children('.scrollNext').fadeOut(100);
            $(this).children('.scrollPrev').fadeOut(100);
            $(this).children('.scrollerWrapper').css('margin-left', '0px');
            $(this).children('.scrollerWrapper').css('margin-right', '0px');
            $(that).children('.content').children('.scrollerItem').css('margin-right', '27px');
        }
        //Scroll Next
        $(this).children('.scrollNext').click(function() {
            var scrollInstance = $(that).children('.content');
            var scrollAmmount = parseInt($(scrollInstance).children('.scrollerItem').width());
            var scrollCurrentPosition = parseInt(scrollInstance.css('left'));
            if (!scrollCurrentPosition) { scrollCurrentPosition = 0; };
            if (($(that).width() - scrollInstance.width()) < (scrollCurrentPosition) && !nogo) {
                nogo = true;
                $(scrollInstance).animate({ left: (scrollCurrentPosition - scrollAmmount) }, 500, function() { nogo = false; });
            }
        });
        //Scroll Previous
        $(this).children('.scrollPrev').click(function() {
            var scrollInstance = $(that).children('.content');
            var scrollAmmount = parseInt($(scrollInstance).children('.scrollerItem').width());
            var scrollCurrentPosition = parseInt(scrollInstance.css('left'));
            if (!scrollCurrentPosition) { scrollCurrentPosition = 0; };
            if ((scrollCurrentPosition + scrollAmmount) < 1 && !nogo) {
                nogo = true;
                $(scrollInstance).animate({ left: (scrollCurrentPosition + scrollAmmount) }, 500, function() { nogo = false; });
            }
        });
    });
}

function SetDefaultFieldValues() {
	$('*[class~=initialValue]').each(function() {
		var text = $(this).text(); //labelin teksti
		var field = $('#' + $(this).attr("for")); //kenttä

		if (!field) return; // ei löytynyt kenttää, mihin viitata

		//asetetaan labelin teksti, jos ei ole vielä valuessa tavaraa
		if (field.val() == '' || field.val() == text) {
			field.val(text).addClass("noValue");
		}

		//fokuksen sattuessa tyhjennetään, jos label kentässä
		field.focus(function() { if ($(this).val() == text) { $(this).val('').removeClass("noValue"); } });

		//fokuksen poistuessa palautetaan label, jos tyhjä
		field.blur(function() { if ($(this).val() == '') { $(this).val(text).addClass("noValue"); } });
	});

	//tyhjennetään submitissa labelin tekstit
	$('form').submit(function() {
		$('*[class~=initialValue]').each(function() {
			var text = $(this).text();
			var field = $('#' + $(this).attr("for"));

			if (!field) return;

			if (field.val() == text) {
				field.val('');
			}
		});
	});
}

