// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

var PoiseOverlay = Class.create({
	
	initialize: function(overlay_id, page_id, autoclose) {						
		this.overlay_item = $(overlay_id);
		this.page_id = page_id;
		this.cookie_name = this.page_id+"_"+overlay_id+"_hide";
		this.autoclose = autoclose || true;
		
		trigger_html = "<div id=\""+overlay_id+"_trigger\"><p><a href=\"#"+overlay_id+"\">Product News</a></p></div>"
		
		$(this.overlay_item).parentNode.insert({ top: trigger_html});
		
		$(this.overlay_item).insert({ bottom: "<span href='#' class=\"close\"><span>close</span></span>" });
		
		this.close_link = $$("#"+overlay_id+" span.close");
		this.close_link.invoke('observe', 'click', this.close.bind(this));
		
		hide_overlay = readCookie(this.cookie_name);
		
		if(!(hide_overlay=="true")) { this.show(this.autoclose); }			
		
		_self = this;
		Event.observe(overlay_id+"_trigger", 'click', function(event) {
			event.stop();
			_self.show(false);
		});
	},
	
	show: function(autohide) {
		_self = this;
		Effect.BlindDown(this.overlay_item, { duration: 1.0 });	
		if(autohide==true) {
			setTimeout('_self.close()', 18000)
		}
	},
	
	close: function() {
		Effect.BlindUp(this.overlay_item, { duration: 1.0 });		
		createCookie(this.cookie_name, true, 24);
	}
	
});


// Display Mobile Overlay if Mobile device is detected. 
//
var PoiseMobileOverLay = Class.create({
	
	initialize: function() {
	
		this.cookie_name = "PoiseMobileOverlay";
		
		MobileFlag = readCookie(this.cookie_name);		
		
		//if the device is mobile and user have not been here before show overlay
		if(this.isMobile()) {				  
			if(!(MobileFlag=="true")){
			//Show Mobile Overlay
					$('mobile_overlay_banner').show();
					createCookie(this.cookie_name, true, 1);
				}
		};	
		
		Event.observe('close', 'click', function(){
			$('mobile_overlay_banner').hide();			
		});
	}, 
	
	// Detect Mobile Devices
	isMobile: function(){
		var userAgent = navigator.userAgent.toLowerCase();
		var mobile = /(iPhone)|(iPod)|(android)|(blackberry)|(Windows Phone OS 7)|(symbian)/gi.test(userAgent);
		
		if (mobile) {
			// if android, go through another round of testing
			if (/android/i.test(userAgent) && !(/mobile/i.test(userAgent))) {
				 return false;
			}					
			if (mobile) {
				// assuming there are corresponding pages on the mobile site
			   return true;
			}
	 	}		
		else{
			return false
		}
	}
			
});


