/***
 * 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;
var classic_use_lightbox = false;

function article_popup(){
	window.open(url_larger+id_array[current]+"/",'LargerImage','height=750,width=850,scrollbars=yes');
}
function updatePicList(iPic) {
	if (typeof pic_list == 'undefined') {
		return false;
	}
		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+'<br /><a href="' + pic_list[current] + '">Download Audio</a>';
			} else if (file_types[current] == "Image" && order_link_array[current] && PURCHASE_PHOTO_INFO && PURCHASE_PHOTO_INFO != 'disableall') {
				var order_link = '<a style="font-size:8pt;" href="[[MYCAPTURE_URL]]mycapture/remoteimage.asp?backtext=[[BACK_TEXT]]&backurl='+window.location.href+'&thumbpath='+pic_list[current]+'&previewpath='+pic_list[current]+'&pricingsheetid=[[PRICING_ID]]&originalname='+original_filenames[current]+'">[[LINK_TEXT]]</a> - ';
				
				//for (var attr in PURCHASE_PHOTO_INFO) {
				//	order_link = order_link.replace('[['+attr.toUpperCase()+']]', PURCHASE_PHOTO_INFO[attr]);
				//}
				var order_link = '<a href="'+PURCHASE_PHOTO_INFO.MYCAPTURE_URL+'">Order This Image</a> - ';
				cutline_obj.innerHTML = order_link+cutline_obj.innerHTML;
			}
		}
		
		// x of x text (current pic)		
		if (classic_use_lightbox == true) {
			var temp =$('big_pic')
			var vl_link = (view_larger_array[current]) ? '&nbsp;<a href="javascript:;" imagegroup="big_pic" style="text-decoration:underline;">View Larger</a>' : '';
			if (c_pic_container) 
				c_pic_container.innerHTML = (current + 1) + ' of ' + pic_list.length + vl_link;	
		}
		else {
			var vl_link = (view_larger_array[current]) ? '&nbsp;<a href="javascript:article_popup();" style="text-decoration:underline;">View Larger</a>' : '';
			if (c_pic_container) 
				c_pic_container.innerHTML = (current + 1) + ' of ' + pic_list.length + vl_link;
		}
}

function ss_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 ss_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) {
	if (document.getElementById(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()" alt="'+cutlines[current]+'" title="'+cutlines[current]+'"/>';
    	// fade in image
    	for(i = 0; i < 100; i++) {
        	setTimeout("changeOpac(" + i + ",'big_pic')",(timer * speed));
        	timer++;
    	}
	if (classic_use_lightbox == true) {
		$('big_pic').onclick = '';
		Element.writeAttribute($('big_pic'), "imagegroup", "lightbox");
	}
    }
    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") {
			var t = (thumb_array[current]==''?vid_launch_pic:thumb_array[current]);
    		document.getElementById(imageid).innerHTML = '<img src="'+t+'" 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);
	clearTimeout(global_timeout);
	var container = document.getElementById("big_pic_container");
	
		
	if (pic_list[current].substring(0, 4).toLowerCase() == 'rtmp') {
		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 = pic_list[current];
		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);
		
		googleTrack(id_array[current], 'Article Page', null, null, '/article/detail/')
	}
	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>';
						  
		googleTrack(id_array[current], 'Article Video', 'Article Detail', 'Play Video', '/article/detail/')
	}
}

function googleTrack(item_id, item_description, event, action, url) {
	if (event==null)
		event = 'Multimedia';
	if (action==null)
		action = 'Play Video';
	if (url==null)
		url = '/multimedia/play/';

	if (pageTracker!=null)
		pageTracker._trackEvent(event,action,url+item_id+' - '+item_description);
	else
		urchinTracker(url+item_id);
}

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=800,height=420');
}