
var site = 'http://videos.silicon.com/';
var sitename = 'silicon.com videos';
var debug = false;

var current_video = null;
var current_tab = null;
var current_franchise = null;

var firstload = true;


function hidePlaylist() {
	if (debug) alert('Debugging information:' + "\n\n" + 'hidePlaylist(); called');
	var div = document.getElementById('ab_playlist_div');
	var tab = document.getElementById('ab_playlist_tab');
	if (tab && div) {
		div.style.height = '0px';
		tab.className = '';
	}
	return(true);
}

function addMedia(id, now) {
	if (debug) alert('Debugging information:' + "\n\n" + 'addMedia(' + id + ', ' + (now ? 'true' : 'false') + '); called');
	var videoPlayer = document.getElementById('player');
	videoPlayer.addMedia(id, now);
	if (!now) { showPlaylist(); }
	return(true);
}

function updateVideoPageLivery(id, title, franchise, embeddable) {
	if (document.getElementById('dyn') && updateVideoPageLivery2) {
		updateVideoPageLivery2(id, title, franchise, embeddable);
	} else {
		setTimeout('updateVideoPageLivery2(' + id + ', \'' + title + '\', ' + franchise + ', ' + (embeddable ? 'true' : 'false') + ');', 1000);
		return(true);
	}
}

function updateVideoPageLivery2(id, title, franchise, embeddable) {
	if (debug) alert('Debugging information:' + "\n\n" + 'updateVideoPageLivery(' + id + ', \'' + title + '\', ' + franchise + ', ' + (embeddable ? 'true' : 'false') + '); called');
	current_video = id;
	current_franchise = franchise;
	if (firstload) {
		ajaxLoadTab(onloadtab.tab, onloadtab.sort, onloadtab.page, onloadtab.franchise, onloadtab.search);
		firstload = false;
	} else if (current_tab == 'playing') {
		ajaxLoadTab(null, null, null, franchise, null);
	}
	ajaxLoadInfo(id);
	ajaxCommentGetList(id);

	var url = site + id + '.htm';
	var url_escaped = encodeURIComponent(url).replace(/%20/g, '+');
	var title_escaped = encodeURIComponent(title).replace(/%20/g, '+');

	var share_delicious = document.getElementById('share_delicious');
	if (share_delicious) share_delicious.href = 'http://del.icio.us/post?title=' + title_escaped + '&url=' + url_escaped;

	var share_digg = document.getElementById('share_digg');
	if (share_digg) share_digg.href = 'http://www.digg.com/submit?phase=2&url=' + url_escaped + '&title=' + title_escaped + '&media=video';
	
	var share_facebook = document.getElementById('share_facebook');
	if (share_facebook) share_facebook.href = 'http://www.facebook.com/share.php?u=' + url_escaped + '&t=' + title_escaped;

	var share_slashdot = document.getElementById('share_slashdot');
	if (share_slashdot) share_slashdot.href = 'http://slashdot.org/slashdot-it.pl?op=basic&url=' + url_escaped;

	var share_stumbleupon = document.getElementById('share_stumbleupon');
	if (share_stumbleupon) share_stumbleupon.href = 'http://www.stumbleupon.com/submit?title=' + title_escaped + '&url=' + url_escaped;
	
	var contents_getlink = document.getElementById('contents_getlink');
	if (contents_getlink) contents_getlink.value = url;

	var contents_embed = document.getElementById('contents_embed');
	var ab_embed_tab = document.getElementById('ab_embed_tab');
	if (embeddable && contents_embed && ab_embed_tab) {
		contents_embed.value = '<!-- Start CNET Embeded Player --><div style="width: 400px; overflow: hidden;"><object type="application/x-shockwave-flash" data="' + site + 'embed/' + id + '" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" width="400" height="265" allowfullscreen="true"><param name="allowScriptAccess" value="always" /><param name="movie" value="' + site + 'embed/' + id + '" /><param name="loop" value="false" /><param name="allowFullScreen" value="true" /><param name="quality" value="high" /><param name="bgcolor" value="white" /></object><div style="width: 400px; height: 26px; background-color: white; overflow: hidden;"><ul style="text-align: left; margin: 5px 0; padding: 2px; font-size: 10px; font-family: arial; color: black;"><li style="list-style: none; float: left; margin-right: 5px;"><a href="' + site + id + '.htm" style="color: black; display: inline;">' + title + '</a></li><li style="list-style: none; float: left; margin-right: 5px;"><strong>|</strong></li><li style="list-style: none; float: left;"><a href="' + site + '" style="color: black; display: inline;">' + sitename + '</a></li></ul></div></div><!-- Finish CNET Embeded Player -->';
		ab_embed_tab.style.display = 'block';
	} else {
		contents_embed.value = '';
		ab_embed_tab.style.display = 'none';
	}
	return(true);
	
}

// Tab panel

var ajax_tab = new sack();

function ajaxTabLoading() {	
	if (debug) alert('Debugging information:' + "\n\n" + 'ajaxTabLoading(); called');
	var tabdiv = document.getElementById('dyn');	
	if (tabdiv) {
		removeChildNodes(tabdiv);
		var div = createTag('div', null, { 'align' : 'center' })
		div.appendChild(createTag('br'));
		div.appendChild(createTag('br'));
		div.appendChild(createTag('img', null, { 'src' : '/i/s5/gl/but/ajax-loader.gif'}));
		div.appendChild(createTag('p', 'Fetching list of videos...'));
		div.appendChild(createTag('br'));
		tabdiv.appendChild(div);
	}
	return(true);
}

function ajaxTabCompleted() {
	if (debug) alert('Debugging information:' + "\n\n" + 'ajaxTabCompleted(); called');
	var dyn = document.getElementById('dyn');
	if (dyn) dyn.innerHTML = ajax_tab.response;
	return(true);
}

function ajaxLoadTab(tab, sort, page, franchise, search) {
	if (debug) alert('Debugging information:' + "\n\n" + 'ajaxLoadTab(' + tab + ', ' + sort + ', ' + page + ', ' + franchise + ', ' + search + '); called');
	current_tab = (tab ? tab : current_tab);
	
	var tabs = new Array('playing', 'top', 'all', 'advertiser', 'search');
	for (i = 0; i < tabs.length; i++) {
		tabdiv = document.getElementById('tab-' + tabs[i]);
		
		if (tabdiv) {
			if (current_tab == tabs[i]) {	
				check_selected = new RegExp('\\b'+'selected'+'\\b').test(tabdiv.className);	
				if (!check_selected) {
					tabdiv.className = 'selected ' + tabdiv.className;
				}
			} else {				
				tabdiv.className = tabdiv.className.replace(/selected/i, '');
			}
		}
	}
	ajax_tab.resetData();
   	ajax_tab.setVar('tab', (current_tab ? current_tab : ''));
   	ajax_tab.setVar('sort', (sort ? sort : ''));
   	ajax_tab.setVar('page', (page ? page : ''));
   	ajax_tab.setVar('franchise', (current_tab == 'playing' && current_franchise ? current_franchise : (franchise ? franchise : '')));
   	ajax_tab.setVar('search', (search ? search : ''));
	ajax_tab.requestFile = '/ajax-tab.htm';
   	ajax_tab.onLoading = ajaxTabLoading;
   	ajax_tab.onCompletion = ajaxTabCompleted;
  	ajax_tab.runAJAX();
	//return(true);
}


// Video info panel

var ajax_info = new sack();

function ajaxInfoLoading() {
	if (debug) alert('Debugging information:' + "\n\n" + 'ajaxInfoLoading(); called');
	var info = document.getElementById('infoBox');
	if (info) {
		removeChildNodes(info);
		var div = createTag('div', null, { 'align' : 'center' })
		div.appendChild(createTag('br'));
		div.appendChild(createTag('br'));
		div.appendChild(createTag('img', null, { 'src' : '/i/s5/gl/but/ajax-loader.gif'}));
		div.appendChild(createTag('p', 'Fetching info for this video...'));
		div.appendChild(createTag('br'));
		info.appendChild(div);
	}
	return(true);
}

function ajaxInfoCompleted() {
	if (debug) alert('Debugging information:' + "\n\n" + 'ajaxInfoCompleted(); called');
	document.getElementById('infoBox').innerHTML = ajax_info.response;
	return(true);
}


function ajaxLoadInfo(id) {
	if (debug) alert('Debugging information:' + "\n\n" + 'ajaxLoadInfo(' + id + '); called');
	ajax_info.resetData();
	ajax_info.setVar('id', id);	
	ajax_info.requestFile = '/ajax-videoinfo.htm';
	ajax_info.onLoading = ajaxInfoLoading;
	ajax_info.onCompletion = ajaxInfoCompleted;
	ajax_info.runAJAX();
	return(true);
}


// Comments panel

var ajax_comment = new sack();

function ajaxCommentLoading() {
	if (debug) alert('Debugging information:' + "\n\n" + 'ajaxCommentLoading(); called');
	var e = document.getElementById('comment-responses');
	if (e) {
		removeChildNodes(e);
		var div = createTag('div', null, { 'align' : 'center' });
		div.appendChild(createTag('br'));
		div.appendChild(createTag('br'));
		div.appendChild(createTag('img', null, { 'src' : '/i/s5/gl/but/ajax-loader.gif'}));
		div.appendChild(createTag('p', 'Fetching comments for this video...'));
		div.appendChild(createTag('br'));
		e.appendChild(div);
		}
	return(true);
}

function ajaxCommentCompleted() {
	if (debug) alert('Debugging information:' + "\n\n" + 'ajaxCommentCompleted(); called');
	var e = document.getElementById('comment-responses');
	if (e) {
		removeChildNodes(e);
		var response = eval('(' + ajax_comment.response + ')');
		if (typeof(response.comments.length) == 'number' && response.comments.length > 0) {
			for (var n = 0; n < response.comments.length; n++) {
				var comment = response.comments[n];
				var div = createTag('div', null, { 'class' : 'commentpost', 'className' : 'commentpost' });
					var div_content = createTag('div', null, { 'class' : 'commentcontent', 'className' : 'commentcontent' });
						var p = createTag('p', comment.name, { 'class' : 'postInfo', 'className' : 'postInfo' });
						p.appendChild(createTag('span', comment.timestamp));
						div_content.appendChild(p);
						var p_comment = createTag('p');
						p_comment.innerHTML = comment.text;
						div_content.appendChild(p_comment);
					div.appendChild(div_content);
					var div_buttons = createTag('div', null, { 'class' : 'commentbuttons clearfix', 'className' : 'commentbuttons clearfix' });
						var link = createTag('a', null, { 'class' : 'replybutton', 'className' : 'replybutton', 'href' : '#reply' });
							link.appendChild(createTag('img', null, { 'src' : '/i/v/btn/add-comment.jpg', 'alt' : 'Reply' }));
						div_buttons.appendChild(link);
					div.appendChild(div_buttons);
				e.appendChild(div);
			}
		} else {
			e.appendChild(createTag('p', 'Be the first to post a comment...', { 'class' : 'firstComment', 'className' : 'firstComment' }));
		}
		ajaxCommentAlert(response.alert);
	}
	return(true);
}

function ajaxCommentAlert(text) {
	if (debug) alert('Debugging information:' + "\n\n" + 'ajaxCommentAlert(' + text + '); called');
	var alert = document.getElementById('alert');
	if (typeof(text) == 'string' && text.length) {
		alert.innerHTML = text;
		alert.style.display = 'block';
	} else {
		alert.style.display = 'none';
	}
	return(true);
}

function ajaxCommentGetList(video, comment, name, email, dontdisplayname, location, occupation) {
	if (debug) alert('Debugging information:' + "\n\n" + 'ajaxCommentGetList(' + video + ', ' + comment + ', ' + name + ', ' + email + ', ' + dontdisplayname + ', ' + location + ', ' + occupation + '); called');
	if (typeof(ajax_comment) == 'object') {
	  	ajax_comment.resetData();
		ajax_comment.setVar('id', video);
		if (typeof(comment) == 'string' && comment.length) {
			ajax_comment.setVar('comment', comment);
			ajax_comment.setVar('name', name);
			ajax_comment.setVar('email', email);
			ajax_comment.setVar('dontdisplayname', (dontdisplayname ? '1' : '0'));
			ajax_comment.setVar('location', location);
			ajax_comment.setVar('occupation', occupation);
		}

	    ajax_comment.requestFile = '/ajax-comments.htm';

	    ajax_comment.onLoading = ajaxCommentLoading;
	    ajax_comment.onCompletion = ajaxCommentCompleted;
	    ajax_comment.runAJAX();

	}
	return(true);
}


function ajaxCommentPost(video, commentfield_id, namefield_id, emailfield_id, dontdisplaynamefield_id, locationfield_id, occupationfield_id) {
	if (debug) alert('Debugging information:' + "\n\n" + 'ajaxCommentPost(' + video + ', ' + commentfield_id + ', ' + namefield_id + ', ' + emailfield_id + ', ' + dontdisplaynamefield_id + ', ' + locationfield_id + ', ' + occupationfield_id + '); called');
	var commentfield = document.getElementById(commentfield_id);
	commentfield.className = '';
	var namefield = document.getElementById(namefield_id);
	namefield.className = '';
	var emailfield = document.getElementById(emailfield_id);
	emailfield.className = '';
	var dontdisplaynamefield = document.getElementById(dontdisplaynamefield_id);
	dontdisplaynamefield.className = '';
	var locationfield = document.getElementById(locationfield_id);
	locationfield.className = '';
	var occupationfield = document.getElementById(occupationfield_id);
	occupationfield.className = '';
	if (commentfield.value.length) {
		if (namefield.value.length) {
			if (emailfield.value.length) {
				if (locationfield.value.length) {
					if (occupationfield.value.length) {
						commentfield.disabled = true;
						namefield.disabled = true;
						emailfield.disabled = true;
						dontdisplaynamefield.disabled = true;
						locationfield.disabled = true;
						occupationfield.disabled = true;
						ajaxCommentGetList(video, commentfield.value, namefield.value, emailfield.value, dontdisplaynamefield.checked, locationfield.value, occupationfield.value);
						commentfield.value = '';
						namefield.value = '';
						emailfield.value = '';
						dontdisplaynamefield.checked = false;
						locationfield.value = '';
						occupationfield.value = '';
						
						commentfield.disabled = false;
						namefield.disabled = false;
						emailfield.disabled = false;
						dontdisplaynamefield.disabled = false;
						locationfield.disabled = false;
						occupationfield.disabled = false;
					} else {
						ajaxCommentAlert('Please supply your occupation in the highlighted field.');
						occupationfield.className = 'userError';
					}
				} else {
					ajaxCommentAlert('Please supply your location in the highlighted field.');
					locationfield.className = 'userError';
				}
			} else {
				ajaxCommentAlert('Please supply a valid email address in the highlighted field.');
				emailfield.className = 'userError';
			}
		} else {
			ajaxCommentAlert('Please supply your name in highlighted field.');
			namefield.className = 'userError';
		}
	} else {
		ajaxCommentAlert('Please supply your comments in the highlighted field.');
		commentfield.className = 'userError';
	}
	return(false);
}

