var thumb_slider = 0;
var news_slider = 0;

$(document).ready(function () {  
	
	/*if(location.pathname == '/'){
		var COOKIE_NAME = 'flash_loaded';
		if ($.cookie(COOKIE_NAME) != null) {
			//The cookie exists. No need to do anything.:)
		} else {
			//The cookie doesn't exist. Create one and open the thickbox window.
			$.cookie(COOKIE_NAME, 'true', {expires: 1, path: '/'});
			tb_show('', '#TB_inline?modal=true&height=544&width=544&inlineId=test', null);
			$('#TB_ajaxContent').html('<a href="/infocomm09/"><img src="/style/images/lg_promo.gif"/></a>');
			swfobject.embedSWF("/flash/lg_promo.swf", "TB_ajaxContent", "544", "524", "9.0.0","/flash/expressInstall.swf", null, params, null);
			$('#TB_window').prepend('<div id="close-button"><img src="/style/images/close.gif" onclick="tb_remove()"/></div>');
		}		
	}*/
	

	$('#search').focus(function(){
		if($(this).val() == 'Search LG'){
			$(this).val('');
		}
	}); 
	
	$('#search').blur(function(){
		if($(this).val() == ''){
			$(this).val('Search LG');
		}
	});
	
	$('#email').focus(function(){
		if($(this).val() == 'Email'){
			$(this).val('');
		}
	}); 
	
	$('#email').blur(function(){
		if($(this).val() == ''){
			$(this).val('Email');
		}
	});
	
	$('.cat-top h3').click(function(){
		var toggle = $(this).find('.toggle');
		var products = $(this).parents('.cat-holder').find('.collapsable');
		if($(products).toggle().css('display') == 'none'){
			$(toggle).html('[ + ] Show');
		} else {
			$(toggle).html('[ - ] Hide');
		}
		return false;
	});
	
	if($('h1').html() == 'Search Results'){
		show_all();
	} else if($('h1').html() == 'Products' || $('h1').html() == 'Product Results' || $('h1').html() == 'Product Archive'){
		$('.cat-top h3:first').click();
	} else {
	}
	
	//Fixes an issue where for a single collapsing category is toggled closed on page load
	if($('.collapsable').length == 1){
	    if($('.collapsable').css('display') == 'none'){
	        $('.collapsable').show();
	        $('.toggle').html('[ - ] Hide');
	    }
	}
	
	//Collapse all categories for the /products page
	if($('h1').html() == 'Products' || $('h1').html() == 'Product Archive' || $('h1').html() == 'Product Results'){
	    hide_all();
	    
	    //Display the first category if the products page arrived at using keyword search or category type
	    if($.url.param('keywords') || $.url.param('category')){
	        var results = $('.cat-top h3');
	        if(results.length > 0){
	            $('.cat-top h3:first').click();
	        }
	    }
	}
	
	//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();
	
	//Product Thumbs
	$('#thumbs #arrow-left').click(
	   function(){
	       if(thumb_slider < 0){
    	       thumb_slider += 200;
    	       $('#thumb-slider').animate(
    	           {   left: thumb_slider + 'px'   },
    	           1000
    	       );
	       }
	       updateThumbArrows();
	   }
	);
	$('#thumbs #arrow-right').click(
	   function(){
           thumb_slider -= 200;
	       $('#thumb-slider').animate(
	           {   left: thumb_slider + 'px'   },
	           1000
	       );
	       updateThumbArrows();
	   }
	);
	
	//product subcategories
	$('.product-group-title').click(
	   function(){
	       if($(this).find('span').html() == '[ + ] Show'){
	           $(this).find('span').html('[ - ] Hide');
	           $(this).next().show();
	       } else {
	           $(this).find('span').html('[ + ] Show');
	           $(this).next().hide();
	       }
	   }
	);
	//expand the product subcategories if the only thing loaded
	if($('.collapsable').length == 1){
	    if($('.product-group-title').length == 1){
	       $('.product-group-title:first').click();
	    }
	}
	
	
	updateThumbSliderWidth();
	updateThumbArrows();
	
	//Select the first thumb
	$('#thumb-stage span img:first').click();
	
	
	$(window).bind("resize", resizeWindow);
  	resizeWindow();
});

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

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


function resizeWindow( e ) {
	/*
	if($('body').height() > $('document').height()){
		$('#main').height($('body').height());
	} else {
		$('#main').height($('document').height());
	}
	*/
	//$('#photos').width($('body').width() - 40);
}


function selectThumb(targetImgID, newImageFile, width, height, clickedThumbID){
    if(width > 0 && height > 0){
        var computedheight = 375 * height / width;
    }
    $('#' + targetImgID)
        .load(function(){
                if(width > 0 && height > 0){
                    $('#' + targetImgID)
                        .css('width', '375px')
                        .css('height', computedheight + 'px');
                }
                $(this).fadeIn('fast');
        })
        .fadeOut()
        .attr('src', newImageFile);
    $('#thumb-slider span').removeClass('active');
    /*$('#' + targetImgID + '_container')
        .css('width', '375px')
        .css('height', computedheight + 'px');*/
    $('#' + clickedThumbID).parents('span').addClass('active');
}


function updateThumbSliderWidth(){
    var width = 0;
    $.each(
        $('#thumbs span'),
        function(){
            width += $(this).width() + 20 + parseInt($(this).css('marginLeft'));
        }
    );
    $('#thumb-slider').css('width', width + 'px');
}


function updateThumbArrows(){
    if(thumb_slider < 0){
        $('#thumbs #arrow-left').show();
    } else {
        $('#thumbs #arrow-left').hide();
    }
    var width = 0;
    $.each(
        $('#thumbs span'),
        function(){
            width += $(this).width();
        }
    );
    if($('#thumb-stage').width() > width + thumb_slider){
        $('#thumbs #arrow-right').hide();
    } else {
        $('#thumbs #arrow-right').show();
    }
}


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();
    }
}