current_hash = [];
current_language = '';
window_width = 0;
window_height = 0;
logo = undefined;
menu = undefined;
language_bar = undefined;
toolbar = undefined;
language_wrappers = undefined;
header_image = undefined;
header_image_aspect = 1280 / 750;
header_image_height = undefined;

function log() {
	if (console && console.firebug) {
		console.log.apply(this, arguments)
	} else {
//		alert.apply(this, arguments)
	}
}

function tabletify(tablet_content_selector) {
	var content = $(tablet_content_selector);
	menu = content.find(".menu");
	toolbar = content.find("#toolbar");
	language_wrappers = content.find(".language_wrapper");
	var sections = content.find(".section");
	
	header_image = add_header_image(content);
	logo = add_logo(content);
	language_bar = add_language_bar(content);
	init_language_bar(language_bar);
	init_menu(menu);
	init_toolbar(toolbar);
	init_sections(sections);
	
	set_language("tr");
	
	$(window).resize(on_resize).resize();
	$(window).bind("orientationchange", on_resize);
}

function on_resize() {
	window_width = $(window).width();
	window_height = $(window).height();
	fit_to_screen_width(menu)
	header_image_height = window_width / header_image_aspect;
	if (header_image_height > window_height * .5) {
		header_image.css("top", -(header_image_height - (window_height * .5)));
		header_image_height = window_height * .5;
	} else {
		header_image.css("top", 0);
	}
	language_bar.css("top", header_image_height - 15);
	language_wrappers.css("margin-top", header_image_height);
}

function set_language(to_language) {
	var languages = ["tr", "en"];
//	log("Setting language to " + to_language);
	for (language_id in languages) {
		var language = languages[language_id];
		if (to_language == language) {
			$("#language_" + language).css('display', 'block');
			current_language = language;
		} else {
			$("#language_" + language).css('display', 'none');
		}
	};
	
	if (to_language == "tr") {
		toolbar.find('#back_button').text('Geri');
	} else {
		toolbar.find('#back_button').text('Back');
	}
}

function add_logo(container_div) {
	return container_div.prepend('<div class="logo"><img src="html_images/logo_iphone4.png" /></div>').find(".logo");
}

function add_header_image(container_div) {
	return container_div.prepend('<div class="header_image"><img src="html_images/bg.jpg" /></div>').find(".header_image");
}

function add_language_bar(container_div) {
	return container_div.prepend('<div class="language_bar"><a href="#tr">TR</a><a href="#en">EN</a></div>').find(".language_bar");
}

function init_language_bar(language_bar) {
	language_bar.find("a").click(on_language_click);
}

function init_toolbar(toolbar_q) {
	toolbar_q.css('display', 'block');
	toolbar_q.css('top', '-50px');
	$("#back_button").click(on_back);
}

function init_menu(menu_q) {
	
	fit_to_screen_width(menu_q);
	var menu_items = menu_q.find('a');
	
	menu_items.each(
		function(key, menu_item) {
			var item_q = $(menu_item);
			
			// Submenu
			menu_item.parent_menu = menu_items;
			menu_item.submenu = item_q.parent().find('li a');
			menu_item.submenu_open = function() {
				this.submenu.parent().parent().css("display", "block");
			};
			menu_item.submenu_close = function() {
				this.submenu.parent().parent().css("display", "none");
			};
			
			// Click handler
			item_q.click(function() {
				this.parent_menu.each(
					function(key, item) {
						item.submenu_close();
					}
				);
				if (this.submenu.length > 0) {
					this.submenu_open();
				} else {
					processHash(this.hash);
				};
			})
		}
	);
}

function init_sections(sections_q) {
	sections_q.css('display', 'none');
}

function processHash(hash) {
	hash = hash.split('/');
	hash.shift();
	log("Processing hash: ", hash);
	set_language(hash.shift());
	hide_section(current_hash.concat());
	current_hash = hash;
	show_section(hash.concat());
}

function hide_section(hash) {
	while (hash.length > 0) {
		var section_class = '#' + current_language + '_' + hash.join('_');
		var section_q = $(section_class);
//		section_q.css('display', 'none');
		section_q.animate(
			{"left": window_width + "px"},
			{ 'duration': 500, complete: on_hide_complete }
		);
		hash.pop();
	};
}

function show_section(hash) {
	var child_count = 0;
	while (hash.length > 0) {
		var section_class = '#' + current_language + '_' + hash.join('_');
		var section_q = $(section_class);
		section_q.css('display', 'block');
		if (child_count > 0) {
			section_q.css('padding', '0');
		} else {
			show_toolbar(section_q);
		}
		section_q.css("left", window_width + "px");
		section_q.animate({"left": "0"}, 500);
		fit_to_screen_width(section_q);
		hash.pop();
		child_count++;
	};
	hide_menu();
}

function hide_menu() {
	menu.animate({"left": -window_width + "px"}, 500);
}

function show_menu() {
	menu.animate({"left": "0"}, 500);
}

function show_toolbar(section_q) {
	toolbar.css('display', 'block');
	toolbar.animate({"top": "0"}, 500);
	logo.animate({"top": "35px"}, 750);
	var title = section_q.find('h2').text();
	if (!title.length) {
		switch(section_q.selector) {
			case '#tr_contact':
				title = 'İletişim';
				break;
			case '#en_contact':
				title = 'Contact';
				break;
			case '#tr_news':
				title = 'Haberler';
				break;
			case '#en_news':
				title = 'News';
				break;
		};
	};
	var page_title_q = $("#page_title");
	page_title_q.text(title);
	if (page_title_q.height() > 30) {
		page_title_q.css('margin-top', '2px');
	} else {
		page_title_q.css('margin-top', '12px');
	}
}

function hide_toolbar() {
	toolbar.animate({"top": "-50px"}, 500);
	logo.animate({"top": "0"}, 750);
}

function on_back() {
//	log("Back button clicked.");
	hide_toolbar();
	hide_section(current_hash);
	current_hash = [];
	show_menu();
}

function on_language_click() {
	on_back();
	set_language(this.text.toLowerCase());
}

function on_hide_complete(evt) {
	$(this).css('display', 'none');
}

function fit_to_screen_width(query) {
	query.width(window_width - 2 * parseInt(query.css('padding-left')));
}

