// align the image to solve in the box
function alignImage() {

    var movieWidth = document.getElementById('shot_still').offsetWidth;
    var movieHeight = document.getElementById('shot_still').offsetHeight;
    var movieHeight = movieHeight / -2;

    document.getElementById('shot_title_bg').style.width = movieWidth + 'px';

    var abstand = 720 - movieWidth;
    abstand = abstand / 2;
    document.getElementById('shot_title_bg').style.marginLeft = abstand + 'px';
    document.getElementById('shot_title_container').style.marginLeft = abstand + 'px';
    document.getElementById('shot_title_container').style.width = movieWidth + 'px';

}

function alignImage600() {

    var movieWidth = document.getElementById('shot_still').offsetWidth;
    var movieHeight = document.getElementById('shot_still').offsetHeight;
    var movieHeight = movieHeight / -2;

    document.getElementById('shot_title_bg').style.width = movieWidth + 'px';

    var abstand = 600 - movieWidth;
    abstand = abstand / 2;
    document.getElementById('shot_title_bg').style.marginLeft = abstand + 'px';
    document.getElementById('shot_title_container').style.marginLeft = abstand + 'px';
    document.getElementById('shot_title_container').style.width = movieWidth + 'px';

}


function checkEnter(e){
    var characterCode
    if (e && e.which) {
        e = e
        characterCode = e.which
    } else {
        e = event
        characterCode = e.keyCode
    }
    if (characterCode == 13) {
        document.forms[0].submit()
        return false
    } else {
        return true
    }
}


function toggleunsolved() {
	var mydiv = document.getElementById(id);
    mydiv.style.display = (mydiv.style.display == 'block' ? 'none': 'block');
	
}

function togglebox(id) {
	$(id).toggle();
}

function toggleinline(id) {
    if (document.getElementById) {
        var mydiv = document.getElementById(id);
        mydiv.style.display = (mydiv.style.display == 'inline' ? 'none': 'inline');
    }
}


function slidedown() {
    //if we are already moving down, dont bother
    if (typeof(menuMovement) != 'undefined' && menuMovement == 'down')
    return;
    //if theres an up transition, cancel it
    if (typeof(menuTransition) != 'undefined')
    menuTransition.cancel();
    //find current position and determine duration to match our targetspeed of 230px/s
    var currentPosition = parseInt($('slidedown_menu').style.top)
    var duration = ((Math.abs(currentPosition)) / 115) * 0.5
    var menuTransition = new Effect.Move('slidedown_menu', {
        x: 0,
        y: 0,
        mode: 'absolute',
        duration: duration
    });
    //	menuTransition = new Effect.Move('slidedown_menu', { x: 0, y: 0, mode: 'absolute', duration: duration, transition: Effect.Transitions.Bounce});
    //set new movement state
    var menuMovement = "down"
}

var menuStatus = "up";
function slidemenu() {
	if( menuStatus == "up") {
    	new Effect.Morph('slidedown_menu', {
		  style: 'top:25px;',
		  duration: 0.4
		});
		menuStatus = "down";
		$('slidedown_menu_label').innerHTML = "close menu";
	}
	else {
		new Effect.Morph('slidedown_menu', {
		  style: 'top:-80px;',
		  duration: 0.4
		});
		menuStatus = "up";
		$('slidedown_menu_label').innerHTML = "choose category";
	}
}


function slidelogin() {
    new Effect.toggle('slidedown_login', 'appear', {
	  duration: 0
	});
}


function slidepages() {
    new Effect.toggle('slidedown_pages', 'appear', {
	  duration: 0
	});
}


showUnsolved = readCookie('unsolvedNav'); 


document.observe('dom:loaded', function(){	
	
	if($('nav_shots')) {
		
		if(showUnsolved == 'true' && $('show_unsolved')) {
			$('show_unsolved').checked = "checked";
			$('prev_unsolved_shot').style.display = "block";
			$('next_unsolved_shot').style.display = "block";
			$('prev_shot').style.display = "none";
			$('next_shot').style.display = "none";
		}
		else {
			$('prev_unsolved_shot').style.display = "none";
			$('next_unsolved_shot').style.display = "none";
			$('prev_shot').style.display = "block";
			$('next_shot').style.display = "block";
		}
		
	}
	
	
	
});

if(!showUnsolved) {
	showUnsolved= 'false';
}

function toggleUnsolvedNav() {
	if(showUnsolved == 'false') {
		$('show_unsolved').checked = "checked";
		$('prev_unsolved_shot').style.display = "block";
		$('next_unsolved_shot').style.display = "block";
		$('prev_shot').style.display = "none";
		$('next_shot').style.display = "none";
		showUnsolved = 'true';	
		createCookie('unsolvedNav', 'true', '365');
	}
	else {
		$('prev_unsolved_shot').style.display = "none";
		$('next_unsolved_shot').style.display = "none";
		$('prev_shot').style.display = "block";
		$('next_shot').style.display = "block";
		showUnsolved = 'false';
		createCookie('unsolvedNav', 'false', '365');
	}
}


menuIsHover = readCookie('hoverMenu'); 
if(!menuIsHover)
	menuIsHover= 'true';

function toggleHoverMenu() {
	if(menuIsHover == 'true') {
		$('toggleHoverMenu').update('activate hover-menu');
		menuIsHover = 'false';	
		createCookie('hoverMenu', 'false', '365');
	}
	else {
		$('toggleHoverMenu').update('deactivate hover-menu');
		menuIsHover = 'true';
		createCookie('hoverMenu', 'true', '365');
	}
}



languageList = [
['English', 'en'],
['German', 'de'],
['French', 'fr'],
['Spanish', 'es'],
['Portuguese', 'pt'],
['Armenian', 'am'],
['Bulgarian', 'bg'],
['Chinese', 'cn'],
['Czech', 'cz'],
['Dutch', 'nl'],
['Danish', 'dk'],
['Estonian', 'ee'],
['Finnish', 'fi'],
['Greek', 'gr'],
['Hebrew', 'il'],
['Hungarian', 'hu'],
['Icelandic', 'is'],
['Indian', 'in'],
['Indonesian', 'id'],
['Italian', 'it'],
['Japanese', 'jp'],
['Korean', 'kr'],
['Lithuanian', 'lt'],
['Latvian', 'lv'],
['Norwegian', 'no'],
['Persian', 'ir'],
['Polish', 'pl'],
['Romanian', 'ro'],
['Russian', 'ru'],
['Swedish', 'se'],
['Serbian', 'rs'],
['Thai', 'th'],
['Turkish', 'tr'],
['Ukrainian', 'ua']
];

function addAltName(id) {
    myDiv = new Element('li', {
        'id': "alt_movie[movie_names_attributes][" + id + "]",
		'class': "clearfix"
    });
    myDiv.appendChild(new Element('label', {
        'for': "movie_movie_names_attributes_" + id + "_name"
    }).update('Alternative title:'));
    myDiv.appendChild(new Element('input', {
        id: "movie_movie_names_attributes_" + id + "_name",
        name: "movie[movie_names_attributes][" + id + "][name]",
        size: "30",
        type: "text"
    }));
    langSelector = myDiv.appendChild(new Element('select', {
        name: "movie[movie_names_attributes][" + id + "][language]"
    }));
    languageList.each(function(lang) {
        langSelector.appendChild(new Element('option', {
            value: lang[1]
        }).update(lang[0]));
    });
    myDiv.appendChild(new Element('a', {
        'class': "delete_altname_link",
        onclick: "$('alt_movie[movie_names_attributes][" + (id) + "]').remove();"
    }).update(''));
    $('altnames').appendChild(myDiv)
    $('add_altname_link').onclick = function() {
        addAltName(id + 1);
    }
}

function setUploadId(id, name) { 
	$('shot_movie_id').value = id;
	if($('shot_upload_type').value == 'character'){
		$('choose_character_headline').show();
		new Ajax.Request('/movie/'+id+'/characterlist.js', {
        	asynchronous:true, evalScripts:true
      	});
	}
	else{
		$('upload_form').submit();
	}
}

function setCharacterId(id){
	$('shot_character_id').value = id;
	$('upload_form').submit();
}

function changeUploadTab(i){
	upload_tabs_allowed.push(i);
	upload_tabs_allowed = upload_tabs_allowed.uniq();
	$(i).show();
	upload_tabs.setActiveTab(i);
}

function resetUploadTabs(){
	upload_tabs_allowed=['choose'];
}

function showMovieChoser() {
   /* Effect.BlindDown($('movie_chooser'));
    $('shot_details').hide();
    $('movie_chosen').hide();*/
}


function uploadCaptchaSuccess(){
	$('upload_form').submit();
}




document.observe('calendar:dayselected', function(arg){
	d = new Date(arg.memo.date)
	datepart = d.getFullYear() + "/" + (d.getMonth()+1) +"/"+ d.getDate();
	location.href = "/overview/"+datepart
});

function initialOverviewDisplayOptions(){
	options = null;
	if(readCookie('OverviewDisplayOptions')){
		options = readCookie('OverviewDisplayOptions').evalJSON();

	}
	if(!options || !options.solved || !options.unsolved ||!options.posted){
		options = {solved: 'show', unsolved: "show", posted: "show"}

	}

	$('overview_filter_solved_'+options.solved).writeAttribute("checked", "checked");
	$('overview_filter_unsolved_'+options.unsolved).writeAttribute("checked", "checked");
	$('overview_filter_posted_'+options.posted).writeAttribute("checked", "checked");
	updateOverviewDisplayOptions();
}

function updateOverviewDisplayOptions(){
	options = {
		solved: $('overview_filter_solved_show').checked ? 'show' : 'dontshow',
		unsolved: $('overview_filter_unsolved_show').checked ? 'show' : 'dontshow',
		posted: $('overview_filter_posted_show').checked ? 'show' : 'dontshow'
	};
	createCookie('OverviewDisplayOptions', Object.toJSON(options),365);

	if(options.solved == 'show'){
		$$('#overview_movie_list > li.solved').each(function(e){e.show();});
	}
	else {
		$$('#overview_movie_list > li.solved').each(function(e){e.hide();});
	}

	if(options.unsolved == 'show'){
		$$('#overview_movie_list > li.punsolved').each(function(e){e.show();});
		$$('#overview_movie_list > li.unsolved').each(function(e){e.show();});
	}
	else {
		$$('#overview_movie_list > li.punsolved').each(function(e){e.hide();});
		$$('#overview_movie_list > li.unsolved').each(function(e){e.hide();});
	}

	if(options.posted == 'show'){
		$$('#overview_movie_list > li.posted').each(function(e){e.show();});
	}
	else {
		$$('#overview_movie_list > li.posted').each(function(e){e.hide();});
	}
}



function setAutoFocus() {
	if(readCookie('autoFocus')){
		options = readCookie('OverviewDisplayOptions').evalJSON();

	}
	createCookie('autoFocus', option,365);
}

function setAutoFocusTT(message){
	if(document.getElementById('pinbutton')){tt_pinbutton =	new Tip('pinbutton', message,{"hook":{"tip":"bottomMiddle","mouse":true},"stem":"bottomMiddle","offset":{"x":0,"y":-10},"style":"tt_white_bottom"})};
}



function toggleAutoFocus() {
	
	autoFocus = readCookie('autoFocusOption'); 
	if(!autoFocus) {
		autoFocus= 'false';
		}
	
	if(autoFocus == 'false') {
		$("pinbutton").addClassName("active");
		autoFocus = 'true';	
		createCookie('autoFocusOption', 'true', '365');
		setAutoFocusTT("remove automatical jump to input field");
		$("guess").focus();
	}
	else {
		$("pinbutton").removeClassName("active");
		autoFocus = 'false';
		createCookie('autoFocusOption', 'false', '365');
		setAutoFocusTT("automatically jump to input field when loading a new snapshot");
	}
}


// iE fuck

