(function() {

// Localize jQuery variable
var jQuery;

/******** Load jQuery if not present *********/
if (window.jQuery === undefined || window.jQuery.fn.jquery !== '1.4.4') {
    var script_tag = document.createElement('script');
    script_tag.setAttribute("type","text/javascript");
    script_tag.setAttribute("src",
        "http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js");
    script_tag.onload = scriptLoadHandler;
    script_tag.onreadystatechange = function () { // Same thing but for IE
        if (this.readyState == 'complete' || this.readyState == 'loaded') {
            scriptLoadHandler();
        }
    };
    // Try to find the head, otherwise default to the documentElement
    (document.getElementsByTagName("head")[0] || document.documentElement).appendChild(script_tag);
} else {
    // The jQuery version on the window is the one we want to use
    jQuery = window.jQuery;
    main();
}

/******** Called once jQuery has loaded ******/
function scriptLoadHandler() {
    // Restore $ and window.jQuery to their previous values and store the
    // new jQuery in our local jQuery variable
    jQuery = window.jQuery.noConflict(true);
    // Call our main function
    main(); 
}

/******** Our main function ********/
function main() { 
    jQuery(document).ready(function($) { 
		/******* Load CSS *******/
		
		var twit_script = $("<script>", {
			type: "text/javascript",
			src: "http://platform.twitter.com/widgets.js"
		});
		twit_script.appendTo('head');
		
	
		
		//load the html
		var currenturl = window.location;
		var toadd = '<div id="close">X</div><div id="overlay"><p>Share this wine with friends</p><iframe src="http://www.facebook.com/plugins/like.php?href=' + currenturl +'&amp;send=true&amp;layout=button_count&amp;width=100&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;font&amp;height=21" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:100px; height:21px; display:inline;" allowTransparency="true"></iframe> <a href="http://twitter.com/share" class="twitter-share-button" data-count="horizontal">Tweet</a><br /><p>Chat with Northwest Wines to You</p><p><a href="/nw-wines/blog-drink-northwest.html" rel="external"><img src="/assets/images/footer_blog.png" /></a> <a href="/resources/nw-wines-to-you-live.html" rel="external"><img src="/assets/images/footer_brandlive.png" /></a></p></div><div id="bg"></div>';
		$('body').append(toadd);
		$('#overlay').hide();
		$('#close').hide();
		$('#topDetails form').append('<input type="image" name="share" id="share" class="share" src="http://northwestwinestoyou.com/assets/images/buttons/share.this.hi.jpg" border="0" align="absmiddle" /> ');
		$('.share').click(function(){
			$("body").children(':not(#overlay)').fadeTo("slow", 0.2);
			$('#close').fadeTo(0, 1);
			$('#close').show();
			$('#overlay').show();
			$('#overlay').css("top", ( $(window).height() - $('#overlay').height() ) / 2+$(window).scrollTop() + "px");
			$('#overlay').css("left", ( $(window).width() - $('#overlay').width() ) / 2+$(window).scrollLeft() + "px");
			$('#close').css("top", ( $(window).height() - $('#overlay').height() ) / 2+($(window).scrollTop() + $('#close').height() -16) + "px");
			$('#close').css("left", ( $(window).width() + $('#overlay').width() ) / 2+$(window).scrollLeft() - $('#close').width() +15 + "px");
			$("script").hide();
			return false;
		});
		$('#close').click(function(){
			$('#overlay').hide();
			$("body").children(':not(#overlay)').fadeTo("slow", 1);
			$("script").hide();
			$('#close').hide();
			if(!paused)
				$("#pause").click();
		});
		//actual script
		
        // We can use jQuery 1.4.4 here
		$('a[rel=external]').attr('target', '_blank'); //make external links open in a new window.
    });
}

})(); // We call our anonymous function immediately
