/* Create a namespace */
var EInk = {};

//try to eliminate IE 6 flicker
try {
  document.execCommand("BackgroundImageCache", false, true);
} catch(err) {}

EInk.enableCustomerShowcase = function() {
	$("div#customershowcasecallout a.more").click( function() {
		$(this).parents("div.slideitem").css("z-index", 20).find("div.revealcontent").show();
		return false;
	});
	
	$("div#customershowcasecallout a.close").click( function() {
		$("div.slideitem").css("z-index", 1);
		$("div.revealcontent").hide();
		return false;
	})
	
	$("div#showcaserevealslides a").click( function() {
		$("div#showcaserevealslides").hide();
		$("div#customershowcasecallout div.slide2").slideDown("normal", function() {
			$("div#showcaseviewall").fadeIn();
		});
		return false;
	});
	
	
}

EInk.enableFeatures = function() {
	$("div.featuresection:not(#featuredefault)").append("<a href=\"#\" class=\"close\">Close</a>");
	
	$("div.featuresection a.close").click( function() {
		$("ul#featurenav a").removeClass();
		$("div.featuresection:not(#featuredefault), div.featuresection:not(#featuredefault) a").fadeOut();
		return false;
	});
	
	$("ul#featurenav a").click( function() {
		$("ul#featurenav a").removeClass();
		$(this).addClass("selected");
		var selectedSectionHref = $(this).attr("href");
		$("div.featuresection:not(#featuredefault), div.featuresection:not(#featuredefault) a").fadeOut();
		$("div#"+selectedSectionHref).fadeIn();
		$("div#"+selectedSectionHref).find("a").fadeIn();
		return false;
	});
}

EInk.enableHomepageFeatures = function() {
	// Add play/pause button to main feature photo area
	$("div#homefeaturewrap").append("<a href=\"#\" class=\"playpause featurepause\">Pause</a><a href=\"#\" class=\"playpause featureplay\">Play</a>");
	
	var isCycleActive = true;
	var isCyclePaused = false;
	
	$("div#homefeaturewrap a.featurepause").click(function() {
		$('div#homefeature').cycle('pause');
		$(this).hide();
		$("div#homefeaturewrap a.featureplay").show();
		//$("div#valuepropaccordion h2 a").removeClass("hover");
		return false;
	});
	
	$("div#homefeaturewrap a.featureplay").click(function() {
		$('div#homefeature').cycle('resume', true);
		$(this).hide();
		$("div#homefeaturewrap a.featurepause").show();
		return false;
	});
	
	// Enable homepage image rotation
	$("div#homefeature").cycle({
		timeout: 4000/*,
		before: function(curr, next, opts) {
			//Show the appropriate "hover state"
			$("div#valuepropaccordion h2 a").removeClass("hover");
			if (opts.nextSlide == 1) {
				//$("div#valuepropaccordion h2#homefeaturereadable a").addClass("hover");
				$("#valuepropaccordion").accordion("activate",0);
			} else if (opts.nextSlide == 2) {
				//$("div#valuepropaccordion h2#homefeaturerugged a").addClass("hover");
				$("#valuepropaccordion").accordion("activate",1);
			} else if (opts.nextSlide == 3) {
				//$("div#valuepropaccordion h2#homefeaturegreen a").addClass("hover");
				$("#valuepropaccordion").accordion("activate",2);
			} else {
				$("#valuepropaccordion").accordion("activate",false);
			}
		}*/
	});
	
	/*$("#valuepropaccordion h2").click(function() {
		$('div#homefeature').cycle('pause');
		isCyclePaused = true;
	})*/
	
	// Enable homepage accordion
	$("#valuepropaccordion").accordion({
		collapsible:true,
		active:false,
		change: function(event, ui) {
			$("div#valuepropaccordion h2").addClass("iefix").removeClass("iefix");
		}/*,
		changestart: function(event, ui) { 
			
			imageClass = ui.newHeader.attr("id");
			if (isCyclePaused) {
				if (isCycleActive) {
					$('div#homefeature').cycle('pause');
					$("div#homefeaturewrap a.featurepause").hide();
					$("div#homefeaturewrap a.featureplay").hide();
					$("div#valuepropaccordion h2 a").removeClass("hover");

					if ($("#homefeature img."+imageClass).is(":animated")) {
						$("#homefeature img."+imageClass).stop().animate({
							opacity:1
						}, {
							duration:500,
							queue:false
						});
						$("div#homefeature img").not("."+imageClass).stop().css("opacity",1).fadeOut();
					} else {
						$("div#homefeature img").css("opacity",1).fadeOut();
						$("#homefeature img."+imageClass).fadeIn();
					}

					// give all non-selected headers a new class
					ui.newHeader.removeClass("ui-accordion-notactive");
					$("#valuepropaccordion h2").not("#"+imageClass).addClass("ui-accordion-notactive");

					isCycleActive = false;
				} else {
					$("#homefeature img").css("opacity",1).fadeOut();
					if (imageClass != "" && imageClass != null) {
						$("#homefeature img."+imageClass).fadeIn();

						// give all non-selected headers a new class
						ui.newHeader.removeClass("ui-accordion-notactive");
						$("#valuepropaccordion h2").not("#"+imageClass).addClass("ui-accordion-notactive");
					} else {
						ui.oldHeader.removeClass("ui-state-active");
						$("#valuepropaccordion h2").removeClass("ui-accordion-notactive");
						$("#homefeature img.homefeaturedefault").fadeIn();
					}
				}
			}	
		}*/
	});

}

/* Modal window  */
EInk.enableModalPop = function() {
    $('body').append('<div id="modal-pop" class="jqmWindow"><div id="jqmAjaxTarget"></div><a href="#" class="jqmClose">Close Window</a></div>');
    $('#modal-pop').jqm({
        modal: true, 
        ajax:'@href',
        trigger: 'a.modalpop',
        target: 'div#jqmAjaxTarget',
        toTop: true,
				overlay:80
    });
		$("a.modalpop").click(function() {
			if ($(this).hasClass("modalpopbig")) {
				$("div#modal-pop").addClass("jqmWindowBig");
			} else {
				$("div#modal-pop").removeClass("jqmWindowBig");
			}
		})
};

/* Product accordion */
EInk.enableProductAccordion = function() {
	var myAnimation;
	if ($.browser.msie && $.browser.version=="7.0") {
		myAnimation = false;
	} else {
		myAnimation = "slide";
	}
	$("div#productaccordion").accordion({
		animated: myAnimation,
		collapsible:true,
		change: function(event, ui) {
			$("div#productaccordion h2").addClass("iefix").removeClass("iefix");
		},
		changestart: function(event, ui) {
			//
		}
	})
}

/* Initialize everything when page is ready */
$(document).ready(function() {
	EInk.enableCustomerShowcase();
	EInk.enableFeatures();
	EInk.enableHomepageFeatures();
	EInk.enableModalPop();
	EInk.enableProductAccordion();
	
	// Random backgrounds
	var randomnumber=Math.floor(Math.random()*9) + 1;
	$("body").addClass("background"+randomnumber);
	
	// Initialize Display Products slideshows
	$("div#productslides1").cycle({
		autoHeight: false,
		timeout: 0,
		prev: "div#productslidenav1 a.previous",
		next: "div#productslidenav1 a.next",
		pause: 1,
		after: function(curr, next, opts) {
			$("div#productslidenav1 span.currentslide").html(opts.currSlide+1);
			$("div#productslidenav1 span.totalslides").html(opts.slideCount);
		}
	});
	$("div#productslides2").cycle({
		autoHeight: false,
		timeout: 0,
		prev: "div#productslidenav2 a.previous",
		next: "div#productslidenav2 a.next",
		pause: 1,
		after: function(curr, next, opts) {
			$("div#productslidenav2 span.currentslide").html(opts.currSlide+1);
			$("div#productslidenav2 span.totalslides").html(opts.slideCount);
		}
	});
	$("div#productslides3").cycle({
		autoHeight: false,
		timeout: 0,
		prev: "div#productslidenav3 a.previous",
		next: "div#productslidenav3 a.next",
		pause: 1,
		after: function(curr, next, opts) {
			$("div#productslidenav3 span.currentslide").html(opts.currSlide+1);
			$("div#productslidenav3 span.totalslides").html(opts.slideCount);
		}
	});
	$("div#productslides4").cycle({
		autoHeight: false,
		timeout: 0,
		prev: "div#productslidenav4 a.previous",
		next: "div#productslidenav4 a.next",
		pause: 1,
		after: function(curr, next, opts) {
			$("div#productslidenav4 span.currentslide").html(opts.currSlide+1);
			$("div#productslidenav4 span.totalslides").html(opts.slideCount);
		}
	});
	
	// Enable Customer Showcase Detail slides
	$("div#showcaseslidesbig").cycle({
		autoHeight: false,
		pager: '#showcaseslidepager',
		timeout: 0,
		prev: "div#showcaseslideprevnext a.previous",
		next: "div#showcaseslideprevnext a.next",
		pause: 1,
		after: function(curr, next, opts) {
			$("div#showcaseslideprevnext span.currentslide").html(opts.currSlide+1);
			$("div#showcaseslideprevnext span.totalslides").html(opts.slideCount);
		},
		// callback fn that creates a thumbnail to use as pager anchor 
		pagerAnchorBuilder: function(idx, slide) {
		    var mySlide = idx + 1;
		    return '<a href="#">' + mySlide + '</a>';
		}
	});
	
	// Enable hints on inputs with class "hintable"
	$('.hintable').hinty();
	
});

// Initialize when whole page is loaded
$(window).load( function() {
	//
});
