(function($){	
	$.Accordion = function(options){
		var self = this, animating = false, urlParams;
		
		this.defaults = {
			visibleClass: 'visible', linkClass: 'header'
		};
		
		this.settings = $.extend({}, this.defaults, options);
		var $target = this.settings.target;
		
		this.init = function(){
			var urlParams = $.getParams();
			this.setInitialHeight();
			
			if(typeof urlParams.tab != 'undefined'){
				$('dd', $target).height(0);
			}
			else {
				$('dd', $target).each(function(){
					var liHeight = $(this).find('li').eq(0).height();
					$(this).height(liHeight + 15)
				});
			}
			
			if (typeof urlParams.tab != 'undefined') this.showInitialTab(urlParams.tab);
			
			$('.' + this.settings.linkClass, $target).click(
				function(){
					var $nextPanel = $(this).parent().next('dd');
					if ($nextPanel.is('.' + self.settings.visibleClass)) {
						$('.header img').attr('src', '/PRODUCT_METADATA_0/Products/Phones/N-series/Home/img/arrow_open.gif');
						self.hide($nextPanel);
						return false;
					}
					self.settings.target.height('auto');
					
					if(!animating){
						animating = true;
						//if ($('dd.' + self.settings.visibleClass, $target).size() > 0) self.hide($('dd.' + self.settings.visibleClass, $target));
						self.hide($('dd', $target).not($nextPanel));
						
						$('.header img').attr('src', '/PRODUCT_METADATA_0/Products/Phones/N-series/Home/img/arrow_open.gif');
						self.show($nextPanel);
						$(this).find('img').attr('src', '/PRODUCT_METADATA_0/Products/Phones/N-series/Home/img/arrow_close.gif');
					}
					
					return false;
				}
			);
			
			this.equalizeHeight();
		};
		
		this.show = function($elem){
			//$elem.prev('dt').find('img').attr('src', '/PRODUCT_METADATA_0/Products/Phones/N-series/Home/img/arrow_close.gif');
			$elem.addClass(this.settings.visibleClass).animate({
				height: $elem.data('initialHeight')
			}, 'normal', function(){
				self.equalizeHeight();
				animating = false;
			});
		};
		
		this.hide = function($elem){
			self.equalizeHeight();
			//$elem.prev('dt').find('img').attr('src', '/PRODUCT_METADATA_0/Products/Phones/N-series/Home/img/arrow_open.gif');
			
			$elem.removeClass(this.settings.visibleClass).animate({
				height: 0
			}, 'normal');
		};
		
		this.equalizeHeight = function(){
			self.settings.targetContainer.height('auto');
			//if(self.settings.targetContainer.height() < self.settings.content.height()) self.settings.targetContainer.height(self.settings.content.height());
			//if(self.settings.targetContainer.height() > self.settings.content.height()) self.settings.content.height(self.settings.targetContainer.height());
			
			/*if(self.settings.targetContainer.height() < self.settings.content.height()) self.settings.targetContainer.animate({
				height: self.settings.content.height()
			}, 'fast');
			
			if(self.settings.targetContainer.height() > self.settings.content.height()) self.settings.content.animate({
				height: self.settings.targetContainer.height()
			}, 'fast');*/
		}
		
		this.setInitialHeight = function(){
			$('dd', $target).each(function(){
				var $this = $(this);
				$this.data('initialHeight', $this.height());
			});
		};
		
		this.showInitialTab = function(tab){
			$('dd.' + tab, $target).addClass(this.settings.visibleClass).height($('dd.' + tab).data('initialHeight'));
			$('dd.' + tab, $target).prev('dt').find('img').attr('src', '/PRODUCT_METADATA_0/Products/Phones/N-series/Home/img/arrow_close.gif');
		};
		
		// timeout needed for webkit browsers to properly calculate the height of the panels
		// additional ms added for IE 6		
		var timeoutLn;
		(jQuery.browser.msie && (parseInt(jQuery.browser.version) <= 6)) ? timeoutLn = 500 : timeoutLn = 100;

		setTimeout(function(){
			self.init();
		}, timeoutLn);
	};
	
	$.getParams = function(){
		var query = window.location.search, params = {};
		var p = query.substring(1, query.length).split('&');
		
		$.each(p, function(k, v){
			var tmp = v.split('=');
			params[tmp[0]] = tmp[1];
		});
		
		return params;
	};
})(jQuery);

