// Interstitial Pluggin

// v .806 msgkill when exiting to adobe website.

// v .805 jg_dialogue'd new windows opening more jg_dialogs, fixed nameing convention. No need to update a tag's id name.

// v .804 fully jquery'd

// v .803 added support for newer versions of safari and chrome. instead of switching the contents of window -> now opens popup.

// v .802 added internal value for internal linking without having url launch in window
// use parameter {internal: true} when calling pluggin.

// v .801 Placed iframe for ie6 form elements.

// v .8

// Notes on using this plugin

// DOES NOT PLAY WELL WITH FULL SCREEN FLASH!
// Do not use anim time when dealing with flash objects
(function ($) {
  $.fn.jg_dialogue = function (settings) {
		settings = $.extend({
			url: null,
			flash: false, // a div with flash in it. usefull for ie8 features
			
			// false : Normal click on link launches interstitial
			// true : Launches interstitial immediately (probably through flashInterfaceCall)
			// now : Launches url immediately without interstitial (probably through flashInterfaceCall)
			// click: Launches url on click, without interstitial. Usefull for just opening links in new window.
			call: false, 
			type: 'pdf', // what kind of dialog? 'ext', 'pdf', 'msg', 'image'
			
			top: false, //distance from top of center option
			
			//markup of mask
			mask_opacity: .8,
			mask_color: false,
			mask_anim: null,
			
			siteurl: '',
			sitename: '',
			
			// Tracking Function
			fn_track: null,
			
			// Interstitial Launch Function
			fn_launch: null,
			
			//markup of External Link Message
			msg : false,
			
			// Exit Messege
			msg_exit : false,
			internal: false
		}, settings);
				
	 return this.each(function () {
															
			// optimization:
			var e = $(this);
			
			var e_url = settings.url || e.attr('href');
			var e_flash = settings.flash;
			var e_call = settings.call;
			var e_type = settings.type;
			var e_top = settings.top;
			var e_opacity = settings.mask_opacity;
			var e_color = settings.mask_color;
			var e_anim = settings.mask_anim;
			
			// removes onclick function and fires it when the launch button is clicked
			var e_fntrack = settings.fn_track || e.attr('onclick'); e.removeAttr('onclick');
			
			var e_fnlaunch = settings.fn_launch;
			var e_name = 'jg_dialog_' + (e.attr('id') || Math.floor(Math.random()*100001));
			var e_maskdiv = null;
			
			var e_int = settings.internal;
			
			var b_wrap = null;
			var b_mask = null;
			var b_space = null;
			var b_frame = null;
			
			//which msg to use?
			var e_msg = '';
			var e_exitmsg = '';
			
			e_msg = settings.msg;
			
			// pluggin behavior: Click for interstitial, Initiate interstitial, Initiate URL
			switch (e_call){
				case false : // Launch Interstitial on click
					e.click(jg_btn);
					break;
				case true : // Launch Interstitial now
					jg_opendialog();
					break;
				case 'now' : // Launch Link NOW!
					jg_launchurl();
					break;
				case 'click' : // Launch Link on Click!
					e.click(function(){
						
						switch (typeof(e_fnlaunch)){
							case 'string' : eval(e_fnlaunch); break;
							case 'function' : e_fnlaunch(); break;
						}
						
						jg_launchurl();
						
						e.blur();
						return false;
					});
					break;
			}
			
			// Button Function
			function jg_btn(){
				jg_opendialog();
				e.blur();
				return false;
			}
			
			// Initiates Interstitial
			function jg_opendialog(){
				b_frame = $('<iframe id="' + e_name + '_iframe" class="jg_iframe" src="" width="1" height="1" scrolling="no" frameborder="0"></iframe>');
				b_frame.css({opacity: 0});
				b_wrap = $('<div id="' + e_name + '" class="jg_wrapper"></div>');
				b_wrap.css({'z-index': zTop()});
				
				b_mask = $('<span id="' + e_name + '_mask" class="jg_mask"></span>');
				if ($('.jg_wrapper').length > 0)e_color = $('.jg_mask').css('background-color');
				if (e_color)b_mask.css({'background-color' : e_color});
				b_mask.css({opacity: e_opacity}).click(jg_msgkill);
				if (e_anim)b_mask.css({opacity: 0}).animate({opacity: e_opacity}, e_anim);
				
				b_wrap.append(b_mask);
				
				if (check_ie8_flash()){
					// setups of flash on IE8 - Full screen flash on IE8 has some issues...
					//$('#' + settings.flash).css({opacity: 1 - settings.mask_opacity});
				}
				
				// sets height 
				b_space = $('<span class="jg_space">&nbsp;</span>');
				b_wrap.append(b_space);
				
				b_wrap.append(e_msg); // adds code		
				
				//for adobe links
				b_wrap.find('a.jg_adobe').click(function(){			
					b_wrap.find('div.jg_msg').html(e_exitmsg);
					b_wrap.find('a.jg_cancel').click(jg_msgkill);				
					b_wrap.find('a.jg_targeturl').jg_dialog({call: 'click', type:'ext', url: $(this).attr('href')}).click(jg_msgkill);
						
					return false;
				});
				
				//display the acrobat warning again?
				b_wrap.find('a.jg_checkbox').click(function(){
					$(this).toggleClass('jg_checked');
					if ($(this).hasClass('jg_checked')){
						jg_cookieset(1);
					} else {
						jg_cookieset(0);
					}
					
					$(this).blur();
					return false;
				});
				
				//Adds button kill feature
				b_wrap.find('.jg_cancel, .jg_space').click(jg_msgkill);
				
				//Adds URL launch function
				b_wrap.find('a.jg_targeturl').click(function(){
					jg_launchurl();
					return false;
				});
				
				$('body').append(b_frame).append(b_wrap);
				// Does the initial mask resizing
				jg_resizemask();
				
				
				//console.log(typeof(e_fnlaunch));
				// Launches Custom function to happen after dialog launches
				// Useful for flash intergration or adding interactivity
				switch (typeof(e_fnlaunch)){
					case 'string' :
						eval(e_fnlaunch);
						break;
					case 'function' :
						e_fnlaunch();
						break;
				}
				
				// WINDOW RESIZING
				$(window).bind('resize', jg_resizemask);	
			}
				
			function jg_launchurl(){
				// If tracking is involved
				switch (typeof(e_fntrack)){
					case 'string' : eval(e_fntrack); break;
					case 'function' : e_fntrack(); break;
				}
				// The windowline for opening pop ups
				
				if (e_url != "#" && e_url != "" && !e_int){
					var wline = "";
					wline = 'toolbar=1,scrollbars=1,location=1,statusbar=1,menubar=1,resizable=1,width=600,height=500';
					if($.browser.safari && parseInt($.browser.version) < 400){ //if its not a Safari
						window.location.href = e_url; //mac fix for OS X
					} else {
						var thelink = window.open(e_url, (e_name + '_window'), wline);
					}
				} else {
					if (e_int) window.location.href = e_url;
				}
				
				jg_msgkill();
			}
			
			//removes all interstitial messages. initiated by the jg_cancel button or clicking on the mask.
			function jg_msgkill(){
				/*
				if (settings.flash == false){
					
				} else {
					if (check_ie8_flash()){
						
					}
				}
				*/
				
				if (b_wrap){
					b_wrap.remove();
					b_frame.remove();
					$(window).unbind('resize', jg_resizemask);
				}
				//jg_resizemask();
					
				return false;
			}
			
			function jg_resizemask(){
				b_wrap.add(b_frame).add(b_mask).css({width: 1, height: 1});
				var h = Math.max($(window).height(), $('body').outerHeight(), $(document).height());
				var w = $(window).width();
				b_wrap.add(b_frame).add(b_mask).css({width: w, height: h});
				
				if (!settings.top){
					b_space.height(Math.max(0, ($(window).height() - b_wrap.find('.jg_msg').outerHeight()) / 2 + $(window).scrollTop()));
				} else {
					b_space.height(e_top + $(window).scrollTop());
				}
				
				var h2 = Math.max($(window).height(), $('body').outerHeight(), $(document).height());
				if (h2 > h){
					$('.jg_wrapper').add($('.jg_wrapper_iframe')).add('.jg_mask').css({height: h2});
				}
			}
			
			function jg_cookieset(cookieValue){
				var expires = new Date( );
				expires.setTime(expires.getTime( ) + (1000 * 60 * 60 * 24 * 1)); //days ahead
				document.cookie = "acrobatint" + "=" + escape(cookieValue) + "; path=/" + "; expires=" + expires.toGMTString();
			}
			
			//FUNCTION-- retrieves a cookie - jsv 1.0
			function jg_cookieget() {
				var cookieNameStr = "acrobatint" + "=";
				var dc = document.cookie;
				if(dc.length > 0){
					var beginStr = dc.indexOf(cookieNameStr);
					if(beginStr != -1){
						beginStr += cookieNameStr.length;
						var endStr = dc.indexOf(";", beginStr);
						if(endStr == -1) {endStr = dc.length;}
						return unescape(dc.substring(beginStr, endStr));
					}
				}
				return null;
			}
			
			function jg_cookiedel() {
				document.cookie = "acrobatint" + "=; expires=Thu, 01-Jan-70 00:00:01 GMT" + "; path=/";
			}
			
			function check_ie8_flash(){
				if ($.browser.msie && $.browser.version >= 8 && $.browser.version < 9 && settings.flash != null && settings.flash != false){ // ie8 flash issues
					return true;
				} else {
					return false;
				}
			}
			
			function zTop(){
				// sets the z-index
				var zorder = 0;
				$('*').each(function(){
					var ztemp = parseInt($(this).css('z-index'));
					if (ztemp > zorder)zorder = ztemp;
				});
				return zorder + 100;
			}
			
    });
  };
})(jQuery);
