// © Emily Ridge 2011
// www.emilyridge.com

//This function acts as a cick handler for the internal links. 
//It calls up the XML file and searches using the given id.
function loadXML(id, fragment_type)
{	
	//Declaring variables
	var $fragment;
	var fragment_id;
	var $title;
	var $description;
	var $links;
	var video;
	var name;
	
	//Loading XML file
	$.get("campaign.xml",function(data){
	  $(data).find('fragment').each(function(){
	
            $fragment = $(this);
			
			//This is also used to add a class to the selected link
            fragment_id = $fragment.attr('id');
			
			$title = $fragment.find('title').text();
            $description = $fragment.find('description').text();
			
			if(fragment_id==id)
			{
				//Adding a class to the selected link			
				//document.getElementById(fragment_id).classList.add('campaign_selected_inner_links');
				$('#'+fragment_id).addClass('campaign_selected_inner_links');
				
				//Adding the title and description
				//document.getElementById('fragment-'+fragment_type+'-title').innerHTML=title;
				$('#fragment-'+fragment_type+'-title').html($title);
				
				//Needed for section showing video
				if(fragment_id=='Etudiants-6'||fragment_id=='Ecoles-6')
				{
					//Remove old links
					$('#fragment-'+fragment_type+'-links').html('');
					
					name = $fragment.find('name').text();
					video = $fragment.find('video').text();
					$('#fragment-'+fragment_type+'-text').html('<span class="campaign_test_name">'+name+'</span><br/><span class="campaign_test_description">'+$description+'</span><iframe width="416" height="241" src="'+video+'" frameborder="0" allowfullscreen></iframe>');
				}
				
				else if(fragment_id=='fragment-1'||fragment_id=='fragment-2'||fragment_id=='fragment-3')
				{
					//Remove old links
					$('#fragment-'+fragment_type+'-links').html('');
					$('#fragment-'+fragment_type+'-text').html($description);
				}
				else
				{
					$('#fragment-'+fragment_type+'-text').html($description);
					$links='<span>Pour en savoir plus</span><ul>';
					
					$fragment.find('single_link').each(function() 
					{ 
						$links=$links+'<li><a target="_blank" title="'+$(this).find('single_title').text()+'" href="'+$(this).find('single_url').text()+'">'+$(this).find('single_title').text()+'</a></li>';
					});   
					
					$links=$links+'</ul>';
					
					//Insert external links into page
					$('#fragment-'+fragment_type+'-links').html($links);
				}	
			}
			
			else
			{
				//Removing the class for all non selected links			
				$('#'+fragment_id).removeClass('campaign_selected_inner_links');
			}
        });
   });
}

