(function() {
	motorola.shareThisCallback = function(SharedObject) {
		var hash = document.location.hash;
		if (hash) {
			hash = hash.replace(/video\[(.*?)\]/,'');
		}
		SharedObject.properties.url = document.location.toString().replace(document.location.hash,hash) + (motorola.supportKernel.xml ? 'video[' + motorola.supportKernel.xml + ']' : '');
		
		//webtrends
		var productId = getMetaTag('WT.pn.id');
		dcsMultiTrack('WT.ti', 'Share ' + motorola.supportKernel.videoTitle + ' video from ' + productId + ' Support Page', 'WT.cg_s', 'Share This', 'DCSext.action', 'Share This;' + motorola.supportKernel.videoTitle + ';Click');
	};
	var hash = location.hash;
	Pagination.implement({
		isFirstPage: function() { return this.page === 1; },
		isLastPage: function() { return this.page === this.numberPages; }
	});
	var topicHeaderClick = function(e) {
		var evt = new Event(e);
		evt.stop();
		var slider = this.retrieve('fx.slide');
		if (slider.open) {
			this.getParent().removeClass('open').addClass('closed');
		} else {
			this.getParent().addClass('open').removeClass('closed');
		}
		slider.toggle();
	};
	var periodicDisplay = function(property,first,second) {
		this.setStyle(property,first).setStyle(property,second);
	};
	motorola.pages.SupportKernel = new Class({
		carrierSelect: null,
		container: new Element('div',{id:'flashWrapper',html:'<div id="flashContent"></div>'}),
		modal: null,
		sliders: [],
		xml: null,
		videoTitle: null, // webtrends
		initialize: function() {
			var play = new Element('div',{'class':'play'});
			addEvent('domready',this.domready.pass([play],this));
			addEvent('load',this.load.bind(this));
		},
		domready: function(play) {
			motorola.utils.PngFix.fixClass('png');
			var options = {
				onCreate: this.modalCreate.bind(this),
				onHideComplete: function(modal) { modal.element.addClass('hidden'); },
				onHideStart: this.hideFlash.bind(this),
				onShowComplete: this.setupFlash.bind(this),
				onShowStart: function(modal) {
					if (Browser.Engine.trident) {
						modal.element.setStyles({top:getScroll().y + (getSize().y / 2)});
					}
					modal.element.removeClass('hidden');
				}
			};
			if (Browser.Engine.trident) { options.elShownSelector = '.modalShownIE6'; }
			this.modal = new Modal(this.container,options);
			this.modal.element.adopt(new Element('a',{'class':'close',href:'#',events: {click:this.closeModal.bind(this)}}));
			this.modal.element.get('morph').set('.modalHidden');
			this.setupBlinds.delay(50,this);
			this.setupSearch.delay(100,this);
			this.setupCarrier.delay(200,this);
			this.setupVideo.delay(300,this);
			if ($('videoWrapper')) { this.setupCarousel.delay(300,this,[play]); }
		},
		load: function() {
			this.setupArrowLinks();
		},
		changeCarrier: function(e) {
			var dropdown = e.target;
			var value = dropdown.value;
			var allManuals = $$('#manuals .arrowLinks li');
			allManuals.setStyle('display','block');
			allManuals.filter(function(li,i) {
				return li.hasClass('carrier');
			}).each(function(li) {
				li.setStyle('display','none');
			});
			if (value) {
				var carrierManuals = $$('#manuals .arrowLinks li.' + value);
				carrierManuals.each(function(li) {
					var clazz = li.get('class').split(' ');
					clazz.remove(value);
					clazz.remove('carrier');
					var type = clazz[0];
					$$('#manuals .arrowLinks li.' + type).setStyle('display','none');
					$$('#manuals .arrowLinks li.' + type + '.' + value).setStyle('display','block');
				});
			}
			document.getElement('#manuals .bottom-cap').setStyle('height','auto').setStyle('height','4px');
		},
		closeModal: function(e) {
			var evt = e ? new Event(e) : e;
			if (evt) evt.stop();
			this.xml = null;
			this.modal.backdrop.setStyle('visibility','hidden');
			this.modal.hide();
		},
		hideFlash: function(modal) {
			$(document.body).removeClass('hideframes');
			modal.element.removeClass('closeable');
			$('flashWrapper').empty().adopt(new Element('div',{id:'flashContent'}));
		},
		openModalWebtrends: function(e,a) {
			this.videoTitle = a.get('title');
			this.openModal(e,a.get('href'));
		},
		openModal: function(e,xml) {
			var evt = e ? new Event(e) : e;
			if (evt) evt.stop();
			this.xml = xml;
			this.modal.backdrop.setStyles({height:getScrollSize().y,visibility:'visible'});
			this.modal.show();
		},
		hidePage: function(els,pagination) {
			this.carousel.videoMask.get('morph').removeEvents().addEvent('onComplete',this.showPage.pass([els,pagination],this)).start({'opacity':'0'});
		},
		modalCreate: function(modal) {
			modal.element.addClass('hidden').get('morph').set(modal.config.elHiddenSelector);
			modal.backdrop = new Element('div',{'class':'backdrop'});
			$(document.body).adopt(modal.backdrop);
		},
		searchSubmit: function(e) {
			var evt = new Event(e);
			var field = $('faq-search').getElement('input[type=text]');
			var value = field.get('value');
			if (!value) {
				evt.stop();
			}
		},
		setupArrowLinks: function() {
			$('product').getElements('.arrowLink').each(motorola.utils.autoSizeArrowLink);
			
			//moto: added if condition
			if ($('topics')) {
				$('topics').getElements('.arrowLink').each(motorola.utils.autoSizeArrowLink);
			}
		},
		setupBlinds: function() {
			var sliders = this.sliders;
			var topicHeaders = $$('.topic .header');
			var topicBodys = $$('.topic .body');
			var bottomCap = document.getElement('#faqs .bottom-cap');
			var sliderOptions = {
				duration: motorola.fxoptions.duration,
				transition: motorola.fxoptions.transition
			};
			topicHeaders.each(function(header,i) {
				var body = topicBodys[i];
				var slider = new Fx.Slide(body, sliderOptions);
				var periodic;
				var period = slider.options.duration / slider.options.fps;
				if (Browser.Engine.trident5) {
					slider.addEvent('onStart',function() {
						periodic = periodicDisplay.periodical(period,bottomCap,['height','auto','4px']);
					});
					slider.addEvent('onComplete',function() {
						$clear(periodic);
					});
				}
				header.store('fx.slide',slider);
				header.addEvent('click',topicHeaderClick.bind(header));
				slider.hide();
				sliders.push(slider);
			});
		},
		setupCarrier: function() {
			var carrierSelect = $('carrier');
			if (!carrierSelect) { return; }
			var selectOptions = $extend($clone(motorola.fxoptions),{
				'duration': 250,
				'animate': Browser.Engine.webkit,
				'onChange': this.changeCarrier
			});
			this.carrierSelect = new MOTO.StyledForm.Dropdown(carrierSelect,selectOptions);
		},
		setupCarousel: function(play) {
			var videoWrapper = $('videoWrapper')
			videoWrapper.getElements('img.video').each(function(img) {
				img.getParent().adopt(play.clone());
			});
			var videos = videoWrapper.getChildren();
			videos.each(function(li) { li.addEvent('click',this.openModalWebtrends.bindWithEvent(this,li.getElement('a'))); },this);
			if (videos.length <= 3) { return; }
			this.carousel = new Pagination(videos,{onPage:this.hidePage.bind(this),pageSize:3,wrap:false});
			var next = new Element('a',{id:'carouselNext','class':'carouselControl',href:'#',events:{
				click: this.carousel.getNextPage.bind(this.carousel)
			}});
			next.addEvent('click',motorola.utils.stopEvent);
			var previous = new Element('a',{id:'carouselPrevious','class':'carouselControl',href:'#',events:{
				click: this.carousel.getPreviousPage.bind(this.carousel)
			}});
			videos.slice(3).each(function(el) { el.dispose(); });
			previous.addEvent('click',motorola.utils.stopEvent);
			this.carousel.videoWrapper = videoWrapper;
			this.carousel.videoMask = $('videoMask');
			this.carousel.videoContent = $('videoContent');
			$('videoContent').adopt(next,previous);
		},
		setupFlash: function(modal) {
			$(document.body).addClass('hideframes');
			modal.element.addClass('closeable');
			var swfSource = motorola.modal.swf;
			var expressInstallPath = motorola.globals.expressInstallPath;
			var flashvars = {videoXML:this.xml,locale:motorola.swf.locale};
			var params = {menu:'false', scale:'noscale', salign:'LT', allowScriptAccess:'always', bgcolor:'#ffffff',wmode:'opaque',allowFullScreen:'true'};
			var attributes = {id:'MOTO'};
			swfobject.embedSWF(swfSource, 'flashContent', '800', '450', '9.0.114', expressInstallPath, flashvars, params, attributes);
		},
		setupLightbox: function(module) {
			var a = module.getElement('.action a');
			module.adopt(a.clone().addClass('action').set('html',''));
			module.getElements('a').addEvent('click',this.openModal.bindWithEvent(this,[a.get('href')]));
		},
		setupVideo: function() {
			var xml = hash.replace(/.*video\[(.*?)\].*/,'$1');
			if (xml && xml.match(/\.xml$/)) {
				this.openModal.delay(100,this,[null,xml]);
			}
		},
		setupSearch: function() {
			var faqSearch = $('faq-search');
			if (!faqSearch) { return; }
			var field = faqSearch.getElement('input[type=text]');
			field.set('value',field.get('title') || '');
			faqSearch.addEvent('submit',this.searchSubmit);
		},
		showPage: function(els,pagination) {
			this.carousel.videoContent.removeClass('firstPage').removeClass('lastPage');
			if (pagination.isFirstPage()) { this.carousel.videoContent.addClass('firstPage'); }
			else if (pagination.isLastPage()) { this.carousel.videoContent.addClass('lastPage'); }
			this.carousel.videoWrapper.getChildren().dispose();
			this.carousel.videoWrapper.adopt(els);
			this.carousel.videoMask.get('morph').removeEvents().start({'opacity':'1'});
		}
	});
})();
motorola.supportKernel = new motorola.pages.SupportKernel();

function getMetaTag(name) {
	var metaTags = document.getElementsByTagName("META");
	for(var i=0;i<metaTags.length; i++) {
		if(metaTags[i].name == name) {
			return metaTags[i].content;
		}
	}
	return '';
}
