var pw = (function(){
	var pw = function() {
		return {
			init: function() {
				console.log('init');
			},
			scrolling: false,
			fn: {}
		};
	};
	return pw();
})();

/*
 Parallax Module
 - Idea by Ian Lunn (http://www.ianlunn.co.uk/)
*/

pw.fn.parallaxModule = (function(){
	var $window = $(window);
	var $slide1 = $('#slide1');
	var $slide2 = $('#slide2');
		$slide2.find("h2:not(:first)").css({opacity: 0});	// Set Default State
	var $slide2obj = $('#slide2-obj');
	var $slide3 = $('#slide3');
		$slide3.find("h2:not(:first)").css({opacity: 0});	// Set Default State
	var $slide4 = $('#slide4');
	var $slide4obj = $('#slide4-obj');
	var $slide5 = $('#slide5');
	var $slide5obj = $('#slide5-obj');
	var $slide6 = $('#slide6');
	var $slide8 = $('#slide8');
	var $slide8obj = $('#slide8-obj');
	
	var windowWidth = $window.width(); // get the width of the window
	var windowHeight = $window.height(); // get the height of the window
	
	// method that is called for every pixel the user scrolls. Determines the position of the background
	/* arguments: 
		x = horizontal position of background
		windowHeight = height of the viewport
		pos = position of the scrollbar
		adjuster = adjust the position of the background
		inertia = how fast the background moves in relation to scrolling
	*/
	var newPos = function(x, windowHeight, pos, adjuster, inertia){
		return x + "% " + (-((windowHeight + pos) - adjuster) * inertia)  + "px";
	}
	
	// method to be called whenever the window is scrolled or resized
	var move = function(){ 
		var pos = $window.scrollTop(); //position of the scrollbar

		if($slide1.hasClass("inview")) {
			toggleFixedItems(pos);
		}
		
		if($slide2.hasClass("inview")){
			//call the newPos function and change the background position
			$slide2.css({'backgroundPosition': newPos(50, windowHeight, pos, 1700, 0.20)});
			$slide2obj.css({'backgroundPosition': newPos(50, windowHeight, pos, 1650, 0.15)});
			var index = -1;
			if(pos > 0 && pos < 1350) {
				index = 0;
			} else if(pos > 1350 && pos < 1750) {
				index = 1;
			} else if(pos > 1750 && pos < 2250) {
				index = 2;
			} else if(pos > 2250 && pos < 2850) {
				index = 3;
			}
			
			var $titles = $slide2.find("h2");
			if(index != -1) {
				if($titles.eq(index).css("opacity") == 0 && pw.scrolling == false ) { 
					$slide2.find("h2")
						.stop(true, true)
						.animate({opacity:0}, 300)
						.eq(index)
						.stop(true, true)
						.animate({opacity:1}, 500);
				}
			}
		}
		
		if($slide3.hasClass("inview")){
			$slide3.css({'backgroundPosition': newPos(50, windowHeight, pos, 4580, 0.25)});
			
			var index = 0;
			if(pos > 3400 && pos < 4000) {
				index = 0;
			} else if(pos > 4000 && pos < 4900) {
				index = 1;
			}			
			var $titles = $slide3.find("h2");
			
			if($titles.eq(index).css("opacity") == 0 && pw.scrolling == false ) { 
				$slide3.find("h2")
					.stop(true, true)
					.animate({opacity:0}, 300)
					.eq(index)
					.stop(true, true)
					.animate({opacity:1}, 500);
			}
		}
		
		if($slide4.hasClass("inview")){
			$slide4.css({'backgroundPosition': newPos(50, windowHeight, pos, 5000, 0.25)});
			$slide4obj.css({'backgroundPosition': newPos(50, windowHeight, pos, 3700, 0.1)});
		}
		
		if($slide5.hasClass("inview")){
			$slide5.css({'backgroundPosition': newPos(50, windowHeight, pos, 7400, 0.5)});
			$slide5obj.css({'backgroundPosition': newPos(50, windowHeight, pos, 7400, 0.2)});
		}
		
		if($slide6.hasClass("inview")){
			$slide6.css({'backgroundPosition': newPos(50, windowHeight, pos, 8250, 0.5)});
		}
		
		if($slide8.hasClass("inview")){
			//$slide8.css({'backgroundPosition': newPos(50, windowHeight, pos, 8900, 0.3)});
			$slide8obj.css({'backgroundPosition': newPos(50, windowHeight, pos, 8950, 0.3)});	
			
			/* if(pos < 9777) {
				//$slide8obj.css({'backgroundPosition': newPos(50, windowHeight, pos, 8950, 0.3), 'background-attachment': 'fixed'});	
			} else {
				$slide8obj.css({'backgroundPosition': '50% -445px', 'background-attachment': 'scroll'});
			} */
			
		}
	}
	
	var checkUrlHash = function() {
		var hash = window.location.hash;
		if(hash != "") {
			$("#nav a[href=#" + hash + "]").trigger("click");
			move();
		}
	};
	
	var activateFlashMap = function(el) {
		// Hide Content
		//$(el).hide();
		
		// Show Flash Map
		var flashvars = {},
		flashvars = {wmode:"transparent",mapURL: "http://s.esetsmartsecurity.com/images/map.jpg",earthURL: "http://s.esetsmartsecurity.com/earth.DAE",dataURL: "http://s.esetsmartsecurity.com/data.xml"},
		attributes = {};
		swfobject.embedSWF("http://s.esetsmartsecurity.com/earth.swf", "flashWrp", "600", "600", "9.0.0","scripts/expressInstall.swf", flashvars, {}, attributes, function(ev) {
			$("#flashWrp").css("display", "block");
		});
	};
	
	var checkResolutionBugs = function() {
		windowWidth = $window.width();
		if(windowWidth < 1224) {
			$slide1.find(".story-logo").css({
				'top': '3px',
				'left': '5px'
			});
		} else {
			$slide1.find(".story-logo").css({
				'top': '50px',
				'left': '25px'
			});
		}
	}
	
	var toggleFixedItems = function(pos) {
		if(pos >= 0 && pos < 228) {
			$("#story-logo, #socials").fadeOut();
		} else {
			$("#story-logo, #socials").fadeIn();
		}
	}
	
	var attachHandlers = function(){
		// apply the class "inview" to a section that is in the viewport
		$('.slide').bind('inview', function (event, visible, topOrBottomOrBoth) {
			if (visible == true) {
			    if (topOrBottomOrBoth == 'top')
				{
				  // top part of element is visible
				}
				else if (topOrBottomOrBoth == 'bottom')
				{
				  // bottom part of element is visible
				}
				else
				{
				  	if($.isFunction(pw.fn.navigationModule.changeStatus)) {
						pw.fn.navigationModule.changeStatus(this);
					}
				}
				$(this).addClass("inview");

			} else {
				$(this).removeClass("inview");
			}
		});
	
		$window.resize(function(){ // if the user resizes the window...
			move(); // move the background images in relation to the movement of the scrollbar
			//checkResolutionBugs();
		});		
		
		$window.bind('scroll', function(){ // when the user is scrolling...
			$("#aToolTip").remove();
			move(); // move the background images in relation to the movement of the scrollbar
			//checkResolutionBugs();
		});
		
		$("#imgMapWrp").one("click", function() {
			activateFlashMap(this);
		});
		
	};
	
	var init = function() {
		attachHandlers();
		checkUrlHash();
		//checkResolutionBugs();
		toggleFixedItems(0);
		
		// Set right positions after onload event
		// cause browser remebers last position of scrollbar
		setTimeout(function(){
			move();
		}, 350);
		activateFlashMap(this);
	}

	
	return {
		init: function() {
			init();
		}
	};
})();

/*
 Navigation Module
*/

pw.fn.navigationModule = (function(){	
	var changeStatus = function(el) {
		var slideId = $(el).attr("id");
		var navItem = $("#nav a[href=#" + slideId + "]");
		
		setClass(navItem);
	};
	
	var setClass = function (el) {
		$("#nav li.active").removeClass("active");
		$(el).parent().addClass("active");	
	};
	
	var attachHandlers = function(){

	};

	var init = function() {
		$('#nav, #socials li.dl').localScroll({
			onBefore: function() {
				$("#aToolTip").remove();
				pw.scrolling = true;
			},
			onAfter: function() {
				pw.scrolling = false;
			}
		});
		attachHandlers();
	}
	
	return {
		init: init,
		changeStatus: changeStatus
	};	
})();


/*
 Infiltrations Slide
*/

pw.fn.infiltrationsModule = (function(){	
	var getPadding = function(el, direction) {
		var val = el.css("padding-" + direction);
		return parseInt(val, 10) || 0;
	};
	
	var showBubble = function(el, ev) {
		var $detail = $(el).next();
		
		var offset = ($detail.outerWidth() -  $(el).width())/2;
		var elPos = $(el).position();
		
		var left = elPos.left - offset;
		var top = elPos.top - offset;
		
		var heightOffset = ($detail.outerWidth() - $detail.outerHeight()) / 2;
		var paddingTop = getPadding($detail, 'top') + heightOffset;
		var paddingBottom = paddingTop;
				
		$detail
			.css({
				left: left,
				top: top,
				paddingTop: paddingTop,
				paddingBottom: paddingBottom,
				zIndex: 9999
			})
			.stop(true, true)
			.fadeIn(110);

	};
	
	var hideBubble = function(el, ev) {
		$("#slide5 dd")
			.css({
				zIndex:500
			})
			.stop(true, true)
			.hide();
	};
	
	var attachHandlers = function(){
		$("#slide5 dt").bind("mouseover", function(ev){
			showBubble(this);
		});
		$("#slide5 dd").bind("mouseout", function(ev){
			var relTarget = ev.relatedTarget || ev.toElement;

			if($(relTarget).parents("dd").size() == 0 && $(relTarget).is("dd") == false) {
				hideBubble(this);
			}
		});
	};

	var init = function() {
		attachHandlers();
	}
	
	return {
		init: init
	};	
})();


/*
 Init Modules
*/
pw.fn.parallaxModule.init();
pw.fn.navigationModule.init();
pw.fn.infiltrationsModule.init();

// Init "LightBox"
$("a[rel^='playVideo']").prettyPhoto({
	show_title: false,
	social_tools: "",
	theme: 'dark_rounded',
	default_width: 980,
	default_height: 620,
	changepicturecallback: function(){
		try {_gaq.push(['_trackEvent', 'Video', 'Play', language]);} catch (e) {};
	},
	callback: function(){
		try {_gaq.push(['_trackEvent', 'Video', 'Close', language]);} catch (e) {};
	}
});

// Init Tooltips
$("#socials a").aToolTip({
	fixed: true,
	xOffset: -7,
	yOffset: -3	
});

$("#nav a").aToolTip({
	fixed: true,
	xOffset: -7,
	yOffset: 6	
});
