var domains = Array();
var language_codes = Array();
var languages = Array();
var strings = Array();

var country = -1; var language = -1; var device = -1; var instr = 0; var lastCountry;

var filtered_list = Array();

var currentVideo = String();
var lastTrailer = String();
var faq_back = String();

var cc = $.getURLParam("cc");
var lc = $.getURLParam("lc");

function filter_list() {
	var i, o, od; filtered_list = [];
	for(i = 0; i < domains[country]['list'].length; i++)
		if(domains[country]['items'][i]['synopsis'][language])
			for(o = 0; o < domains[country]['items'][i]['odf'].length; o++)
				for(od = 0; od < domains[country]['items'][i]['odf'][o]['devices'].length; od++)
					if(domains[country]['devices'][device] == domains[country]['items'][i]['odf'][o]['devices'][od])
						filtered_list.push(domains[country]['list'][i]);
}

function order_new_last() {
	var i, j, temp;
	for(i = filtered_list.length - 1; i > 0; i--) {
		for(j = 0; j < i; j++) if(domains[country]['items'][filtered_list[j]]['daysleft'] > domains[country]['items'][filtered_list[j + 1]]['daysleft']) {
			temp = filtered_list[j];
			filtered_list[j] = filtered_list[j + 1];
			filtered_list[j + 1] = temp;
		}
	}
}

function order_new_first() {
	var i, j, temp;
	for(i = filtered_list.length - 1; i > 0; i--) {
		for(j = 0; j < i; j++) if(domains[country]['items'][filtered_list[j]]['daysleft'] < domains[country]['items'][filtered_list[j + 1]]['daysleft']) {
			temp = filtered_list[j];
			filtered_list[j] = filtered_list[j + 1];
			filtered_list[j + 1] = temp;
		}
	}
}

function clear_tab() {
	var i;
	
	for(i = 0; i < 15; i++) {
		var row = Math.floor(i / 5) + 1;
		var col = i - 5 * (row - 1) + 1;
		var selection = "#movie_selection_" + row + "_" + col;

		$(selection + " .movie_selection_poster").attr({src: ''});
		$(selection + " .movie_selection_indicator").text('');
		$(selection + " .movie_selection_title").text('');
		$(selection).css({'display': 'none'});
	}
}

function display_tab() {
	var i;
	
	for(i = 0; i < filtered_list.length; i++) {
		var row = Math.floor(i / 5) + 1;
		var col = i - 5 * (row - 1) + 1;
		var selection = "#movie_selection_" + row + "_" + col;
		var daysleft = domains[country]['items'][filtered_list[i]]['daysleft'];
		
		if(daysleft > 60) {
			$(selection + " .movie_selection_indicator").text(strings['main_new']);
			$(selection + " .movie_selection_indicator").css({'color': '#d1d1d1'});
		}
		if((daysleft < 60) && (daysleft > 30)) {
			$(selection + " .movie_selection_indicator").text('');		
		}
		if((daysleft < 30) && (daysleft > 7)) {
			$(selection + " .movie_selection_indicator").text(daysleft + ' ' + strings['main_days_left']);
			$(selection + " .movie_selection_indicator").css({'color': '#6c6c6c'});
		}
		if(daysleft <= 7) {
			$(selection + " .movie_selection_indicator").text(daysleft + ' ' + strings['main_days_left']);
			$(selection + " .movie_selection_indicator").css({'color': '#dc0000'});
		}
		if(daysleft == 1) {
			$(selection + " .movie_selection_indicator").text('1 ' + strings['main_day_left']);
			$(selection + " .movie_selection_indicator").css({'color': '#dc0000'});
		}

		$(selection + " .movie_selection_poster").attr({src: domains[country]['items'][filtered_list[i]]['poster_m']});
		$(selection + " .movie_selection_title").text(domains[country]['items'][filtered_list[i]]['title'][language]);
		$(selection).css({'display': 'block'});
	}
}

function fill_html_gaps() {
	// main

	$("#movie_selection_change").text(strings['main_banner_change']);

	if(device >= 0) $("#main_footer_contact").attr({href: ('http://www.sonyericsson.com/cws/support/phones/topic/playnowarenamovies/' + domains[country]['devices'][device].toLowerCase() + '?lc=' + codes['language'][domains[country]['language_codes'][language]] + '&cc=' + codes['country'][domains[country]['name']])});
	$("#main_se_com").attr({href: ('http://www.sonyericsson.com/cws/home?lc=' + codes['language'][domains[country]['language_codes'][language]] + '&cc=' + codes['country'][domains[country]['name']])});

	$("#movie_selection_your_language_info").text(strings['main_banner_language'] + ' ');

	if(language >= 0) $("#movie_selection_your_language").text(domains[country]['languages'][language]);

	$("#movie_selection_registered_country_info").text(strings['country_your_selections_country'] + ' ');
	$("#movie_selection_registered_country").text(strings['country_' + domains[country]['name'].replace(/\s/, '_').toLowerCase()]);

	if(device >= 0) {
		$("#movie_selection_handset_model_info").text(strings['country_your_selections_phone'] + ' ');
		$("#movie_selection_handset_model").text(domains[country]['devices'][device]);
	}

	$(".tab_1").text(strings['main_newly_added']);
	$(".tab_2").text(strings['main_last_chance']);

	$("#main_footer_help").html(strings['main_footer_help']);
	$("#main_footer_contact").text(strings['main_footer_contact']);
	$("#main_footer_legal").text(strings['main_footer_legal']);

	// pg

	$("#pg_certificate_info").text(strings['pg_info']);
	$("#pg_cancel_text").text(strings['pg_cancel']);
	$("#pg_download_text").text(strings['pg_download']);

	// country

	$("#country_info1").html(strings['country_header_text_left']);
	$("#country_info2").html(strings['country_header_text_right']);
	$("#country_info2").html(strings['country_header_text_right']);

	if(language >= 0) $("#country_phone_language").html(strings['country_your_selections_language'] + ' <b>' + domains[country]['languages'][language] + '</b>'); // Your language is...
	
	$("#country_phone_country").html(strings['main_banner_country'] + ' <b>' + strings['country_' + domains[country]['name'].replace(/\s/, '_').toLowerCase()] + '</b>'); // You have chosen...

	if(device >= 0) $("#country_phone_handset").html(strings['country_your_selections_phone'] + ' <b>' + domains[country]['devices'][device] + '</b>'); // Phone model is...

	// instrgen

	$("#instrgen_dontshow").text(strings['instrgen_dontshow']);
	$("#instrgen_info_topleft").text(strings['instrgen_select']);
	$("#instrgen_info_topcenter").text(strings['instrgen_transfer']);
	$("#instrgen_info_topright").text(strings['instrgen_enjoy']);
	$("#instrgen_info_bottomleft").html(strings['instrgen_1']);
	$("#instrgen_info_bottomcenter").html(strings['instrgen_2']);
	$("#instrgen_info_bottomright").html(strings['instrgen_3']);

	// trailer

	$("#trailer_download_text").text(strings['trailer_footer_download']);

	// faq

	$("#faq_title").text(strings['faq_header']);
	if(device >= 0) $("#faq_text").html(strings['faq_html'] + '<a href="http://www.sonyericsson.com/cws/support/phones/topic/playnowarenamovies/' + domains[country]['devices'][device].toLowerCase() + '?lc=' + codes['language'][domains[country]['language_codes'][language]] + '&cc=' + codes['country'][domains[country]['name']] + '" target="new">www.sonyericsson.com/support</a>');
	else $("#faq_text").html(strings['faq_html']);

	if(device >= 0) $("#faq_readmore_url").attr({href: ('http://www.sonyericsson.com/cws/support/phones/topic/playnowarenamovies/' + domains[country]['devices'][device].toLowerCase() + '?lc=' + codes['language'][domains[country]['language_codes'][language]] + '&cc=' + codes['country'][domains[country]['name']])});
	$("#faq_readmore_url").text(strings['faq_footer']);

	// eula

	$("#eula_title").text(strings['eula_header']);
	$("#eula_text").html(strings['eula_html']);
	$("#eula_agree_text").text(strings['eula_agree']);
	$("#eula_cancel_text").text(strings['eula_cancel']);
	$("#eula_close_text").text(strings['eula_close']);

	$(".se_main").attr({href: ('http://www.sonyericsson.com/cws/home?lc=' + codes['language'][domains[country]['language_codes'][language]] + '&cc=' + codes['country'][domains[country]['name']])});
	$(".se_main").text('http://www.sonyericsson.com');
	
	// country map
	
 	$("#" + domains[country]['name'].replace(/\s/, '').toLowerCase() + "_text").html(strings['country_map_text'] + '<br/><span class="country_map_name">' + strings['country_' + domains[country]['name'].replace(/\s/, '_').toLowerCase()] + '</span>')
}

function select_device(sel) {
	if(domains[country]['devices'][sel]) {
		if(domains[country]['devices'].length > 1) {
			if(device == 0) {
				$("#country_select_hs_top_sel").css({'display': 'none'});
				$("#country_select_hs_top_unsel").css({'display': 'block'});
			}
			else if(device == (domains[country]['devices'].length - 1)) {
				$("#country_select_hs_bottom_sel").css({'display': 'none'});
				$("#country_select_hs_bottom_unsel").css({'display': 'block'});			
			}
			else $("#country_select_hs_" + device).css({'background-color': '#ffffff'});
		}
	
		$("#country_select_hs_" + device).css({'color': '#3f3f3f'});
	
		if(domains[country]['devices'].length == 1) {
			$("#country_select_hs_" + sel).css({'background': 'url("images/country_hs_sel.png")'});
		} else {
			if(sel == 0) {
				$("#country_select_hs_top_sel").css({'display': 'block'});
				$("#country_select_hs_top_unsel").css({'display': 'none'});
			}
			else if(sel == (domains[country]['devices'].length - 1)) {
				$("#country_select_hs_bottom_sel").css({'display': 'block'});
				$("#country_select_hs_bottom_unsel").css({'display': 'none'});
			}
			else $("#country_select_hs_" + sel).css({'background-color': '#4e9219'});
		}
		
		$("#country_select_hs_" + sel).css({'color': '#ffffff'});
	
		device = sel;
	
		$("#country_phone_handset").html(strings['country_your_selections_phone'] + ' <b>' + domains[country]['devices'][device] + '</b>'); // Phone model is...
		$("#main_footer_contact").attr({href: ('http://www.sonyericsson.com/cws/support/phones/topic/playnowarenamovies/' + domains[country]['devices'][device].toLowerCase() + '?lc=' + codes['language'][domains[country]['language_codes'][language]] + '&cc=' + codes['country'][domains[country]['name']])});
		$("#faq_text").html(strings['faq_html'] + '<a href="http://www.sonyericsson.com/cws/support/phones/topic/playnowarenamovies/' + domains[country]['devices'][device].toLowerCase() + '?lc=' + codes['language'][domains[country]['language_codes'][language]] + '&cc=' + codes['country'][domains[country]['name']] + '" target="new">www.sonyericsson.com/support</a>');
		$("#faq_readmore_url").attr({href: ('http://www.sonyericsson.com/cws/support/phones/topic/playnowarenamovies/' + domains[country]['devices'][device].toLowerCase() + '?lc=' + codes['language'][domains[country]['language_codes'][language]] + '&cc=' + codes['country'][domains[country]['name']])});
		
		$("#country_handset").css({'background': 'url("images/' + domains[country]['devices'][device] + '.png")'});
		$("#movie_selection_handset_model_info").text(strings['country_your_selections_phone'] + ' ');
		$("#movie_selection_handset_model").text(domains[country]['devices'][device]);
		
		$.cookie('sd_se_device2', device, {expires: 30});
		
		filter_list();
		order_new_first();
		clear_tab();
	
		if(language == -1) {
			$("#country_select_info").text(strings['country_info_select_language']);
		} else if(language >= 0) {
			$("#country_check").css({'display': 'block'});
			$("#country_select_info").text(strings['country_info_select_check']);
		}
		
		
	} else select_device(0);
}

function clear_country_lists() {
	var i;
	for (i = 0; i < domains.length; i++) $("#country_select_cs_" + i).text('');
	for (i = 0; i < domains[country]['languages'].length; i++) $("#country_select_ls_" + i).text('');
}

function fill_country_lists() {
	var i;

	for (i = 0; i < domains.length; i++) $("#country_select_cs_" + i).text(country_translate[domains[i]['name'].replace(/\s/, '_').toLowerCase()]);
	for (i = 0; i < domains[country]['languages'].length; i++) $("#country_select_ls_" + i).text(domains[country]['languages'][i]);
}

function get_language(code, domain) {
	var i;
	for (i = 0; i < domains[domain]['languages'].length; i++)
		if(domains[domain]['languages'][i] == language_codes[code])
 			return i;
}

function load_language_strings(code) {
	$.ajax({
		type: "GET",
		url: "locale/" + code + ".xml",
		dataType: "xml",
		success: function(xml) {
			$(xml).find('string').each(function() {
				strings[$(this).attr('name')] = $(this).text();
			});

			$("#selections").text(strings['country_your_selections']);
			
			$.cookie('sd_se_language', language, {expires: 30});

			if((device == -1) && (language == -1)) $("#country_select_info").text(strings['country_info_select_language_phone']);
			if((device >= 0) && (language == -1)) $("#country_select_info").text(strings['country_info_select_language']);
			if((device == -1) && (language >= 0)) $("#country_select_info").text(strings['country_info_select_phone']);
			if((device >= 0) && (language >= 0)) $("#country_select_info").text(strings['country_info_select_check']);

			if(domains[country]['devices'].length == 1) {
				filter_list();
				order_new_first();
				clear_tab();
			
				$("#country_check").css({'display': 'block'});
				$("#country_select_info").text(strings['country_info_select_check']);
			}

			fill_country_lists();
			fill_html_gaps();

			var country_code = codes['country'][domains[country]['name']];
			var language_code = codes['language'][domains[country]['language_codes'][language]];
			var sonyericsson_logo_url = 'http://www.sonyericsson.com/cws/home?cc=' + country_code + '&lc=' + language_code;

			$("#sonyericsson_logo_url").attr({href: sonyericsson_logo_url});
		}
	});
}

function select_language(sel) {
	if(sel < domains[country]['languages'].length) {
		if(domains[country]['languages'].length == 1) {
			$("#country_select_ls_0").css({'width': '99px', 'height': '17px', 'padding-top': '3px', 'padding-left': '20px', 'color': '#ffffff', 'cursor': 'pointer'});
			$("#country_select_ls_bottom_sel").css({'display': 'none'});
			$("#country_select_ls_bottom_unsel").css({'display': 'none'});
			$("#country_select_ls_top_sel").css({'display': 'none'});
			$("#country_select_ls_top_unsel").css({'display': 'none'});
			$("#country_ls_sel").css({'display': 'block'});
		} else {
			if(language == 0) {
				$("#country_select_ls_top_sel").css({'display': 'none'});
				$("#country_select_ls_top_unsel").css({'display': 'block'});			
			}
			else if(language == (domains[country]['languages'].length - 1)) {
				$("#country_select_ls_bottom_sel").css({'display': 'none'});
				$("#country_select_ls_bottom_unsel").css({'display': 'block'});
			} else $("#country_select_ls_" + language).css({'background-color': '#ffffff'});
		}
	
		$("#country_select_ls_" + language).css({'color': '#3f3f3f'});
	
		language = sel;

		if(domains[country]['languages'].length == 1) {
			$("#country_select_ls").css({'top': '467px' , 'height': '20px'});
			$("#country_select_ls").html('<div id="country_select_ls_0">' + strings['country_' + domains[country]['languages'][0].replace(/\s/, '_').toLowerCase()] + '</div>');
			$("#country_select_ls_0").css({'width': '99px', 'height': '17px', 'padding-top': '3px', 'padding-left': '20px', 'color': '#ffffff', 'cursor': 'pointer'});
			$("#country_select_ls_bottom_sel").css({'display': 'none'});
			$("#country_select_ls_bottom_unsel").css({'display': 'none'});
			$("#country_select_ls_top_sel").css({'display': 'none'});
			$("#country_select_ls_top_unsel").css({'display': 'none'});
			$("#country_ls_sel").css({'display': 'block'});			
		} else {
			if(sel == 0) {
				$("#country_select_ls_top_sel").css({'display': 'block'});
				$("#country_select_ls_top_unsel").css({'display': 'none'});
			} else if(sel == (domains[country]['languages'].length - 1)) {
				$("#country_select_ls_bottom_sel").css({'display': 'block'});
				$("#country_select_ls_bottom_unsel").css({'display': 'none'});
			} else $("#country_select_ls_" + sel).css({'background-color': '#4e9219'});
		}
	
		$("#country_select_ls_" + sel).css({'color': '#ffffff'});
	
		clear_country_lists();
	
		load_language_strings(domains[country]['language_codes'][sel]);
		
		if(device >= 0)	$("#country_check").css({'display': 'block'});
		
	} else select_language(0);
}

function get_country(code) {
	var i;
	for (i = 0; i < domains.length; i++)
		if(domains[i]['name'] == codes['country'][code])
 			return i;
}

function select_country(sel) {
	if(sel < domains.length) {
		language = -1;
		device = -1;
	
		$("#country_check").css({'display': 'none'});
	
		if(country == 0) {
			$("#country_select_cs_top_sel").css({'display': 'none'});
			$("#country_select_cs_top_unsel").css({'display': 'block'});
		}
		else if(country == (domains.length - 1)) {
			$("#country_select_cs_bottom_sel").css({'display': 'none'});
			$("#country_select_cs_bottom_unsel").css({'display': 'block'});
		}
		else $("#country_select_cs_" + country).css({'background-color': '#ffffff'});
	
		$("#country_select_cs_" + country).css({'color': '#3f3f3f'});

		if(domains.length == 1) {
			$("#country_select_cs_top_sel").css({'display': 'none'});
			$("#country_select_cs_top_unsel").css({'display': 'none'});
			$("#country_select_cs_bottom_sel").css({'display': 'none'});
			$("#country_select_cs_bottom_unsel").css({'display': 'none'});
			$("#country_cs_sel").css({'display': 'block'})
		} else {
			if(sel == 0) {
				$("#country_select_cs_top_sel").css({'display': 'block'});
				$("#country_select_cs_top_unsel").css({'display': 'none'});
			}
			else if(sel == (domains.length - 1)) {
				$("#country_select_cs_bottom_sel").css({'display': 'block'});
				$("#country_select_cs_bottom_unsel").css({'display': 'none'});
			}
			else $("#country_select_cs_" + sel).css({'background-color': '#4e9219'});
		}
		
		$("#country_select_cs_" + sel).css({'color': '#ffffff'});
		if(country >= 0) $("#country_map_" + domains[country]['name'].replace(/\s/, '').toLowerCase()).css({'display': 'none'});
	
		country = sel;
	
		$("#country_map_" + domains[country]['name'].replace(/\s/, '').toLowerCase()).css({'display': 'block'});
		$("#" + domains[country]['name'].replace(/\s/, '').toLowerCase() + "_text").html(strings['country_map_text'] + '<br/><span class="country_map_name">' + strings['country_' + domains[country]['name'].replace(/\s/, '_').toLowerCase()] + '</span>')
	
		$.preload(domains[country]['posters'], {
			onFinish: function() {
	
			}
		});
	
		$("#selections").text(strings['country_your_selections']);
		$("#movie_selection_registered_country_info").text(strings['country_your_selections_country'] + ' ');
		$("#movie_selection_registered_country").text(domains[country]['name']);
		$("#country_phone_country").html(strings['main_banner_country'] + ' <b>' + domains[country]['name'] + '</b>'); // You have chosen...
	
		$("#country_phone_language").text('');
		$("#country_phone_handset").text('');
		$("#country_handset").css({'background': ''});
	
		$("#country_select_ls").html('');
		$("#country_select_hs").html('');
		
		$.cookie('sd_se_country', country, {expires: 30});
	
		if(domains[country]['languages'].length == 1) {
			select_language(0);
		} else {
			$("#country_ls_sel").css({'display': 'none'});
			$("#country_ls_unsel").css({'display': 'none'});

			var i;

			for(i = 0; i < domains[country]['languages'].length; i++) {
				$("#country_select_ls").css({'top': (467 - i * 20) + 'px' , 'height': ($("#country_select_ls").height() + 20) + 'px'});
				$("#country_select_ls_top_sel").css({'top': (467 - i * 20) + 'px'});
				$("#country_select_ls_top_unsel").css({'top': (467 - i * 20) + 'px'});

				$("#country_select_ls").html($("#country_select_ls").html() + '<div id="country_select_ls_' + i + '" onClick="select_language(' + i + ');">' + domains[country]['languages'][i] + '</div>');

				$("#country_select_ls_" + i).css({'width': '99px', 'height': '17px', 'padding-top': '3px', 'padding-left': '20px', 'cursor': 'pointer'});
				if(i > 1) $("#country_select_ls_" + (i - 1)).css({'background-color': '#ffffff'});
			}

			$("#country_select_ls_top_sel").css({'display': 'none'});
			$("#country_select_ls_top_unsel").css({'display': 'block'});
			$("#country_select_ls_bottom_unsel").css({'display': 'block'});

			load_language_strings('eng');
			
			$("#country_select_info").text(strings['country_info_select_language']);
		}

		if(domains[country]['devices'].length == 1) {
			$("#country_select_hs").css({'top': '467px' , 'height': '20px'});
			$("#country_select_hs").html('<div id="country_select_hs_0" onClick="select_device(0);">' + domains[country]['devices'][0] + '</div>');
			$("#country_select_hs_0").css({'width': '41px', 'height': '17px', 'padding-top': '3px', 'padding-left': '8px', 'color': '#ffffff', 'cursor': 'pointer'});
			$("#country_select_hs_bottom_sel").css({'display': 'none'});
			$("#country_select_hs_bottom_unsel").css({'display': 'none'});
			$("#country_select_hs_top_sel").css({'display': 'none'});
			$("#country_select_hs_top_unsel").css({'display': 'none'});
			$("#country_hs_sel").css({'display': 'block'});

			device = 0;
	
			$.cookie('sd_se_device2', 0, {expires: 30});
			
			$("#country_phone_handset").html(strings['country_your_selections_phone'] + ' <b>' + domains[country]['devices'][device] + '</b>'); // Phone model is...
			$("#country_handset").css({'background': 'url("images/' + domains[country]['devices'][device] + '.png")'});
			$("#movie_selection_handset_model_info").text(strings['country_your_selections_phone'] + ' ');
			$("#movie_selection_handset_model").text(domains[country]['devices'][device]);
		} else {
			$("#country_hs_sel").css({'display': 'none'});
			$("#country_hs_unsel").css({'display': 'none'});

			var i;

			for(i = 0; i < domains[country]['devices'].length; i++) {
				$("#country_select_hs").css({'top': (467 - i * 20) + 'px' , 'height': ($("#country_select_hs").height() + 20) + 'px'});
				$("#country_select_hs_top_sel").css({'top': (467 - i * 20) + 'px'});
				$("#country_select_hs_top_unsel").css({'top': (467 - i * 20) + 'px'});
				
				$("#country_select_hs").html($("#country_select_hs").html() + '<div id="country_select_hs_' + i + '" onClick="select_device(' + i + ');">' + domains[country]['devices'][i] + '</div>');

				$("#country_select_hs_" + i).css({'width': '41px', 'height': '17px', 'padding-top': '3px', 'padding-left': '8px', 'cursor': 'pointer'});
				if(i > 1) $("#country_select_hs_" + (i - 1)).css({'background-color': '#ffffff'});
			}
			
			$("#country_select_hs_top_sel").css({'display': 'none'});
			$("#country_select_hs_top_unsel").css({'display': 'block'});
			$("#country_select_hs_bottom_unsel").css({'display': 'block'});
		}
		
		if((domains[country]['devices'].length == 1) && (domains[country]['languages'].length == 1)) {
			filter_list();
			order_new_first();
			clear_tab();
		
			$("#country_check").css({'display': 'block'});
			$("#country_select_info").text(strings['country_info_select_check']);
		}
		
		$("#tab_1_sel").css({'display': 'block'});
		$("#tab_1_unsel").css({'display': 'none'});
		$("#tab_2_sel").css({'display': 'none'});
		$("#tab_2_unsel").css({'display': 'block'});
	} else select_country(0);
}

function prepare_metadata(id) {
	var selection = id.split("_");
	var row = selection[2];
	var col = selection[3];

	var i = 5 * (row - 1) + (col - 1);

	var daysleft = domains[country]['items'][filtered_list[i]]['daysleft'];

	var o, od, odf;
	for(o = 0; o < domains[country]['items'][filtered_list[i]]['odf'].length; o++)
		for(od = 0; od < domains[country]['items'][filtered_list[i]]['odf'][o]['devices'].length; od++)
			if(domains[country]['devices'][device] == domains[country]['items'][filtered_list[i]]['odf'][o]['devices'][od])
				odf = o;

	currentVideo = domains[country]['items'][filtered_list[i]]['odf'][odf]['url'];

	$("#pg_certificate").text(strings['pg_certificate'] + ' ' + domains[country]['items'][filtered_list[i]]['certificate'][language]);
	$("#pg_poster").css({'background-image': 'url("' + domains[country]['items'][filtered_list[i]]['poster_l'] + '")'});
	
	$("#pg_title").text(domains[country]['items'][filtered_list[i]]['title'][language]);

	$("#trailer_poster").attr({src: domains[country]['items'][filtered_list[i]]['poster_s']});
	$("#trailer_title").text(domains[country]['items'][filtered_list[i]]['title'][language]);

	var duration;
	
	if(domains[country]['items'][filtered_list[i]]['odf'][odf]['duration'] >= 60) {
		duration = Math.floor(domains[country]['items'][filtered_list[i]]['odf'][odf]['duration'] / 60) + ' min';
	} else {
		duration = domains[country]['items'][filtered_list[i]]['odf'][odf]['duration'] + ' sec';
	}

	$("#trailer_details").html('<b>' + strings['trailer_header_year'] + '</b> ' + domains[country]['items'][filtered_list[i]]['year'][language] + '<br/><b>' + strings['trailer_header_director'] + '</b> ' + domains[country]['items'][filtered_list[i]]['director'][language] + '<br/><b>' + strings['trailer_header_category'] + '</b> ' + domains[country]['items'][filtered_list[i]]['category'][language] + '<br/><b>' + strings['trailer_header_length'] + '</b> ' + duration);
	$("#trailer_details2").html('<b>' + strings['trailer_header_actors'] + '</b> ' + domains[country]['items'][filtered_list[i]]['cast'][language] + '<br/><b>' + strings['trailer_header_certified'] + ': ' + domains[country]['items'][filtered_list[i]]['certificate'][language] + '</b>');

	var size;
	
	if(domains[country]['items'][filtered_list[i]]['odf'][odf]['size'] >= 1000000) {
		size = Math.floor(domains[country]['items'][filtered_list[i]]['odf'][odf]['size'] / 1000000) + ' MB';
	} else {
		size = '0.' + Math.floor(domains[country]['items'][filtered_list[i]]['odf'][odf]['size'] / 100000) + ' MB';
	}

	$("#trailer_download_size").text(size);
	$("#trailer_description").text(domains[country]['items'][filtered_list[i]]['synopsis'][language]);
	if(daysleft == 1) $("#trailer_days_left").text(domains[country]['items'][filtered_list[i]]['daysleft'] + ' ' + strings['trailer_footer_day_left']);
	else $("#trailer_days_left").text(domains[country]['items'][filtered_list[i]]['daysleft'] + ' ' + strings['trailer_footer_days_left']);

	flashembed("trailer_player", {src: 'FlowPlayerDark.swf', version: [9, 0], width: domains[country]['items'][filtered_list[i]]['trailer_w'] + 'px', height: '268px', bgcolor: "#151515"},
                                 {config: {autoPlay: false, loop: false, autoBuffering: true, controlBarBackgroundColor: '0x1b1b1b', initialScale: 'fit', videoFile: domains[country]['items'][filtered_list[i]]['trailer'], accelerated: true, showMenu: false}});

	$("#trailer_player").css({'display': 'none', 'left': (380 - (domains[country]['items'][filtered_list[i]]['trailer_w'] / 2)) + 'px'});
}

function clear_metadata() {
	$("#pg_certificate").html('');
	$("#pg_poster").attr({src: ''});
	$("#pg_title").html('');
	$("#trailer_poster").attr({src: ''});
	$("#trailer_title").html('');
	$("#trailer_details").html('');
	$("#trailer_details2").html('');
	$("#trailer_download_size").html('');
	$("#trailer_description").html('');
	$("#trailer_pg_box").html('');
	$("#trailer_days_left").html('');
	
	lastTrailer = '';
}

function show_trailer() {
	if(lastTrailer) $("#trailer_player").html(lastTrailer);

	$("#trailer").css({'top': ($(window).scrollTop() + 40)});
	$("#trailer").fadeIn(1, function() {
		$("#trailer_player").css({'display': 'block'});
		$("#trailer_description").jScrollPane({showArrows: true, scrollbarWidth: 11});
	});
}

function hide_trailer() {
	$("#trailer_player").html('');
	$("#trailer").css({'display': 'none'});
}

function open_help() {
	$("#fader").css({'display': 'block'});
	$("#instrgen").css({'top': ($(window).scrollTop() + 40)});
	$("#instrgen").css({'display': 'block'});
	$("#instrgen_checkbox").attr({src: 'images/checkbox.png'});

	if(instr == 1) {
		if($.cookie('sd_se_info_dontshow') == '1') $("#instrgen_checkbox").attr({src: 'images/checkbox_checked.png'});
	} else if(instr == 0) {
		if($.cookie('sd_se_dontshow') == '1') $("#instrgen_checkbox").attr({src: 'images/checkbox_checked.png'});
	}
}

function open_eula() {
		$("#fader").css({'display': 'block'});
		$("#eula").css({'top': ($(window).scrollTop() + 40)});
		$("#eula").css({'display': 'block'});

		if(lastCountry != 1) {
			$("#eula_agree").css({'display': 'none'});
			$("#eula_agree_text").css({'display': 'none'});
			$("#eula_cancel").css({'display': 'none'});
			$("#eula_cancel_text").css({'display': 'none'});
			$("#eula_close").css({'display': 'block'});
			$("#eula_close_text").css({'display': 'block'});
		}

		$("#eula_text").jScrollPane({showArrows: true, scrollbarWidth: 11});
}

$(document).ready(function() {
	$(document).disableTextSelect();

	$.preload(['W995', 'K800', 'background', 'black', 'checkbox', 'checkbox_checked', 'close', 'close2', 'country_background', 'country_check', 'country_cs_bottom_sel', 'country_cs_bottom_unsel', 'country_cs_top_sel', 'country_cs_top_unsel', 'country_hs_bottom_sel', 'country_hs_bottom_unsel', 'country_hs_sel', 'country_hs_top_sel', 'country_hs_top_unsel', 'country_hs_unsel', 'country_ls_bottom_sel', 'country_ls_bottom_unsel', 'country_ls_sel', 'country_ls_top_sel', 'country_ls_top_unsel', 'download', 'download_big', 'eula_background', 'fader', 'faq', 'faq_background', 'faq_readmore', 'footer_background', 'germany', 'help_icon', 'hongkong', 'instr_background', 'instrgen_background', 'movie_selection_change', 'movies_logo', 'norway', 'pg_agree', 'pg_background', 'pg_cancel', 'eula_close', 'pg_download', 'play_now_logo', 'russia', 'sonyericsson_logo', 'sweden', 'switzerland', 'tab1_sel', 'tab1_unsel', 'tab2_sel', 'tab2_unsel', 'thenetherlands', 'trailer_background', 'trailer_pg_box', 'unitedkingdom', 'alt_left', 'alt_right', 'alt_centre'],
		{base: 'images/', ext: '.png', onFinish: function() {
			$.ajax({
				type: "GET",
				url: "languagecodes.xml",
				dataType: "xml",
				success: function(codes_xml) {			
					$(codes_xml).find('language').each(function() {
						language_codes[$(this).attr('code')] = $(this).text();
						languages.push($(this).text());
					});

					var posters = new Array();

					$.ajax({
						type: "GET",
						url: "videos.xml",
						cache: false,
						dataType: "xml",
						success: function(xml) {
							var i = 0; var ii = 0;

							$(xml).find('domain').each(function() {
								domains[i] = new Array();
								domains[i]['name'] = $(this).attr('label');

								$("#country_select_cs").css({'top': (467 - i * 20) + 'px' , 'height': ($("#country_select_cs").height() + 20) + 'px'});
								$("#country_select_cs").html($("#country_select_cs").html() + '<div id="country_select_cs_' + i + '" onClick="select_country(' + i + ');">' + country_translate[domains[i]['name'].replace(/\s/, '_').toLowerCase()] + '</div>');
								$("#country_select_cs_top_sel").css({'top': (467 - i * 20) + 'px'});
								$("#country_select_cs_top_unsel").css({'top': (467 - i * 20) + 'px'});

								$("#country_select_cs_" + i).css({'position': 'absolute', 'top': (20 * i) + 'px', 'width': '129px', 'height': '17px', 'padding-top': '3px', 'padding-left': '20px', 'cursor': 'pointer'});

								if(i > 1) $("#country_select_cs_" + (i - 1)).css({'background-color': '#ffffff'});

								ii = 0;
								domains[i]['languages'] = new Array();
								domains[i]['language_codes'] = new Array();

								$(this).find('language').each(function() {
									domains[i]['language_codes'][ii] = $(this).text()
									domains[i]['languages'][ii] = language_codes[domains[i]['language_codes'][ii]];
									
									ii++;
								});

								ii = 0; domains[i]['devices'] = new Array();
								$(this).find('device').each(function() {
									if($(this).parent().parent().attr('label') == domains[i]['name']) {									
										domains[i]['devices'][ii] = $(this).text();

										ii++;
									}
								});

								domains[i]['posters'] = new Array();
								domains[i]['list'] = new Array();
								
								ii = 0; domains[i]['items'] = new Array();
								$($(this)).find('item').each(function() {
									var startDateTime = $(this).find('start').text().split('T'); var sdt = startDateTime;
									var startDate = startDateTime[0].split('-');
									var startTime = startDateTime[1].split(':');

									var endDateTime = $(this).find('end').text().split('T'); var edt = endDateTime;
									var endDate = endDateTime[0].split('-');
									var endTime = endDateTime[1].split(':');

									var startDateTime = new Date;
									var nowDateTime = new Date;
									var endDateTime = new Date;

									startDateTime.setDate(startDate[2]);
									startDateTime.setDate(startDate[2]);
									startDateTime.setMonth(startDate[1]-1);
									startDateTime.setMonth(startDate[1]-1);
									startDateTime.setYear(startDate[0]);
									startDateTime.setYear(startDate[0]);
									startDateTime.setHours(startTime[0]);
									startDateTime.setMinutes(startTime[1]);

									endDateTime.setDate(endDate[2]);
									endDateTime.setDate(endDate[2]);
									endDateTime.setMonth(endDate[1]-1);
									endDateTime.setMonth(endDate[1]-1);
									endDateTime.setYear(endDate[0]);
									endDateTime.setYear(endDate[0]);
									endDateTime.setHours(endTime[0]);
									endDateTime.setMinutes(endTime[1]);

									if((startDateTime < nowDateTime) && (endDateTime > nowDateTime)) {
										domains[i]['items'][ii] = new Array();

										domains[i]['items'][ii]['daysleft'] = Math.ceil((endDateTime.getTime() - nowDateTime.getTime()) / (1000 * 60 * 60 * 24));
	
										domains[i]['items'][ii]['poster_l'] = $(this).find('cover1').attr('url');
										domains[i]['items'][ii]['poster_m'] = $(this).find('cover2').attr('url');
										domains[i]['items'][ii]['poster_s'] = $(this).find('cover3').attr('url');

										domains[i]['posters'].push(domains[i]['items'][ii]['poster_m']);

										domains[i]['items'][ii]['trailer'] = $(this).find('trailer').attr('url');
										domains[i]['items'][ii]['trailer_w'] = $(this).find('trailer').attr('width');
										domains[i]['items'][ii]['trailer_h'] = $(this).find('trailer').attr('height');

										o = 0; domains[i]['items'][ii]['odf'] = new Array();
										$(this).find('odf').each(function() {
											domains[i]['items'][ii]['odf'][o] = new Array();

											domains[i]['items'][ii]['odf'][o]['url'] = $(this).attr('url');
											domains[i]['items'][ii]['odf'][o]['size'] = $(this).attr('size');
											domains[i]['items'][ii]['odf'][o]['duration'] = $(this).attr('duration');

											od = 0; domains[i]['items'][ii]['odf'][o]['devices'] = new Array();
											$(this).find('device').each(function() {
												domains[i]['items'][ii]['odf'][o]['devices'][od++] = $(this).text();
											});
											
											o++;
										});
										
										var l;

										domains[i]['items'][ii]['certificate'] = new Array();
										$(this).find('certificate').each(function() {
											l = get_language($(this).attr('language'), i);
											domains[i]['items'][ii]['certificate'][l] = $(this).text();
										});
										
										domains[i]['items'][ii]['synopsis'] = new Array();
										$(this).find('synopsis').each(function() {
											l = get_language($(this).attr('language'), i);
											domains[i]['items'][ii]['synopsis'][l] = $(this).text();
										});
										
										domains[i]['items'][ii]['title'] = new Array();
										$(this).find('title').each(function() {
											l = get_language($(this).attr('language'), i);
											domains[i]['items'][ii]['title'][l] = $(this).text();
										});

										domains[i]['items'][ii]['year'] = new Array();
										$(this).find('year').each(function() {
											l = get_language($(this).attr('language'), i);
											domains[i]['items'][ii]['year'][l] = $(this).text();										
										});

										domains[i]['items'][ii]['director'] = new Array();
										$(this).find('director').each(function() {
											l = get_language($(this).attr('language'), i);
											domains[i]['items'][ii]['director'][l] = $(this).text();										
										});

										domains[i]['items'][ii]['category'] = new Array();
										$(this).find('category').each(function() {
											l = get_language($(this).attr('language'), i);
											domains[i]['items'][ii]['category'][l] = $(this).text();										
										});
										
										domains[i]['items'][ii]['cast'] = new Array();
										$(this).find('cast').each(function() {
											l = get_language($(this).attr('language'), i);
											domains[i]['items'][ii]['cast'][l] = $(this).text();										
										});

										domains[i]['list'].push(ii);

										ii++;
									}
								});
								
								i++;
							});

							if(cc) {
 								country = get_country(cc.toLowerCase());
 								select_country(country);
							} else {
								if(domains.length == 1) select_country(0);
								else {
									$("#country_map").css({'background': ''});
									$("#country_select_info").text('Select the country of your phone service.');
								}

								if($.cookie('sd_se_country')) {
									select_country($.cookie('sd_se_country'));
								}
							}

							if(lc) {
								select_language(get_language(codes['language'][lc.toLowerCase()], country));
							} else if($.cookie('sd_se_language')) {
								select_language($.cookie('sd_se_language'));
							}

							if($.cookie('sd_se_device2')) {
								select_device($.cookie('sd_se_device2'));
							}
							
							$.preload(['FlowPlayerDark.swf'], {
								onFinish: function() {
									$("#preloader").fadeOut('slow', function() {
										$("body").css({'background': 'url("images/background.png") #ffffff top repeat-x'});
										$("#main_body").css({'display': 'block'});
										$("#footer").css({'display': 'block'});
										
										if(!((device >= 0) && (language >= 0) && (country >=0))) {
											$("#fader").fadeIn(0, function() {
												$("#country").css({'top': ($(window).scrollTop() + 40)});
												$("#country").css({'display': 'block'});
											});
										} else {
											if($.cookie('sd_se_eula_dontshow') != '1') {
												$("#fader").fadeIn(0, function() {
													lastCountry = 1;
													open_eula();
												});
											} else {
												display_tab();

												if($.cookie('sd_se_dontshow') != '1') {
													$("#fader").fadeIn(0, function() {
														$("#instrgen").css({'top': ($(window).scrollTop() + 40)});
														$("#instrgen").css({'display': 'block'});
													});
												}
											}
										}
									});
								}
							});
						}
					});		
				}
			});
		}
	});

	$("#country_check").click(function() {
		if((country >= 0) && (language >= 0) && (device >= 0)) {
			$("#country").css({'display': 'none'});
			
			lastCountry = 1;

			if($.cookie('sd_se_eula_dontshow') != '1') open_eula();
			else if($.cookie('sd_se_dontshow') != '1') {
				$("#instrgen").css({'top': ($(window).scrollTop() + 40)});
				$("#instrgen").css({'display': 'block'});
			} else {
				$("#fader").css({'display': 'none'});
				display_tab();
			}
		}
	});

	$("#instrgen_close").click(function() {
		instr = 0;
		$("#instrgen").css({'display': 'none'});
		$("#fader").css({'display': 'none'});
		display_tab();
	});

	$("#instrgen_faq").click(function() {
		faq_back = '#instrgen';
		$("#instrgen").css({'display': 'none'});
		$("#faq").css({'top': ($(window).scrollTop() + 40)});
		$("#faq").css({'display': 'block'});
		$("#faq_text").jScrollPane({showArrows: true, scrollbarWidth: 11});
	});

	$("#faq_close").click(function() {
		$("#faq").css({'display': 'none'});
		$(faq_back).css({'display': 'block'});
	});
	
	$("#faq_readmore").click(function() {
		window.open('http://www.sonyericsson.com/cws/support/phones/topic/playnowarenamovies/' + domains[country]['devices'][device].toLowerCase() + '?lc=' + codes['language'][domains[country]['language_codes'][language]] + '&cc=' + codes['country'][domains[country]['name']], '');
	});

	$("#instrgen_checkbox").toggle(
		function() {
			$("#instrgen_checkbox").attr({src: 'images/checkbox_checked.png'});
			if(instr == 1) $.cookie('sd_se_info_dontshow', '1', {expires: 30});
			else $.cookie('sd_se_dontshow', '1', {expires: 30});
		},
		function() {
			$("#instrgen_checkbox").attr({src: 'images/checkbox.png'});
			if(instr == 1) $.cookie('sd_se_info_dontshow', '0', {expires: 30});
			else $.cookie('sd_se_dontshow', '0', {expires: 30});
		}
	);

	$("#movie_selection_change").click(function() {
		var l = language;
		var d = device;

		$("#fader").css({'display': 'block'});
		$("#country").css({'top': ($(window).scrollTop() + 40)});
		$("#country").css({'display': 'block'});		

		$("#movie_selection_registered_country_info").text('');
		$("#movie_selection_registered_country").text('');
		$("#movie_selection_your_language_info").text('');
		$("#movie_selection_your_language").text('');
		$("#movie_selection_handset_model_info").text('');
		$("#movie_selection_handset_model").text('');

		select_country(country);
		select_language(l);
		select_device(d);
		fill_html_gaps();
	});

	$(".movie_selection_download").hover(
		function() {
			var selection = $(this).parent().get(0).id.split("_");
			var row = selection[2];
			var col = selection[3];
			var i = 5 * (row - 1) + (col - 1);
		
			$("#alt").css({'display': 'block'});
			$("#alt_centre").text(strings['main_download']);
		},
		function() {
		$("#alt").css({'display': 'none'});
		$("#alt_centre").text('');
	}
	);

	$(".movie_selection_download").click(function() {
		prepare_metadata($(this).parent().get(0).id);

		$("#fader").css({'display': 'block'});

		$("#pg").css({'top': ($(window).scrollTop() + 40)});
		$("#pg").css({'display': 'block'});
	});

	$(".movie_selection_poster").hover(
		function() {
			var selection = $(this).parent().get(0).id.split("_");
			var row = selection[2];
			var col = selection[3];
			var i = 5 * (row - 1) + (col - 1);

			var daysleft = domains[country]['items'][filtered_list[i]]['daysleft'];
			
			if(daysleft > 30) {
				$("#movie_selection_" + row + "_" + col + " .movie_selection_indicator").css({'color': '#39d01f'});
			}
			if((daysleft < 30) && (daysleft > 7)) {
				$("#movie_selection_" + row + "_" + col + " .movie_selection_indicator").css({'color': '#ffffff'});
			}
			if(daysleft <= 7) {
				$("#movie_selection_" + row + "_" + col + " .movie_selection_indicator").css({'color': '#ff0000'});
			}

			$("#alt").css({'display': 'block'});

			$("#alt_centre").text(domains[country]['items'][filtered_list[i]]['title'][language]);
		},
		function() {
			var selection = $(this).parent().get(0).id.split("_");
			var row = selection[2];
			var col = selection[3];
			var i = 5 * (row - 1) + (col - 1);

			var daysleft = domains[country]['items'][filtered_list[i]]['daysleft'];
			
			if(daysleft > 30) {
				$("#movie_selection_" + row + "_" + col + " .movie_selection_indicator").css({'color': '#d1d1d1'});
			}
			if((daysleft < 30) && (daysleft > 7)) {
				$("#movie_selection_" + row + "_" + col + " .movie_selection_indicator").css({'color': '#6c6c6b'});
			}
			if(daysleft <= 7) {
				$("#movie_selection_" + row + "_" + col + " .movie_selection_indicator").css({'color': '#dc0000'});
			}

			$("#alt").css({'display': 'none'});
			$("#alt_centre").text('');
		}
	);

	$(".movie_selection_poster").click(function() {
		$("#fader").css({'display': 'block'});
		
		prepare_metadata($(this).parent().get(0).id);
		show_trailer();
	});

	$("#pg_cancel").hover(
		function() {
			$("#alt").css({'display': 'block'});
			$("#alt_centre").text(strings['pg_cancel']);
		},
		function() {
			$("#alt").css({'display': 'none'});
			$("#alt_centre").text('');
		}
	);

	$("#pg_cancel").click(function() {
		$("#pg").css({'display': 'none'});

		if(lastTrailer) show_trailer();
		else $("#fader").css({'display': 'none'});
	});

	$("#pg_download").hover(
		function() {
			$("#alt").css({'display': 'block'});
			$("#alt_centre").text(strings['pg_download']);
		},
		function() {
			$("#alt").css({'display': 'none'});
			$("#alt_centre").text('');
		}
	);

	$("#pg_download").click(function() {
		$("#pg").css({'display': 'none'});

		if($.cookie('sd_se_eula_dontshow') != '1') open_eula();
		else {
			window.open(currentVideo);

			if($.cookie('sd_se_info_dontshow') != '1') {
				// INSTR
				instr = 1;
				open_help();
			} else {
				$("#fader").css({'display': 'none'});
			}
		}
	});

	$("#eula_agree").click(function() {
		if(lastCountry != 1) {
			window.open(currentVideo,  strings['pg_download']);
	
			$("#eula").css({'display': 'none'});
	
			if($.cookie('sd_se_info_dontshow') != '1') {
				// INSTR
				instr = 1;
				open_help();
			} else {
				$("#fader").css({'display': 'none'});
			}
		} else {
			display_tab();
			$.cookie('sd_se_eula_dontshow', 1, {expires: 30});

			$("#eula").css({'display': 'none'});
			$("#eula_close").css({'display': 'none'});
			$("#eula_cancel").css({'display': 'block'});
			$("#eula_agree").css({'display': 'block'});
	
			if(lastTrailer) show_trailer();
			else if(lastCountry == 1) {
				$("#instrgen").css({'top': ($(window).scrollTop() + 40)});
				$("#instrgen").css({'display': 'block'});
				lastCountry = 0;
			} else $("#fader").css({'display': 'none'});		
		}
	});

	$("#eula_cancel").click(function() {
		$("#eula").css({'display': 'none'});
		$("#eula_agree").css({'display': 'block'});

		if(lastTrailer) show_trailer();
		else if(lastCountry == 1) {
			$("#country").css({'top': ($(window).scrollTop() + 40)});
			$("#country").css({'display': 'block'});

			select_country(country);
			select_language(language);
			select_device(device);
		} else $("#fader").css({'display': 'none'});
	});
	
	$("#eula_close").click(function() {
		$("#eula").css({'display': 'none'});
		$("#fader").css({'display': 'none'});
	});

	$("#trailer_download").hover(
		function() {
			$("#alt").css({'display': 'block'});
			$("#alt_centre").text(strings['main_download']);
		},
		function() {
			$("#alt").css({'display': 'none'});
			$("#alt_centre").text('');
		}
	);

	$("#trailer_download").click(function() {
		lastTrailer = $("#trailer_player").html();

		hide_trailer();

		$("#pg").css({'top': ($(window).scrollTop() + 40)});
		$("#pg").css({'display': 'block'});
	});

	$("#trailer_close").click(function() {
		clear_metadata();

		hide_trailer();

		$("#fader").css({'display': 'none'});
	});

	$("#help_icona").click(function() {
		open_help();
	});
	
	$(".tab_1").click(function() {
		clear_tab();
		order_new_first();
		display_tab();

		$("#tab_1_sel").css({'display': 'block'});
		$("#tab_1_unsel").css({'display': 'none'});
		$("#tab_2_sel").css({'display': 'none'});
		$("#tab_2_unsel").css({'display': 'block'});
	});

	$(".tab_2").click(function() {
		clear_tab();
		order_new_last();
		display_tab();

		$("#tab_1_sel").css({'display': 'none'});
		$("#tab_1_unsel").css({'display': 'block'});
		$("#tab_2_sel").css({'display': 'block'});
		$("#tab_2_unsel").css({'display': 'none'});
	});
});

$().mousemove(function(e) {$("#alt").css({'top': e.pageY + 10, 'left': e.pageX + 10});});

