$(document).ready(function(){
	if($('#tearaway').length > 0){
		tearAway.setup();
	}else{
		pageCurl.setup();
	}
});

var tearAwayModal = {
	open: function(data){
		tearAwayModal.overlay = $('<div class="overlay tearaway" />').css('height',$(document).height()).click(tearAwayModal.close).hide();
		tearAwayModal.box = $('<div class="modal  tearaway" />').html(data).hide();
		$('<a class="close" href="#">close</a>').click(tearAwayModal.close).insertBefore($('h2',tearAwayModal.box));
		if($('img.tool',tearAwayModal.box).length>0)$('img.tool',tearAwayModal.box).click(tearAwayModal.image);
		tearAwayModal.overlay.appendTo($(document.body));
		tearAwayModal.box.appendTo($(document.body)).css('margin-top',-tearAwayModal.box.height()/2);
		tearAwayModal.overlay.fadeTo(250,0.8);
		tearAwayModal.box.fadeIn();
	},
	close: function(e){
		if(e)e.preventDefault();
		if(tearAwayModal.overlay){
		tearAwayModal.overlay.fadeOut(500,function(){tearAwayModal.overlay.remove()});
		tearAwayModal.box.fadeOut(500,function(){tearAwayModal.box.remove()});
		}
	},
	image: function(){
		var img = $(this);
		tearAwayModal.box.fadeOut(200,function(){
			tearAwayModal.box.children().toggle(img.hasClass('large'));
			img.toggleClass('large').show();
			tearAwayModal.box.fadeIn();
		});
	}
}

var tearAway = {
	tips: {},
	pages: {},
	setup: function(){
		tearAway.tip.setup();
		if(!$.support.opacity)$('ol.resources li ul li:nth-child(even)').addClass('even');
		
		//Get Data
		$.get('/include/tearAwayData.html',function(data){
			var parser = $('<div />').html(data);
			$('ul#tips li',parser).each(function(){
				tearAway.tips[this.id] = $.trim(this.innerHTML.replace("\n",''));
			});
			$('div#pages div',parser).each(function(){
				tearAway.pages[this.id] = $.trim(this.innerHTML.replace("\n",''));
			});
			$('div.diagram li').each(function(){
				var id = this.id;
				$(this).data('tip',tearAway.tips[id]);
				var content = tearAway.pages[id]!=null?tearAway.pages[id]:false;
				$(this).data('content',content);
				$(this).bind(tearAway.tip.functionality);
				if(content==false){
					$(this).addClass('noClick');
					$(this).unbind('click');
				}
			});
		});	
	},
	tip:{
		setup: function(){
			tearAway.tip.el = $('<div class="tooltip"><p></p><p class="details">Click for more detail</p><span class="point"></span></div>').hide();
			tearAway.tip.content = $('p:first',tearAway.tip.el);
			$(document.body).append(tearAway.tip.el);
		},
		functionality:{
			mouseenter: function(){
				tearAway.tip.content.html($(this).data('tip'));
				tearAway.tip.height = tearAway.tip.el.height()/2;
				//$('p.details',tearAway.tip.el).toggle(!$(this).hasClass('noClick'));//Toggle click here if there is no content
				if($.support.opacity)tearAway.tip.el.fadeTo(100,1);
				else tearAway.tip.el.show();
			},
			mousemove: function(e){
				//tearAway.tip.el.css({'left':e.pageX-320,top:e.pageY-tearAway.tip.height});
				//tearAway.tip.el.toggleClass(e.pageX-320<0);
				var left = e.pageX-320<0;
				tearAway.tip.el.toggleClass('left',left);
				var top = e.pageY-tearAway.tip.height;
				if(!$.support.opacity)top = e.pageY-(tearAway.tip.el.height()/2);
				tearAway.tip.el.css({'left':left?(e.pageX+60):(e.pageX-320),'top':top});
				//tearAway.tip.el.css({'backgound':'#666'});
			},
			mouseleave: function(){
				tearAway.tip.el.hide();
			},
			click: function(e){
				e.preventDefault();
				tearAwayModal.open($(this).data('content'));
			}
		}
	}
}

var pageCurl = {
	test: {
		isIPad: function(){ return navigator.userAgent.match(/iPad/i) !== null; },
		isIPhone: function(){ return navigator.userAgent.match(/iPhone/i) !== null; },
		isIOS: function(){ return pageCurl.test.isIPhone() || pageCurl.test.isIPad(); },
		isAndroid: function(){ return navigator.userAgent.match(/Android/i) !== null; }
	},
	setup: function(){
		
		pageCurl.el = $('<div class="curl" />');
		pageCurl.a = $('<a href="/buyers_guide"><img src="/css/buyersFold.png"/><span>Find the Right Solution to Fight Frugalnomics</span></a>');
		pageCurl.el.append(pageCurl.a);
		
		$(document.body).append(pageCurl.el);
		
		pageCurl.css.before = {width:pageCurl.el.width(),height:pageCurl.el.height()};
		pageCurl.hover();
		
		if(pageCurl.test.isIOS() || pageCurl.test.isAndroid()){
			pageCurl.el.css('top',(window.pageYOffset + window.innerHeight - 80));
			window.onscroll = function() {
				pageCurl.el.css('top',(window.pageYOffset + window.innerHeight - 80));
			};
		}else{
			setTimeout(pageCurl.flutter,pageCurl.flutterDelay);
		}
	},
	position: function(){
		pageCurl.el.stop().animate({
			bottom: $(document).height()-$(window).scrollTop()+pageCurl.el.height()
		},200);
	},
	css: {
		after: {width:307, height:307},
		flutter: {width:90, height:90}
	},
	flutterDelay: 4000,
	flutter: function(){
		if(pageCurl.el.height() < pageCurl.css.flutter.height){
			pageCurl.el.stop().animate(pageCurl.css.flutter,500,function(){
				pageCurl.el.animate(pageCurl.css.before,500);
			});
		}
		setTimeout(pageCurl.flutter,pageCurl.flutterDelay);
	},
	hover: function(){
		pageCurl.el.hover(
			function() { //On hover...
				if(pageCurl.test.isIOS() || pageCurl.test.isAndroid()){
					pageCurl.el.css(pageCurl.css.after);
					pageCurl.el.css('top',(window.pageYOffset + window.innerHeight - pageCurl.css.after.height));
				}else{
					pageCurl.el.stop().animate(pageCurl.css.after,500);
				}
				
			},
			function() {//On out...
				pageCurl.el.stop().animate(pageCurl.css.before,300);
			}
		);
	}
};
