$(document).ready(function() {
	$('#submitSu').click(function() {
		this.disabled = true;
		this.value = 'Please wait...';
	});
	
	rez.init();
});


function playFlv(file) {
	swfobject.removeSWF('video-area');
	var tmp = document.getElementById('video-area');
    if (tmp) { tmp.innerHTML = '<div id="video"></div>'; }

	var flashvars = {
		file: 				file,
        autostart: 			'true',
		stretching: 		'exactfit',
		controlbar: 		'none'
	};
  	var params = {
        allowfullscreen: 	'true',
        allowscriptaccess: 	'always'
    };
    var attributes = {
        id: 				'video-area',
        name: 				'video-area'
    };
	swfobject.embedSWF('/mediaplayer-4.1.60/player.swf', 'video', '265', '186', '7', '#ffffff', flashvars, params, attributes);
}


function playerReady(obj) { player.playerReady(); };
function playerStateListener(state) { playlist.stateListener(state); };

var playlist = {
	stateListener: function(state) {}
}

var player = {
	playerWrapperId: 	 'video-area',
	playerPlaceholderId: 'video',
	playerId: 			 'rezpondaPlayer',
	height: 			  186,
	width: 				  265,
	instance:   		  new Object(),
	isPlaylist: 		  false,
	init: function() {
		this.play('');
	},
	playerReady: function() {
		this.instance = document.getElementById(player.playerId);
		this.instance.addModelListener('STATE', function(state) {});
	},
	playClever: function(index) {
		var item = rez.data[index];
		var conf = sConf[item.sType];
		if (conf.playlist) {
			var playlistUri = 'http://' + subdomainName + '.' + domainName + '/';
			playlistUri += 'rss/2.0/service/' + item.sType + '/?page=' + rez.params.page + '&limit=' + rez.params.rows;
		
			player.play(playlistUri, {
	            item: 			index,
	            playlist: 		'right',
	            playlistsize: 	player.width,
			});
			return;
		}


		switch (item.sType) {
			case 'twelveSecondes':
			case 'blip':
			case 'qik':
			case 'phreadz':
			case 'seesmic':
				player.play(item.mData);
				break;
			case 'youtube':
				player.play(escape(item.link));
				break;
			case 'vimeo':
				player.playVimeo(item);
				break;
			case 'viddler':
				player.playViddler(item);
				break;
			case 'revver':
				player.playRevver(item);
				break;
		/*	case 'seesmic':
				player.playSeesmic(item);
				break; */
			case 'bambuser':
				player.playBambuser(item);
				break;
			case 'howcast':
				player.playHowcast(item);
				break;
			case 'twitter':
				alert('yay! its twitter');
				break;
			default: 
				alert('unknown media type');
		}
	},
	// Common format
	play: function(theFile, optParams) {
		this.remove();
		
		var flashvars = {
        	file: 				theFile,
            autostart: 			'true',
			backcolor:			'0x37C5DD',
			frontcolor:			'0xDFF8FB',
			lightcolor:			'0xDFF8FB',
			screencolor:		'0xDFF8FB',
			stretching: 		'exactfit',
        };
		
		if (optParams) {
			if (optParams.image) flashvars.image = optParams.image;
			if (optParams.item) flashvars.item = optParams.item;
			if (optParams.playlist) {
				flashvars.playlist = optParams.playlist;
				flashvars.repeat = 'list';
				this.isPlaylist = true;
			}
            if (optParams.playlistsize) flashvars.playlistsize = optParams.playlistsize;
			if (optParams.skin) flashvars.skin = optParams.skin;
			if (optParams.controlbar) flashvars.controlbar = optParams.controlbar;
		}
		
        var params = {
            allowfullscreen: 	'true',
            allowscriptaccess: 	'always'
        };
        var attributes = {
            id: 				this.playerId,
            name: 				this.playerId
        };
        swfobject.embedSWF('/mediaplayer-4.1.60/player.swf', this.playerPlaceholderId, this.width, this.height, '9.0.115', false, flashvars, params, attributes);
	},
	// Vimeo
	playVimeo: function(item) {
		this.remove();
		
		var flashvars = {
        	file: 				item.mData,
            autostart: 			'true',
        };
        var params = {
            allowfullscreen: 	'true',
            allowscriptaccess: 	'always'
        };
        var attributes = {
            id: 				this.playerId,
            name: 				this.playerId
        };
		swfobject.embedSWF(item.mData, this.playerPlaceholderId, this.width, this.height -1, '9.0.115', false, flashvars, params, attributes);

	},
	// Seesmic
	playSeesmic: function(item) {
		this.remove();
		
		var flashvars = {
        	video: 				videoId,
            version: 			'threadedplayer',
        };
        var params = {
            allowfullscreen: 	'true',
            allowscriptaccess: 	'always',
			bgcolor:  			'#000000',
			movie: 				'http://seesmic.com/embeds/wrapper.swf'
        };
        var attributes = {
            id: 				this.playerId,
            name: 				this.playerId
        };
		swfobject.embedSWF('http://seesmic.com/embeds/wrapper.swf', this.playerPlaceholderId, this.width, this.height - 1, '9.0.115', false, flashvars, params, attributes);
	},
	//Viddler
	playViddler: function(item) {
		this.remove();

		var flashvars = {};
        var params = {
            allowfullscreen: 	'true',
            allowscriptaccess: 	'always',
			movie: 				item.mData
        };
        var attributes = {
            id: 				this.playerId,
            name: 				this.playerId
        };
		swfobject.embedSWF(item.mData, this.playerPlaceholderId, this.width, this.height - 1, '9.0.115', false, flashvars, params, attributes);
	},
	// Revver
	playRevver: function(item) {
		this.remove();
		
		var flashvars = {
        	file: 				item.mData,
            autostart: 			'true',
        };
        var params = {
            allowfullscreen: 	'true',
            allowscriptaccess: 	'always'
        };
        var attributes = {
            id: 				this.playerId,
            name: 				this.playerId
        };
		swfobject.embedSWF(item.mData, this.playerPlaceholderId, this.width, this.height -1, '9.0.115', false, flashvars, params, attributes);
	},
	// Howcast
	playHowcast: function(item) {
		this.remove();

		var flashvars = {};
        var params = {
            allowfullscreen: 	'true',
            allowscriptaccess: 	'always',
			movie: 				item.mData
        };
        var attributes = {
            id: 				this.playerId,
            name: 				this.playerId
        };
		swfobject.embedSWF(item.mData, this.playerPlaceholderId, this.width, this.height - 1, '9.0.115', false, flashvars, params, attributes);
	},
	// Bambuser
	playBambuser: function(item) {
		this.remove();

		var flashvars = {
			vid: 				item.guid,
			autostart:  		'yes'
		};
        var params = {
            allowfullscreen: 	'true',
            allowscriptaccess: 	'always',
			movie: 				'http://bambuser.com/r/player.swf'
        };
        var attributes = {
            id: 				this.playerId,
            name: 				this.playerId
        };
		swfobject.embedSWF('http://bambuser.com/r/player.swf', this.playerPlaceholderId, this.width, this.height - 1, '9.0.115', false, flashvars, params, attributes);
	},
	remove: function() {
		this.isPlaylist = false;
		swfobject.removeSWF(this.playerId);
        var tmp = document.getElementById(this.playerWrapperId);
        if (tmp) { tmp.innerHTML = '<div id=' + this.playerPlaceholderId + '></div>'; }
		scroll(0, 0);
	},
};

var rez = {
	loggedIn: false,
	data: new Object(),
	params: { page: 0, rows: 10 },
	init: function() {
		var flashvars = {
			dataFile: 			'http://me.dm/flash/mono/rezpondr.xml',
	       	kenBurnsMode: 		'random',
	       	showVersionInfo: 	'false',
	       	showLogo: 			'false',
	       	showControls: 		'false',
	       	showImageInfo: 		'never'
		};
	 	var params = {
	     	allowfullscreen: 	'true',
	     	allowscriptaccess: 	'always',
			movie: 				'http://me.dm/flash/mono/dm.swf'
	 	};
	 	var attributes = {
	     	id: 				'flash-area',
	     	name: 				'flash-area'
	 	};
		swfobject.embedSWF('http://me.dm/flash/mono/dm.swf', 'flash-area', '599', '292', '7', '#ffffff', flashvars, params, attributes);

		
		this.load();
		rez.timer();
	},
	timer: function() {
		setTimeout(function() {
			rez.load();
			rez.timer();
		}, 30000);
	},
/*	checkState: function(force) {
		var force = force == true ? 'force=true' : '';
		var params = '';
		for (var type in rez.services) {
			params += '&params[' + type + '][limit]=' + rez.services[type].rows;
			params += '&params[' + type + '][page]=' + rez.services[type].page;
			params += '&params[' + type + '][service]=' + rez.services[type].service;
		}
		
		$.ajax({
	        type:   	'get',
	        url:    	'/services/state?' + force,
			dataType: 	'json',
			data: 		params,
	        success: 	function(response) {
				if (!checkServerResponse(response)) { return; }
				for (var type in response.result.services) {
					if (response.result.services[type].items) {
						rez.customRowBuild(type, { type: 'init' }, response.result.services[type]);
						rez.data[type] = response.result.services[type].items;
					}
				}
				
				if (response.result.streamPlayer) {
					livestream = response.result.streamPlayer;
					player.init();
				}
			}
		});
	},*/
	load: function(argObj) {
		if (argObj) {
			if (argObj.type == 'forward') {
				rez.params.page++;
			} else if (argObj.type == 'backward') {
				if (rez.params.page != 0) rez.params.page--;
				else return;
			} else if (argObj.rows == 'more') {
				rez.params.rows += 10;
			} else if (argObj.rows == 'less') {
				if (rez.params.rows != 10) rez.params.rows -= 10;
				else return;
			}
		}

		var params = '&limit=' + rez.params.rows;
		params += '&page=' + rez.params.page;
		
		$.ajax({
	        type:   	'get',
	        url:    	'/index/items',
			dataType: 	'json',
			data:   	params,
	        success: 	function(response) {
				rez.build(response.result.items);
			}
		});
	},
	build: function(data) {
		rez.data = data;
		$('#line-divs').empty();
		$.each(data, function (i, e) {
			var div = $('<div class="line-div" />').appendTo('#line-divs');
			var img = $('<img class="rowThumbnail"/>').appendTo(div).attr({
				src: e.pData,
				title: e.title,
				alt: e.title
			}).click(function() {
				player.playClever(i);
			});
		});
	}
};


/*	build: function(data) {
		$.each(data, function(i, m) {
			rez.rowBuildVideo('audioboo', m, function() {
				rez.services.audioboo.play(i);
			});
		});	
	},
	play: function(index) {
		var item = rez.data.audioboo[index];
		
		if (this.service == 0) {
			var playlistUri = 'http://' + subdomainName + '.' + domainName + '/rss/2.0/service/audioboo?page=' + this.page + '&limit=' + this.rows;
		} else {
			var playlistUri = 'http://' + subdomainName + '.' + domainName + '/rss/2.0/service/' + this.service;
		}
		
		player.play(playlistUri, {
        //    image: 			item.pData,
            item: 			index,
            playlist: 		'right',
            playlistsize: 	(player.width - 320)
		});
	}
};*/