/*global $ */
window.Alienware = window.Alienware || {};
Alienware.com = Alienware.com || {};
Alienware.com.microsite = Alienware.com.microsite || {};
Alienware.com.microsite.M11x = function(params) {
    /**
    * @param {String}       params.something      	any var received
    */

	//this.something = params.something || "default value";
	
    this.initialize();
	
}


Alienware.com.microsite.M11x.prototype = {
    initialize: function() {
        
		$this = this;
		
		//$this.initEyeFlash();
		$this.initTabs();
		//$this.initTabExpansionWindows();
		$this.initLightBox();
		$this.initGallery();
		$this.initSubmitPopup();
        	
    },
	
	initTabs: function() {
		var currentActiveTab, currentActiveRow = null;

		currentActiveTab = $(".tabs a.active");
		currentActiveTab = currentActiveTab[0];

		if ( $(".productTabs .productRowGroup.first").length > 0  ) {
			//alert( $(".productTabs .productRowGroup.first") )
			currentActiveRow =  $(".productTabs .productRowGroup.first");
			$(currentActiveRow).removeClass("first");
			//currentActiveRow = currentActiveRow[0];
			//alert(currentActiveRow.id);
			var allRows = $(".productTabs .productRowGroup");
			for (var i = 0; i < allRows.length; i++) {
				if ( $(allRows)[i].style.display == "block" ) {
					currentActiveRow = 	$(allRows)[i];
				}	
			}
			//alert( $(currentActiveRow)[0].id );
		} else {
			currentActiveRow =  $(".productTabs .productRowGroup:first");
			//alert(": first");
		}
		// currentActiveRow = $(".productTabs .productRowGroup:first");
		
		/* If URL asks for another tab*/


		function changeTab(tab,row) {
				if ($(currentActiveRow).id != $(row)[0].id) {
					$(currentActiveRow).css("display","none");
					$(currentActiveRow).removeClass("first");
					$("span",currentActiveTab).removeClass("active");
				}
				
				$(row).css("display","block");
				$("span",tab).addClass("active");
				
				currentActiveTab = tab;
				currentActiveRow = $(row);
		}

		function changePanelOnly(row) {
				if ($(currentActiveRow).id != $(row)[0].id) {
					$(currentActiveRow).css("display","none");
					$(currentActiveRow).removeClass("first");
					$("span",currentActiveTab).removeClass("active");
				}
				
				$(row).css("display","block");
				
				currentActiveTab = currentActiveTab;
				currentActiveRow = $(row);
		}

		function setupTabs(){
			// commented out tab expand arrows for the disable of dropdown function
			var $rowGroup = $('.productTabs .productRowGroup');
			
			$rowGroup.each(function(){
				//$('.productTabs a.tab_1 span').append("<span class='expand'></span>");
				$('.row:not(:first)',$(this)).css("display","none");											  
			});
						
			$('.productTabs .specsExpand a').css("display","block");
		}
		
		setupTabs();
		
		var $tabs = $('.tabs a');
		
		$tabs.each(function(intIndex){
			$(this).bind("click", function(e){
				e.preventDefault();	
				
				var $rowGroup = $(".productTabs .productRowGroup").eq(intIndex); 
				changeTab(this,$rowGroup);
			
				if ($("span.active",this).length > 0) {
					$(".row:not(:first)",$rowGroup).slideDown(); //changed from slideToggle and moved below changeTab to disable dropdown function
				}
				
				//Change EYE
				/*
				*/
				if (intIndex == 2) {
					$(".micrositeEye").addClass("Desktops-GulfTown-Area-51");
				} 
				else {
					$(".micrositeEye").removeClass("Desktops-GulfTown-Area-51");
				}
				
				if (intIndex == 1) {
					$(".micrositeEye").addClass("Laptops-HardwareHeaven");
				} 
				else {
					$(".micrositeEye").removeClass("Laptops-HardwareHeaven");
				}
				
				
			});
		});
		
		
		
		/* NOTE: The below function is tailored to only a SPECIFIC banner!!!! */
		$(".bannerPanel").bind("click", function(e){
			e.preventDefault();	
			
			var $rowGroup = $(".productTabs .productRowGroup").eq(0); 
			changePanelOnly($rowGroup);
		
			/*
			if ($("span.active",this).length > 0) {
				$(".row:not(:first)",$rowGroup).slideDown(); //changed from slideToggle and moved below changeTab to disable dropdown function
			}
			*/
		
		});
						
	},
	
	initTabExpansionWindows: function() {
		function expandWindow(obj){
			$(".specsExpand",obj).click(function(e){
				e.preventDefault();	
				$(".specsWindow",obj).slideDown();
				$(".specsCollapse",obj).css("display", "block");
				$(".specsCollapse a",obj).css("display","block");
			});
			$(".specsCollapse a",obj).click(function(e){
				e.preventDefault();	
				$(".specsWindow",obj).slideUp();
				$(".specsCollapse",obj).css("display", "none");
				$(".specsCollapse a",obj).css("display","none");
			});	
		}
		
		$(".row li").each(function(){
			expandWindow(this);
		});

	},
	
	initGallery: function() {
		$(".gallery .galleryThumbs a").click(function(e){
			e.preventDefault();
			var mainImg = $(".gallery #mainImg img");
			var newImg = $(this).attr("href") || mainImg.attr("src");
			mainImg.attr("src", newImg);
		});
	},
	
	initEyeFlash: function() {	
		var flashvars 	= {};
		var params 		= { bgcolor: "#000000", base: 'http://image.alienware.com/images/microsite/holiday_2009/flash/', allowscriptaccess: "always", wmode: "transparent" };
		var attributes 	= { };
		
		swfobject.embedSWF( "http://image.alienware.com/images/microsite/holiday_2009/flash/flash_eye_holiday.swf", "eyeContainer", "1450", "700", "9.0.0","expressInstall.swf", flashvars, params, attributes );
		
		//position flash
		$('#eyeContainer').css({
			left: "-250px",
			top: "0px"
		});

    },
	
/*	initSubmitPopup: function(){
		$(".popupWinFormSubmit").each(function(){
			$this = $(this);
			$(".closeWin a" ,$this).click(function(e){
				e.preventDefault();
				$this.hide();
			});
		});	
	},
*/	
	initLightBox: function() {
		$('#Map area').lightBox();
		$('#Map2 area').lightBox();
		$('#perfChart').lightBox();
	}
	
};

/*
Alienware.com.microsite.M11x.submitPopup = function (answer){
	var a = answer.toLowerCase();
	var validAnswer = a.indexOf("yes");
	if (validAnswer != 0) validAnswer = a.indexOf("yeah");
	if (validAnswer != 0) validAnswer = a.indexOf("yea");
	if (validAnswer != 0) validAnswer = a.indexOf("yup");
	if (validAnswer != 0) validAnswer = a.indexOf("yep");
		
	$(".popupWinFormSubmit").each(function(){
		$this = $(this);
		$this.show();

		if (validAnswer != -1) {
			$(".contentNo",$this).hide();
			$(".contentYes",$this).show();
		}

	});	
}
*/

$(document).ready(function() {

	new Alienware.com.microsite.M11x({});
	
});
