$(document).ready(function(){
	$wall = $('#boxes');
	nf.init();
});

(function () {
	 nf = { 
				loadedData: null,
				isMSIE: /*@cc_on!@*/false,
				box_content: null,
				fbLoginStatus: null,
				pHistory: [],
				siteURL: "http://theearthnews.jp/",
				fbAppID: '229456710423001',
				defTitle: "ジアスニュース",
				limit:20,
				onTransition: false,
				loadedDataArray: {},
				boxInnerContent: null,
				isFirst: true,
				hrefURL: null,
				boxID: null,
				pageNum: null,
				currentPageNum: 1,
				offset: null,
				tag: "",
				currentCat: "",
				openedBox: "",
				targetCategory: null,
				targetID: null,
				tempTargetID: null,
				xhr: null,
				seachWords: null,
				entry_length: null,
				xmlToLoad: {
					all:'getlist.php',
					search:'search.php'
				},
	 
				init: function (){
				
					fb.facebookSetUp();
					$(window).hashchange(
						function(){
							nf.hashChangeEvent();
						}
					);
					//$("#header").delay(200).animate({'opacity' : '1'}, 800);
					$("#header").delay(200).animate({'opacity' : '1'}, 800);
					
					$("#search_input, #search_result_input").live("keydown", function(e){
						if(e.keyCode == 13){
							 if($(this).attr("id") == "search_input"){
								nf.search("search");
							 } else {
								nf.search("result");
							 }
						}
					});
					

					$('#btn_form_submit, #btn_form_result_submit').live("click", function(){
							 if($(this).attr("id") == "btn_form_submit"){
								nf.search("search");
							 } else {
								nf.search("result");
							 }
					});
					
					
				
					$("#nav img.btnOff").hover(
						function(){$(this).stop().animate({'opacity' : '0'}, 500);}, 
						function(){$(this).stop().animate({'opacity' : '1'}, 500);}
					);
				
					$(".alpha_obj").live("mouseover", function(){$(this).stop().animate({'opacity' : '0.6'}, 200);});
					$(".alpha_obj").live("mouseout", function(){$(this).stop().animate({'opacity' : '1'}, 200);});
				
					$('.flash_lamp').css({background:'#ffffff'}).live("mouseover", function(){
						$(this).children().stop().css({opacity:0.4}).animate({opacity:1},600);
					});				

					$("#nav ul li, #home").click(function(){
						nf.currentPageNum = 1;
						if($(this).attr("id") == "home"){
							nf.currentCat = "";
							nf.loadXML(nf.xmlToLoad.all, "",1, "", "","");
						} else {
							nf.currentCat = nf.getCategoryID($(this).attr("id"));
							nf.loadXML(nf.xmlToLoad.all,"category", 1, "", nf.currentCat,"");
						}
						location.hash = "";
					});
					
					$('#pageNav ul li.page_top').live("click", function(){
						$.scrollTo(0, 500 );
					});

					$("#pageNav ul li:not(.page_top)").live("click", function(){

						var contPath = location.hash.split("/");
						
						if(nf.currentPageNum == $(this).html()){
							return false;
						} else {
							loadedDataArray = {};
							nf.currentPageNum = $(this).html();
							if(nf.currentCat == "search_result"){
								nf.getSearchResult (nf.xmlToLoad.search, nf.currentPageNum, nf.seachWords);
						} else if(nf.currentCat == "search_tag"){
							location.hash = "";
							nf.loadXML(nf.xmlToLoad.all, "search_tag", nf.currentPageNum 	, "", "",nf.tag);
						} else if (nf.currentCat != "" && nf.currentCat != "search_result"){
								location.hash = "";
								nf.loadXML(nf.xmlToLoad.all, "category", nf.currentPageNum, "", nf.currentCat,"");
						}else{
								location.hash = "";
								nf.loadXML(nf.xmlToLoad.all, "", nf.currentPageNum, "", "","");
						}
						}
					});
					
					
					$(".tags ul li").live("click", function(){
						location.hash = "";
						nf.currentCat = "search_tag";
						nf.tag = encodeURI($(this).html());
						nf.loadXML(nf.xmlToLoad.all, "search_tag", 1, "", "",nf.tag);
					})
					
					
					$('.btnLogin').click(function(){
						//window.location.hash = "#";
						//document.title = nf.defTitle;
						// if(currentBox){
								// closeBox(currentBox);
							// }
						 FB.login(function(response) { 
						 }); return false;
					});

					$('.btnLogout').click(function(){
						
						//window.location.hash = "#";
						//document.title = nf.defTitle;
						// if(currentBox){
								// closeBox(currentBox);
							// }

						FB.logout(function(response) {
							var image = document.getElementById('fb_image');
							image.src = "";
							var name = document.getElementById('fb_name');
							name.innerHTML = ""
							nf.fbLoginStatus = 1;
							fb.showLogIn();
						}); return false;
					});
				
					if(location.hash){
						var contPath = location.hash.split("/");
						var n = contPath[2];
						nf.loadXML(nf.xmlToLoad.all,"", "", n, "","");
						
					} else {
						nf.loadXML(nf.xmlToLoad.all,"", 1, "", "","");
					}
				},

				loadXML: function (_xmlToLoad, action, pageNum, entry_id, catId,tagWord){
						$("#boxes").animate({opacity:"0"}, 400, "easeOutCubic", function(){
						$("#boxes").html('<img src="img/ajax-loader.gif" />');
						$("#boxes").attr("style", "	margin: 0;	min-height:97%; height: 100%; 	position: relative;");
						$("#pageNav").hide();
						$("#boxes").animate({opacity:"1"}, 0);
						if(nf.xhr) nf.xhr.abort();
						nf.xhr = $.ajax({
							type: 'get',
							url:_xmlToLoad,
							data:{a:action, id:entry_id, page:pageNum, limit:nf.limit, cat:catId, tag:tagWord},
							dataType:"xml",
							error:function(xhr, status, errorThrown) { 
							}, 
							success:function(data){
								nf.loadedData = data;
								$("#boxes").html('');
								nf.handleXML();
							}
						});
					});
				},


				pageNation: function(_n){
					nf.pageNum = Math.ceil(_n / nf.limit);
					
					var p = "" 
					p =  '<ul class="clearfix"><li class="page_top"></li>';
					for (i=1;i<=nf.pageNum;i++){
						p += "<li>"+i+"</li>"
					}
						p += "</ul>";
					$("#pageNav").html(p);
				},
				
				search: function(type){
						nf.currentPageNum = 1;
						location.hash = "";
						nf.currentCat = "search_result";
						if(type == "search"){
							nf.seachWords = $("#search_input").val();
							$("#search_result_input").val("");
							$("#search_input").val("");
						} else {
							nf.seachWords = $("#search_result_input").val();
							$("#search_result_input").val("");
							$("#search_input").val("");
						}
						nf.getSearchResult(nf.xmlToLoad.search, nf.currentPageNum, encodeURI(nf.seachWords));
				},
				
				getSearchResult: function (_xmlToLoad, pageNum, keyWords){
						$("#boxes").animate({opacity:"0"}, 400, "easeOutCubic", function(){
						$("#boxes").html('<img src="img/ajax-loader.gif" />');
						$("#pageNav").hide();
						$("#boxes").attr("style", "	margin: 0;	min-height:97%; height: 100%; 	position: relative;");
						$("#boxes").animate({opacity:"1"}, 0);
						if(nf.xhr) nf.xhr.abort();
						nf.xhr = $.ajax({
							type: 'get',
							url:_xmlToLoad,
							data:{page: pageNum, limit: nf.limit, key: keyWords},
							dataType:"xml",
							error:function(xhr, status, errorThrown) { 
								//alert(errorThrown+'\n'+status+'\n'+xhr.statusText); 
							}, 
							success:function(data){
								nf.loadedData = data;
								$("#boxes").html('');
								if($("count",nf.loadedData).text() == 0){
									var resutContents = '<div id="search_result"><div><p><span class="search_words">'+decodeURI(keyWords)+'</span>に該当する記事は見つかりませんでした。</p>';
										  resutContents += '<p>There is no noun matching your search.</p>';
										  resutContents += '<div id="search_box"><ul class="clearfix"><li><img src="img/label_search.png" alt="Search:"/></li><li class="searchbox"><input id="search_result_input" type="text" class="form-text keywords" value="" size="15"  name="search_theme_form" maxlength="128"><img src="img/btn_search.png" alt="search" id="btn_form_result_submit" name="search" /></li></ul></div>';
										  resutContents += '<p class="search_result_note">※キーワードに誤字、脱字が無いかご確認ください。<br />';
										  resutContents += '※別のキーワードで検索を試してみてください。</p></div></div>';
										  $("#boxes").html(resutContents);
								} else {
									nf.handleXML();
								}
								
								
							}
						});
					});
				},

				handleXML :function(){
					nf.entry_length = $("count",nf.loadedData).text();
					nf.currentPageNum = $("page",nf.loadedData).text();
					
					if(nf.entry_length == 0){
						$("#boxes").html('<div class="no_entry"><p>該当する記事がみつかりません。</p></div>');
					} else {
						nf.arrangeBoxes();
						$("#pageNav").show();
					}
				},

				arrangeBoxes: function(){
						nf.loadedDataArray = {};
						var contPath = location.hash.split("/");
	

						
						$("#boxes").html('');

						var n;
						var len = $("entry",nf.loadedData).length;
						if( len < nf.limit){
							n = len;
						} else {
							n = nf.limit;
						}
						
						nf.pageNation(nf.entry_length);
						nf.setCurrenPage();
						nf.pageNum = Math.ceil(nf.entry_length / nf.limit);
						nf.offset = (nf.currentPageNum - 1) * nf.limit ;
						
						// if(nf.pageNum == nf.currentPageNum){
							// n = nf.entry_length % nf.limit;
						// }
						
						nf.openedBox = "";
						var count = 0;
						var box_style;

						$("#boxes").animate({opacity:"0"}, 0);
						
						$("entry",nf.loadedData).each(function(i){
						
									nf.loadedDataArray[$("id",  this).text()] = this;
									
									box_style = $("category", this).text();
									
									var w = ($("thumbnail_width",  this).text())?($("thumbnail_width",  this).text()) : 705;
									var h = ($("thumbnail_height",  this).text())?($("thumbnail_height",  this).text()) : 355;
									var date = $("published",  this).text();
									var isNew = $("new",  this).text();
									var title = nf.trimStr($("title",  this).text(), 62);

									if(!$("boxlink",  this).text() || $("boxlink",  this).text() == ""){
										nf.hrefURL = "#!/contents/"+ $("id",  this).text();
									} else {
										nf.hrefURL = $("boxlink",  this).text();
									}
										
										nf.boxID = "box" + $("id",  this).text();

										nf.box_content = '<div id="'+nf.boxID+'" class="box '+box_style+'">'+isNew+'<div class="box_cont"><a class="flash_lamp thumbnail" href="'+nf.hrefURL+'" target="'+$("target",  this).text()+'"><img src="'+$("thumbnail",  this).text()+'" width="'+w+'" height="'+h+'" /></a></div><div class="box_bottom"><p>'+date+'</p><p><a href="'+nf.hrefURL+'" target="'+$("target",  this).text()+'">'+title+'</a></p></div></div>';
										
										$("#boxes").append(nf.box_content);		
										
										// if(isNew == 1){
											// ("#"+nf.boxID).append("<p>new</p>");	
										// }
										
										var boxH = eval(h) + 68;
										$("#"+nf.boxID+" .box_cont").css({"width":w+"px", "height":h+"px"});
										$("#"+nf.boxID).css({"width":w+"px", "height":boxH+"px"});
										
										nf.preloadImg($("thumbnail", this).text(), $("id", this).text(), w, h);
										
									
										if(i == n-1){
											if(nf.isMSIE){
												$(".box_bottom").corner("12px bottom");
											}
											nf.callMasonry();
											$("#boxes").delay(600).animate({opacity:"1"}, 800, "easeOutCubic");
													if(contPath[2]){
														nf.isFirst = false;
														var t = contPath[2];
														nf.targetID = contPath[2];
														t = "box" + String(t);
														nf.openBox(t);
													}
										}

								});

					},
					
				eventResized: function(){
					nf.onTransition = true;
					$wall.masonry({
							columnWidth: 230, 
							itemSelector: '.box:not(.invis)',
							animate: true,
							animationOptions: {
								duration: 500,
								easing: 'easeOutQuint',
								queue: false
							}
						},
						function(){
							nf.onTransition = false;
						});
				},
						
				callMasonry: function(_targetBox){
					
					$(_targetBox).exResize({
						callback : function(){
							if(nf.onTransition){
								return false;
							} else {
								nf.eventResized();
							}
						}
					});
					
					$wall.masonry({
							columnWidth: 230, 
							itemSelector: '.box:not(.invis)',
							animate: true,
							animationOptions: {
								duration: 500,
								easing: 'easeOutQuint',
								queue: false
							}
						},
						function(){
							if(_targetBox){	
								$(_targetBox).delay(500).animate({'opacity' : '0'}, 0, "easeOutCubic", function(){
									var off = $(_targetBox).offset();
									$.scrollTo(off.top - 50, 500, function(){
										$(_targetBox).delay(500).animate({'opacity' : '1'}, 400, "easeOutCubic", function(){
										$(_targetBox + " iframe").css("visibility", "visible");
										});
									});				
								});
								}
							});
						},

				hashChangeEvent: function (){
					document.title = "THE EARTH NEWS | ジアス・ニュース";
					if(location.hash){
					var contPath = location.hash.split("/");			
					if(contPath[2]){
						if($("id", nf.loadedDataArray[contPath[2]]).text()){
							nf.targetID = contPath[2];
							tempTargetID = "box" + nf.targetID;
							nf.openBox(tempTargetID);
						} else {
							nf.loadXML(nf.xmlToLoad.all,"", "", contPath[2], "", "");
						}
					}
				}
				
				
				},


				setCurrent: function (_current){		
					$("#menu li img").css("display","inline");
						switch(_current){
							case "news":
								$(".menu01 img.btnOff").css("display","none");
							break;
							case "shopping":
								$(".menu02 img.btnOff").css("display","none");
							break;
							case "collection":
								$(".menu03 img.btnOff").css("display","none");
							break;
							case "shop_list":
								$(".menu04 img.btnOff").css("display","none");
							break;
							case "life_stage":
								$(".menu05 img.btnOff").css("display","none");
							break;
						}
					if(!_current){
						$("#menu li img").css("display","inline");
					}
				},
				
				setCurrenPage:function(){
					$("#pageNav ul li:eq("+nf.currentPageNum+")").addClass("current");
				},


				openBox: function(_targetBox){

					var target = _targetBox.replace("box","");
					$("#"+_targetBox).animate({opacity: 0}, 200, "easeOutCubic", function(){
					$("#"+_targetBox).css({"width":"", "height":""});
								
					var shareURL = location.hash;
					var pTitle =  $("title" ,nf.loadedDataArray[target]).text();
					var isNew =  $("new" ,nf.loadedDataArray[target]).text();
					
					document.title = pTitle
					_gaq.push(['_trackPageview', location.pathname+location.hash]);
					
					var tweetTitle = "【ジアスニュース】"+pTitle.replace('"','');;
					//alert(tweetTitle);
					//$("meta[property=og:title]").attr("content", $("title" ,nf.loadedDataArray[target]).text());
					//var twitterShare = '<script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script><a href="http://twitter.com/share" class="twitter-share-button" data-url="'+nf.siteURL+shareURL+'" data-count="none">Tweet</a>';
					
					var twitterShare = '<a href="http://twitter.com/share" class="twitter-share-button" data-text="'+tweetTitle+'" data-count="none"  data-url="'+nf.siteURL+shareURL+'" data-via="the_earth_news" data-lang="ja">ツイート</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script>'
								
					var mixiShare = '<a href="http://mixi.jp/share.pl" class="mixi-check-button" data-key="b57fa3add07d6f180260e8be95cfc49fdbee2ac2" data-button="button-1" data-url="'+nf.siteURL+shareURL+'">Check</a><script type="text/javascript" src="http://static.mixi.jp/js/share.js"></script>';
								
					var fbShare = '<fb:like href="'+nf.siteURL+shareURL+'" layout="button_count" show_faces="false" width="80" font=""></fb:like>';

					var socialURL = location.hash.replace("#!", "c");  
							
					var fbShare2 = '<a name="fb_share" href="http://www.facebook.com/sharer.php?u='+nf.siteURL+nf.shareURL+'&t='+document.title+'">Share</a><script src="http://static.ak.fbcdn.net/connect.php/js/FB.Share" type="text/javascript"></script>';
					
					var gPlus = '<g:plusone size="medium"></g:plusone><script type="text/javascript" src="https://apis.google.com/js/plusone.js"></script>';
					
					var shareCont ;
					
					if($("widget" ,nf.loadedDataArray[target]).text() == "非表示"){
						var shareCont = '';
					} else {
						var shareCont = '<div class="share_btns"><ul class="clearfix"><li id= "share_tw">'+twitterShare+'</li><li id="share_mixi">'+mixiShare+'</li><li id="share_fb2">'+fbShare+'</li><li id="share_fb">'+fbShare2+'</li><li id="share_g">'+gPlus+'</li></ul></div>';
					}
								
								

								
					var cont = isNew;
					cont += $("mainimage" ,nf.loadedDataArray[target]).text() ;
					cont += '<div class="box_contents_body"><p class="post_date">'+$("updated" ,nf.loadedDataArray[target]).text()+'</p><h2>'+$("title" ,nf.loadedDataArray[target]).text()+'</h2><h3>'+$("summary" ,nf.loadedDataArray[target]).text()+'</h3>';

					cont += '<p class="box_contens_text">'+$("contents" ,nf.loadedDataArray[target]).text()+'</p>';
					cont += shareCont+'</div><div class="box_contents_footer"><div class="tags">'+$("tags" ,nf.loadedDataArray[target]).text()+'</div><div class="related_post clearfix">'+$("related_post" ,nf.loadedDataArray[target]).text()+'</div><script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script><fb:comments href="'+nf.siteURL+shareURL+'" num_posts="5" width="640"></fb:comments></div>';
								
					$("#"+_targetBox).html(cont);
					$("#"+_targetBox + " iframe").css("visibility", "hidden");
					
					var boxHeight = $("#"+_targetBox+" .main_images .slide_item").height();
					$("#"+_targetBox+" .main_images").css("height",boxHeight);
					
					FB.XFBML.parse();
					$("#"+_targetBox).css("width", 680);
					
					nf.slider(_targetBox);
					
					nf.reloadFrames();
					
					if(nf.isMSIE){
						$(".box_contents_footer").corner("12px bottom");
						$(".main_images .slide_item").corner("12px top");
					} else {
						$(".main_images .slide_item img").corner("12px top");
					}
					
					
					
					if(nf.openedBox != ""){
						nf.closeBox(nf.openedBox);
					}
					nf.openedBox = _targetBox;
					nf.callMasonry("#"+_targetBox);
					// $("#"+_targetBox).exResize({
						// callback : function(){
							// if(nf.onTransition){
								// return false;
							// } else {
								// nf.eventResized();
							// }
						// }
					// });
					});
				},

				closeBox: function(_targetBox){

								var tempID = _targetBox.replace("box","");
								var w = $("thumbnail_width", nf.loadedDataArray[tempID]).text();
								var h = $("thumbnail_height", nf.loadedDataArray[tempID]).text();
								var date = $("published",  nf.loadedDataArray[tempID]).text();
								var isNew = $("new",  nf.loadedDataArray[tempID]).text();
								var title = nf.trimStr($("title",  nf.loadedDataArray[tempID]).text(), 62);
								
								if(!$("boxLink" ,nf.loadedDataArray[tempID]).text()){
									nf.hrefURL = "#!/contents/"+ $("id", nf.loadedDataArray[tempID]).text();
								} else {
									nf.hrefURL = "#!/" + $("boxLink" ,nf.loadedDataArray[tempID]).text();
								}
									

								nf.boxInnerContent = '<div class="box_cont">'+isNew+'<a class="flash_lamp thumbnail" href="'+nf.hrefURL+'"><img src="'+$("thumbnail" ,nf.loadedDataArray[tempID]).text()+'"  width="'+w+'" height="'+h+'" /></a></div><div class="box_bottom"><p>'+date+'</p><p><a href="'+nf.hrefURL+'">'+title+'</a></p></div>';

								$("#"+_targetBox).html(nf.boxInnerContent);
								
								if(nf.isMSIE){
									$("#"+_targetBox).corner("12px");
								} else {
									$("#"+_targetBox+" .thumbnail img").corner("12px top");
								}
								
								$("#"+_targetBox).css("width", parseInt(w));
				},

				getCategoryID: function(catName){
					var catId;
					switch(catName){
						case "air_and_water":
							catId = 25;
						break;
						case "green_and_being":
							catId = 24;
						break;
						case "human_and_society":
							catId = 23;
						break;
						case "ideas_and_products":
							catId = 27;
						break;
						case "information":
							catId = 1;
						break;
					}
					return catId;
				},


				preloadImg: function(img, id, width, height){
					var loader = new ImageLoader(img);
					 $("#box"+id+" a.thumbnail").html('<img class="loader" src="img/ajax-loader2.gif" width="24" height="24" />');
					loader.loadEvent = function(){
								 $("#box"+id+" a.thumbnail").html('<img src="'+img+'" width="'+width+'" height="'+height+'" alt=""/>');
								 $("#box"+id+" img").css("opacity","0");
								 $("#box"+id+" img").animate({opacity:"1"}, 400);
								 
								 if(nf.isMSIE){
									$("#box"+id).corner("12px top");
								} else {
									$("#box"+id+" img").corner("12px top");
								}
					}
					loader.load();
				},

				reloadFrames: function(){

					if ($.browser.msie && $.browser.version > 6)
					{
					
					} else {

						$('iframe').each(function(){
						   $(this).attr('src', $(this).attr('src'));
						});
						$('iframe.twitter-share-button').attr('src', $(this).attr('src'));
					}
					FB.Share.renderAll();
				},
				
				slider: function(target){
					
					var targetBox = $("#"+ target);
					var len = $(".slide_item", targetBox).length;
					var current = 1;
					var n = 0;
					//			alert(len);
					if(len > 1){
						$(".slide_item:gt(0)", targetBox).hide().css("opacity", 0);
						$(".box_contents_body", targetBox).prepend('<div class="slide_control clearfix"><ul class="clearfix"><li class="slide_left"></li><li class="slide_right alpha_obj"></li></ul><p>'+current+' / '+len+'</p></div>');
						if(n == 0){
							$(".slide_left").css({"opacity":0.6});
							$(".slide_right").css({"cursor":"pointer"});
						}
					
						$(".slide_left", targetBox).live("click", function(){
							if (n == 0) return false;
							$('.slide_item:eq('+n+')', targetBox).stop().animate({"opacity": 0}, 400, "easeOutCubic", function(){});
							n = n - 1;
							current = n+1;
							$('.slide_item:eq('+n+')', targetBox).show().animate({"opacity" : 1}, 400);
							$('.slide_control p').html(current+' / '+len);
							$(".slide_right, .slide_left", targetBox).css({"opacity": 1, "cursor":"pointer"}).addClass("alpha_obj");
							if(n == 0){
								$(".slide_left").css({"opacity":0.6, "cursor": "default"});
								$(".slide_left").removeClass("alpha_obj");
							}
						});
					
						$(".slide_right", targetBox).live("click", function(){
							
							if (n == len-1) return false;
							$('.slide_item:eq('+n+')', targetBox).stop().animate({"opacity": 0}, 400, "easeOutCubic", function(){});
							n = n + 1;
							current = n+1;
							$('.slide_item:eq('+n+')', targetBox).show().animate({"opacity": 1}, 400);
							$('.slide_control p').html(current+' / '+len);
							$(".slide_right, .slide_left").css({"opacity": 1, "cursor":"pointer"}).addClass("alpha_obj");
							if(n == len-1){
								$(".slide_right").css({"opacity":0.6, "cursor": "default"});
								$(".slide_right").removeClass("alpha_obj");
							} 
						});				
					
					}
					
					
					
				},
				
				trimStr: function(str, num){
					   var tempStr = encodeURI(str);
					   var ns = "";
					   var s = 0;
					   var m = 0;
					   var lc = 0;
					   var isSingle = true;
					   var ostr;
					   for(i=0;i<tempStr.length; i++) {

							if(tempStr.substr(i,1) == "%"){
								isSingle = false;
								i += 8;
							} else {
								isSingle = true;
							}
							if(!isSingle){
								s += 2;
							} else {
								m++;
							}
							if((s+m) < num){
							ns += str.substr(lc,1);
							}
							lc++;	
						}
						if((s+m) > num){
								ostr = ns + "..."

							} else {
								ostr = str;		
						}
						return unescape(ostr);
				}

	},
	
	fb = {
			facebookSetUp: function (){

				FB.init({ 
							appId: nf.fbAppID, 
							cookie:true, 
							status:true, 
							xfbml:true,
							channelUrl : nf.siteURL
				});
		 
	 
				FB.Event.subscribe('auth.login', function(response) {
					FB.api('/me', function(user) {
						   if(user != null) {
								var image = document.getElementById('fb_image');
								image.src = 'http://graph.facebook.com/' + user.id + '/picture';
								var name = document.getElementById('fb_name');
								name.innerHTML = "Welcome "+user.name;
								nf.fbLoginStatus = 2;
								fb.hideLogIn();
							 }
					});
				});
	 
				FB.Event.subscribe('auth.logout', function(response) {
					var image = document.getElementById('fb_image');
					image.src = "";
					var name = document.getElementById('fb_name');
					name.innerHTML = ""
					nf.fbLoginStatus = 1;
					fb.showLogIn();
				});


				FB.getLoginStatus(function(response) {
						if (response.session) {
								nf.fbLoginStatus = 2;
								FB.api('/me', function(user) {
								   if(user != null) {
										fb.hideLogIn();
										 var image = document.getElementById('fb_image');
										 image.src = 'http://graph.facebook.com/' + user.id + '/picture';
										 var name = document.getElementById('fb_name');
										 name.innerHTML = "Welcome "+user.name;
									   }
									});
						} else {		  
							fb.showLogIn();
							nf.fbLoginStatus = 1;
							FB.api('/me', function(user) {
							   if(user != null) {
									var image = document.getElementById('fb_image');
									image.src = '';
									var name = document.getElementById('fb_name');
									name.innerHTML = '';
								}
							});
						}
				});


			},

			hideLogIn: function(){
				$(".btnLogin").hide();
				$(".btnLogout").show();
				$("#fb_loggedIn_cont").css("display","block");
			},

			showLogIn: function(){
				$(".btnLogout").hide();
				$(".btnLogin").show();
				$("#fb_loggedIn_cont").css("display","none");	
			}
	
	}

})(jQuery);
