// Notes on using this plugin

// DOES NOT PLAY WELL WITH FLASH!
// 1)  REMOVE ANIMATIONS WHEN USED IN CONJUNCTION WITH FLASH. NO FADE INS!
//     Transparency is okay, just do not animate it.
//     For the future. Make pluggin adaptable to whether or not flash is used.
// 2)  BE CAREFULL WHEN CREATING LIGHTBOXES WITH FLASH INSIDE.
//     It might look good, but ie6 looks aliased. Blocky.
//     The only way to do this succesfully is to not animate the box fading in.
//     Also you'll have to force the browser to recognize the box by doing
//     something inert like $('#container').css({dispaly: 'block'}), then use the swfobject code
// 3)  NO PNG'S IN INTERSTITIAL MESSAGE!
//     ie6 no likey
// 4)  Because of security uses. You'll have to test on a server and not your local.
//     Flash cannot call javascript locally. It can only do it online... unless I'm missing something?
// 5)  Okay.... IE8 is all wierdo. Apparently it doesn't like dynamically added elements (div's) with
//     transparent effects rendered on top of flash elements. It freaks out and crashes the browser.
//     The alternative is to decrease the opacity of the containing flash element to fake a mask div.
//     Its a little fuzzy to think about but it works.

jQuery.fn.qi_dialog = function(settings) {
	settings = $.extend({
		url: null,
		flash: false, // a div with flash in it. usefull for ie8 features
		call: false, // use 'now' to initiate dialog through other means (eg. from flash)
		type: 'pdf', // what kind of dialog? 'ext', 'pdf', 'msg', 'image'
		close_method: null, // how to close the mask?
		
		launchurl: false, //Launch url immediately? Either on Click or Initiated by Flash
		
		// FUTURE IMPROVEMENTS
		//center: true, //chooses which to center on (horizontal). 'window', 'page', 'div_name'
		top: false, //distance from top of center option
		//right: null, //distance from right of center option
		//bottom: null, //distance from bottom of center option
		//left: null, //distance from left of center option
		
		//stack: false, //allows stacking?
		//target: 'blank',
		//metric: false, //use metric tracking? null or metric number
		
		//markup of mask
		mask: '<div id="qi_mask"></div>',
		mask_opacity: .6,
		
		siteurl: '',
		sitename: '',
		trk: null,
		trkfunc: null,
		
		//markup of PDF message
		msg_pdf :'<div class="qi_msg"><p><a href="http://www.adobe.com/products/acrobat/readstep2.html" target="_blank" class="qi_adobe"><img class="pdfIcon" src="images/qi_getadobereader.gif" width="88" height="31" border="0" align="right" hspace="0" vspace="0"/></a>The requested information is provided in<br/>Portable Document Format (PDF).</p><p>To view and print this document you\'ll need to install a copy of the free Adobe<sup>&reg;</sup> Acrobat<sup>&reg;</sup> Reader<sup>&reg;</sup>. If you already have Adobe Acrobat Reader installed click &quot;VIEW PDF&quot; below.</p><p>If you need the Acrobat Reader you can download it from the <a href="http://www.adobe.com/products/acrobat/readstep2.html" target="_blank" class="qi_adobe">Adobe Acrobat Reader Download page</a>.</p><p><a class="qi_btn qi_btn_pdf qi_targeturl" href="#"></a><a class="qi_btn qi_cancel" href="#"></a></p><p><a class="qi_checkbox" href="#">&nbsp;</a> Don\'t show me this message again.</p></div>',
		
		//markup of External Link Message
		msg_ext :'<div class="qi_msg"><p>You are now leaving the SITEURL website.</p><p>SITENAME is not responsible for the content presented by any independent website, including any advertising claims, special offers, illustrations, names or endorsements.</p><p>Thank you for visiting SITEURL</p><p><a class="qi_btn qi_targeturl" href="#"></a><a class="qi_btn qi_cancel" href="#"></a></p><p> </p></div>',
		
		//markup of HTML Message
		msg_html :'<div class="qi_msg"></div>',
		
		//markup of Image
		msg_img :'<div class="qi_msg"><p>SITENAME is not responsible for the content presented by any independent website, including any advertising claims, special offers, illustrations, names or endorsements.</p><p>Thank you for visiting SITEURL</p><p><a class="qi_btn qi_targeturl" href="#"><img src="images/btn_launch.gif" alt="View PDF" width="110" height="64"/></a><a class="qi_btn qi_cancel" href="#"><img src="images/btn_cancel.gif" alt="Cancel" width="110" height="64"/></a></p><p> </p></div>'
	}, settings);
	
	// save data from attributes
	//$(this).data('msgname', 'qi_wrapper_' + $(this).attr('id')); //custom wrapper name
	//$(this).data('url', $(this).attr('href')); //URL location
	
	settings.msg_ext = settings.msg_ext.replace(/SITEURL/ig, settings.siteurl);
	settings.msg_ext = settings.msg_ext.replace(/SITENAME/ig, settings.sitename);
	
	settings.msgname = 'qi_wrapper_' + $(this).attr('id');
	if (settings.url == null){
		settings.url = $(this).attr('href');
	}
	
	if (settings.flash == null){
		settings.flash = false;
	}
	
	if ($(this).attr('onclick')){
		settings.trkfunc = $(this).attr('onclick');
		$(this).removeAttr('onclick');
	}
	
	// pluggin behavior: Click for interstitial, Initiate interstitial, Initiate URL
	if (!settings.call){
		$(this).click(qi_btn);
	} else {
		if (!settings.launchurl){
			qi_opendialog();
		} else {
			qi_launchurl();
		}
	}
	
	// Button Function
	function qi_btn(){
		if (!settings.launchurl){
			if (settings.type != 'pdf'){
				qi_opendialog();
			} else {
				if (qi_cookieget() == 1){
					qi_launchurl();
				} else {
					qi_opendialog();
				}
			}
		} else {
			qi_launchurl();
		}
		$(this).blur();
		return false;
	}
	
	// Initiates Interstitial
	function qi_opendialog(){
		//sends other existing interstitals below the mask.
		$('.qi_wrapper').css({'z-index': 4999});
		
		// creates the mask if not created and sends to top
		if ($('#qi_mask').length == 0){
			if (check_ie8_flash()){ // ie8 flash issues
				$('#' + settings.flash).css({opacity: 1 - settings.mask_opacity});
			} else {
				$('body').append(settings.mask);
				if (settings.flash == false){
					$('#qi_mask').css({opacity: 0}).animate({opacity: settings.mask_opacity}, 300);
				} else {
					$('#qi_mask').css({opacity: settings.mask_opacity});
				}
			}
		}
		$('#qi_mask').css({'z-index': 5000});
		
		// creates message wrapper if not created and sends above mask
		if ($('#' + settings.msgname).length == 0){
			$('body').append('<div id="' + settings.msgname + '" class="qi_wrapper"></div>');
		}
		//$('#' + settings.msgname).css({opacity: 0, 'z-index': 5001});
		$('#' + settings.msgname).css({'z-index': 5001});
		
		//which msg to use?
		var msg = '';
		switch (settings.type){
			case 'pdf' : msg = settings.msg_pdf; break;
			case 'ext' : msg = settings.msg_ext; ; break;
			case 'html' : msg = settings.msg_html; break;
			case 'img' : msg = settings.msg_img; break;
		}
		
		//Fades in Dialogue
		//Adds message kill feature
		//$('#' + settings.msgname).append(msg).animate({opacity: 1}, 200).click(qi_msgkill);
		$('#' + settings.msgname).append(msg).click(qi_msgkill);
		$('#' + settings.msgname + ' .qi_msg, .qi_cancel').click(function(){
			return false;
		});
		// there might be links on this interstitial?
		$('#' + settings.msgname + ' .qi_msg a').click(function(){
			return true;
		});
		
		//for adobe links
		$('a.qi_adobe').click(function(){
			//$('#' + settings.msgname).empty();
			//qi_dialog_launch(this.href);
			
			$('div.qi_msg').html('<p>You are now leaving the www.bayerforwomen-us.com website. <br/>Bayer HealthCare Pharmaceuticals Inc. is not responsible for the content presented by any independent website, including any advertising claims, special offers, illustrations, names, or endorsements. <br>Thank you for visiting www.bayerforwomen-us.com.</p><p><a href="http://www.adobe.com/products/acrobat/readstep2.html" target="_blank" class="qi_btn qi_btn_go"></a><a class="qi_btn qi_btn_cancel qi_cancel" href="#"></a></p><p> </p>');
			
			$('a.qi_cancel').click(qi_msgkill);
			$('a.qi_btn_go').click(function(){
				var turl = settings.url;
				settings.url = $(this).attr('href');
				settings.type = 'ext';
				qi_launchurl();
				settings.url = turl;
				settings.type = 'pdf';
				
				return false;
			});
			return false;
		});
		
		//display the acrobat warning again?
		$('a.qi_checkbox').click(function(){
			$(this).toggleClass('qi_checked');
			if ($(this).hasClass('qi_checked')){
				qi_cookieset(1);
			} else {
				qi_cookieset(0);
			}
			
			$(this).blur();
			return false;
		});
		
		//Adds button kill feature
		$('.qi_cancel').click(qi_msgkill);
		
		//Adds URL launch function
		$('a.qi_targeturl').click(function(){
			qi_launchurl();
			return false;
		});
		
		qi_resizemask();
	}
		
	function qi_launchurl(){
		// If tracking is involved
		if (settings.trk != null){
			//alert(settings.trk);
			eval(settings.trk);
		} else if (settings.trkfunc != null){
			//alert(settings.trkfunc);
			settings.trkfunc();
		}
		
		// The windowline for opening pop ups
		var wline = "";
		switch (settings.type){
			case 'pdf' : wline = 'directories=no,location=no,menubar=no,resizable=yes,scrollbars=yes,status=no,titlebar=yes,toolbar=no'; break;
			case 'ext' : wline = 'toolbar=1,scrollbars=1,location=1,statusbar=1,menubar=1,resizable=1,width=600,height=500'; break;
		}
	
		if(!$.browser.safari){ //if its not a Safari
			var thelink = window.open("", "new_window", wline);
			thelink.location.replace(settings.url);
		} else {
			window.location.href = settings.url; //mac fix for OS X
		}
		
		qi_msgkill();
	}
	
	//removes all interstitial messages. initiated by the qi_cancel button or clicking on the mask.
	function qi_msgkill(){
		if (settings.flash == false){
			$('#qi_mask, .qi_wrapper').animate({opacity: 0}, 200, function(){
				$('#qi_mask, .qi_wrapper').remove();
			});
		} else {
			if (check_ie8_flash()){
				$('#' + settings.flash).css({opacity: 1});
			}
			$('#qi_mask, .qi_wrapper').remove();
		}
	}
	
	
	// WINDOW RESIZING
	$(window).resize(function(){qi_resizemask();});
	function qi_resizemask(){
		$('#qi_mask, .qi_wrapper').css({width: 1, height: 1});
		var h = Math.max($(window).height(), $('body').outerHeight(), $(document).height());
		var w = $(window).width();
		$('#qi_mask, .qi_wrapper').css({width: w, height: h});
		$('#' + settings.msgname + ' .qi_msg').css({marginTop: settings.top || Math.max(0, ($(window).height() - $('#' + settings.msgname + ' .qi_msg').outerHeight()) / 2 + $(window).scrollTop())});
	}
	
	function qi_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 qi_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 qi_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;
		}
	}
};
