// remap jQuery to $
(function($){
	$.giggle = {
			urlParamValue: function(name){
			    var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(window.location.href);
			    if (!results)
			    { 
			        return 0; 
			    }
			    return results[1] || 0;
			}
	};
	/**
	* PAGE MASK PLUGIN
	@ Requires Master.css
	*/
	$.fn.mask = function(options) {
	    var opts = $.extend({}, $.fn.mask.defaults, options);
	    return this.each(function() {
	        $this = $(this);
	        var o = $.meta ? $.extend({}, opts, $this.data()) : opts;
	        var iframe = $("<iframe class='layermask' frameborder='0'></iframe>");
	        var layer = $("<div id='disableLayer'></div>");
	        o.setDimensions(iframe, $this);
	        o.setDimensions(layer, $this);
	        $this.before(layer);
	        layer.after(iframe);

	    });
	};
	$.fn.mask.defaults = {
	    setDimensions: function(elem, page) {
	        var offset = page.offset();
	        elem.width(page.width()).height(page.height()).css({ "left": offset.left + "px", "top": offset.top + "px" }).animate({ opacity: 0.1 });
	    }
	};
	/**
	* POPUP WINDOW PLUGIN
	*/
	$.fn.openWindow = function(options) {
	    var opts = $.extend({}, $.fn.openWindow.defaults, options);
	    return this.click(function() {
	        $this = $(this);
	        var o = $.meta ? $.extend({}, opts, $this.data()) : opts;
	        var src = $this.attr('href');
	        var specifics = 'width=' + o.winWidth + ',height=' + o.winHeight + ',scrollbars=' + o.winScroll + ',resizable=' + o.winResize;
	        window.open(src, o.winName, specifics);
	        return false;
	    });
	};
	$.fn.openWindow.defaults = {
	    winName: 'new',
	    winWidth: '500',
	    winHeight: '500',
	    winScroll: 'yes',
	    winResize: 'no'
	};

	/**
	* Extended jCarousel PLUGIN
	@ Requires contentarticles/assets/javascript/jquery.jcarousel.min.js
	*/
	$.fn.pageCarousel = function(options) {
		var opts = $.extend({}, $.fn.pageCarousel.defaults, options);
		return this.each(function() {
	        $this = $(this);
	        var o = $.meta ? $.extend({}, opts, $this.data()) : opts;
			var roundAbout = new Object();
			var navElem = $('a.' + o.navElemClass);
			
			roundAbout.navForCurrentSlide = 0;
			roundAbout.mycarousel_interaction = function (carousel) {
				carousel.clip.hover(function() {
					carousel.stopAuto();
				}, 
				function() {
					carousel.startAuto();
				});
			
				navElem.bind('click',function() {
					carousel.startAuto(0);
		 			carousel.scroll($.jcarousel.intval(this.id));
		 			return false;
				});
			}
			roundAbout.mycarousel_navigator = function (carousel, item, idx, state) {
				//console.log(carousel +" "+item+" "+idx+" "+state);
				var arrayAssignment = (idx - 1);
				navElem.eq(roundAbout.navForCurrentSlide).removeClass(o.navOnClass);
				navElem.eq(arrayAssignment).addClass(o.navOnClass);
				roundAbout.navForCurrentSlide = arrayAssignment;
			}
			$this.jcarousel({
		       	auto:6,
		    	wrap: 'last',
		    	buttonNextHTML: null,
		        buttonPrevHTML: null,
		        initCallback: roundAbout.mycarousel_interaction,
				itemVisibleInCallback: {
					onBeforeAnimation: roundAbout.mycarousel_navigator
				}
		    });
	    });
	};
	$.fn.pageCarousel.defaults = {
	    navElemClass: 'widget-nav',
	    navOnClass: 'on'
	};
	
		
/**
	* ICON NAV PLUGIN
	*/
	$.fn.iconHover = function(options) {
	    var opts = $.extend({}, $.fn.iconHover.defaults, options);
    	this.find('li').hover(
			function(){
				if(!$.browser.msie) {
					var $BgPos = $(this).children('a').css('background-position');
					var $BgPosArr = $BgPos.split(' ');
					$(this).find('div').css('left', '0');
					$(this).children('a').css('background-position', $BgPosArr[0] + ' -86px');
				} else {
					var $BgPos = $(this).children('a').css('background-position-y');
					$(this).find('div').css('left', '0');
					$(this).children('a').css('background-position-y', '-86px');
				}
			}, 
			function(){
				if(!$.browser.msie) {
					var $BgPos = $(this).children('a').css('background-position');
					var $BgPosArr = $BgPos.split(' ');
					$(this).find('div').css('left', '-999em');
					$(this).children('a').css('background-position', $BgPosArr[0] + ' 0px');
				} else {
					var $BgPos = $(this).children('a').css('background-position-y');
					$(this).find('div').css('left', '-999em');
					$(this).children('a').css('background-position-y', '0px');
				}
			}
		)
	};
	$.fn.iconHover.defaults = {};

	/**
	* giggle GUARANTEE MODAL
	@ Requires the exact markup and css to render
	*/
	$.fn.giggleGuarantee = function(options){
		var opts = $.extend({}, $.fn.giggleGuarantee.defaults, options);
		return this.click(function(e){
			e.preventDefault();
			// Add Scroll Methods to babyScroller Object
			var o = $.meta ? $.extend({}, opts, $this.data()) : opts,
			babyScroller = function() {
				this.WhichSlideIsVisible = 0;
				this.UserClickedThisButton = 'null'; 
				this.currentScrollValue = 0; 
				this.newScrollValue = 0; 
				this.totalContentWidth = $('#content1').width();
			};
			babyScroller.prototype = {
				scrollThing: function(direction) {
					if (direction === 'right') {
						$(o.leftClickElem).css('display','block');
						if ( (this.currentScrollValue < 2272) ) { 
							this.newScrollValue+=568;
							$(o.mainView).animate({scrollLeft:this.newScrollValue},'slow');
							this.currentScrollValue = this.newScrollValue;
							this.WhichSlideIsVisible = (this.currentScrollValue / 568) + 1;
						}
					}
					if (direction === 'left') {
						$(o.rightClickElem).css('display','block');
			
						if ( (this.currentScrollValue > 0) ) {
							this.newScrollValue-=568;
							$(o.mainView).animate({scrollLeft:this.newScrollValue},'slow');
							this.currentScrollValue = this.newScrollValue;
							this.WhichSlideIsVisible = (this.currentScrollValue / 568) + 1;
						}	
					}
					if (this.currentScrollValue === 2272) {
						$(o.rightClickElem).css('display','none');
					}
					if (this.currentScrollValue === 0) {
						$(o.leftClickElem).css('display','none');
					}
				}
			};
			// Create object references
			var baby = new babyScroller(),
			slides = document.getElementById(o.slidesContainer),
			slidesReference = $('#' + o.slidesContainer),
			closeBtn = $(o.closeClass);
			// Set page dimensions 
			o.initDocWidth = $(document.body).width(); 
			o.initDocHeight = $(document.body).height(); 
			// Create opacity overlay and popup layer
			o.fadeToBlack(baby, slides, slidesReference, closeBtn);
			o.slideShow(slidesReference);
			// Set event handlers for scrolling left and right
			// Pass in babyScroller Object...
			o.initScrollEvents(baby, o.leftClickElem, o.rightClickElem);
		});
	};
	$.fn.giggleGuarantee.defaults = {
		slidesContainer: 'guarantee-slides',
		mainView: '#aBigWindow',
		leftClickElem: '#story1',
		rightClickElem: '#story2',
		closeClass: '.closer',
		fadeToBlack: function(obj, slides, slidesReference, closeBtn) {
			var ggBoxbg = document.createElement('div');
			ggBoxbg.setAttribute('id','ggFader');
			ggBoxbg.setAttribute('class','closer');
			document.body.appendChild(ggBoxbg);
			document.body.appendChild(slides);
			closeBtn.live('click',function () {
				$(this).die();
				$('body').find('#ggFader').remove();
				slidesReference.css('display','none');
				$(window).unbind();
				obj.currentScrollValue = 0;
				obj.newScrollValue = 0; 
			});
			$('#ggFader').css ({'display':'block','width':this.initDocWidth, 'height':this.initDocHeight, 'position':'absolute', 'top':'0px','left':'0px', 'zIndex':9998, 'backgroundColor':'#000','opacity':0});
			$('#ggFader').animate({'opacity':.5});
			$(window).resize(function() { 
				$('#ggFader').css({'width':$(document.body).width(), 'height':'2000px'});
			});
		},
		slideShow: function(slidesReference) {
			slidesReference.css({'top':'221px','left':'310px','display':'block','zIndex':9999});
		},
		initScrollEvents: function(obj, left, right){
			$(left).live('click', function(){
				obj.scrollThing('left');
				return false;
			});
			$(right).live('click', function(){
				obj.scrollThing('right');
				return false;
			});
		}
	};
})(this.jQuery);
