"use strict";
/*global window: true, document: true, jQuery: true, intel: true, location: true */
var intel = (!intel) ? {} : intel;
(function ($) {
	intel.appstore = {
		// method to display an object
		showIt : function (showWhatObj) {
			if (intel.appstore.checkDisplay(showWhatObj)) {
				if ($.browser.msie) {
					showWhatObj.show();
				} else {
					showWhatObj.fadeIn('slow');
				}
			}
		},
		// method to hide an object
		hideIt : function (hideWhatObj) {
			if ($.browser.msie) {
				hideWhatObj.hide();
			} else {
				hideWhatObj.fadeOut('slow');
			}
		},
		// method to an object display state
		checkDisplay : function (checkWhatObj) {
			if (checkWhatObj.css('display') === 'none') {
				intel.appstore.hideAll();
				return true;
			} else {
				return false;
			}
		},
		// method to hide all modal objects
		hideAll : function (doNotHideObj) {
			if ($('.appstore-faq-container') && doNotHideObj !== $('.appstore-faq-container')) {
				$('.appstore-faq-container').hide();
			}
			if ($('.appstore-carousel-container') && doNotHideObj !== $('.appstore-carousel-container')) {
				$('.appstore-carousel-container').hide();
			}
		},
		init : function () {
			var featuredAppDetails;
			// fix ServiceMark for IE6 and IE7
			if ($.browser.msie === true && ($.browser.version === "6.0" || $.browser.version === "7.0")) {
				$("#appstore-download-text .servicemark").html('<sup>SM</sup>');
			}
			// activate the download area elements
			$('#appstore-download-text').click(function () {
				waCustomLink(this,'appstore:en_us:links','o','wa_custom42=products:en_us:net_app_store:download_windows&wa_eCustom25=products:en_us:net_app_store:download_windows&wa_eCustom10=dnld:software_3rdparty&wa_events=se_cust15,se_cust10,se_points');
				location.href = $('#appstore-download-link-windows').attr('href');
			});
			$('#appstore-download-area-bg').click(function () {
				waCustomLink(this,'appstore:en_us:links','o','wa_custom42=products:en_us:net_app_store:download_windows&wa_eCustom25=products:en_us:net_app_store:download_windows&wa_eCustom10=dnld:software_3rdparty&wa_events=se_cust15,se_cust10,se_points');
				location.href = $('#appstore-download-link-windows').attr('href');
			});
			// activate the close button on the carousel popup
			$('.appstore-carousel-container .appstore-carousel-container-inner .appstore-carousel-closer').click(function (event) {
				intel.appstore.hideIt($('.appstore-carousel-container'));
			});
			// activate the close button on the faq popup
			$('.appstore-faq-container .appstore-faq-closer').click(function (event) {
				intel.appstore.hideIt($('.appstore-faq-container'));
			});
			// activate the FAQs link
			$('#appstore-faq-link').click(function (event) {
				event.preventDefault();
				intel.appstore.showIt($('.appstore-faq-container'));
			});
			// activate the featured app links
			$('.appstore-featured-apps div.appstore-featured-app a').click(function (event) {
				event.preventDefault();
				var imgCount, controlsArr = [], i;
				intel.appstore.showIt($('.appstore-carousel-container'));
				featuredAppDetails = $(this).parent().next('.appstore-featured-app-item').clone();
				//inject the featured app contents into the target div
				if ($('.appstore-carousel-container .appstore-carousel-container-inner .appstore-carousel-container-inner-target').find('.appstore-featured-app-item').length > 0) {
					$('.appstore-carousel-container .appstore-carousel-container-inner .appstore-carousel-container-inner-target').find('.appstore-featured-app-item').replaceWith(featuredAppDetails);
				} else {
					$('.appstore-carousel-container .appstore-carousel-container-inner .appstore-carousel-container-inner-target').append(featuredAppDetails);
				}
				// check to see if there is a carousel
				if ($('.appstore-carousel-container .appstore-carousel-container-inner .appstore-carousel-container-inner-target .appstore-featured-app-item-carousel.imageSliderExt .jCarouselLite')) {
					// get carousel image count
					imgCount = $('.appstore-carousel-container .appstore-carousel-container-inner .appstore-carousel-container-inner-target .appstore-featured-app-item-carousel.imageSliderExt .jCarouselLite').find('li').length;
					// build the pagination
					for (i = 0; i < imgCount; i += 1) {
						controlsArr.push(".appstore-featured-app-item-carousel.imageSliderExt ." + (1 + i));
						$('.appstore-carousel-container .appstore-carousel-container-inner .appstore-carousel-container-inner-target .appstore-featured-app-item-carousel.imageSliderExt .carousel-buttons').append('<button class="' + (1 + i) + ' numbered"></button>');
					}
					// initialize the carousel
					$('.appstore-carousel-container .appstore-carousel-container-inner .appstore-carousel-container-inner-target .appstore-featured-app-item-carousel.imageSliderExt .jCarouselLite').jCarouselLite({visible: 1, btnGo: controlsArr});
					// activate the pagination buttons to show proper state
					$('.appstore-carousel-container .appstore-carousel-container-inner .appstore-carousel-container-inner-target .appstore-featured-app-item-carousel.imageSliderExt .carousel-buttons').find('button').each(function () {
						$(this).bind("click focus", function () {
							$('.appstore-carousel-container .appstore-carousel-container-inner .appstore-carousel-container-inner-target .appstore-featured-app-item-carousel.imageSliderExt .carousel-buttons').find('button').each(function () {
								$(this).removeClass('has-focus');
							});
							$(this).addClass('has-focus');
						});
					});
					// set the first carousel item to show it has focus
					$('.appstore-carousel-container .appstore-carousel-container-inner .appstore-carousel-container-inner-target .appstore-featured-app-item-carousel.imageSliderExt .carousel-buttons').find('button:first').addClass('has-focus');
				}
			});
		}
	};

}(jQuery));
//Global JS Inits using jQuery
jQuery(document).ready(function () {
	intel.appstore.init();
});

