﻿var arr_cinema = new Array();
var strCity = "";
var hashMovie;
var hashTheater;
var theaterId = -1;
//var twoSelectors = false;
window.onload = function () {
    if( location.href.indexOf( "#" ) != -1 ) {
        var preHash = location.hash;
        var theHash = preHash.substr(1);
        if( theHash.indexOf( "&" ) != -1 ) {
            var hashArray = theHash.split( "&" );
            var hashArrayTheater = hashArray[0].split( "=" );
            var hashArrayMovie = hashArray[1].split( "=" );
            hashMovie = hashArrayMovie[1];
            hashTheater = hashArrayTheater[1];
            //alert(hashMovie);
            //alert(hashTheater);
            var where = $("DIV#"+hashMovie).attr("where");
	    var mcu = $("DIV#"+hashMovie).attr("mcu");
            //var where = document.getElementById(hashMovie + "_" + hashTheater)
            //alert(where);
            //twoSelectors = true;
            //document.getElementById("cinema_" + hashTheater).setAttribute("class", "activ");
            display_cinema_events( hashTheater );
	    theaterId = hashTheater;
            display_events( hashMovie, 0, where, mcu );
            
        } else {
            var hashArray = theHash.split( "=" );
            if( hashArray[0].indexOf("movie") != -1 ) {
                hashMovie = hashArray[1];
                hashTheater = null;
                var where = $("DIV#"+hashMovie).attr("where");
		var mcu = $("DIV#"+hashMovie).attr("mcu");
                //twoSelectors = false;
                display_events( hashMovie, 0, where, mcu );
            } else if ( hashArray[0].indexOf("cinema") != -1 ) {
                hashTheater = hashArray[1];
                hashMovie = null;
                //twoSelectors = false;
                display_cinema_events( hashTheater );
            } else {
                twoSelectors = false;
                hashMovie = null;
                hashTheater = null;
            }
        }
        
        
    } else {
        hashMovie = null;
        hashTheater = null;
    }
}
function display_cinema_events(id) {
	$(".ico_close").show();
    //if( ($("#cinema_"+id).attr("id") != undefined) || (id == 0) ) {
    if( ( $("#cinema_" + id ).length ) || ( id == 0 ) ) {
		smith = 0;
        var cinemaMcu = $("#cinema_" + id).attr("mcu");
		theaterId = id;
        location.hash="#cinema="+id;

	events_div = $("DIV", $("#cinema_events"));
	events_div_activ = $("DIV.activ", $("#cinema"));
	for (i=0;i<events_div.length;i++) {
		if ($(events_div[i]).attr("class") == 'activ' && $("#cinema_"+id).attr("class") == undefined && events_div_activ.length == 0) { 
			smith = $(events_div[i]);
		} else {
			id_place = $(events_div[i]).attr("id").split("_");
			if (in_array(id_place[0], arr_cinema[id]) == true) {
				$(events_div[i]).attr("class", "borc");
			} else {
				$(events_div[i]).attr("class", "grey");
			}
		}
	}
	
	if (smith == 0) {
		if ($("#cinema_"+id).attr("id")!=undefined) {
			id_place = $("#cinema_"+id).attr("id").split("_");
			$("#cinema_detal_h2_"+id).html('<a href="'+strCity+'/cinema/place/'+cinemaMcu+'">'+$("#cinema_"+id).text()+'</a>');
		}
				
		if ($("#cinema_detal_"+id).html() != null) {
			re = /\" style=\"WIDTH:/g;
			rep_data = $("#cinema_detal_"+id).html().replace(re, 'div_top_th" style="WIDTH:');
			$("#detal_right_data").html(rep_data);
		}
		//cinema_div = $("DIV[@cl=cinema_left]", $("#cinema"));
		cinema_div_tmp = $("DIV", $("#cinema"));
		for (i=0;i<cinema_div_tmp.length;i++) {
			if ($(cinema_div_tmp[i]).attr("cl")!=undefined) {
				$(cinema_div_tmp[i]).attr("class", "greyb");
			} else {
				if ($(cinema_div_tmp[i]).attr("class") != "ddd2") {
					$(cinema_div_tmp[i]).attr("class", "");
				}
			}
		}
		$("#cinema_"+id).attr("class", "activ");
	} else {
		where = $("#cinema_events DIV.activ");
		$("#cinema_"+id).attr("class", "activ");
		display_events($(smith).attr("id"), 1, $(where).attr("where"), $(where).attr("mcu") );
	}
    }
}
function display_events(id, type, where, mcu) {
	$(".ico_close").show();
    //if ( $("#"+id).attr("id") != undefined ) {
    if ( $("#" + id ).length ) {
	//alert( $("#"+id).attr("id") );
	if ($("#"+id).attr("class") != 'grey') {
            
                //alert(genreText);
		cinema_tpl = $("DIV.activ", $("#cinema"));
		($("#"+id).attr("class") == "activ" && cinema_tpl.length == 1 && type==0) ? x = 1 : x = 0;
		events_div = $("DIV", $("#cinema_events"));
		for (i=0;i<events_div.length;i++) {
			if ($(events_div[i]).attr("class") == 'activ') { 
				$(events_div[i]).attr("class", "");
			}
		}
		$("#"+id).attr("class", "activ");
		id_tpl = id.split("_");
		activ_events = $("DIV.activ", $("#cinema_events"));
                location.hash = "#movie=" + id_tpl[0] + "_" + id_tpl[1];
                genreText = $("DIV#"+id_tpl[0]+"_"+id_tpl[1]).attr("genre");
                out_cinema = '<table cellpadding="0" cellspacing="0" class="right_tbl"><tr><td colspan="2"><h2><a href="/'+where+'/events/'+mcu+'">'+$(activ_events[0]).text()+'</a></h2>';
                out_cinema += '<div class="genre">' + genreText + '</div>';
                out_cinema += '</td></tr><tbody><tr class="tbl_shapka"><td><div style="padding-left:8px;">Кинотеатр</div></td><td class="div_top_th"><div class="ico_time">Время</div><div class="ico_tg">Взр.</div><div>Студ.</div><div>Дет.</div></td></tr></tbody>';		
		if (cinema_tpl.length == 0 || x == 1) {
			for (var key in arr_cinema) {
				if (in_array(id_tpl[0], arr_cinema[key]) == true) {
					$("#cinema_"+key).attr("class", "borc");
					var cinemaMcu = $("#cinema_" + key).attr("mcu");
					out_cinema += '<tr class="td_bot_punkt"><td style="padding:6px 0 0 10px; width:120px;">';
					
					if ($("#cinema_"+key).attr("lang") == "fav") {
						out_cinema += '<img src="./images/ico_hard.png" width="10" height="9" style="margin:0px 6px 0 -10px;" />';
					}
					
					out_cinema +='<a href="'+strCity+'/cinema/place/'+cinemaMcu+'" title="'+$("#cinema_"+key).text()+'">'+$("#cinema_"+key).text()+'</a></td><td  style="padding:6px 0 0 0;" id="time" style="font-size:70%" class="td_price">';
					div_events = $("TD", $("#tbody_place_"+id_tpl[0]+"_"+key));
					re = /\" style=\"WIDTH:/g;
					rep_data = $(div_events[1]).html().replace(re, 'div_top_th" style="WIDTH:');
					out_cinema += rep_data;
					out_cinema += '</td></tr>';
				} else {
					$("#cinema_"+key).attr("class", "greyb");
				}
			}
			out_cinema += '</table>';
			$("#detal_right_data").html(out_cinema);
		} else {
			cinema_div = $("DIV", $("#cinema"));
			/*for (i=0;i<cinema_div.length;i++) {

				if ($(cinema_div[i]).attr("dir") == "smith") {
					alert("smith");
				}
			}*/
			for (i=0;i<cinema_div.length;i++) {
				if ($(cinema_div[i]).attr("class") == "activ") {
					title = $(cinema_div[i]).text();
					id_cinema = $(cinema_div[i]).attr("id").split("_");
				}
			}
			id_events = id.split("_");
			td_table = $("TD", $("#tbody_place_"+id_events[0]+"_"+id_cinema[1]));
			out = '';                        
						/*address = $("DIV.address", "#cinema_address_phones_"+theaterId);
                        phones = $("DIV.phones", "#cinema_address_phones_"+theaterId);*/
                        genreText = $("DIV#"+id_events[0]+"_"+id_events[1]).attr("genre");
                        //alert($(address).html());
						addressphones = $("DIV", "#cinema_address_phones_"+theaterId);
                        addressText = $(addressphones[0]).html();
                        phonesText = $(addressphones[1]).html();
			var cinemaMcu = $("#cinema_" + id_cinema[1]).attr("mcu");
                        switch(theaterId) {
                            case -1: location.hash="#cinema=" + id_events[1] + "&movie=" + id_events[0]+"_"+id_events[1];
                                break;
                            default:
			    location.hash="#cinema=" + theaterId + "&movie=" + id_events[0]+"_"+id_events[1];
                        }
                        //address = 0; phones = 0;
			out += '<table class="right_tbl right_one_events"><tr><td colspan="2" style="padding:0" class="adressphones"><h2><a href="'+strCity+'/cinema/place/'+cinemaMcu+'">'+title+'</a></h2>';
                        out += '<div class="address">' + addressText + ' </div>';
                        out += '<div class="phones">' + phonesText + ' </div>';
                        out += '</td></tr><tr><td colspan="2" class="h3cinema"><h3><a href="/'+where+'/events/'+mcu+'">'+$(td_table[0]).text()+'</a></h3>';
                        out += '<div class="genre">' + genreText + '</div></td></tr>';
                        
						//out += '<tbody><tr class="tbl_shapka"><td><div style="padding-left:8px;">Кинотеатр</div></td><td class="div_top_th"><div class="ico_time">Время</div><div class="ico_tg">Взр.</div><div>Студ.</div><div>Дет.</div></td></tr></tbody><tr><td class="we12">';	
						
						out += '<tbody class="tbl_shapka"><tr><td class="div_top_th"><div>Время</div><div>Взр.</div><div>Студ.</div><div>Дет.</div></td></tr></tbody><tr><td class="we12">';
			for (i=1;i<td_table.length;i++) {
				out += $(td_table[i]).html();
			}
			out += '</td></tr></table>';
			$("#detal_right_data").html(out);
			}
	} else {
		reset_cinema_events();
		display_events(id, 0, where, mcu);
	}
    }
}
function reset_cinema_events() {
	display_cinema_events(0);
	events_div = $("DIV", $("#cinema_events"));
	for (i=0;i<events_div.length;i++) {
		$(events_div[i]).attr("class", "");
	}
	$("#detal_right_data").html('');
}
function in_array(needle, haystack, strict) {
    var found = false, key, strict = !!strict;
    for (key in haystack) {
        if ((strict && haystack[key] === needle) || (!strict && haystack[key] == needle)) {
            found = true;
            break;
        }
    }
    return found;
}
function smith() {
	cinema_div = $("DIV[@qwe=div_hide]", $("#cinema"));
	for (i=0;i<cinema_div.length;i++) {
		$(cinema_div[i]).css("display", "none");
	}
	cinema_div_one = $("DIV[@div-time=1]", $("#cinema"));
	for (i=0;i<cinema_div_one.length;i++) {
		$(cinema_div_one[i]).css("width", "50px");
	}
		
	/*cinema_div = $("DIV[@cl=cinema_left]", $("#cinema"));
	for (i=0;i<cinema_div.length;i++) {
		$(cinema_div[i]).attr("class", "");
	}
	$("#cinema_"+id).attr("class", "activ");*/
}

function ico_close() {
	divs = $("DIV", $("#cinema"));
	for (i=0; i<divs.length; i++) {
		if ($(divs[i]).attr("id")!='') {
			if ($(divs[i]).attr("id").length <=10) {
				$(divs[i]).attr("class", "cinema_left");
			}
		}
	}
	
	divs2 = $("DIV", $("#cinema_events"));
	for (i=0;i<divs2.length;i++) {
		$(divs2[i]).attr("class", "");
	}
	
	$("#detal_right_data").html($("#detal_right_data_old").html());
	$(".ico_close").hide();
}
