/***
 * pic_list.js
 * Contents: contains functions that handle the ad
 *			 detail picture list navigation.
 * Matthew T. Carter
 **/

var in_ss = false;
var current = 0;
var global_timeout;

function popup(){
	window.open(url_larger+id_array[current]+"/",'LargerImage','height=750,width=850,scrollbars=yes');
}
function updatePicList(iPic) {
		current = iPic;
		cutline_obj = document.getElementById("cutline_actual");
		c_pic_container = document.getElementById("pic_list_current");
		
		if (cutline_obj) {
			blendimage('big_pic_container',500);
			cutline_obj.innerHTML = cutlines[current] + "<br /><b>" + media_author[current] + "</b> " + media_author2[current] + "";
			if (file_types[current] == "Audio")
				cutline_obj.innerHTML = cutline_obj.innerHTML+'<a href="' + pic_list[current] + '">Download Audio</a>';
		}
		
		// x of x text (current pic)
		if (c_pic_container) c_pic_container.innerHTML = (current+1) + ' of ' + pic_list.length + '&nbsp;<a href="javascript:popup();" style="text-decoration:underline;">View Larger</a>';
}

function next() {
	updatePicListNav(false);
	in_ss = false;
	clearTimeout(global_timeout);
	if (current < (pic_list.length-1)) {
		current += 1;
		updatePicList(current);
	}
	else {
		current = 0;
		updatePicList(current);
	}
}

function prev() {
	updatePicListNav(false);
	in_ss = false;
	clearTimeout(global_timeout);
	if (current > 0) {
		current -= 1;
		updatePicList(current);
	}
	else {
		current = (pic_list.length-1);
		updatePicList(current);
	}
}

function toggle_ss() {
	in_ss = !in_ss;
}

function rotate() {
	if (in_ss == true) {
		current = (current == (pic_list.length-1)) ? 0 : current + 1;
		updatePicList(current);
		global_timeout = setTimeout("rotate()",global_timeout_time);
	}
}



updatePicList(0);

// fade functions
function changeOpac(opacity, id) {
    var object = document.getElementById(id).style;
    object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = "alpha(opacity=" + opacity + ")";
}

function blendimage(imageid, millisec) {
    var speed = Math.round(millisec / 100);
    var timer = 0;

    if (file_types[current] == "Image") {
    	document.getElementById(imageid).innerHTML = '<img id="big_pic" src="'+pic_list[current]+'" style="filter:alpha(opacity=0);opacity:0;-moz-opacity:0;display:block;position:relative;z-index:1;'+((media_links.length > 0) && (media_links[current] != '#') ? 'cursor:pointer;' : '' )+'" onclick="gotoLink()" />';
    	// fade in image
    	for(i = 0; i < 100; i++) {
        	setTimeout("changeOpac(" + i + ",'big_pic')",(timer * speed));
        	timer++;
    	}
    }
    else {
    	if (file_types[current] == "Doc") {
    		document.getElementById(imageid).innerHTML = '<center><br /><br /><br /><br /><br /><a href="'+pic_list[current]+'" target="_blank" style="margin-bottom:20px;"><img src="'+doc_type_pic+'" border="0" style="float:none;width:50px;height:50px;text-decoration:none;" /><br />(Click to download document.)</a></center><br /><br /><br /><br /><br /><br /><br />';
    	}
    	if (file_types[current] == "Video") {
    		document.getElementById(imageid).innerHTML = '<img src="'+vid_launch_pic+'" width="338" height="254" onclick="launchVideo(\''+pic_list[current]+'\');" style="cursor:pointer;" />';
    		// document.getElementById(imageid).innerHTML = '<object type="video/x-ms-wmv" data="'+pic_list[current]+'" width="338" height="254"><param name="src" value="'+pic_list[current]+'" /><param name="autostart" value="false" /><param name="controller" value="true" /></object>';
    	}
    	if (file_types[current] == "Audio") {
    		document.getElementById(imageid).innerHTML = '<img src="'+audio_launch_pic+'" width="338" height="254" onclick="launchAudio(\''+pic_list[current]+'\');" style="cursor:pointer;" />';
    	}
    	if (file_types[current] == "Flash") {
    		// flash
    		document.getElementById(imageid).innerHTML = '<embed src="'+pic_list[current]+'" autostart="false" loop="false" mute="false" style="background-color:#666666;" width="338" height="254"></embed>';
    	}
    }
}

function launchVideo(s) {
	in_ss = false;
	updatePicListNav(false);
	var container = document.getElementById("big_pic_container");
	if (pic_list[current].substring(pic_list[current].length - 3).toLowerCase() == 'flv') {
		// we have to create a div inside of big_pic_container so it doesn't get
		// converted to an object tag by swfobject.js
		var flash_embed_div = document.createElement('div');
		flash_embed_div.id = 'flv_embed';
		container.innerHTML = '';
		container.appendChild(flash_embed_div);
		
		var video_url = 'rtmp' + pic_list[current].substring(4, pic_list[current].length - 4);
		var flashvars = {};
		flashvars.width = '338';
		flashvars.height = '254';
		flashvars.source = video_url;
		var params = {};
		params.wmode = "transparent";
		params.allowscriptaccess = "always";
		var attributes = {};
		swfobject.embedSWF(fs_url + "flash/videoplayers/flvplayer.swf", "flv_embed", flashvars.width, flashvars.height, "9.0.0", fs_url + "scriptaculous/expressInstall.swf", flashvars, params, attributes);
	}
	else {
		// OLD METHOD: container.innerHTML = '<object type="video/x-ms-wmv" data="'+pic_list[current]+'" width="338" height="254"><param name="src" value="'+pic_list[current]+'" /><param name="autostart" value="true" /><param name="controller" value="true" /><param name="showstatusbar" value="true" /></object>';
		container.innerHTML = 	'<object id="MediaPlayer" width="338" height="254" classid="CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6" codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701" standby="Loading Video" type="application/x-oleobject">' +
								'<param name="URL" value="'+pic_list[current]+'" />' +
								'<param name="AutoStart" value="True" />' +
								'<param name="ShowControls" value="True" />' +
								'<param name="ShowStatusBar" value="True" />' +
								'<embed type="application/x-mplayer2" pluginspage="http://www.microsoft.com/windows/mediaplayer/en/download/" src="'+pic_list[current]+'" width="338" height="254" name="MediaPlayer" autostart="True" ShowStatusBar="1" ShowControls="1" ></embed>' +
								'</object>';
	}
}

function launchAudio(s) {
	in_ss = false;
	updatePicListNav(false);
	var container = document.getElementById("big_pic_container");
	container.innerHTML = '<embed src="'+pic_list[current]+'" autostart="true" loop="false" mute="false" style="background-color:#666666;" width="338" height="254"></embed>';
}

function player_popup(href) {
	media_player = window.open(href,'2','toolbar=no,scrollbars=no,location=no,statusbar=no,menubar=no,resizable=no,width=760,height=420');
}