var global_uuid = "";


//var tiles = [1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30];
var tiles = [24, 5, 10, 19, 23, 4, 25, 17, 18, 3, 11, 9, 12, 28, 16, 29, 22, 26, 30, 15, 27, 13, 20, 14, 8, 2, 21, 7, 1, 6];

var tiling_speed = 5;

$(document).ready(function(){

$("div.header_menu_element").append("<img src=\"images/hmenu-w-corner.png\" class=\"hmenu_w_corner\" alt=\"\">");
$("div.header_menu_element").append("<img src=\"images/hmenu-e-corner.png\" class=\"hmenu_e_corner\" alt=\"\">");
$("div.nav_menu_element, div.nav_menu_element2").prepend("<img src=\"images/nmenu-w-bullet.png\" class=\"nmenu_w_bullet\" alt=\"\">");

$("div.nav_menu_element span").click( function(){
	var that = $(this).parent("div.nav_menu_element")[0];
	if (!that['id']) return false;

	$("div.nav_menu_element").removeClass("nav_menu_element_active");
	$("div.nav_menu_element2").removeClass("nav_menu_element2_active");
	$(that).addClass("nav_menu_element_active");
	if ( $(that).children("div.default_subgroup").length>0 ){
		$(that).children("div.default_subgroup").addClass("nav_menu_element2_active");
		$("div.r_col").children("span").hide();
		//$("span."+$(that).children("div.default_subgroup")[0].id+"_group").show();
		$("span."+$(that).children("div.default_subgroup")[0].id+"_group").fadeIn(300);
	} else {
		$("div.r_col").children("span").hide();
		$("span."+that.id+"_group").fadeIn(300);
		//$("span."+that.id+"_group").show();
	}
	
	if (that.id=='demo'){
		$.get("http://"+location.host+"/provide_uuid.pl", "text", readyToPlay);
	} else {
		$("div.plugin_box").empty();
	}
	
});

$("div.nav_menu_element2").click( function(){
	$("div.nav_menu_element").removeClass("nav_menu_element_active");
	$("div.nav_menu_element2").removeClass("nav_menu_element2_active");
	$(this).parents("div.nav_menu_element").addClass("nav_menu_element_active");
	$(this).addClass("nav_menu_element2_active");
	if ($(this).hasClass("ah_stop_me")){
		serve(this);
		return false;
	}
	$("div.r_col").children("span").hide();
	//$("span."+this.id+"_group").show();
	$("span."+this.id+"_group").fadeIn(300);
});


function readyToPlay(uuid){
		if (!uuid) return false;
		uuid.replace("\n", "");
		//if (uuid.length!=36) return false;
		global_uuid = uuid;
		setWidgetUUID(uuid);
		setWidgetUrl("avscript://st.active-video.ru/90c82384-e5e6-415f-9db3-900b22255919/default.sce");
		invokePlugin("div.plugin_box", AUTO_PLAY_WIDGET);
}

$("div.v_banner").click( function(){
	var b_id = this.id;
	if (!b_id) return false;
	switch(b_id){
	case 'v_banner1':
		b_uuid = 5;
		break;
	case 'v_banner2':
		b_uuid = 3;
		break;
	case 'v_banner3':
		b_uuid = 4;
		break;
	case 'v_banner4':
		b_uuid = 1;
		break;
	case 'v_banner5':
		b_uuid = 2;
		break;
	case 'v_banner6':
		b_uuid = 7;
		break;
	case 'v_banner7':
		b_uuid = 8;
		break;
	case 'v_banner8':
		b_uuid = 6;
		break;
	}
	$.get("http://"+location.host+"/demo_showadv.pl", {"user_uuid": global_uuid, "adv_uuid": b_uuid});
	$(this).children("img").animate({
		width: "+=20px",
		height: "+=20px",
		left: "-=13px",
		top: "12px",
		borderWidth: "6px"
		}, 100, 
		 function(){
			$(this).animate({
			width: "-=20px",
			height: "-=20px",
			left: "+=13px",
			top: "20px",
			borderWidth: "0"
				}, 200)
		
		}
		);
});

function serve(what){
	return false;
}

function localGoto(group_name){
	if ( $("div#"+group_name).hasClass("nav_menu_element2") ) $("div#"+group_name).trigger("click");
	else $("div#"+group_name+" span").trigger("click");
}

$(".local_goto").click( function(){
	var dest = $(this).children("input").val();
	if (dest) localGoto(dest);
});

//$("div#sites span").trigger("click");

rebind();

function rebind(){
$("div.qa_window").unbind("mouseover");
$("div.qa_window").unbind("mouseout");
$("div.qa_window").unbind("click");

$("div.qa_window").not(".qa_window_current").mouseover( function(event){
	event.stopPropagation();
	$("div.qa_window_current").removeClass("qa_opaque");
	$("div.qa_window").removeClass("qa_window_hover");
	$(this).addClass("qa_window_hover");
	//$(this).children("div.qa_fill").animate({ 
    //   opacity:0.9
    // }, 100 );
});
$("div.qa_window").not(".qa_window_current").mouseout( function(event){
	event.stopPropagation();
	$("div.qa_window").removeClass("qa_window_hover");
	$("div.qa_window_current").addClass("qa_opaque");
	//$(this).children("div.qa_fill").animate({ 
    //    opacity:0.3
    // }, 100 );
});

$("div.qa_window").not("div.qa_window_current").click( function(){
	$("div.qa_window_current").show();
	$("div.qa_window_current").removeClass("qa_opaque");
	$("div.qa_window_current").removeClass("qa_window_current");
	$("div.qa_window").removeClass("qa_window_hover");
	$(this).addClass("qa_window_current");
	$(this).addClass("qa_opaque");
	var target = this.id.substring(7);
	if (target.length==0) return false;

	$("div.stained_container").removeClass("now_cccptv");
	$("div.stained_container").removeClass("now_diving");
	$("div.stained_container").removeClass("now_expo");
	$("div.stained_container").removeClass("now_religion");
	$("div.stained_container").removeClass("now_cinema");
	$("div.stained_container").addClass("now_"+target);

	//$("div.stained_bg").fadeOut(100);
	//$("div.stained_"+target).fadeIn(500);

	$("div.stained_puzzle img.puzzle_"+tiles[0]).not(".puzzle_"+target).fadeOut(tiling_speed);
	$("div.stained_puzzle img.puzzle_"+tiles[0]).filter(".puzzle_"+target).fadeIn(tiling_speed, returnWarpNextTile(1, target));

	rebind();
});

}

for (j=1;j<=tiles.length;j++){
	$("div.stained_puzzle").append("<img src=\"images/puzzle-cccptv-"+j+".jpg\" class=\"puzzle_cccptv puzzle_"+j+"\" alt=\"\">");
	$("div.stained_puzzle").append("<img src=\"images/puzzle-religion-"+j+".jpg\" class=\"puzzle_religion puzzle_"+j+"\" alt=\"\">");
	$("div.stained_puzzle").append("<img src=\"images/puzzle-cinema-"+j+".jpg\" class=\"puzzle_cinema puzzle_"+j+"\" alt=\"\">");
	$("div.stained_puzzle").append("<img src=\"images/puzzle-diving-"+j+".jpg\" class=\"puzzle_diving puzzle_"+j+"\" alt=\"\">");
	$("div.stained_puzzle").append("<img src=\"images/puzzle-expo-"+j+".jpg\" class=\"puzzle_expo puzzle_"+j+"\" alt=\"\">");
}
$("div.stained_puzzle img.puzzle_cccptv").show();

function returnWarpNextTile(tile, target){
	return function(){
		var next_tile = tiles[tile+1];
		var cur_tile = tiles[tile];
		$("div.stained_puzzle img.puzzle_"+cur_tile).not(".puzzle_"+target).fadeOut(tiling_speed);
		if (next_tile) $("div.stained_puzzle img.puzzle_"+cur_tile).filter(".puzzle_"+target).fadeIn(tiling_speed, returnWarpNextTile(tile+1, target));
		else $("div.stained_puzzle img.puzzle_"+cur_tile).filter(".puzzle_"+target).fadeIn(tiling_speed);
	}
}

});
 

