function addEvent(element, type, handler) {

    if (!handler.$$guid) handler.$$guid = addEvent.guid++;
    if (!element.events) element.events = {};

    var handlers = element.events[type];
    if (!handlers) {
        handlers = element.events[type] = {};

        if (element["on" + type]) {
            handlers[0] = element["on" + type];
        }
    }

    handlers[handler.$$guid] = handler;
    element["on" + type] = handleEvent;
}



addEvent.guid = 1;

function removeEvent(element, type, handler) {

    if (element.events && element.events[type]) {
        delete element.events[type][handler.$$guid];
    }

};



function handleEvent(event) {

    event = event || window.event;
    var handlers = this.events[event.type];

    for (var i in handlers) {
        this.$$handleEvent = handlers[i];
        this.$$handleEvent(event);
    }
}



function init() {
var manga_select = document.getElementById('serie_name');

	if (manga_select != null) {
    		addEvent(manga_select, 'change', goToManga);

		var change_genre_select = document.getElementById('change_genre');

		if (change_genre_select != null) {
			addEvent(change_genre_select, 'change', goToGenre);
		}
	}
	else {
		loadingStageEnd();

		if (!loadallpages) {
			document.navigation.page_select.selectedIndex = 0;
			//document.navigation.page_select_bottom.selectedIndex = 0;
		}


		var chapter_select = document.getElementById('chapter_select');
		//var chapter_select_bottom = document.getElementById('chapter_select_bottom');
		var page_select = document.getElementById('page_select');
		//var page_select_bottom = document.getElementById('page_select_bottom');
		var next_page = document.getElementById('next_page');
		var next_page_bottom = document.getElementById('next_page_bottom');
		var back_page = document.getElementById('back_page');
		var back_page_bottom = document.getElementById('back_page_bottom');
		var manga_page = document.getElementById('mangaPage');

		
		addEvent(chapter_select, 'change', goToChapter);
		//addEvent(chapter_select_bottom, 'change', goToChapterBottom);
		addEvent(page_select, 'change', goToPage);
		//addEvent(page_select_bottom, 'change', goToPageBottom);
		addEvent(manga_page, 'load', doneLoading);
	}
}


function goToManga(e) {
	var serie_name = document.navigation.serie_name.value;

	if (serie_name != 1) {
		window.location.href = "/serie-archive/mangas-" + serie_name + "/";
	}
	else {
		window.location.href = "/";
	}
}

function goToGenre(e) {
	var genre_name = document.getElementById('change_genre').value;

	if (genre_name != 'none') {
		window.location.href = "/manga-archive/mangas-genre=" + genre_name + "/";
	}
}


function goToChapter(e) {
	
	var chapter_title = document.navigation.chapter_select.value;
	window.location.href = "/" + current_serie_url + "/chapter-" + chapter_title + "/";
}



//function goToChapterBottom(e) {

	//var chapter_title = document.navigation.chapter_select_bottom.value;
	//window.location.href = "/" + current_serie_url + "/chapter-" + chapter_title + "/";
//}



function goToPage(e) {

	var page_select = document.navigation.page_select.value;
	//document.navigation.page_select_bottom.selectedIndex = document.navigation.page_select.selectedIndex;
	setPage(current_serie, current_chapter, page_select);
}



//function goToPageBottom(e) {

	//var page_select = document.navigation.page_select_bottom.value;
	//document.navigation.page_select.selectedIndex = document.navigation.page_select_bottom.selectedIndex;
	//setPage(current_serie, current_chapter, page_select);
//}



function goBackPage(e) {

	if (document.navigation.page_select.selectedIndex != 0) {
		document.navigation.page_select.selectedIndex = document.navigation.page_select.selectedIndex - 1;
		//document.navigation.page_select_bottom.selectedIndex = document.navigation.page_select_bottom.selectedIndex - 1;
		
		var current_page = document.navigation.page_select.value;
		setPage(current_serie, current_chapter, current_page);
	}
}


function goNextPageKey(e) {
	var browser = navigator.appName;

	if (browser == "Microsoft Internet Explorer") {

		if (!e) var e = window.event;
   		
		if (e.keyCode) {
        		keycode = e.keyCode;
        		if ((keycode == 39) || (keycode == 37)) {
            			window.event.keyCode = 0;
        		}
    		} else {
       		keycode = e.which;
    		}
	}
	else {
		if (e.which) {
       		keycode = e.which;
    		} else {
        		keycode = e.keyCode;
   		}
	}

	if (keycode == 39) {
		goNextPage();
	} 
	else if (keycode == 37) {
		goBackPage();
	}

}


function goNextPage(e) {

	if (document.navigation.page_select.selectedIndex != (document.navigation.page_select.options.length - 1)) {
		document.navigation.page_select.selectedIndex = document.navigation.page_select.selectedIndex + 1;
		//document.navigation.page_select_bottom.selectedIndex = document.navigation.page_select_bottom.selectedIndex + 1;

		var current_page = document.navigation.page_select.value;
		setPage(current_serie, current_chapter, current_page);
	}
	else if (chapter_title_next != "false") {
		window.location.href = "/" + current_serie_url + "/chapter-" + chapter_title_next + "/";
	}
	
	if (!loadallpages) {
		window.scrollTo(0,0);
	}
}


function setPage (serie_name, chapter_title, page_number) {
	
	if (loadallpages) {
		window.location.href = "/index.php?serie=" + current_serie_url + "&chapter=" + current_chapter_url + "&page_nr=" + page_number;
	}
	else {
		var new_page_number = page_number - 1; 
		var url_image = host_name + serie_name + "/" + chapter_title + "/compressed/" + filename_pages[new_page_number];
	
		loadingStage (); 
		document.getElementById("pageimg").innerHTML = "<img src='" + url_image +"' id='mangaPage'>";

		//if (document.getElementById("pageimg").innerHTML != "<img src='" + url_image +"' id='mangaPage'>") {
			//document.getElementById("mangaPage").src = url_image;
		//}
	
		var manga_page = document.getElementById('mangaPage');
		addEvent(manga_page, 'load', doneLoading);
	
		document.getElementById('mangaPage').width = 730;
	
		image_loaded = 0;
		setTimeout("checkImageLoad ();", 2000);
	}
}



function checkImageLoad () {

	if (image_loaded == 0) {
		//alert("not loaded");	
	}
}



function checkButtons () {

	if (document.navigation.page_select.selectedIndex == 0) {
		document.navigation.back_page.disabled = true;
		document.navigation.back_page_bottom.disabled = true;
	}
	else {
		document.navigation.back_page.disabled = false;
		document.navigation.back_page_bottom.disabled = false;
	}

	
	if (document.navigation.page_select.selectedIndex == (document.navigation.page_select.options.length - 1) && chapter_title_next == "false") {
		document.navigation.next_page.disabled = true;
		document.navigation.next_page_bottom.disabled = true;
	}
	else {
		document.navigation.next_page.disabled = false;
		document.navigation.next_page_bottom.disabled = false;
	}
}



function doneLoading (e) {
	loadingStageEnd ();
}





function loadingStageEnd () {
	//If it gets into timout function, it will know image is loaded
	image_loaded = 1;


	//Delete loading images
	document.getElementById('mangaLoader').style.display = "none";
	document.getElementById('mangaLoaderBottom').style.display = "none";

	checkButtons (); //Activate buttons

	var origi_width, origi_heigth; //Create variables

	
	//Get original width and heigth of image
	if (typeof document.getElementById("mangaPage").naturalWidth != 'undefined') {
  		origi_width = document.getElementById('mangaPage').naturalWidth;
  		origi_heigth = document.getElementById('mangaPage').naturalHeight;
	}
	else {
		var manga_page = new Image;
		manga_page.src = document.getElementById("mangaPage").src;
		origi_width = manga_page.width;
		origi_heigth = manga_page.height;
	}


	if (origi_width == 'undefined' || origi_width == 0) {
		//if could not get width of image
		document.getElementById("pageimg").style.height = "300px";
		origi_width = 1000;
		origi_heigth = 1000;
	}
	else {
		//Width and heigth found
		var container_height;
		var manga_page_element = document.getElementById('mangaPage');

		//calculate manga page heigth
		if (origi_width < 731) {
			container_height = origi_heigth;
			document.getElementById('mangaPage').width = origi_width;
			
			//Create onclick next page event
			addEvent(manga_page_element, 'click', goNextPage);
		}
		else {
			container_height = origi_heigth * (730 / origi_width);
			
			//Create onclick popup event
			addEvent(manga_page_element, 'click', launchPopup);
		}
		
		document.getElementById("pageimg").style.height = container_height + "px"; //insert manga page heigth
	}

	


	//Insert original width and heigth of image for popup page
	image_page_width = origi_width;
	image_page_height = origi_heigth;

	//When people point mouse too manga page, make it an pointer mouse
	document.getElementById('mangaPage').style.cursor = "pointer";
}



function loadingStage () {

	document.navigation.next_page.disabled = true;
	document.navigation.next_page_bottom.disabled = true;
	document.navigation.back_page.disabled = true;
	document.navigation.back_page_bottom.disabled = true;
	

	document.getElementById('mangaLoader').style.display = "";
	document.getElementById('mangaLoaderBottom').style.display = "";
}



function launchPopup (e) {
	popupwindow(document.getElementById("mangaPage").src, (image_page_width + 25), (image_page_height + 20));
}



function popupwindow(setfilename, setwidth, setheight) {

	win = window.open('','','scrollbars=1,statusbar=0,menubar=0,resizable=0,width=' + setwidth + ',height=' + setheight)
	win.document.write("<html><head><title>" + current_serie + " - " + current_chapter + "</title></head><body style='margin:0px;padding:0px;'><table width=100% height=100%><tr><td><img src='" + setfilename + "'></td></tr></table></body></html>")
	win.document.close()
}

function openuploadwindows(seturl) {
	MyWindow = window.open(seturl,'','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,width=537,height=800');
}

var image_page_width, image_page_height, image_loaded, init_started;
addEvent(window, 'load', init);
addEvent(document, 'keydown', goNextPageKey);
