var display_state = "player";

function toggle_display() {
	$j.each(["player","seo_body","seo_meta","links"], function(i,display_str) {

		var div_selector = "#"+display_str+"_code";
		if(display_str == display_state) {
			$j(div_selector).show();
		}
		else {
			$j(div_selector).hide();
		}
	});
}

$j(document).ready(function(){
	
	toggle_display();
	$j("#player_id_code").focus();
	$j("#player_id_code").select();
	
    $j("#project_id_select").click(function(){
    	$j("#project_id_input").focus();
    	$j("#project_id_input").select();
    });
	
	$j('.ldt_json_link').click(function(e) {
    	e.preventDefault();
 		link = $j(e.target);
 		$j("#res_link_{{player_id}}").load(link.attr("href"));
 		$j("#res_link_{{player_id}}").show();
 		return false;
    });

    $j('#player_button').click(function(e) {
    	display_state = "player";
    	toggle_display();
    });
    $j('#seo_body_button').click(function(e) {
    	display_state = "seo_body";
    	toggle_display();
    });
    $j('#seo_meta_button').click(function(e) {
    	display_state = "seo_meta";
    	toggle_display();
    });
    $j('#links_button').click(function(e) {
    	display_state = "links";
    	toggle_display();
    });

   $j("#copy_button").click(function(e) {
 	   alert($j("#"+display_state+"_code").html());
   });
   
   $j("#player_button").click(function(){
	   $j("#player_id_code").focus();
      	$j("#player_id_code").select();
   });
	
   $j("#seo_body_button").click(function(){
   	$j("#seo_body_id_code").focus();
      	$j("#seo_body_id_code").select();
   });
   $j("#seo_meta_button").click(function(){
   	$j("#seo_meta_id_code").focus();
      	$j("#seo_meta_id_code").select();
   });
    
});

function toggle() {
	wrapper="wrapper_"+player_id;
	wrapper_id="#"+wrapper;
	var element = document.getElementById(wrapper);
	var text = document.getElementById("displayPlayer");
	var type_embed=document.getElementById("type_embed_div");
	if(element.style.display==="none") {
		if (type_embed.checked){
			var new_code=document.getElementById("embed_code_var").value;
			$j(wrapper_id).html(new_code);
		}
		else{
			wrapper = "#wrapper_"+player_id;
			iframeUrl= iframe_url+"&polemic="+polemic+"&show_mic_record="+show_mic_record+"&annotations_list="+annotations_list+"";
			player_width=570;
			player_height=678;
			if(annotations_list!='False'){
				player_width+=430;
			}
			if(polemic!='False'){
				player_height+=100;
				
			}
			if(show_mic_record!='False'){
				
				player_height+=55;
			}
			frame = document.createElement("iframe");
		    frame.width = player_width;
		    frame.height = player_height;
		    frame.src = iframeUrl;
		    frame.frameBorder = 0;
		   	frame.seamless = "seamless";
		   	$j(wrapper).html(frame);
			
		}
    	element.style.display="block";
		text.innerHTML = translate_hide;
  	}
	else {
		$j(wrapper_id).html('');
    	element.style.display="none";
		text.innerHTML = translate_show;
	}
} 

function div_event(){
	wrapper = "wrapper_"+player_id;
	wrapper_id = "#"+wrapper;
	var element = document.getElementById(wrapper);
	var new_code=document.getElementById("embed_code_var").value;
	var code_embed=document.getElementById("player_id_code");
	if(element.style.display=="block"){
		$j(wrapper_id).html(new_code);
	}
	code_embed.value= new_code;
	$j("#player_id_code").focus();
	$j("#player_id_code").select();	
}	

function iframe_event(){
	var code_embed=document.getElementById("player_id_code");
	wrapper = "wrapper_"+player_id;
	wrapper_id = "#"+wrapper;
	var element = document.getElementById(wrapper);
	iframeUrl= iframe_url+"&polemic="+polemic+"&show_mic_record="+show_mic_record+"&annotations_list="+annotations_list+"";
	player_width=570;
	player_height=678;
	if(annotations_list!='False'){
		player_width+=430;
	}
	if(polemic!='False'){
		player_height+=100;
		
	}
	if(show_mic_record!='False'){
		
		player_height+=55;
	}
	
	if(element.style.display=="block"){
		frame = document.createElement("iframe");
	    frame.width = player_width;
	    frame.height = player_height;
	    frame.src = iframeUrl;
	    frame.frameBorder = 0;
	   	frame.seamless = "seamless";
	   	$j(wrapper_id).html(frame);
	}
   	iframe_code="<iframe src='"+iframeUrl+"' width='"+player_width+"' height='"+player_height+"' seamless='seamless'></iframe>";
	$j("#player_id_code").val(iframe_code);
	$j("#player_id_code").focus();
	$j("#player_id_code").select();
}	
