/******************************************************************************
 ***	2009/03/11	dmagalnick	File created
 ***							main.js
 ***							JavaScript file to handle the display of the
 ***							samples and features
 ***	2012/02/07	dmagalnick	File created
 ***							Adding media type of "html5" which will allow
 ***							for sniffing of Flash on the user's browser.
 ***							If available, the movie (MP4) will play in a
 ***							SWF. If not available, the movie will play in
 ***							HTML 5.
 ******************************************************************************/

function __ShowFeatures()
{
	var arr_subjects = __getSubjects();
	var o = "";
	o += "<div id=\"divContentBlockForTabs\">\n";
	o += " <div id=\"divTabsRow\">\n";
	o += "  <ul>\n";
	for ( var i = 0; i < arr_subjects.length; i++ )
	{
	//	generate the tabs from the array
		o += "   <li><a href=\"javascript:void(0);\" onClick=\"__getTabContent( " + i + ", 0 );\" id=\"tab_" + i + "\">" + arr_subjects[i] + "</a></li>\n";
	}
	o += "  </ul>\n";
	o += " </div>\n";
	o += "</div>\n";
	o += "<div id=\"divTabsContent\">\n";
	o += "  <table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" width=\"100%\">\n";
	o += "   <tr>\n";
	o += "    <td valign=\"top\" width=\"320\"><div id=\"div_image\">No image available</div></td>\n";
	o += "    <td valign=\"top\">\n";
	o += "     <div style=\"margin-left: 10px;\">\n";
	o += "      <div class=\"bold " + __getClassColor() + "\" id=\"div_title\"></div>\n";
	o += "      <div style=\"margin-left: 10px;\" id=\"div_desc\"></div>\n";
	o += "      <div style=\"margin-left: 10px; margin-top: 20px;\" id=\"div_links\"></div>\n";
	o += "      <div class=\"t4l_small bold grey\" style=\"margin-left: 10px; margin-top: 5px;\">Visit <a href=\"http://connect.tech4learning.com/\" target=\"_blank\">Connect</a> for more examples of what educators are creating.</div>\n";
	o += "     </div>\n";
	o += "    </td>\n";
	o += "   </tr>\n";
	o += "  </table>\n";
	o += "</div>\n";
	
	document.write( o );
	__getShowFeatures();
}

function __getShowFeatures()
{
	var str_div = "featured";
	var arr_samples = __getSamples();
	var arr_featured = new Array();
	
	//	begin getting samples that can show in featured
	for ( var i = 0; i < arr_samples.length; i++ )
	{
	//	loop through all samples arrays
		for ( var j = 0; j < arr_samples[i].length; j++ )
		{
		//	loop through all samples in current samples array
			var obj_sample = arr_samples[i][j];
			if ( obj_sample["allowInFeatured"] )
			{
			//	sample is allowed in featured
			//	set the subject and sample index values
			//	add it to array
				obj_sample["subjectIndex"] = i;
				obj_sample["sampleIndex"] = j;
				arr_featured.push( obj_sample );
			}
		}
	}
	//	end getting samples that can show in featured
	
	var int_howMany = arr_featured.length;
	if ( int_howMany > 0 )
	{
	//	there is at least 1 allowed in featured
		var int_rand = Math.floor( int_howMany * Math.random() );
			//	get random number from 0 to howMany
		var obj_sample = arr_featured[ int_rand ];
		__getTabContent( obj_sample["subjectIndex"], obj_sample["sampleIndex"] );
	}
}

function __ShowSamples( mix_which )
{
document.write( "" ); return;
	document.write( __getShowSamples( mix_which ) );
		//	get the HTML display first
	
	//	begin determining if need to populate a specific block or all
	var arr_subjects = __getSubjects();
	var arr_samples = __getSamples();
	var bln_show1 = false;
	var int_which = null;
	
	if ( typeof( mix_which ) == "number" )
	{
		if ( mix_which < arr_subjects.length ) { int_which = mix_which; bln_show1 = true; }
	}
	if ( typeof( mix_which ) == "string" )
	{
		for ( var i = 0; i < arr_subjects.length; i++ )
		{
			if ( mix_which == arr_subjects[i] ) { int_which = i; bln_show1 = true; }
		}
	}
	//	end determining if need to populate a specific block or all
	
	//	begin populating variable fields
	var int_start = 0;
	var int_stop = arr_subjects.length;
		//	set start and stop points of loop
		//	assume 0, array length
	if ( bln_show1 ) { int_start = int_which; int_stop = ( int_which + 1 ); }
		//	for showing 1, reset start and stop values
	for ( var i = int_start; i < int_stop; i++ ) { __PopulateVariableFieldsSamples( i, 0 ); }
		//	loop through all subjects in range
		//	populate variable fields for first index of each
	//	end populating variable fields
}

function __getShowSamples( mix_which )
{
	var arr_subjects = __getSubjects();
	var arr_samples = __getSamples();
	var bln_show1 = false;
	var int_which = null;
	var o = "";
	
	//	begin determining if need to show a specific block or all
	if ( typeof( mix_which ) == "number" )
	{
		if ( mix_which < arr_subjects.length ) { int_which = mix_which; bln_show1 = true; }
	}
	if ( typeof( mix_which ) == "string" )
	{
		for ( var i = 0; i < arr_subjects.length; i++ )
		{
			if ( mix_which == arr_subjects[i] ) { int_which = i; bln_show1 = true; }
		}
	}
	//	end determining if need to show a specific block or all
	
	//	begin displaying
	var int_start = 0;
	var int_stop = arr_subjects.length;
		//	set start and stop points of loop
		//	assume 0, array length
	if ( bln_show1 ) { int_start = int_which; int_stop = ( int_which + 1 ); }
		//	for showing 1, reset start and stop values
	var bln_first = true;
	for ( var i = int_start; i < int_stop; i++ )
	{
	//	loop through all subjects in range
		o += "<div>\n";
		o += " <fieldset class=\"collapsible collapsed\">\n";
		o += "  <legend class=\"bold\">" + arr_subjects[i] + "</legend>\n";
		o += "  <table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" width=\"100%\">\n";
		o += "   <tr>\n";
		o += "    <td valign=\"top\" width=\"320\"><div id=\"div_image_" + i + "\"></div></td>\n";
		o += "    <td valign=\"top\">\n";
		o += "     <div style=\"margin-left: 10px;\">\n";
		o += "      <div class=\"bold\" id=\"div_title_" + i + "\"></div>\n";
		o += "      <div style=\"margin-left: 10px;\" id=\"div_desc_" + i + "\"></div>\n";
		o += "      <div style=\"margin-top: 10px;\" id=\"div_links_" + i + "\"></div>\n";
		o += "     </div>\n";
		o += "    </td>\n";
		o += "   </tr>\n";
		o += "  </table>\n";
		o += " </fieldset>\n";
		o += "</div>\n";
		
		bln_first = false;
	}
	//	end displaying
	
	return o;
}

function __getTabContent( int_index1, int_index2 )
{
	var arr_samples = __getSamples();
	var obj_sample = arr_samples[ int_index1 ][ int_index2 ];
	__SetActiveTab( int_index1 );
	__getImage( obj_sample );
	__getTitle( obj_sample );
	__getDesc( obj_sample );
	__getSamplesLinks( obj_sample, int_index1, int_index2 );
}

function __getTitle( obj_sample ) { document.getElementById( "div_title" ).innerHTML = obj_sample["title"]; }
function __getDesc( obj_sample ) { document.getElementById( "div_desc" ).innerHTML = obj_sample["desc"]; }

function __getImage( obj_sample, mix_index1 )
{
	if ( obj_sample["type"] == "image" )
	{
	//	get for image
		document.getElementById( "div_image" ).innerHTML = "<a href=\"" + obj_sample["url2"] + "\" target=\"_blank\"><img src=\"" + obj_sample["url1"] + "\" border=\"0\" alt=\"" + obj_sample["title"] + "\"></a>";
	}
	if ( obj_sample["type"] == "movie" )
	{
	//	get for local movie
		var so = new SWFObject('/userfiles/flash/player.swf','mpl','320','240','9');
		so.addParam('allowscriptaccess','always');
		so.addParam('allowfullscreen','true');
		so.addParam('flashvars','&author=Tech4Learning, Inc&description=' + obj_sample["title"] + '&duration=5:15&file=' + obj_sample["url1"] + '&image=' + obj_sample["url2"] + '&stretching=exactfit');
		so.write( 'div_image' );
	}
	if ( obj_sample["type"] == "html5" )
	{
	//	get for local movie
		jwplayer('div_image').setup({
			flashplayer: '/userfiles/flash/player-5.9.swf',
			file: '' + obj_sample["url1"] + '',
			height: 240,
			width: 320,
			image: '' + obj_sample["url2"] + '',
			controlbar: {
				idlehide: true,
				position: 'over'
			},
			plugins: {
				gapro: { accountid: 'UA-4157067-1' }
			}
		});
	}
	if ( obj_sample["type"] == "ning" )
	{
	//	get for ning movie
		str_html = "<embed wmode=\"opaque\" src=\"http://static.ning.com/recipes4success/widgets/video/flvplayer/flvplayer.swf?v=3.14.3%3A17089\" FlashVars=\"config=" + obj_sample["url1"] + "&amp;video_smoothing=on&amp;autoplay=off\" width=\"320\" height=\"240\" bgColor=\"#B4D9F5\" scale=\"noscale\" allowScriptAccess=\"always\" allowFullScreen=\"true\" type=\"application/x-shockwave-flash\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\"></embed>";
		document.getElementById( "div_image" ).innerHTML = str_html;
	}
}

function __getSamplesLinks( obj_sample, int_index1, int_index2 )
{
	var o = "";
	var arr_samples = __getSamples();
	var int_howMany = arr_samples[ int_index1 ].length;
	
	if ( int_howMany > 1 )
	{
	//	only show samples links if there are more than 1 in list
		o += "<span class=\"bold grey\">More " + obj_sample["subject"] + " samples:</span>";
		for ( var i = 0; i < int_howMany; i++ )
		{
			str_display = ( i + 1 ) < 10 ? "0" + ( i + 1 ) : ( i + 1 );
			if ( i == int_index2 ) { o += "<span style=\"margin-left: 10px;\" class=\"grey\">" + str_display + "</span>"; }
			else { o += "<span style=\"margin-left: 10px;\"><a href=\"javascript:void(0);\" onClick=\"__getTabContent( " + int_index1 + ", " + i + " )\" class=\"bold\">" + str_display + "</a></span>"; }
		}
	}
	
	document.getElementById( "div_links" ).innerHTML = o;
}

function __SetActiveTab( int_tab )
{
//	simply determines the "active" tab
//	this does not affect the content area
	var arr_subjects = __getSubjects();
	for ( var i = 0; i < arr_subjects.length; i++ )
	{
	//	deactivate all tabs
		document.getElementById( "tab_" + i ).className = "";
	}
	document.getElementById( "tab_" + int_tab ).className = "active";
		//	set specified tab to active
}

// http://kevin.vanzonneveld.net
// +   original by: Philip Peterson
// +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// +      input by: AJ
// +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// %          note: info on what encoding functions to use from: http://xkr.us/articles/javascript/encode-compare/
// *     example 1: urlencode('Kevin van Zonneveld!');
// *     returns 1: 'Kevin+van+Zonneveld%21'
// *     example 2: urlencode('http://kevin.vanzonneveld.net/');
// *     returns 2: 'http%3A%2F%2Fkevin.vanzonneveld.net%2F'
// *     example 3: urlencode('http://www.google.nl/search?q=php.js&ie=utf-8&oe=utf-8&aq=t&rls=com.ubuntu:en-US:unofficial&client=firefox-a');
// *     returns 3: 'http%3A%2F%2Fwww.google.nl%2Fsearch%3Fq%3Dphp.js%26ie%3Dutf-8%26oe%3Dutf-8%26aq%3Dt%26rls%3Dcom.ubuntu%3Aen-US%3Aunofficial%26client%3Dfirefox-a'
function __UrlEncode( str )
{
    var histogram = {}, histogram_r = {}, code = 0, tmp_arr = [];
    var ret = str.toString();
    
    var replacer = function(search, replace, str) {
        var tmp_arr = [];
        tmp_arr = str.split(search);
        return tmp_arr.join(replace);
    };
    
    // The histogram is identical to the one in urldecode.
    histogram['!']   = '%21';
    histogram['%20'] = '+';
    
    // Begin with encodeURIComponent, which most resembles PHP's encoding functions
    ret = encodeURIComponent(ret);
    
    for (search in histogram) {
        replace = histogram[search];
        ret = replacer(search, replace, ret) // Custom replace. No regexing
    }
    
    // Uppercase for full PHP compatibility
    return ret.replace(/(\%([a-z0-9]{2}))/g, function(full, m1, m2) {
        return "%"+m2.toUpperCase();
    });
    
    return ret;
}

