var mPlaylistTargetX = 0;
var mPlaylistX = 0;
var mLocX = 0;
var mLocDir = 0;
var mLocIndex = 0;
var mIsAuthenticated = 0;

function logout() {
	FB.getLoginStatus(function(response) {
		if (response.session) {
			FB.logout(function(response) {
                ui({
                    action : 'smartLogout',
                    form : ''
                });
			});
  		} else {
            ui({
                action : 'smartLogout',
                form : ''
            });
 		}
        document.getElementById('uploadHelper').src = '/facebook-connect.web?action=smartLogout';
	});
}


function moveLocLeft(size) {
	mLocDir = 0;
	mLocX = 960;
	getById('locContainer').scrollLeft = mLocX;
	for(i=0; i<size;i++) {
		if(i == mLocIndex) {
			getById('locHome'+mLocIndex).style.left =  '960px';
			getById('locHome'+mLocIndex).style.top =  '0px';
		} else {
			getById('locHome'+i).style.top =  '-419px';
		}
	}		
	if(mLocIndex <= 0) {
		mLocIndex = size-1;
	} else {
		mLocIndex--;
	}
	getById('locHome'+mLocIndex).style.left = '0px';
	getById('locHome'+mLocIndex).style.top =  '0px';			
}

function moveLocRight(size) {
	mLocDir = 1;
	mLocX = 0;
	getById('locContainer').scrollLeft = mLocX;
	for(i=0; i<size;i++) {
		if(i == mLocIndex) {
			getById('locHome'+mLocIndex).style.left =  '0px';
			getById('locHome'+mLocIndex).style.top =  '0px';
		} else {
			getById('locHome'+i).style.top =  '-419px';
		}
	}	
	mLocIndex = (mLocIndex+1)%size;	
	getById('locHome'+mLocIndex).style.left = '960px';
	getById('locHome'+mLocIndex).style.top =  '0px';			
}

function idleLoc() {
	if (getById('locContainer')) {
		if(mLocDir == 0) {
			if (mLocX > 0) {
				mLocX -= 30;
				if (mLocX < 0)
					mLocX = 0;
			}
		}
		if(mLocDir == 1) {
			if (mLocX < 960) {
				mLocX += 30;
				if (mLocX > 960)
					mLocX = 960;
			}
		}
		getById('locContainer').scrollLeft = mLocX;
	}
}

function movePlaylistLeft() {
	mPlaylistTargetX -= 945;
	if (mPlaylistTargetX < 0)
		mPlaylistTargetX = 0;
}

function movePlaylistRight() {
	w = getGeometry(getById('playlist_main')).w;
	np = Math.floor(w / 945);
	if (np * 945 == w)
		np = np - 1;
	mPlaylistTargetX += 945;
	if (mPlaylistTargetX > 945 * np)
		mPlaylistTargetX = 945 * np;
}

function idlePlaylist() {
	if (getById('playlist_container')) {
		if (mPlaylistX < mPlaylistTargetX) {
			mPlaylistX += 30;
			if (mPlaylistX > mPlaylistTargetX)
				mPlaylistX = mPlaylistTargetX;
		} else if (mPlaylistX > mPlaylistTargetX) {
			mPlaylistX -= 30;
			if (mPlaylistX < mPlaylistTargetX)
				mPlaylistX = mPlaylistTargetX;
		}
		getById('playlist_container').scrollLeft = mPlaylistX;
	}
}

function checkTextarea(event) {
	if (event.keyCode == 13)
		submitLiveComment();
}

function hideCommentLabel() {
	getById('commentLabel').style.display = 'none';
	getById('comment').focus();
}

function showCommentLabel() {
	if (getById('comment').value == '')
		getById('commentLabel').style.display = 'block';
}

function hideAdminCommentLabel() {
	getById('adminCommentLabel').style.display = 'none';
	getById('adminComment').focus();
}

function showAdminCommentLabel() {
	if (getById('adminComment').value == '')
		getById('adminCommentLabel').style.display = 'block';
}

function submitLiveComment() {
	if (mIsAuthenticated == 0) {
		openLoginForm();
	} else {
		ui({
			comment : getById('comment').value,
			action : 'addLiveComment',
			method : 'addLiveComment'
		});
		comment: getById('comment').value = '';
	}
}

function submitAdminComment() {
	if (mIsAuthenticated == 0) {
		openLoginForm();
	} else {
		ui({
			comment : getById('adminComment').value,
			action : 'addAdminComment',
			method : 'addAdminComment'
		});
		comment: getById('adminComment').value = '';
	}
}

function moveDialog(y) {
	//getById('form').style.height = y + 'px';
}

function openLoginForm() {
	ui({
		form : 'login',
		reset : 1
	});
}

function openProfileForm() {
	ui({
		form : 'profile',
		reset : 1
	});
}

function openRegistrationForm() {
	ui({
		form : 'registration',
		reset : 1
	});
}

function closeForm() {
	ui({
		form : ''
	});
}

function submitForm() {
	ui(form2params(document.forms['smartForm']));
}

function ui(params) {
	htmlRequest("/zelig/home-support.web", params);
}

function loadUi() {
    //if(typeof mExtraParams != 'undefined') {
     //   htmlRequest("/zelig/home-support.web", mExtraParams);
    //} else
        htmlRequest("/zelig/home-support.web", {embed:0,form:''});
 }

function refreshAdminMessage() {
	htmlRequest("/zelig/home-support-idle.web", {});
}

function init() {
	setInterval(refreshAdminMessage, 10000);
	setTimeout(loadUi, 0);
	setInterval(idlePlaylist, 13);
	setInterval(idleLoc, 17);
}

setTimeout(init, 0);
