var currentsel = 0;
var news_slider = 0;

$(document).ready(function () {
	var default_text = "Search all of LG";
	if ($('#searchbox').val() === ""){
		$('#searchbox').val(default_text);
	}
	$('#searchbox').focus(function(){
		if ($(this).val() === default_text){
			$(this).val("");
		}
	});
	$('#searchbox').blur(function(){
		if ($(this).val() === ""){
			$(this).val(default_text);
		}
	});
	
	var newsletter_text = "Enter Email";
	if ($('#newsletterq').val() === ""){
		$('#newsletterq').val(newsletter_text);
	}
	$('#newsletterq').focus(function(){
		if($(this).val() == newsletter_text){
			$(this).val('');
		}
	}); 
	
	$('#newsletterq').blur(function(){
		if($(this).val() == ''){
			$(this).val(newsletter_text);
		}
	});
	
	$('#news-year').change(function(){
		window.location = window.location.pathname + "?year="+ $(this).val();
	}); 
	
	//News Thumbs
	$('#news-left').click(
	   function(){
	       if(news_slider < 0){
	           news_slider += 311;
	           $('#news-slider').animate(
	               {   left: news_slider + 'px'    },
	               1000
	           );
	       }
	       updateNewsArrows();
	   }
	);
	$('#news-right').click(
	   function(){
	       news_slider -= 311;
	       $('#news-slider').animate(
	           {   left: news_slider + 'px' },
	           1000
	       );
	       updateNewsArrows();
	   }
	);
	
	updateNewsSliderWidth();
	updateNewsArrows();
  	    
    $('.quick .title .plus').click(function(){
    	// first determine current state of the menu, before we perform close all
    	var menu_state = $(this).html();
    	
    	// close all menu (this would include the menu being clicked, hence the need to check status
    	$('.title').removeClass('title-open');
    	$('.title').addClass('title-closed');
    	$('.plus').html("[+]");
    	$('.quick-container').slideUp('normal');
    	
    	// open the clicked menu only if it was previously closed
    	if (menu_state === "[+]"){
    		// open current menu
    		$(this).html("[&#8211;]");
    		$(this).parents(".title").removeClass('title-closed');
    		$(this).parents(".title").addClass('title-open');
    		$(this).parents(".title").next().slideDown('normal');
    	}
    });
    
    // navigation container style 2, clicking on the title will expand/contract menu    
    $('.quick-expand .title').click(function(){
    	// first determine current state of the menu, before we perform close all
    	var menu_state = $(this).find('.plus').html();
    	
    	// close all menu (this would include the menu being clicked, hence the need to check status
    	$('.title').removeClass('title-open');
    	$('.title').addClass('title-closed');
    	$('.plus').html("[+]");
    	$('.quick-container').slideUp('normal');
    	
    	// open the clicked menu only if it was previously closed
    	if (menu_state === "[+]"){
    		// open current menu
    		$(this).find('.plus').html("[&#8211;]");
            $(this).removeClass('title-closed');
            $(this).addClass('title-open');
            $(this).next().slideDown('normal');
    	}
    });
    
    $('#current-country').toggle(
    	function(){
    		$('#country-list').fadeIn("fast");
    	},
    	function(){
    		$('#country-list').fadeOut("fast");
    	}
    );
});

function show_all(){
	$('.toggle').html('[ - ] Hide');
	$('.collapsable').show();
}

function hide_all(){
	$('.toggle').html('[ + ] Show');
	$('.collapsable').hide();
}

function updateNewsSliderWidth(){
    var width = 0;
    $.each(
        $('.news-item'),
        function(){
            width += $(this).width() + 20 + parseInt($(this).css('marginLeft'));
        }
    );
    $('#news-slider').css('width', width + 'px');
}


function updateNewsArrows(){
    if(news_slider < 0){
		$('#news-left').show();
    } else {
		$('#news-left').hide();
    }
    var width = 0;
    $.each(
        $('.news-item'),
        function(){
            width += $(this).width();
        }
    );
    if($('#news-stage').width() > width + news_slider){
		$('#news-right').hide();
    } else {
		$('#news-right').show();

    }
}

function clearText(thefield){
if (thefield.defaultValue==thefield.value)
thefield.value = ""
}

function disable_fields(id){
	$('#'+id+' input').attr("disabled","disabled");
  $('#'+id+' select').attr("disabled","disabled");
}
function enable_fields(id){
	$('#'+id+' input').removeAttr("disabled");
  $('#'+id+' select').removeAttr("disabled");
}

