showVideoPopUp = function(embed_code, vdo_source)
{
   dimensions = getVideoDimensions(embed_code)
   newWindow= window.open ("", "","status=1,width="+dimensions[0]+",height="+dimensions[1]+",resizable=1");
   
	content = "<html>"
				+"<head><title>HD Centre Video</title></head>"
				+"<body style='padding:0px; margin:0px;'>"
					+'<div style="padding: 30px; background:#D8C39E; padding-bottom:50px;">'
					+embed_code
					+'<div style="font-size:12px;color:#9A221D">Source: '+vdo_source+'</div>'
					+"</div>"
				+"</body>"
			  +"</html>";

   var doc = newWindow.document;
   doc.open("text/html", "replace");
   doc.write(content);
   doc.close();
}

showInfoDisclaimer = function()
{  
  newWindow= window.open ("", "","status=1, width=600, height=322, resizable=1");
	content = "<html>"
				+"<head><title>Disclaimer</title></head>"
				+"<body style='padding:0px; margin:0px;'>"
					+'<div style="padding: 10px 30px 30px 50px; background:#eeeae2 url(\'/sites/all/themes/hdcentre/img/logo-grey.gif\') no-repeat bottom right;">'
					  +'<p>This web site contains pages automatically generated with content from the third parties and links to other web sites. The HD Centre takes '
    				+'no responsibility whatsoever for such third-party information. Moreover, we have no knowledge or control of information contained in other '
    				+'web sites and take no responsibility whatsoever for such information.</p>'
    				+'<p>We have made - and will continue to make - efforts to provide accurate and up-to-date information. However, we make no warranty or '
    				+'representation, express or implied, that the information contained or referenced herein is accurate or complete.</p>'
    				+'<p>HD Centre website users are encouraged to submit their feedback, suggestions and opinions in order to continually '
    				+'improve the information sharing experience by writing to <b><a href="mailto:pr@hdcentre.org">pr@hdcentre.org</a></b></p>'
    			+"</div>"
				+"</body>"
			  +"</html>";

   var doc = newWindow.document;
   doc.open("text/html", "replace");
   doc.write(content);
   doc.close();
}


showRelatedTopicInfo = function()
{
  hardcoded_topics = ($(this).hasClass('hardcoded-topics')) ? true : false;
  newWindow= window.open ("", "","status=1, width=500, height=218, resizable=1");
	content = "<html>"
				+"<head><title>Related Topics</title></head>"
				+"<body style='padding:0px; margin:0px;'>"
					+'<div style="padding: 10px 30px 30px 50px; background:#eeeae2 url(\'/sites/all/themes/hdcentre/img/logo-grey.gif\') no-repeat bottom right;">'
					  +'<p>Related Topics are ';
	content += (hardcoded_topics == false) ? 'automatically ' : '';
	content +=  'generated by the NewsCred Platform from third party news articles from the last 30 days. Click on'
					    +' a Related Topic to get more information on that topic. This feature complements the editorial content already on the HD Centre web site,'
					    +' and helps us ensure we remain a useful channel of information for you on topics relevant to conflict and peacemaking</p>'
					    +'<a href="javascript:showInfoDisclaimer();">Disclaimer</a>'
    			  +"</div>"
				+"</body>"
				+'<script type="text/javascript" src="/sites/all/themes/hdcentre/js/newscred.js"></script>'
			  +"</html>";

   var doc = newWindow.document;
   doc.open("text/html", "replace");
   doc.write(content);
   doc.close();
}

showImagePopUp = function(img_element)
{  
  newWindow= window.open ("", "","status=1, width=480, height=361, resizable=1");
	content = "<html>"
				    +"<head><title>NewsCred Image</title></head>"
				    +'<body style="padding-top:30px; margin:0px; background:#D8C39E">'
					    +'<center><img src="'+$(img_element).attr('src')+'"/></center>'
					    +'<p style="float:right;"><img src="/sites/all/themes/hdcentre/img/logo.gif"/></p>'
				    +"</body>"
			      +"</html>";

   var doc = newWindow.document;
   doc.open("text/html", "replace");
   doc.write(content);
   doc.close();
   return;
}
getVideoDimensions = function(embed_code)
{
	//explicit type casting for IE
	embed_code = embed_code.toString();
	width  = embed_code.match(/width="?[\d]+"?/gi).toString().match(/[\d]+/)[0]
	height = embed_code.match(/height="?[\d]+"?/gi).toString().match(/[\d]+/)[0]
	
	return new Array(parseInt(width,10) + 65, parseInt(height,10) + 95);
}


$(document).ready(function()
{
	$('span.info-disclaimer').click(showInfoDisclaimer);
	/*$('a.related-image').click(function()
	{
	  showImagePopUp($(this).children('img'));
	  return;
	});*/
	$('img.video_thumbnail').click(function()
	{
		embed_code = $(this).parent().siblings('span.video_embed_code').html();
		vdo_source = $(this).parent().siblings('span.video_source_name').text();
		showVideoPopUp(embed_code,vdo_source);
		return;
	});
	
	$('a.video_title').click(function()
	{
		embed_code = $(this).parent().siblings('span.video_embed_code').html();
		vdo_source = $(this).parent().siblings('span.video_source_name').text();
		showVideoPopUp(embed_code,vdo_source);
		return;
	});
	
	$('a#read-more-topics').click(function()
	{
	  switch($('#more-topics').css('display'))
		{
		    case 'none'  : 
		        $('#more-topics').slideDown(1000); 
		        $(this).text('Less Topics');
		        break;
		    case 'block' : 
		        $('#more-topics').slideUp(1000);
		        $(this).text('More Topics');
		        break;
		}
		return;
	});
	
	$('a#read-more-conflicts').click(function()
	{
	    switch($('#more-conflicts').css('display'))
		  {
		    case 'none'  : 
		        $('#more-conflicts').slideDown(1000); 
		        $(this).text('Less conflicts');
		        break;
		    case 'block' : 
		        $('#more-conflicts').slideUp(1000); 
		        $(this).text('More conflicts');
		        break;
		  }
		  return;
	});
	
	$('a#see-more-videos').click(function()
	{
		switch($('#more-videos').css('display'))
		{
		    case 'none': 
		        $('#more-videos').slideDown(1000);
		        $(this).text('Less Videos');
		        break;
		    case 'block': 
		        $('#more-videos').slideUp(1000); 
		        $(this).text('More Videos');
		        break;
		}
		return;
	});
	
	$('span#see-more-images').click(function()
	{
		switch($('div#more-images').css('display'))
		{
		    case 'none'  : 
		        $('div#more-images').slideDown(1000); 
		        $(this).text('Less Images');
		        break;
		    case 'block' : 
		        $('div#more-images').slideUp(1000); 
		        $(this).text('More Images');
		        break;
		}
		return;
	});
	
	$('a#read-more-articles').click(function()
	{
		switch($('span#more-articles').css('display'))
		{
		    case 'none':
		        $('span#more-articles').slideDown(1000);
		        $(this).text('Less Articles');
		        break;
		    
		    case 'block':
		        $('span#more-articles').slideUp(1000);
		        $(this).text('More Articles');
		        break;
		}		
		return;
	});
	
	$('a#read-more-journals').click(function()
	{
		switch($('span#more-journals').css('display'))
		{
		    case 'none':
		        $('span#more-journals').slideDown(1000);
		        $(this).text('Less Journals');
		        break;
		    
		    case 'block':
		        $('span#more-journals').slideUp(1000);
		        $(this).text('More Journals');
		        break;
		}		
		return;
	});
	
	/*$('a.see-more').click(function()
	{
	  content_type = $(this).attr('id').split('-')[2];
	  content_area = $('span#see-more-'+content_type)
	  switch(content_area.css('display'))
	  {
	    
	  }
	})*/
	
	$('img.related-topic-logo').click(showRelatedTopicInfo);
});