
var jsTabbedContent = 
{
	locked: false,
	slide: null,

	intro: function()
	{
		$('.content ul.selector li').append($('<div class="shutter"></div>'));

		$('.content ul.selector li div.shutter').each(function(i)
		{
			$(this).stop().animate({height:0},((i+1)*300),"easeOutBack",function()
			{
				$(this).animate({height:10},300,"easeOutBack");
			});
		});
		window.setTimeout(jsTabbedContent.init,1250);
	},

	init: function()
	{
		$('.content div.holder > ul').each(function(k)
		{
			$(this).attr('id','slide'+k+'-0').addClass('slide');
			if($(this).filter(".products,.promotions").length > 0)
			{
				var uls = Math.ceil($(this).find('li').length/4);
				if(uls > 1)
				{
					var last = this;
					for(var i=1; i < uls; i++)
					{
						var ul = $('<ul class="slide" id="slide'+k+'-'+i+'"></ul>');
						($(this).filter(".products").length > 0) ? ul.addClass("products") : ul.addClass("promotions");
						$(this).find("li").each(function(j) {(j >= (i*4) && j <= ((i*4)+3)) ? $(this).clone().appendTo(ul) : null;});
						
						ul.find('li:last').css('border',0);

						ul.insertAfter(last);
						last = ul;
					}
					$(this).find("li:gt(3)").remove()
				}
			}
			$(this).find('li:last').css('border',0);
		});
		
		$('.content ul.selector li').each(function(i)
		{	
			$(this).attr('id','item'+i+'-0')
				.hover(function() {jsTabbedContent.onTabMouseOver(this);},function() {jsTabbedContent.onTabMouseOut(this);})
				.click(function() {jsTabbedContent.onTabClick(this);});
		});

		$('.content div.holder ul.categories > li').each(function(i)
		{
			$(this).css({zIndex:(1000-i)});
		});
		
		$('.content div.holder ul.categories li ul').find('> li:last').css({borderRight:"solid 1px #e4e4e4",borderLeft:"solid 1px #e4e4e4",borderBottom:"solid 1px #e4e4e4"});
		$('.content div.holder ul.categories > li:last').css({border:0});
		
		$('.content div.holder ul.categories li.title').click(function()
		{
			if($(this).find('a').length > 0)
			{
				location.href = $(this).find('a').get(0).href;
			}
		});

		$('.content div.holder ul.categories li ul').hover(
			function()
			{
				$(this).children('li.title').css({backgroundColor:'#bdbebe',color:'#ffffff'}).children('a').css({color:'#ffffff'});
				$(this).children('li').not('.title').show();
			},
			function()
			{
				$(this).children('li.title').css({backgroundColor:'transparent',color:'#c5003d'}).children('a').css({color:'#c5003d'});
				$(this).children('li').not('.title').hide();
			}
		);

		$('.content div.holder ul.categories > li > ul > li').hover(
			function()
			{
				$(this).children('> ul').children('> li').not('br').show();
			},
			function()
			{
				$(this).children('> ul').children('> li').not('br').hide();
			}
		);

		$('.content ul.selector li:first').click();
	},
	
	onTabClick: function(el)
	{
		jsDropdownList.close();

		if(!jsTabbedContent.locked)
		{
			if($('.content .slide:animated').length == 0 && $(el).attr('locked') != 'true')
			{
				$(el).attr('locked','true').css({cursor:'default'}).find('.shutter').stop().animate({height:0},500,"easeOutBack");
				$(el).siblings('li').attr('locked','false').trigger('mouseleave').css({cursor:'pointer'});

				jsTabbedContent.transition(el);
				$('.pager').remove();
				var group = $(el).attr('id').replace('item','').substr(0,$(el).attr('id').replace('item','').length-2);
				var pages = $("ul[id^=slide"+group+"]").length;
				if(pages > 1)
				{
					var pager = $('<ul class="pager" style="display:none;"></ul>');
					for(var i=0; i < pages; i++)
					{
						if(i == 0)
						{
							var li = $('<li id="pager'+group+'-0">'+(i+1)+'</li>');
							li.addClass("active");
						}
						else
						{
							var li = $('<li id="pager'+group+'-'+(i)+'">'+(i+1)+'</li>');
							li.click(function() {jsTabbedContent.onPageClick(this);});
						}
						pager.append(li);
					}
					pager.appendTo('.holder');
				}
			}
		}
	},

	onPageClick: function(el)
	{
		jsDropdownList.close();

		if(!jsTabbedContent.locked)
		{
			jsTabbedContent.transition(el);
			$('.pager li').each(function()
			{
				if($(this).attr('id') == $(el).attr('id'))
				{
					$(this).unbind().addClass('active');
				}
				else
				{
					$(this).unbind().removeClass('active').click(function() {jsTabbedContent.onPageClick(this);});
				}
			});
		}
	},

	onTabMouseOver: function(el)
	{
		if($(el).attr('locked') != 'true')
		{
			$(el).find('.shutter').stop().animate({height:0},500,"easeOutBack");
		}
	},

	onTabMouseOut: function(el)
	{
		if($(el).attr('locked') != 'true')
		{
			$(el).find('.shutter').stop().animate({height:10},300,"easeOutBack");
		}
	},

	transition: function(el)
	{
		jsTabbedContent.lock();
		var current = null;

		$('.content .slide').each(function(i) {($(this).css('top') == '1px') ? current = $(this) : null;});
		(current != null) ? current.animate({top:-200},1000,"easeOutQuad",function() {$(this).css({top:240});}) : null;
		
		jsTabbedContent.slide = el;
		window.setTimeout("jsTabbedContent.contentIn()",500);
	},

	contentIn: function()
	{
		var el = jsTabbedContent.slide;
		jsTabbedContent.slide = null;

		var max = $('#'+$(el).attr('id').replace('pager','slide').replace('item','slide')).children('li').length;

		$('#'+$(el).attr('id').replace('pager','slide').replace('item','slide')).css('top',1).children('li').each(function(i)
		{
			if(i == (max-1))
			{
				$(this).css({position:'relative',top:((i+1)*200)}).stop().animate({top:0},((i+1)*600),"easeOutExpo",function()
				{
					jsTabbedContent.unlock();
					$('.pager').show();
				});
			}
			else
			{
				$(this).css({position:'relative',top:((i+1)*200)}).stop().animate({top:0},((i+1)*600),"easeOutExpo");
			}
		});
	},

	lock: function()
	{
		this.locked = true;
	},

	unlock: function()
	{
		this.locked = false;
	}
}

var jsDropdownList = 
{	
	timer: null,

	init: function()
	{
		$('li.dropdown').next('li').css({paddingTop:20,backgroundPosition:"0 25px"});

		var stateOpen = {height:14};
		var stateClose = {height:160};
		var length = $("div.links ul > li.dropdown ul li").length;

		stateOpen = {height:14,top:30};
		stateClose = {height:(160+((length-7)*16)),top:-((length-7)*16)};

		var timing = Math.max(length*40,500);

		$("div.links ul > li.dropdown span").hover(function() {$(this).parent("li").toggleClass('hover');},function() {$(this).parent("li").toggleClass('hover');}).click(function()
		{
			if($(this).parent("li").css("height").replace("px","") > 14)
			{
				$(this).parent("li").stop().animate(stateOpen,timing,"easeInSine");
			}
			else
			{
				$(this).parent("li").stop().animate(stateClose,timing,"easeOutSine");
			}
		});

		$('div.links ul > li.dropdown').unbind().hover(
			function()
			{
				window.clearTimeout(jsDropdownList.timer);
			},
			function()
			{
				jsDropdownList.timer = window.setTimeout("jsDropdownList.close()",500);
			}
		);
	},

	close: function()
	{
		if($("div.links ul > li.dropdown").length != 0)
		{
			if($("div.links ul > li.dropdown").css("height").replace("px","") > 14)
			{
				$("div.links ul > li.dropdown span").click();
			}
		}
	}
}

var jsMultiDropdownList =
{
	timer: null,

	init: function()
	{
		$('div.links ul.multidropdown').next('div').css({marginTop:30});
		var defaultTop = 30+$('div.links ul.multidropdown > li').length*19;

		$('<li class="dropdown" style="top:'+defaultTop+'px;z-index:1000;"></li>').appendTo('div.links ul.multidropdown');
		
		$('div.links ul.multidropdown li').not(".dropdown").css("cursor","pointer").click(function()
		{
			if($(this).attr('locked') != 'true')
			{
				$(this).attr('locked','true').css({cursor:"default",color:"#c5003d"}).siblings('li').not('.dropdown').attr('locked','false').css({cursor:"pointer",color:"#afafaf"});
				
				var span = "<span> = "+$(this).html()+"</span>";

				if($(this).attr('title') != null && $(this).attr('title') != "")
				{
					span = "<span> = "+$(this).attr('title')+"</span>";
				}

				var ul = $(this).children('ul').clone().css("display","block");
				
				var stateOpen = {height:14,top:defaultTop};
				var stateClose = {top:192-(defaultTop-30)};
				
				var length = ul.find('li').length;
				
				var height = ((length+1)*18)+12;

				if((height/2) < (192-(defaultTop-30)))
				{
					stateClose = {height:height,top:defaultTop-(height/2)};
				}
				else
				{
					stateClose = {height:(192+((length-7)*16)),top:-((length-7)*16)};
				}

				var timing = Math.max((ul.find('li').length)*40,500);

				$('div.links ul.multidropdown li.dropdown').stop().animate(stateOpen).removeClass("open");
				$(this).siblings('.dropdown').empty().append(span).append(ul);

				$('div.links ul.multidropdown li.dropdown span').hover(function() {$(this).parent("li").toggleClass('hover');},function() {$(this).parent("li").toggleClass('hover');}).click(function()
				{
					if($(this).parent("li").css("height").replace("px","") > 14)
					{
						$(this).parent("li").stop().animate(stateOpen,timing,"easeInSine").removeClass("open");
					}
					else
					{
						$(this).parent("li").stop().animate(stateClose,timing,"easeOutSine").addClass("open");
					}
				});

				$('div.links ul.multidropdown li.dropdown').unbind().hover(
					function()
					{
						window.clearTimeout(jsMultiDropdownList.timer);
					},
					function()
					{
						jsMultiDropdownList.timer = window.setTimeout("jsDropdownList.close()",500);
					}
				);
			}
		});

		$('div.links ul.multidropdown li:first').click();
	}
}

var jsEmailCaptureForm =
{
	init: function()
	{
		($.browser.msie) ? $(".emailCapture input.submit").css("top",3) : null;
		
		$(".emailCapture input.submit").hover(
			function()
			{
				this.src=this.src.replace('-right.jpg','-right-red.jpg');
			},
			function()
			{
				this.src=this.src.replace('-right-red.jpg','-right.jpg');
			}
		);

		$(".emailCapture input.text").each(function()
		{
			$(this).attr("defaultvalue",$(this).val())
				.focus(function()
				{
					($(this).val() == $(this).attr('defaultvalue')) ? $(this).val("") : null;
				})
				.blur(function()
				{
					($(this).val() == "") ? $(this).val($(this).attr('defaultvalue')) : null;
				});
		});
	}
}

var jsScroller =
{
	init: function()
	{
		$(".scroller").append('<img src="img/home/icon-scroll-up.jpg" class="up" alt="" />');
		$(".scroller").append('<img src="img/home/icon-scroll-down.jpg" class="down" alt="" />');
		
		var height = 40;

		$('.scroller div li').each(function()
		{
			height -= $(this).height();
		});

		$(".scroller img.down").hover(function() {this.src=this.src.replace('-down.jpg','-down-red.jpg');},function() {this.src=this.src.replace('-down-red.jpg','-down.jpg');}).mousedown(function()
		{
			$('.scroller div ul').animate({top:height},2000);
		});

		$(".scroller img.up").hover(function() {this.src=this.src.replace('-up.jpg','-up-red.jpg');},function() {this.src=this.src.replace('-up-red.jpg','-up.jpg');}).mousedown(function()
		{
			$('.scroller div ul').animate({top:0},2000);
		});

		$(".scroller img.up,.scroller img.down").mouseup(function()
		{
			$('.scroller div ul').stop();
		});
	}
}

var jsPNGfix =
{
	init: function()
	{
		var ie6 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 6.0") != -1);
		
		if(ie6)
		{
			$('div.content img').each(function()
			{
				this.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" +this.src+ "',sizingMethod='image')";
				this.src = "img/blank.gif";
			});
		}
	}
}

var jsServiceLink =
{
	init: function()
	{
		if($("#gcscForm").length > 0)
		{
			if($("#gcscForm").attr('title') == "UK")
			{
				document.gcscForm.CHAR_SET.value='';
				document.gcscForm.LANGCODE_CHARSET.value = 'en,ISO-8859-1';
				document.gcscForm.LANG_CODE.value='en';
				document.gcscForm.COUNTRY_CODE.value='GBR';
				document.gcscForm.SERVICE_KIND.value='1';
				document.gcscForm.SERVER_DOMAIN.value='http://gb.lgservice.com';
				document.gcscForm.COUNTRY_KIND.value='GBR' ;
				document.gcscForm.BUYER_CODE.value='GB000001';
				document.gcscForm.selCountry.value='GBR|http://gb.lgservice.com|1';
				document.gcscForm.target = '_new';
				document.gcscForm.method = 'post';
				document.gcscForm.action= "http://gb.lgservice.com/gcsc/b2c/hpi/main";
			}

			$(".serviceLink").each(function()
			{	
				var cmd = this.title;
				$(this).attr('href',"javascript:void(0);").click(function()
				{
					document.gcscForm.tc.value = cmd; 
					document.gcscForm.submit();
				});
			});
		}
	}
}

window.onload = function()
{
	jsPNGfix.init();
	jsDropdownList.init();
	jsMultiDropdownList.init();
	jsScroller.init();
	
	jsEmailCaptureForm.init();
	jsServiceLink.init();
	
	$("div.search input.submit").hover(function() {this.src=this.src.replace("-search.jpg","-search-red.jpg");},function() {this.src=this.src.replace("-search-red.jpg","-search.jpg");});

	jsTabbedContent.intro();
}
