if (window.jQuery == undefined) {
	document.write('<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript"></script>');
}
var viral = function (options) {
	if (!options.base_path) {
		options.base_path = 'viral/';
	}
	window.fbAsyncInit = function() {
		if (window.FBN == undefined) {
			window.FBN = FB;
		}
	    FBN.init({
	        appId: '',
	        status: true,
	        cookie: true,
	        xfbml: false
	    });
	    FBN.Event.subscribe('edge.create', function(href, widget) {
	    	viral.close(true);
	    });
	    jQuery('.fb_button').live('click', function () {
	    	setTimeout(function () {
	    		viral.close(true);
	    	}, 3000);
	    });
	    FBN.XFBML.parse(jQuery('.viral-button-container').get(0));
	};
	
	viral.setCookie = function (name, value, expires, path, domain, secure) {
		if (expires) {
			var expiresDate = new Date(new Date().getTime() + (expires * 1000));
		}
	    document.cookie = name + "=" + escape(value) + ((expires) ? "; expires=" + expiresDate.toGMTString() : "") + ((path) ? "; path=" + path : "") + ((domain) ? "; domain=" + domain : "") + ((secure) ? "; secure" : "");
	}
	
	viral.getCookie = function (name) {
		var cookies = document.cookie.split(';');

		for ( i = 0; i < cookies.length; i++ ) {
			var cookie = cookies[i].split('=');
			if (cookie[0].replace(/^\s+|\s+$/g, '') == name) {
				if (cookie.length > 1) {
					return unescape(cookie[1].replace(/^\s+|\s+$/g, ''));
				}
				return null;
			}
		}
		return null;
	}
	
	viral.close = function (cookie) {
		jQuery('.viral-overlay').css('display', 'none');
    	jQuery('.viral-message-box').css('display', 'none');
    	jQuery('.viral-affiliate-link').css('display', 'none');
    	
    	if (cookie) {
    		viral.setCookie('viralLikePage', '1', options.expires);
    		if (options.new_window) {
    			window.open(options.new_window);
    		}
    	}
	}
	
	jQuery(document).ready(function () {
		var viralDisplayTimes = viral.getCookie('viralDisplayTimes');
		if (viralDisplayTimes && viralDisplayTimes > 0) {
			viral.setCookie('viralDisplayTimes', parseInt(viralDisplayTimes - 1));
			if (viralDisplayTimes > options.times) {
				viral.setCookie('viralDisplayTimes', options.times);
			}
			return;
		} else {
			viral.setCookie('viralDisplayTimes', options.times);
		}
		if (viral.getCookie('viralLikePage')) {
			return;
		}
		if (window.FB) {
			jQuery.getScript(options.base_path + 'all.js');
		} else {
			jQuery.getScript(document.location.protocol + '//connect.facebook.net/en_US/all.js');
		}
		
		jQuery('<link rel="stylesheet" type="text/css" href="' + options.base_path + 'style.css">').appendTo(jQuery('head'));
		
		var width = 360;
		var height = 192;
		
		// make overlay
		jQuery('<div class="viral-overlay"></div>')
		      .appendTo(jQuery('body'))
		      .height(jQuery(document).height())
		      .width(jQuery(document).width());
	      
	    // make message box
	    jQuery('<div class="viral-message-box"></div>')
	          .appendTo(jQuery('body'))
	          .height(height)
	          .width(width)
	          .css('left', (jQuery(document).width() - width) / 2)
	          .css('top', (jQuery(window).height() - height) / 2);
	
	    jQuery('<div class="viral-content"></div>')
	          .appendTo(jQuery('.viral-message-box'));
	
	    jQuery('<div class="viral-border"></div>')
	          .appendTo(jQuery('.viral-message-box'));
	
	    jQuery('<div class="viral-title">' + options.title + '</div>')
	          .appendTo(jQuery('.viral-message-box .viral-content'));
	    
	    if (options.close_mode == 'button') {
	    	jQuery('<div class="viral-close-button"></div>')
	    		.appendTo(jQuery('.viral-title')).click(function () {
	    			viral.close();
	    		});
	    }
	
	    jQuery('<div class="viral-text">' + options.content + '</div>')
	    	  .appendTo(jQuery('.viral-message-box .viral-content'));
	  
	    jQuery('<div class="viral-button"><div class="viral-top-line"></div></div>')
	          .appendTo(jQuery('.viral-message-box .viral-content'));

	    jQuery('.viral-overlay').css('opacity', options.transperent);
	    jQuery('.viral-overlay').css('filter', 'alpha(opacity=' + (options.transperent * 100) + ')');//options.transperent * 100);
        jQuery('.viral-overlay').css('-ms-filter', '"progid:DXImageTransform.Microsoft.Alpha(Opacity=' + options.transperent * 100 + ')"');
	    
	    if (options.type == 'like') {
	    	jQuery('<div class="viral-button-container"><fb:like width="300" href="' + options.url + '"></fb:like></div>')
	    		  .appendTo(jQuery('.viral-message-box .viral-button'));
	    }
	    if (options.type == 'share') {
	    	jQuery('<div class="viral-button-container"><fb:share-button href="' + options.url + '" type="button_count"></fb:share-button></div>')
  		          .appendTo(jQuery('.viral-message-box .viral-button'));
	    }
	    if (options.show_counter && options.timeout) {
	    	jQuery('<div class="viral-counter"></div>')
	              .appendTo(jQuery('.viral-message-box .viral-button'));
	    	viral.counter = options.timeout;
	    	if (options.close_mode == 'timer') {
		    	viral.interval = setInterval(function () {
		    		jQuery('.viral-message-box .viral-counter').html('Or wait ' + (--viral.counter) + ' seconds');
		    	}, 1000);
	    	} else {
	    		jQuery('.viral-message-box .viral-counter').html('<a href="javascript: viral.close();">' + (options.close_text.length ? options.close_text : 'Close') + '</a>');
	    	}
		}
	    if (options.affiliate.length) {
	    	jQuery('<div class="viral-affiliate-link"><a href="http://ilikeviral.com?ap_id=' + options.affiliate + '">Want more visitors? Press here to get this plug in.</a></div>')
	    		  .css('top', (jQuery(window).height() - height) / 2 + height + 25)
	    		  .appendTo(jQuery('body'));
	    }
	    jQuery('<img src="http://ilikeviral.com/images/1px.gif" width="1" height="1" style="display: none" />').appendTo(jQuery('body'));
	});
	
	if (options.timeout && options.close_mode == 'timer') {
		setTimeout(function () {
			viral.close();
		}, options.timeout * 1000);
	}
}
