/* ------------------------------------------------------------------------------------------------------------
 *      See jquery_functions.js for instructions on changing the counter values.
 * ------------------------------------------------------------------------------------------------------------ */

$(document).ready(function(){

    //showError('TEST showError() !! Sidan du s&ouml;ker kan tyv&auml;rr inte visas. Var v&auml;nlig f&ouml;rs&ouml;k senare!<br/>Eller g&aring; till <a href="#">SBABs websida</a>.');
	var hasPageObj = true;
    try
    {
	   pageObj.init();
    }
    catch (exc)
    {
	   hasPageObj = false;
	}   
    initToolTip();
	initOverlay();
	
	disableCounters(); // shade-out counters

	setGrade(4); // Grade (Page 4)
	
	initSlider('WrapSliderArea','SliderArea','SliderAreaResult',0,400,1,true);
	initSlider('WrapSliderFee','SliderFee','SliderFeeResult',0,20000,100,true);
	
	if (hasPageObj)
	{
		pageObj["areaSlider"] = {input: 'SliderAreaResult', slider: 'SliderArea'};
		pageObj["feeSlider"] = {input: 'SliderFeeResult', slider: 'SliderFee'};
		pageObj["onUpdateValues"] = inputValueChanged;
	}
	
	initCounters();
	
	restrictToFloat($('.allowInt'));
	restrictToNumbers($('#SliderAreaResult'));
	//restrictToNumbers($('#street'));
	
	$('.Calculation').click(function(){
		showCustomLiteBox();
	    return false;
	});
	
	$('a.Terms').click(function(){
		showTerms();
	    return false;
	});
	
   $('a[rel=over]').hover(function(){;
		var path = $(this).children('img').attr('src');
		path = path.replace('.png','_over.png');
		//path = path.replace('.gif','_over.gif');
		$(this).children('img').attr('src',path);
	},function(){
		var path = $(this).children('img').attr('src');
		path = path.replace('_over.png','.png');
		//path = path.replace('_over.gif','.gif');
		$(this).children('img').attr('src',path);
	});
   
	// ---------------------------------------------------------
	//      Handle users with low screen resolution
	// ---------------------------------------------------------
   if (screen.height <= 768) 
   {
	   jQuery(".Next").remove();
	   jQuery(".Prev").remove();
	   jQuery(".SmallBrowserNextButton").removeClass("hide");
   }
   else
   {
	   jQuery("body").css("min-width", "1180px");	   
   }
   
   
   // Disable active menu click
   disableLinkClick($('a.Unclickable'));
   disableLinkClick($('.Nav li a.Active'));
   
	$('.Nav a.Disabled')
		.css('opacity',0.4)
		.click(function(){ return false; });
   
   // Handle menu click
   // Set current menu active and updates the body class which is handled to control which page is visible (by css)
   $('.Nav a:not(:.Disabled)').click(function(){
		if(!$(this).hasClass('Active'))
		{
			$('.Nav a.Active').removeClass('Active');
			if($(this).hasClass('Menu1'))
			{
				$('.Nav a.Menu1').addClass('Active');
				$('body').removeClass().addClass('Page1');
			}
			else if($(this).hasClass('Menu2'))
			{
				$('.Nav a.Menu2').addClass('Active');
				$('body').removeClass().addClass('Page2');
			}
			else if($(this).hasClass('Menu3'))
			{
				$('.Nav a.Menu3').addClass('Active');
				$('body').removeClass().addClass('Page3');
			}
			else if($(this).hasClass('Menu4'))
			{
				$('.Nav a.Menu4').addClass('Active');
				$('body').removeClass().addClass('Page4');
			}
		}
	});
	
	// Handle plus minus buttons click (Number of Rooms, Page3)
	$('.RoundedPlusMinus[alt=+]').click(function(){
	    var val = $('#numRooms').attr('value');
	    if(val)
	    {
	    	val = Math.round(parseFloat(val) * 2) / 2;
	    	val = Math.min(val, 19);
	    	val = Math.max(val, 1);
	    	val += (val < 4) ? 0.5 : 1;
	    }
	    else
	    {
	        val = 1;
	    }
	    $('#numRooms').attr('value',val);
	});
	
	$('.RoundedPlusMinus[alt=-]').click(function(){
	    var val = $('#numRooms').attr('value');
	    if(val)
	    {
	    	val = Math.round(parseFloat(val) * 2) / 2;
	    	val = Math.min(val, 20);
	    	val = Math.max(val, 1);	    	
	    	val -= (val > 4) ? 1 : (val > 1) ? 0.5 : 0;  
	    }
	    else
	    {
	        val = 1;
	    }
	    $('#numRooms').attr('value',val);
	});
	
	// ---------------------------------------------------------
	//      Handle dropdown arrow key function
	// ---------------------------------------------------------
	$('.DropDown').parents('fieldset').find('input.txt').each(function(){
	    this.setAttribute('autocomplete', 'off');
	});
		
	$('.DropDown').hover(function(){
	    $(this).addClass('Over');
	},function(){
	    $(this).removeClass('Over');
	});
	
	$(document).keydown(function(e){
	    if($('.DropDownWrap').hasClass('Active'))
	    {
	        if(e.which == 38)
	        {
	            dropdownSelectPrev();
	            return false;
	        }
	        else if(e.which == 40)
	        {
	            dropdownSelectNext();
	            return false;
	        }
	        else if(e.which == 13) // hide dropdown if user presses return key
	        {
	            e.preventDefault();
	            $('.DropDownWrap.Active').removeClass('Active');
	        }
	    }
	    // Note: Do not place return false outside the if-cases, since that will ignore all keyboard input
	});
	
	function dropdownSelectNext()
	{
	    var $lis = $('.DropDownWrap.Active .DropDown ul').children();
	    var curr = $lis[0];
	    var hasActive = false;

        $lis.each(function(i){
            if($(this).children().hasClass('Active'))
            {
                cur = this;
                hasActive = true;
                $(this).children().removeClass('Active');
                //$(this).next().children().addClass('Active');
                
                // if we reach the end of the list, restart from the top
                // else activate next in the list
                if( $($(this).next()).get(0) == null )
                {
                    $(curr).children().addClass('Active'); // set first element (in the list) to Active
                    dropdownUpdateInput($(curr).children('a'));
                }
                else
                {
                    $(this).next().children().addClass('Active');
                    dropdownUpdateInput($(this).next().children('a'));
                }
                
                return false;
            }
        });
        
        if(!hasActive)
        {
            $(curr).children().addClass('Active');
        }
	}
	
	function dropdownSelectPrev()
	{
	    var $lis = $('.DropDownWrap.Active .DropDown ul').children();
	    var curr = $lis[$lis.length-1];
	    var hasActive = false;

        $lis.each(function(i){
            if($(this).children().hasClass('Active'))
            {
                cur = this;
                hasActive = true;
                $(this).children().removeClass('Active');
                //$(this).prev().children().addClass('Active');
                
                // if we reach the end of the list, restart from the top
                // else activate next in the list
                if( $($(this).prev()).get(0) == null )
                {
                    $(curr).children().addClass('Active'); // set first element (in the list) to Active
                    dropdownUpdateInput($(curr).children('a'));
                }
                else
                {
                    $(this).prev().children().addClass('Active');
                    dropdownUpdateInput($(this).prev().children('a'));
                }
                
                return false;
            }
        });
        
        if(!hasActive)
        {
            $(curr).children().addClass('Active');
        }
	}
	
	function dropdownUpdateInput(handle, strValue)
	{
	    var input = $(handle).parents('fieldset').find('input.txt');
	    var value = $(handle).html();
	    if(strValue)
	    {
	        value = strValue;
	    }
	    input.attr('value',value);
	}	
	$('.ui-slider-handle').mousedown(function(){
		if (!jQuery(this).parent().hasClass('ui-slider-disabled'))
		{
			counterStartLoop('CounterMax');
			counterStartLoop('CounterMin');
		}
    });
    
    $('.ui-slider-handle').mouseup(function(){
        counterStopLoop('CounterMax');
        counterStopLoop('CounterMin');
    });
 });
