
var jsTabbedContent = 
{
	locked: false,
	slide: null,

	intro: function()
	{
		$('.content ul.selector li').append($('<div class="shutter"></div>'));

		if($('.content ul.selector li').length == 2)
		{
			$('.content ul.selector li:first').css({width:441});
			$('.content ul.selector li:first div.shutter').css({width:461});

			$('.content ul.selector li:last').css({width:319});
			$('.content ul.selector li:last div.shutter').css({width:339});
		}

		if($('.content ul.selector li').length == 4)
		{
			$('.content ul.selector li').css({width:180});
			$('.content ul.selector li div.shutter').css({width:200});
		}

		$('.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('background','none');

						ul.insertAfter(last);
						last = ul;
					}
					$(this).find("li:gt(3)").remove()
				}
			}
			if($(this).filter(".events").length > 0)
			{
				var uls = Math.ceil($(this).find('li').length/2);
				if(uls > 1)
				{
					var last = this;
					for(var i=1; i < uls; i++)
					{
						var ul = $('<ul class="slide" id="slide'+k+'-'+i+'"></ul>');
						ul.addClass("events");
						$(this).find("li").each(function(j) {(j >= (i*2) && j <= ((i*2)+1)) ? $(this).clone().appendTo(ul) : null;});
						
						ul.find('li:last').css('background','none');

						ul.insertAfter(last);
						last = ul;
					}
					$(this).find("li:gt(1)").remove()
				}
			}
			$(this).find('li:last').css('background','none');
		});
		
		$('.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').css({cursor:'pointer'}).find('.shutter').stop().animate({height:10},300,"easeOutBack");

				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");
			$(el).siblings('li[locked=true]').find('.shutter').stop().animate({height:10},300,"easeOutBack");
		}
	},

	onTabMouseOut: function(el)
	{
		if($(el).attr('locked') != 'true')
		{	
			$(el).find('.shutter').stop().animate({height:10},300,"easeOutBack");
			$(el).siblings('li[locked=true]').find('.shutter').stop().animate({height:0},500,"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()
	{	
		if($('li.dropdown').length > 0)
		{
			var top = 30;
			$('div.links').children().not('h2').each(function()
			{
				$(this).children('li').each(function()
				{
					$(this).css({position:"absolute",top:top,zIndex:1});
					top += $(this).height() + parseInt($(this).css("margin-bottom").replace("px",""));
					if($(this).css("border-top-width").indexOf("px") != -1)
					{
						top += parseInt($(this).css("border-top-width").replace("px","")) + parseInt($(this).css("border-bottom-width").replace("px",""));
					}
				});
				
				if($(this).hasClass('hr'))
				{
					$(this).css({position:"absolute",top:top,zIndex:1});
					top += 3;
				}
			});

			$('div.links > ul > li.dropdown').each(function()
			{
				var length = $(this).find('> ul > li').length;
				var timing = Math.max(length*40,500);
				
				var height = 19+5;
				$(this).find('> ul > li').each(function()
				{
					height += $(this).height() + parseInt($(this).css("margin-bottom").replace("px","")) + parseInt($(this).css("margin-top").replace("px",""));
				});
				
				var top = parseInt($(this).css("top").replace("px",""));

				var stateOpen = {height:14,top:top};
				var stateClose = {height:height,top:top-Math.round(height/2)};

				if(Math.round(height/2) > (192-top))
				{
					var offset = Math.round(height/2) - (192-top);
					stateClose.top = stateClose.top-offset;
				}

				$(this).children('> 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",function() {$(this).css({zIndex:1});});
					}
					else
					{
						jsDropdownList.close();
						$(this).parent("li").css({zIndex:99}).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()
	{
		$('div.links > ul > li.dropdown').each(function()
		{
			if($(this).css("height").replace("px","") > 14)
			{
				$(this).children("span:first").click();
			}
		});		
	}
}

var jsMultiDropdownList =
{
	timer: null,

	init: function()
	{
		$('div.links ul.multidropdown').next('div').css({marginTop:30});
		var top = 30+$('div.links ul.multidropdown > li').length*19;

		$('<li class="dropdown" style="position:absolute;top:'+top+'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 length = ul.children('li').length;
				var timing = Math.max((ul.find('li').length)*40,500);
				
				$('div.links ul.multidropdown li.dropdown').stop().animate({height:14,top:top}).removeClass("open");
				$(this).siblings('.dropdown').empty().append(span).append(ul);
				
				var height = 19+5;
				$("li.dropdown > ul > li").each(function()
				{
					height += $(this).height() + parseInt($(this).css("margin-bottom").replace("px","")) + parseInt($(this).css("margin-top").replace("px",""));
				});

				var stateOpen = {height:14,top:top};
				var stateClose = {height:height,top:top-Math.round(height/2)};
				
				if(Math.round(height/2) > (192-top))
				{
					var offset = Math.round(height/2) - (192-top);
					stateClose.top = stateClose.top-offset;
				}

				$('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,img.logo').each(function()
			{
				this.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" +this.src.replace(/\(/g, "%28").replace(/\)/g, "%29")+  "',sizingMethod='image')";
				this.src = "img/blank.gif";
			});

			$('div.holder ul.news li').not('.more').css({backgroundImage:'url(img/home/icon-arrow-list.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";
			}
			
			if($("#gcscForm").attr('title') == "IT")
			{
				document.gcscForm.CHAR_SET.value = '';
				document.gcscForm.LANGCODE_CHARSET.value = 'it,ISO-8859-1';
				document.gcscForm.LANG_CODE.value = 'it';
				document.gcscForm.COUNTRY_CODE.value = 'ITA';
				document.gcscForm.SERVICE_KIND.value = '1';
				document.gcscForm.SERVER_DOMAIN.value = 'http://italy.lgservice.com';
				document.gcscForm.COUNTRY_KIND.value = 'ITA' ;
				document.gcscForm.BUYER_CODE.value = 'IT000001';
				document.gcscForm.selCountry.value = 'ITA|http://it.lgservice.com|1';
				document.gcscForm.target = '_new';
				document.gcscForm.method = 'post';
				document.gcscForm.action = "http://italy.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();
	
	// search button rollover
	$("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");
	});

	//hero flash embed (paths to sub movies are read from the HTML page)
	// modify - cdn
	var so = new SWFObject("../flash/banner-mps.swf", "hero", "928", "287", "9", "#FFFFFF");
	//var so = new SWFObject("/flash/Home2.swf", "hero", "780", "250", "9", "#FFFFFF");
	so.addParam("allowScriptAccess", "always");
	so.addParam("wmode", "opaque");
	
	$("div#flashHero > div > a > span").each(function(i)
	{
		so.addVariable("var"+(i+1), $(this).text());
	});

	$("div#flashHero > div > a").each(function(i)
	{
		this.target = "_blank";
		so.addVariable("link"+(i+1), this.href.replace(/&/g,"%26"));
	});

	// change timing here
	so.addVariable("time1", 4000);

	if(!so.write("flashHero"))
	{
		// flash alternate functionality
		$('div.hero').append('<ul>');
		/*
		functionality which automatically recognizes sub level div under div.hero
		and writes the navigation automatically
		*/
		$('div.hero >div').each (function (i){
			var icount =i+1;
			$('div.hero ul').append ('<li>'+icount+'</li>');			
			})		
		$('div.hero div:eq(0)').show();
		$('div.hero ul li').click(function()
		{
			$(this).not('.active').addClass('active').siblings('li').removeClass('active');
			$('div.hero div:eq('+(parseInt($(this).text())-1)+')').show().siblings('div').hide();
		});
		
	}

	jsTabbedContent.intro();
}

