var META;
(function($) {
	META = {
		setup: {
			player:function() {
				META.hash.get();
				META.setup.iframeChecker();
				PLAYER.embed();
			},
			iframeChecker:function() {
				var tryElse = true
				if(window.top != window) { 
					try {
						var up = window.top.location + '', here = window.location + '';
						if(up.indexOf(SETTINGS.external) != -1) {
							window.top.LIST.update(SETTINGS.query);
							window.location = (here.indexOf(SETTINGS.external+'player/') != -1) ? SETTINGS.referer : SETTINGS.fbapp.replace(/\?[^\/]*?$/,'')+'home/';
						}
					} catch(err) {
						if(tryElse) window.top.location = SETTINGS.external;
					}
				};
			},
			cookieChecker:function() {
				if(!$.cookie('isInstalled')) {
					$.cookie('poster', SETTINGS.poster, {expires:100, path:'/'});
					$.cookie('query', SETTINGS.query, {expires:100, path:'/'});
					$('#introduction').css('display', 'block'); 
					META.metric('player_intro_show');
					LIST.setup();
				} else if($.cookie('poster')) {
					OUTSIDE.login(function(){
						OUTSIDE.getUser($.cookie('poster'), function(user) { 
							LIST.update( $.extend(user, {query:$.cookie('query'), fbid:user.uid}) );

							$.cookie('poster', null);
							$.cookie('query', null);
						});
					});
				} else {
					LIST.setup();
				};
				$.cookie('isInstalled', true, {expires:100, path:'/'});
			},
			onFbLogin: function() {
				$('iframe').css('height', parseFloat($(window).height()) );
				setTimeout(function() {
					//CK commenting out below line to test plugin bug, where redirecting fails in FF
					//console.log( PLAYER.isLoggedIntoFb );
					if( PLAYER.isLoggedIntoFb == 'unknown' ) {
						META.setup.onFbLogin();
					} else {
						PLAYER.setup();
						META.setup.cookieChecker();
					}
				}, 1000);
			}
		},
		metric:function(name, data) {
			$.post('/metrics/log/'+name+'/', data);
		},
		hash:{
			get:function() {
				$(function() {
					var here = window.location.href+'';
					var hash = window.location.hash+'';
					$('iframe#bkg').attr('src', (hash.indexOf('/') != -1) && (here.replace(/\?.*$/,'').replace(/\#.*$/,'') == SETTINGS.external) ? SETTINGS.fbapp.replace(/\?.*$/,'')+hash.replace(/^\#\//,'')+'?'+SETTINGS.fbapp.replace(/^.*\?/,'') : SETTINGS.referer );
				});
			},
			set:function() {
				$(function() {
					var here = window.location.href+'';
					if( ( (PLAYER.location.href != PLAYER.SETTINGS.external) || (here.indexOf(PLAYER.SETTINGS.external+'home/') == -1) ) ) {
						if(here.indexOf(PLAYER.SETTINGS.external+'home/') != -1)  {
							PLAYER.location.hash = '/';
						} else {
							PLAYER.location.hash = '/'+here.replace(/\//g, '%2F').replace(PLAYER.SETTINGS.external.replace(/\//g, '%2F'), '').replace(/\%2F/g,'/').replace(/\?.*$/,'');
						}
					}
					PLAYER.META.hash.current = PLAYER.location.hash;
					META.hash.check();
				});
			},
			check:function() {
				META.hash.interval = setInterval(function() {
					if(PLAYER.location.hash != PLAYER.META.hash.current) {
						var hash = PLAYER.location.hash;
						PLAYER.history.back(2);
						if(hash.indexOf('/') != -1) {
							var rep = PLAYER.SETTINGS.external.replace(/\?.*$/,'')+hash.replace(/^\#\//,'')+'?'+PLAYER.SETTINGS.external.replace(/^.*?(\?|$)/,'');
							window.location.replace( rep );
						} else if(hash == '') {
							alert('check');
							window.location.replace( PLAYER.SETTINGS.external+'home/' );
						}
						clearInterval(META.hash.interval);
					}
				}, 100)
			}
		},
		cache:{
			add:function(which) {
				if(LIST.batch && (LIST.batch.length > 0)) {
					META.cache[which] = LIST.batch.slice(LIST.now.index);
				};
			},
			recover:function(which, ifNoCache) {
				var cache = PLAYER.META.cache[which];
				if(cache && (cache.length > 1)) {
					if(PLAYER.LIST.now == cache[0]) {cache = cache.slice(1)};
					PLAYER.LIST.update(cache, 1)
				} else {
					ifNoCache()
				}
			},
			remove:function(which) {
				META.cache[which] = false;
			}
		},
		playlistInvitation:function() {
			//OUTSIDE.prompt_permission('publish_stream', function(result){
			//	var hasPermission = 0;
			//	if(result == 'publish_stream') {
			//		$.post("/metrics/log/publish_stream_accept/");
			//	} else if(result == true) {
			//		//do nothing
			//		hasPermission = 0;
			//	} else {
			//		$.post("/metrics/log/publish_stream_reject/");
			//	}
			/*
				if(!$.cookie('hasSharedPlaylist')) {
					$.getJSON("/data/feed/"+ window.BKG.USER.uid +"/", function(data) {
						if(data.feed != '') {
							$.post("/metrics/log/playlist_share_has_music/");
							var songs = data.feed, type = 'mine';
						} else {
							$.post("/metrics/log/playlist_share_no_music/");
							var songs = [], type = 'theirs';
							for(i=0;i<7;i++) {
								if(LIST.batch[i]) {
									songs[i] = LIST.batch[i];
								}
							}
						}
						$.cookie('hasSharedPlaylist', true, {expires:100, path:'/'});
						OUTSIDE.share_playlist(songs, type);
					});		
				}
			*/
			//});
		},
		onReady:function(which, fun) {
			var topmost = window.top.top.top.top;
			if(topmost[which.toUpperCase()] && fun) {
				fun()
			} else {
				if(fun)  {
					META.onReady[which] = fun;
				} else {
					META.onReady[which] && META.onReady[which].apply(window);
				};
			}
	    }
	};

	//helpers
	$.fn.stripe = function() {
		return this.removeClass('alt').each(function(i){
			i%2==0 && $(this).addClass('alt');
			$(this).data('index', i);
		});
	};
})(jQuery);
