/* Init Basics */

$.extend($.blockUI.defaults.overlayCSS, { backgroundColor: '#666' });
//$.blockUI.defaults = ( css : {top: '1px'});


var loginIsShowing = 0;
var languageIsShowing = 0;

$(document).ready(function(){ 

	//loginLinkSRC = $(".loginLink IMG").attr("src");
	loginLinkSRC = "fileadmin/unusuals/images/basics/rollover_login_lo.png";
	
	languagesLinkSRC = $("#languagesLink IMG").attr("src");
	
	logoutLinkSRC = $(".logoutLink IMG").attr("src");
	
	//alert(logoutLinkSRC);
	
	
	
	/* Activate Loginlink */
	$(".loginLink").click( function() {
		
		if($("#loginBox_statusHeader").css("display") == "block"){
			$("#loginBox_statusHeader").hide();
			return;
		}
		
		if($("#loginBox").css("display") == "none"){
			$("#languagesBox").hide();
			$("#loginBox_statusHeader").hide();
			$("#loginBox_forgotPassword").hide();
			$("#loginBox").show();

			
		} else {
			$("#loginBox").hide();
			
		}
	});

	
	/* Activate Languageslink */
	$("#languagesLink").click( function() {
		if($("#languagesBox").css("display") == "none"){
						
			$("#loginBox").hide();
			$("#loginBox_statusHeader").hide();
			$("#loginBox_forgotPassword").hide();
			$("#languagesBox").show();


		} else {
  			$("#languagesBox").hide();
		}	
	});
	
	
	
	/* Rollover logout */
	$(".logoutLink").mouseover( function() {
		$(".logoutLink IMG").attr("src","fileadmin/unusuals/images/basics/rollover_logout.png");
	});
	
	/* Rollout languagelink */
	$(".logoutLink").mouseout( function() {
		$(".logoutLink IMG").attr("src",logoutLinkSRC);
	});



	
	$("BUTTON").mouseover(function() {
		$(this).children("DIV.button").addClass("buttonRollover");
		$(this).children("DIV.button").children("P.button").addClass("buttonRollover");
	});
		
	$("BUTTON").mouseout(function() {
		$(this).children("DIV.button").removeClass("buttonRollover");
		$(this).children("DIV.button").children("P.button").removeClass("buttonRollover");
	});
		
	$("BUTTON").click(function() {
		$(this).children("DIV.button").removeClass("buttonRollover");
		$(this).children("DIV.button").children("P.button").removeClass("buttonRollover");
	});

});




var filesForDelete = new Array();

function changeDisplay(id,displayStyle) {

	if (document.getElementById(id).style.display == 'none') {
		document.getElementById(id).style.display = displayStyle;

	} else {
		document.getElementById(id).style.display = 'none';
	}
}


function showNewFile($uid){
	alert('Here: ' + $uid);
}

function deleteFileOLD(id,existingFileID,target){
	//alert(target)
	if(existingFileID){

		for(i=0;i<filesForDelete.length;++i) {
			if(filesForDelete[i] == id){
				//alert(existingFileID);
				filesForDelete.splice(i, 1);
				return;
			}
		}
		filesForDelete.push(id);
	} else {
		if(document.getElementById('file_' + id + '_file_comment')){
			var target = document.getElementById('file_' + id + '_file_comment');
		} else {
			if(document.getElementById(target)){
				var target = document.getElementById(target);
			} else {
				var target = document.getElementById('allFiles');
			}
		//var target = document.getElementById(target);
		}
		//target = document.getElementById('allFiles');
		//var target = document.getElementById(target);
		//alert(id)
		
		var Knoten = document.getElementById(id);
		target.removeChild(Knoten);
	}
	alert(filesForDelete);
	//alert(document.getElementById('filesForDelete2.bla').value);
	//alert(document.getElementById('tx-ffunusuals-projects_form').filesForDelete2['bla']);
}


function setFilesForDelete() {
//alert(filesForDelete);

	if (document.getElementById('tx-ffunusuals-projects_form').filesForDelete) {
		document.getElementById('tx-ffunusuals-projects_form').filesForDelete.value = filesForDelete.join(",");
	}
return false;	
	
}

/* Auto Complete Functions */
var fadeInSuggestion = function(suggestionBox, suggestionIframe) 
{
	$(suggestionBox).fadeTo(300,0.8);
};
var fadeOutSuggestion = function(suggestionBox, suggestionIframe) 
{
	$(suggestionBox).fadeTo(300,0);
};

function initAutocompleteIOLD(id){
	$('#' + id).Autocomplete(
			{

				source: 'index.php?eID=ff_unusuals',
				delay: 400,
				fx: {
					type: 'slide',
					duration: 400
				},
				autofill: false,
				helperClass: 'autocompleter',
				selectClass: 'selectAutocompleter',
				minchars: 2,
				onShow : fadeInSuggestion,
				onHide : fadeOutSuggestion
			}
		);
}

function formatItem(row) {
		//var row = row.split("|");
			//return row[0] + " (id: " + row[1] + ")";
	return row[1];
}

function formatResult(row) {
	return row[0];
}
		
function initAutocomplete(id, usergroup, width){

	if(! usergroup){
		usergroup = $('INPUT#tx_ffunusuals_search_usergroup').val();
	}
	
	if(! width){
		width = 190;
	}

	$('#' + id).autocomplete('index.php?eID=ff_unusuals&c=' + id + '&usergroup=' + usergroup, {

			delay: 150,
			width: width,
			minChars: 1,

			formatItem: formatItem,
			formatResult: formatResult,
			selectFirst: false,
			type: 'search',
			cacheLength: 1,
			max: 1000
	});
	
	
	jQuery('#' + id).result(function(event, data, formatted) {
		jQuery("#result").html( !data ? "No match!" : "Selected: " + formatted);
	});
}	



function showSearchInProjectfieldResult(field, entryList){
	var targetDiv = '#autocompleteHelper > UL';
	var outputList = new Array();
	
	if(entryList.length > 0){		
		for(var i = 0; i < entryList.length; i++) {
			//self.listedProfessions.push([professionsList[n].id, professionsList[n].name]);
			//outputList[i] = '<li class="selectAutocompleter"><a href="#" class="professionsItemLink" title="Select &quot;' + entryList.id + '&quot;"><p id="profession_' + entryList.name + '">' + entryList[i].name + '</p></a></li>';
			outputList[i] = '<li class="selectAutocompleter" dir="' + i + '" rel="' + entryList[i].name +'"><strong>' + entryList[i].name + '</li></strong>';

  		}
	}
	//alert(outputList.join(''));
	
	position = jQuery.iUtil.getPosition(field);
	alert(position.y);
	
	$(targetDiv).empty();
	$(targetDiv).append(outputList.join(''));
		
	$('#autocompleteHelper').show();
}


function showMessageBox(){
	if($("#messageBox").css("display") == "none"){
		$("#messageBox").slideDown('fast');
	}
}


function openProfileWindow(id,selectedProjects, projectID, showArchive){

	var addParam = '';
	var addParam2 = '';
	var addParam3 = '';

	if(selectedProjects){
		addParam = '&tx_ffunusuals_profile[selectedProjects]=' + selectedProjects;
	}
	
	// only for My Archive Search
	if(projectID){
		addParam2 = '&tx_ffunusuals_profile[projectID]=' + projectID;
	}
	
	if(showArchive){
		addParam3 = '&tx_ffunusuals_profile[showArchive]=1';
	}
	
	
	
	profileWindow = window.open('/index.php?id=36&tx_ffunusuals_profile[uid]=' + id + addParam + addParam2 + addParam3, 'profile', 'resizable=no, toolbar=no, scrollbars=yes, width=1020, height=750, screenX = 0, screenY = 0');

	profileWindow.focus();
}


function openEcardWindow(type,projects_id, name){

	if(type == 'sendProfile'){
		var params = sendEcardsPID + '&tx_ffunusuals_ecards[type]=' + type + '&tx_ffunusuals_ecards[toName]=' + name;	
	} else {
		var params = sendEcardsPID + '&tx_ffunusuals_ecards[type]=' + type + '&tx_ffunusuals_ecards[projects_id]=' + projects_id + '&tx_ffunusuals_ecards[toName]=' + name;
	}

	ecardWindow = window.open('index.php?id=' + params, 'ecard', 'resizable=no, toolbar=no, scrollbars=yes, width=780, height=680, screenX = 0, screenY = 0');

	ecardWindow.focus();
}

function openTrailerWindow(trailerFileName){

	var width = 1024;
	var height = 694;

	trailerWindow = window.open('/fileadmin/unusuals/movies_quicktime/' + trailerFileName, 'trailer', 'resizable=no, toolbar=no, scrollbars=no, width=' + width + ', height=' + height +', screenX = 0, screenY = 0');

	trailerWindow.focus();
}

function openProfileWindowWithURL(url){
	
	profileWindow = window.open(url, 'profile', 'resizable=no, toolbar=no, scrollbars=yes, width=1020, height=750, screenX = 0, screenY = 0');

	profileWindow.focus();
	
}



function updateRegions(targetSelectBox,regionsList){
	//alert(regionsList);
	//alert(selectBox)
	//if (!regionsList) return; 
  	
  	//var targetSelectBox = "#" + selectBox;
  	//alert($(targetSelectBox).attr("class"));
  	
  	//$(targetSelectBox).clearFields();
  	//$(targetSelectBox).removeOption(/./);
  	//alert(regionsList.length);
  	
  	if(regionsList.length > 0){
  		$(targetSelectBox).addOption('--', 'Please choose');
  		for(var n = 0; n < regionsList.length; n++) {
  			$(targetSelectBox).addOption(regionsList[n].id, regionsList[n].name);
  		}
  		$(targetSelectBox).selectOptions("--", true);
  		$(targetSelectBox).removeAttr("disabled");
  		
  	} else {
  		$(targetSelectBox).addOption('--', 'No selection possible');
  	}
  	//$(targetSelectBox).addOption(regionsList, false);
  	
  	
  	//$(targetSelectBox).parent().children(".spinner").hide();
  	$(targetSelectBox).siblings("DIV.outerSpinner").children(".spinner").css({display: 'none'});
  	
  	//$(selectbox).parent().parent().children().children("SELECT.tx-ffunusuals-select-region").removeAttr("disabled");
  	
  	//$(this).parent().parent().children().children("SELECT.tx-ffunusuals-select-region").removeAttr("disabled");
  	//$(this).parent().parent().children().children(".spinner").hide();
}



function changeImage(imgID, imgPath, height, width){
	//alert(imgPath);
	//$('#' + imgID).parent().html('<img src="' + imgPath + '" id="' + imgID + '" height="' + height + '"/>');
	//$(this).html('huhu');
	
	//$("#" + imgID).hide();
	//$("#" + imgID).attr({ src: '' });
	//$("#" + imgID).attr({ height: height, width: width });
	$("#" + imgID).attr({ src: imgPath });
	
	//$("#" + imgID).show();
}



//var messageElement = '';

// Membership Popups
$(document).ready(function(){ 

	var messageElement = $('#showreelFull');
	$.extend($.blockUI.defaults.pageMessageCSS, { width: '800px', height: '800', top:'10%', left:'20%', background: 'transparent', border: '0' });

	$(".membershipPopup").click( function() {
		hideUNUSUALSPlayer();
  		$.blockUI(messageElement);
	});


	var messageElementArchive = $('#archiveFull');
	//$.extend($.blockUI.defaults.pageMessageCSS, { width: '800px', height: '800', top:'10%', left:'20%', background: 'transparent', border: '0' });

	$(".membershipPopupArchive").click( function() {
		hideUNUSUALSPlayer();
  		$.blockUI(messageElementArchive);
	});


	var messageElementSearch = $('#search');
	$.extend($.blockUI.defaults.pageMessageCSS, { width: '800px', height: '800', top:'10%', left:'20%', background: 'transparent', border: '0' });

	$(".membershipPopupSearch").click( function() {
		hideUNUSUALSPlayer();
  		$.blockUI(messageElementSearch);
	});


	var messageElementMain = $('#mainPopup');
	$.extend($.blockUI.defaults.pageMessageCSS, { width: '800px', height: '800', top:'10%', left:'20%', background: 'transparent', border: '0' });

	$(".mainPopup").click( function() {
		hideUNUSUALSPlayer();
  		$.blockUI(messageElementMain);
	});
	

});


function showMembershipPopup(){

	var messageElementMain = $('#mainPopup');
	$.extend($.blockUI.defaults.pageMessageCSS, { width: '800px', height: '800', top:'10%', left:'20%', background: 'transparent', border: '0' });

	hideUNUSUALSPlayer();
  	$.blockUI(messageElementMain);

}







function gotoLogin(uid){

	//loginFromHome = 1;
	
	if(opener && opener.loginFromHome == 1){
		window.location.href = '/show_profile/login.html?redirect=' + uid + '&showProfile=1';
	} else {
		profileWindow = window.open('/show_profile/login.html?redirect=' + uid + '&showProfile=1', 'profile', 'resizable=no, toolbar=no, scrollbars=yes, width=1020, height=750, screenX = 0, screenY = 0');
		profileWindow.focus();
		
	}
	
	
	//location.href = 'profile/login.html?redirect=' + uid;
	//return false;
	
	//profileWindow.focus();
	
	return false;
}

function gotoJoinNow(){
	location.href = 'metanavigation/sign_up.html';
	//window.close();
}



function showLoginWarning(uid, onlyLoginWarning){
	//alert(uid);
	
	//var loginWarning = $('#loginWarning');
	
	//hideUNUSUALSPlayer();
	
	var i = 0;
	var dialogOutput = new Array();
		
	dialogOutput[i++] = '<div id="deleteDialog" class="dialog" style="display:block; cursor: default">';
	dialogOutput[i++] = '<div class="boxLeftTop"></div><div class="boxRightTop"></div><div class="boxTop"></div>';
	dialogOutput[i++] = '<div class="clear">&#160;</div>';
	dialogOutput[i++] = '<div class="boxRightBorder">';
	dialogOutput[i++] = '<div class="boxContent">';
	dialogOutput[i++] = '<h2 id="question" class="darkgray">' + str.question + '</h2>';
	dialogOutput[i++] = '<button type="button" onclick="gotoLogin(' + uid +');"><div class="button"><p class="button">' + str.login + '</p></div></button>&#160;&#160;';
	dialogOutput[i++] = '<button type="button" onclick="gotoJoinNow();"><div class="button"><p class="button">' + str.joinnow + '</p></div></button>&#160;&#160;';
	dialogOutput[i++] = '<button type="button" onclick="unBlockItHome();"><div class="button"><p class="button">' + str.cancel + '</p></div></button>';
	dialogOutput[i++] = '</div>';
	dialogOutput[i++] = '</div>';
	dialogOutput[i++] = '<div class="boxLeftBottom"></div><div class="boxRightBottom"></div><div class="boxBottom"></div>';
	dialogOutput[i++] = '</div>';
				
	//$.blockUI.defaults.pageMessage = dialogOutput.join('');
	//$.blockUI(); 


	$("DIV#loginWarningContent").html(dialogOutput.join(''));

	if(onlyLoginWarning == 'si'){
		//$.blockUI.defaults.pageMessage = dialogOutput.join('');
		//$.blockUI();
		$('DIV#resultWindow').show();
	}
	
	$('DIV#loginWarningContent').show();

	return false;
}




function unBlockItHome(){

	$('DIV#loginWarningContent').hide();
	
	unBlockIt();
	
}

