// global vars
var facebookLikeHandler = null;
var isActiveArea		= false;
var renderTimeoutID		= null;

// on all ready
$( document ).ready ( function (  ) {
	initResizehandler (  );
	facebookLikeHandler = new FacebookLikeHandler (  );
	
	facebookLikeHandler.init ( countryCode );
	// facebookLikeHandler.changeItem ( 145 );
} )

// window resize manager
function initResizehandler (  ) {
	// toggleButtonHolderVisibility ( true );
	
	$( window ).resize ( function (  ) {
		if ( $( document ).width (  ) < 800 ) {
			var posRight = $( document ).width (  ) - 400;
			$("#likeBoxContainer").css({
				'position' 	: 'absolute',
				'bottom' 	: '10px',
				'right'		: posRight,
				'width'		: '120px',
				'height'	: '25px'
			});
		} else {
			$("#likeBoxContainer").css({
				'position' 	: 'absolute',
				'bottom' 	: '10px',
				'right'		: '400px',
				'width'		: '120px',
				'height'	: '25px'
			});
		}
		
		if ( $(document).height (  ) < 695 ) {
		  	// toggleButtonHolderVisibility ( false );
		  } else if ( $(document).width (  ) < 950 ) {
		  	// toggleButtonHolderVisibility ( false );
		  } else if ( $(document).width (  ) < 950 && $(document).height (  ) < 695 ) {
		  	// toggleButtonHolderVisibility ( false );
		  } else {
		  	// toggleButtonHolderVisibility ( true );
		  }
	} );
}

function toggleButtonHolderVisibility ( _status ) {
	switch ( _status ) {
		case "true":
		case true:
			$("#likeBoxContainer").css ( "display", "block" );
		break;
		case "false":
		case false:
			$("#likeBoxContainer").css ( "display", "none" );	
		break;
	}
}

// main
function FacebookLikeHandler (  ) {
	// this.mainUrl 		= "http://www.strellson.com/strellson_collection_items3.php?id=";
	this.mainUrl 		= "http://www.strellson.com/" + countryCode + "/" + pageID + "-" + ( ( pageName == "" ) ? "collection" : pageName ) + "/";
	this.FBcountryCode 	= null;
	
	this.detailCategory = null;
	this.detailItem		= null;
}

FacebookLikeHandler.prototype = {
    init : function ( _countryCode ) {
		var self = this;
		
		switch ( _countryCode ) {
			case "demo":
			case "de":
			case "default":
				self.FBcountryCode = "de_DE";
			break;
			case "en":
				self.FBcountryCode = "en_US";
			break;
			case "fr":
				self.FBcountryCode = "fr_FR";
			break;
			case "ru":
				self.FBcountryCode = "ru_RU";
			break;
		}
		
		window.fbAsyncInit = function (  ) {
			FB.init ( {
				appId	: "144284795598473", 
				status	: true, 
				cookie	: true,
				xfbml	: true,
				channelUrl  : 'http://www.strellson.com/xd_receiver.htm'
			} );
			
			FB.Event.subscribe('edge.create', function(href, widget) {
				// Do something, e.g. track the click on the "Like" button here
				// alert ( self );
				// self.callback_userLikedCollectionItem (  );
				
				_uacct = "UA-241793-7";
				urchinTracker ( "http://www.strellson.com/" + countryCode + "/" + pageID + "-" + pageName + "/" + self.detailCategory + "/" + self.detailItem );
			});
		};
	  
	  (function() {
		var e = document.createElement('script'); e.async = true;
		e.src = document.location.protocol + '//connect.facebook.net/' + self.FBcountryCode + '/all.js';
		document.getElementById('fb-root').appendChild(e);
	  }());
	  
	  return false;
    },
    changeItem : function ( _detailCategory, _detailItem, _pageID ) {
    	var self = this;
    	
    	clearTimeout ( renderTimeoutID );
    	
    	// rondell page - premium : sportswear
    	// deprecated!!! No rondell anymore!!
    	switch ( countryCode ) {
    		case "demo":
    			pageID =  ( _detailCategory == "1" ) ? "4060" : "4063";
    		break;
    		case "de":
    			pageID =  ( _detailCategory == "1" ) ? "4094" : "4095";
    		break;
    		case "en":
    			pageID =  ( _detailCategory == "1" ) ? "4103" : "4012";
    		break;
    		case "fr":
    			pageID =  ( _detailCategory == "1" ) ? "4105" : "4104";
    		break;
    		case "ru":
    			pageID =  ( _detailCategory == "1" ) ? "4136" : "4135";
    		break;
    	}
    	
    	pageID = _pageID;
    	
    	var foo = getPageNameByPageId ( pageID );
    	var newTitle = foo.replace(/ü/g, "ue");
    	var newTitle2 = newTitle.replace(/-2012/g, "");
    	
    	// var newTitle = "newTitle";
    	
    	self.mainUrl = "http://www.strellson.com/" + countryCode + "/" + pageID + "-" + newTitle2 + "/";
    	
    	document.getElementById ( "likeBoxContainer" ).innerHTML = "<fb:like href=\"" + self.mainUrl + _detailCategory + "/" + _detailItem + "\" layout=\"button_count\" show_faces=\"true\" width=\"120\" font=\"lucida grande\" colorscheme=\"dark\" fb_source=\"profile\" ref=\"collectionItemID_" + _detailItem + "\"></fb:like>";
    	
    	renderTimeoutID = setTimeout ( function (  ) {
    		FB.XFBML.parse ( document.getElementById ( "likeBoxContainer" ), function() {
				// alert('I rendered');
			} );
    	}, 500 );
    	
    	// alert ( document.getElementById ( "likeBoxContainer" ).innerHTML );
    	
    	self.detailCategory = _detailCategory;
    	self.detailItem 	= _detailItem;
    	
    	return false;
    },
    getUserAccessTokenObject : function (  ) {
    	var self = this;
    	
		$.ajax({
			url: 'https://graph.facebook.com/oauth/authorize?client_id=144284795598473&redirect_uri=https://graph.facebook.com/oauth/authorize&type=user_agent&display=popup',
			dataType : "jsonp",
			success: function ( data ) {
				for ( var i in data ) {
					for ( var ii in  data[i] ) {
						alert ( ii + " - " +  data[i][ii] )
					}
				}
			}
		});

    	return false;
    },
    userIsPageFanStatus : function ( _uID, _pageID ) {
    	var self = this;
    	
		$.ajax({
			url: 'https://api.facebook.com/method/pages.isFan?page_id=22764457378&uid=682154202&access_token=2227470867%7C2.dhkEi617XYo4kirkGCrAcQ__.3600.1281103200-100000582603126%7CfkVaX1q2cxpXKE8zQJRp33xf_fA.&format=json',
			dataType : "jsonp",
			success: function ( data ) {
				// alert('Load was performed.' + data);
			}
		});

    	return false;
    },
    callback_userLikedCollectionItem : function (  ) {
    	var self = this;
    	
		$.ajax({
			url: 'http://www.strellson.com/scripts/gateway.php?mode=insertLikeAction',
			success: function ( data ) {
				
			}
		});

    	return false;
    },
    callback_userIsPageFanStatus : function ( _params ) {
    	alert ( _params );
    }
}

// flash calls
function changeItem ( _detailCategory, _detailItem, _pageID ) {
	if ( _detailCategory == null || _detailItem == null ) return false;
	
	// alert ( _detailCategory + " - " + _detailItem + " - " + _pageID  );
	
	if ( $("#likeBoxContainer").css ( "display" ) == "none" ) $("#likeBoxContainer").css ( "display", "block" );
	
	document.getElementById ( "likeBoxContainer" ).innerHTML = "loading button...";
	
	facebookLikeHandler.changeItem ( _detailCategory, _detailItem, _pageID );
}

