(function($,c,b){$.map("click dblclick mousemove mousedown mouseup mouseover mouseout change select submit keydown keypress keyup".split(" "),function(d){a(d)});a("focusin","focus"+b);a("focusout","blur"+b);$.addOutsideEvent=a;function a(g,e){e=e||g+b;var d=$(),h=g+"."+e+"-special-event";$.event.special[e]={setup:function(){d=d.add(this);if(d.length===1){$(c).bind(h,f)}},teardown:function(){d=d.not(this);if(d.length===0){$(c).unbind(h)}},add:function(i){var j=i.handler;i.handler=function(l,k){l.target=k;j.apply(this,arguments)}}};function f(i){$(d).each(function(){var j=$(this);if(this!==i.target&&!j.has(i.target).length){j.triggerHandler(e,[i.target])}})}}})(jQuery,document,"outside");

jQuery.cookie = function (key, value, options) {

    // key and at least value given, set cookie...
    if (arguments.length > 1 && String(value) !== "[object Object]") {
        options = jQuery.extend({}, options);

        if (value === null || value === undefined) {
            options.expires = -1;
        }

        if (typeof options.expires === 'number') {
            var days = options.expires, t = options.expires = new Date();
            t.setDate(t.getDate() + days);
        }

        value = String(value);

        return (document.cookie = [
            encodeURIComponent(key), '=',
            options.raw ? value : encodeURIComponent(value),
            options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
            options.path ? '; path=' + options.path : '',
            options.domain ? '; domain=' + options.domain : '',
            options.secure ? '; secure' : ''
        ].join(''));
    }

    // key and possibly options given, get cookie...
    options = value || {};
    var result, decode = options.raw ? function (s) { return s; } : decodeURIComponent;
    return (result = new RegExp('(?:^|; )' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie)) ? decode(result[1]) : null;
};

jQuery(function(){	
	var homePageText = "";
	var userAgent = navigator.userAgent.toLowerCase();
	jQuery.browser.chrome = /chrome/.test(navigator.userAgent.toLowerCase()); 	
	if(jQuery.browser.chrome){
	  userAgent = userAgent.substring(userAgent.indexOf('chrome/') +7);
	  userAgent = userAgent.substring(0,userAgent.indexOf('.'));
	  version = userAgent;
	  // If it is chrome then jQuery thinks it's safari so we have to tell it it isn't
	  jQuery.browser.safari = false;
	}
	// Is this a version of Safari?
	if(jQuery.browser.safari){
	  userAgent = userAgent.substring(userAgent.indexOf('safari/') +7);
	  userAgent = userAgent.substring(0,userAgent.indexOf('.'));
	  version = userAgent;
	}
	if (jQuery.browser.mozilla) {
		homePageText+='<b>To make NUGGETRY your homepage:</b><br>';
		homePageText+='1. From the browser menu, click Tools<br>';
		homePageText+='2. Select Options<br>';
		homePageText+='3. In the General tabs Home Page entry box, type www.nuggetry.com and click OK<br>';
	}
	if (jQuery.browser.chrome) {
		homePageText+='<b>To make NUGGETRY your homepage:</b><br>';
		homePageText+='1. Click the wrench icon at the top right of your screen.<br>';
		homePageText+='2. Select Options<br>';
		homePageText+='3. In the Basics tabs Home Page section, select "Open this page" and type http://www.nuggetry.com, then click Close.';
	}
	if (jQuery.browser.safari) {
		homePageText+='<b>To make NUGGETRY your homepage:</b><br>';
		homePageText+='1. From the browser menu, click on Edit.<br>';
		homePageText+='2. Select Preferences<br>';
		homePageText+='3. On the General tab, type http://www.nuggetry.com in the Home Page entry box and youre done!';
	} 
	if (jQuery.browser.opera) {
		homePageText+='<b>To make NUGGETRY your homepage:</b><br>';
		homePageText+='1. From the browser menu, click tools<br>';
		homePageText+='2. Select Preferences<br>';
		homePageText+='3. On the General tab, type http://www.nuggetry.com in the Home Page entry box and click OK';
	}
	jQuery('.homepage_button .text-info').html(homePageText);
	jQuery("a.bookmark").click(function(e){
		e.preventDefault();
		if (!jQuery.browser.msie) {
			jQuery('.homepage_button .text-info, .homepage_button .kurinka').toggleClass("active_tt");
		}else{
			setHome();
		}
	})
	if (!jQuery.browser.msie) {
		jQuery("a.bookmark").bind("clickoutside",function(){jQuery('.homepage_button .text-info, .homepage_button .kurinka').removeClass("active_tt");});
	}
	
	
	jQuery(".subscribe_news_button").click(function(e){
		e.preventDefault();
		jQuery('.subscribe_news_button .text-info, .subscribe_news_button .kurinka').addClass("active_tt2");
	})
	
	jQuery(".subscribe_news_button").bind("clickoutside",function(e){
		jQuery('.subscribe_news_button .text-info, .subscribe_news_button .kurinka').removeClass("active_tt2");
	});
})
function setHome() 
{ 
   document.body.style.behavior='url(#default#homepage)';
   document.body.setHomePage(window.location.href);
}

jQuery(function(){
	jQuery("#bookmarkme").click(function(e) {
		if(jQuery.browser.chrome || jQuery.browser.safari){
			jQuery(this).attr("href","http://nuggetry.com/"); 
			 alert('Unfortunately, this browser does not support the requested action,'
			 + ' please bookmark this page manually. Just Drag and Drop the button to your bookmarks bar.');
			  return false;
			 
		}
		if(jQuery.cookie('is_bookmarked') == "1") return false;
		
		
		
		jQuery.ajax({
		   type: "GET",
		   url: "http://nuggetry.com/wp-admin/admin-ajax.php",
		   dataType: "jsonp",
		   data: "action=bookmark_action_callback_external&whatever=increment_bookmark_button",
		   success: function(msg){
				jQuery(".bookmark_number").html(msg.data+" Saves");
				jQuery.cookie('is_bookmarked', '1', { expires: 7, path: '/' });
		   }
		});
		
		
		if (window.sidebar) { // Mozilla Firefox Bookmark
			window.sidebar.addPanel(location.href,document.title,"");
		} 
		else if(window.external) { // IE Favorite
			window.external.AddFavorite(location.href,document.title); 
		}
		else if(window.opera && window.print) { // Opera Hotlist
			this.title=document.title;
			return true;
		}
		else { // for Safari, Konq etc - browsers who do not support bookmarking scripts (that i could find anyway)
			 alert('Unfortunately, this browser does not support the requested action,'
			 + ' please bookmark this page manually.');
		}
	})
})


jQuery('#newsletterformfooter input.button').live('click',function(e){
	e.preventDefault();
	var email = jQuery('#newsletterformfooter #email').val();
	var filter = /^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/;
	if (!filter.test(email)) {
		 jQuery('#newsletterformfooter #email').addClass('cn-error');
		 jQuery('#newsletterformfooter input.button').removeClass('cn-ajax-loader');  
		 jQuery('#newsletterformfooter input.button').val('Subscribe');
	}else{
		jQuery.ajax({
		   type: "GET",
		   url: "http://nuggetry.com/wp-content/themes/Nuggetry_2011/lib/plugins/constantcontact/send_external.php",
		   dataType: "jsonp",
		   data: "email="+jQuery('#newsletterformfooter #email').val()+'&postal_code='+jQuery('#newsletterformfooter #zipp').val(),
		   success: function(msg){
			 display(msg.data);
		   }
		});
		function display(msg) {
			jQuery('#email').removeClass('error');  
			jQuery('.subscribe_news_button .text-info').html(msg);
			var subscribersNumb = jQuery('.subscribe_news_button .subscribe span').text();
			var numbArray = subscribersNumb.split(' ')
			var theNumb = parseInt(numbArray[0]);
			jQuery('.subscribe_news_button .subscribe a span').html(jQuery('.subscribe_news_button .subscribe span').text((theNumb+1)+' Subscribed'));
			
		}
	}
});
