var countryKey = 'N/A';
/**
 * social plugin(dependency jquery )
 * @version 0.1
 * @author heshunfeng
 * @update 2011-5-9
 */
if(jQuery){
        (function($){
			var link = [{ //dataSource
            name: 'Facebook',
				icon:'facebook',
                china:true,
                tip:'Share on facebook',
				url: 'http://www.facebook.com/sharer.php?t={title}&u={url}'
			}/*,{    TODO  next version
				name: 'FaceBookLike',
				icon:'atic_facebook_like',
				tip:'Like it on facebook',
				url: 'http://twitter.com/home?status={title}:+{url}'
         }*/
        , {
				name: 'Twitter',
				icon:'twitter',
                china:true,
                tip:'Share on twitter',
				url: 'http://twitter.com/intent/tweet?status={title}:+{url}' //2011-7-4  twitter api changed modify by heshunfeng
        }        /*,{
				name: 'Friendfeed',
				icon:'friendfeed',
                china:true,
                tip:'Share on friendfeed',
				url: 'http://friendfeed.com/share?url={url}&title={title}'
         }*/
        , {
            name: 'Linkedin',
            icon: 'linkedin',
            tip: 'Share on linkedin',
            url: 'http://www.linkedin.com/cws/share?url={url}&title={title}'
			}/*,{
				name: 'Google',
				icon:'google',
                tip:'Share on google',
				url: 'http://www.google.com/buzz/post?url={url}&title={title}'
			}*/,{
				name: 'Reddit',
				icon:'reddit',
                tip:'Share on reddit',
				url: 'http://reddit.com/submit?title={title}&url={url}'
			},{
				name: 'Delicious',
				icon:'delicious',
                tip:'Share on delicious',
				url: 'http://www.delicious.com/post?title={title}&url={url}'
			},{
				name: 'StumbleUpon',
				icon:'stumbleupon',
                tip:'Share on stumbleupon',
				url: 'http://www.stumbleupon.com/submit?title={title}&url={url}'
			},{
				name: 'Digg',
				icon:'digg',
                tip:'Share on digg',
				url: 'http://digg.com/submit?phase=2&title={title}&url={url}'
			}];
			var shareSNS = function(config){
            config = $.extend({
                shortLink: 4,
                threshold: 600
            }, config); //shortCurt button
                var me = this;
				this._init = function(){
                    me.threshold = config.threshold||600;//hover threshold value
                    config.shortLink = config.shortLink  < link.length ? config.shortLink : 4;
                var title = document.title.length > 140 ? encodeURIComponent(document.title.substring(0, 137)) + "..." : encodeURIComponent(document.title);
					var url=encodeURIComponent(location);
                config.obj.after(me.shareBox = $('<div style="z-index: 1000000; position: absolute; visibility: hidden;" id="sharebox"><div id="ats_inner"><div id="ats_head"><span>Bookmark / Share</span><a  id="atsptx">X</a></div><div id="at_hover"></div></div></div>'));
                     this.itemBox = this.shareBox.find("#at_hover:first");
                    var count =0;
					(("china"==countryKey.toLowerCase())?$(link).filter(function(){return !this.china;}):$(link)).each(function(index,item){
                    if (count == 1) {
                        //var length = 136 - url.length;
                        var titleUrl = document.title.length > 118 ? encodeURIComponent(document.title.substring(0, 115)) + "..." : encodeURIComponent(document.title);
                        if(count<config.shortLink){
                            count++;
                            config.obj.append('<a target="_blank" rel="nofollow" title="' + (item.tip || item.name) + '" class="' + item.icon + '" href="' + item.url.replace("{title}", titleUrl).replace("{url}", url) + '">' + item.name + '</a>');
                        }
                        me.itemBox.append('<a target="_blank" rel="nofollow" title="' + (item.tip || item.name) + '" href="' + item.url.replace("{title}", titleUrl).replace("{url}", url) + '" class="atic_' + item.icon + '"><span>' + item.name + '</span></a>');
                    }
                    else {
                        if (count < config.shortLink) {
                            count++;
                            config.obj.append('<a target="_blank" rel="nofollow" title="'+(item.tip||item.name)+'" class="'+item.icon+'" href="'+item.url.replace("{title}",title).replace("{url}",url)+'">'+item.name+'</a>');
                        }
						me.itemBox.append('<a target="_blank" rel="nofollow" title="'+(item.tip||item.name)+'" href="'+item.url.replace("{title}",title).replace("{url}",url)+'" class="atic_'+item.icon+'"><span>'+item.name+'</span></a>');
                    }
					});
                this.shareBox.find("#atsptx:first").click(function() {
                    me._hide()
                });
				};
				this.show = function(){
					if(this.shareBox){
                        if(this.timer){
                            clearTimeout(this.timer);
                        }
                        var curObjOffset = config.obj.offset();
                        var itemHeight =this.itemBox.outerHeight(true);
                        var visualHeight = document.documentElement.clientHeight;
                        var curObjVisualTop = parseInt(curObjOffset.top) - parseInt(jQuery(document).scrollTop());
                        var top =(visualHeight - curObjVisualTop)>30+config.obj.height()+itemHeight?curObjOffset.top+config.obj.height():curObjOffset.top-itemHeight-config.obj.height()-25;
						this.shareBox.css("visibility","visible");
						this.shareBox.css("left",curObjOffset.left+"px");
						this.shareBox.css("top",top+"px");
                    this.shareBox.unbind().hover(function() {
                        me.show();
                    }, function() {
                        me.lazyHide();
                    });
                }
                else {
						this._init();
					}
				};
                this._hide = function(){
                    this.shareBox.css("visibility","hidden");
                };
				this.lazyHide = function(){
                this.timer = setTimeout(function() {
                    me._hide()
                }, this.threshold);
				};
				this._init();
			};
			$.fn.shareSNS = function(){
				return this.each(function(){
                var shareSNSPlugin = new shareSNS({
                    obj: $(this),
                    container: $("body")
                });
					$(this).find('.shareAll:first').hover(function(){
						shareSNSPlugin.show();
					},function(){
                        shareSNSPlugin.lazyHide();
                    });
				});
			}

        var like = function(config) {
          if(countryKey.toLowerCase()=="china"){
            return false;
            }else{
              var url = encodeURIComponent(location);
              if (jQuery(".like").attr("id") == "showroom") {
                  config.obj.append('<iframe src="http://www.facebook.com/plugins/like.php?href=' + url + '&amp;send=false&amp;layout=standard&amp;width=280&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;height=28&amp;locale=en_US" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:280px;height:28px" allowTransparency="true"></iframe>');
              }
              else {
                  config.obj.append('<iframe src="http://www.facebook.com/plugins/like.php?href=' + url + '&amp;send=false&amp;layout=standard&amp;width=399&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;height=28&amp;locale=en_US" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:399px;height:28px" allowTransparency="true"></iframe>');
              }
          }
        };

        $.fn.like = function() {
            return this.each(function() {
                var likePlugin = new like({
                    obj: $(this),
                    container: $("body")
                });
                $(this).find('.liketext').hover(function() {
                    jQuery('.liketext').attr("title","like facebook button");
                });
            });
        }
		})(jQuery);

        jQuery(document).ready(function(){
        //jQuery.getScript("http://membercenter.made-in-china.com/account.do?xcase=parseIPToCountryKey");//check region

        var o = document.createElement("script");
        o.src = "http://membercenter.made-in-china.com/account.do?xcase=parseIPToCountryKey";
        o.type = "text/javascript";
        document.getElementsByTagName("head")[0].appendChild(o);

            var ipLoadTime = 500;
            if(jQuery(".shareSNS").length){
                setTimeout(function(){
                  jQuery(".shareSNS").shareSNS();
                  },ipLoadTime);
            }
        if (jQuery(".like").length) {
            setTimeout(function() {jQuery(".like").like()}, ipLoadTime);
        }
        
        });
    }

