// Used to display ads
ord = Math.random() * 1000000000000000000;
tile = 1;

var Cookie = {
	set: function(name,value,seconds){
		if(seconds){
			d = new Date();
			d.setTime(d.getTime() + (seconds * 1000));
			expiry = '; expires=' + d.toGMTString();
		}else
			expiry = '';
		document.cookie = name + "=" + value + expiry + "; path=/";
	},
	get: function(name){
		nameEQ = name + "=";
		ca = document.cookie.split(';');
		for(i = 0; i < ca.length; i++){
			c = ca[i];
			while(c.charAt(0) == ' ')
				c = c.substring(1,c.length);
			if(c.indexOf(nameEQ) == 0)
				return c.substring(nameEQ.length,c.length);
		}
		return null
	},
	unset: function(name){
		Cookie.set(name,'',-1);
	}
}

function toggleMe(toggleid) {
	Effect.toggle(toggleid, 'blind', {duration: 0.4});
	return false;
}

function popup(window_name, url, p_width, p_height) {
	var new_window = window.open( url, window_name, "status = 1, height = " + p_height +", width = "  + p_width ); 
	new_window.focus();
	return false;
}

function vitalSpotlightRollover(index) {
	titles[last_spotlight].className = "";
	$('spotlight-caption-' + last_spotlight).style.display = "none";
	last_spotlight = index
	titles[index].className = "selected";
	$('spotlight-image').src = spotlight_images[index].src;
	$('spotlight-caption-' + index).style.display = "block";
}

function displayRedboxLogin() {
	RedBox.showInline('login_form');
	setTimeout("$('user_login').focus()", 500);
	return false;
}

function openPopup(href, title, height, width, event, params) {
	if(typeof(params) != 'undefined') {
		params = ',' + params
	}
	element = Event.element(event);
	page_pos = Position.page(element);
	var xpos = ((typeof(window.screenX) == 'undefined') ? window.screenLeft - 50 : window.screenX) + page_pos[0] + 50;
	var ypos = ((typeof(window.screenY) == 'undefined') ? window.screenTop - 50 : window.screenY) + page_pos[1] + 50;
	var win = window.open(href,title,'height=' + height + ',width=' + width + ',top=' + ypos + ',left=' + xpos + params);
	win.focus();
	return false;
}
function validatePoll() {
	var pollChoices = $('poll-entries');
	var selected = false;
	for(var i = 0; i < pollChoices.length; i++) {
		if(pollChoices[i].checked == true) { selected = true; break; }
	}
	if(!selected) { $('vote-response').innerHTML = '<div align="center"><strong>Please choose from the options above.</strong>'; }
}

function checkAvailability(id, value) {
	ajax =  new Ajax.Updater(id, '/users/verify_username', {method:'get',parameters:'login='+value});
}

function previewForumPost() {
	$('preview_area').style.display = 'block';
	quotes = $('topic-quotes').style.display == 'block' ? $('topic-quotes').innerHTML : '';
	body =  encodeURIComponent($('comment_body').value);
	quotes = encodeURIComponent(quotes);
	new Ajax.Updater('preview_area', '/forums/preview_post', {asynchronous:true, evalScripts:true, method:'post', parameters:'body=' + body + '&quotes=' + quotes}); return false;
}

function previewForumTopic() {
	$('preview_area').style.display = 'block';
	body =  encodeURIComponent($('comment_body').value);
	new Ajax.Updater('preview_area', '/forums/preview_post', {asynchronous:true, evalScripts:true, method:'post', parameters:'body=' + body}); return false;
}

function previewSignature() {
	new Ajax.Updater('preview_area', '/community/manage/user_profiles/update_signature', {asynchronous:true, evalScripts:true, method:'put', parameters:'body=' + encodeURIComponent($('comment_body').value) }); return false;
}

function previewFeaturePost() {
	new Ajax.Updater('preview_area', '/manage/features/preview', {asynchronous:true, evalScripts:true, method:'post', parameters:'body=' + encodeURIComponent($('feature_body').value) }); return false;
}

function previewNewsBlogPost() {
	new Ajax.Updater('preview_area', '/manage/news_blogs/preview', {asynchronous:true, evalScripts:true, method:'post', parameters:'body=' + encodeURIComponent($('news_blog_body').value) }); return false;
}

function previewPressReleasePost() {
	new Ajax.Updater('preview_area', '/manage/press_releases/preview', {asynchronous:true, evalScripts:true, method:'post', parameters:'body=' + encodeURIComponent($('press_release_body').value) }); return false;
}

function previewProductGuidePost() {
	new Ajax.Updater('preview_area', '/manage/product_guides/preview', {asynchronous:true, evalScripts:true, method:'post', parameters:'body=' + encodeURIComponent($('product_guide_body').value) }); return false;
}

function writeTopAd() {
	var axel = Math.random() + "";
	var ord = axel * 1000000000000000000;
	var tile = 1;
	document.write('<SCR'+'IPT LANGUAGE="JavaScript1.1" SRC="http://ad.doubleclick.net/adj/site157.tmus;tile=' + tile + ';pos=1;dcopt=ist;sz=728x90;ord=' + ord + '?" ><\/SCRIPT>');
	if ((!document.images && navigator.userAgent.indexOf("Mozilla/2.") >= 0)  || navigator.userAgent.indexOf("WebTV")>= 0) {
	document.write('<A HREF="http://ad.doubleclick.net/jump/site157.tmus;pos=1;tile=' + tile + ';sz=728x90;ord=' + ord + '?" TARGET="_blank">');
	document.write('<IMG SRC="http://ad.doubleclick.net/ad/site157.tmus;pos=1;tile=' + tile + ';sz=728x90;ord=' + ord + '?" WIDTH="728" HEIGHT="90" BORDER="0" ALT=""></A>');
	}
}

function loadHomeVideo(video_album, video_filename, video_index, video_url, video_title, video_id) {
	new Ajax.Updater('video-player-small', '/homepage/play_video?album=' + video_album + '&autostart=true&filename=' + video_filename, {asynchronous:true, evalScripts:true});
	new Ajax.Updater('video-embed-code', '/video/embed_code/update/' + video_album + '/' + video_id, {asynchronous:true, evalScripts:true, onLoading:function(){Element.show('spinner'+video_id)}, onLoaded:function(){Element.hide('spinner'+video_id)}});
	var rows = $('video-list').getElementsByTagName('tr');
	for(i=0;i<rows.length;i++) {
		rows[i].className = "";
	}
	$('video-' + video_index).className = "selected";
	
	$('bookmark-this-video').onclick = function() {
		addthis_url = video_url; 
		addthis_title = video_title; 
		return addthis_click(this);
	}
	$('go-to-video').href = video_url;
	$('embed-video').href = video_url;
	return false;
}


function showPopUp(myvideovar, playTime, c) {
	var myvidurl = "/images/fullscreen.swf?myvideovar=" + myvideovar + "&playTime=" + playTime + "&c=" + c;
	Win = window.open(myvidurl,"subwin",'width=' + (screen.width - 10) + ',height=' + (screen.height - 30) + ',resizable=0,menubar=no,scrollbars=yes,status=no' );
	if($('video-player-small') && $('video-player-small-paused')) {
		$('video-player-small').style.left = '-10000px';
		$('video-player-small-paused').style.left = '11px';
		Element.hide($('video-player-small'));
	}
	else if($('video-player') && $('video-player-paused')) {
		$('video-player').style.left = '-10000px';
		$('video-player-paused').style.left = '24px';
		Element.hide($('video-player'));
	}
	newWinInterval = setInterval(checkMe, 1000);
}

function checkMe() {
	if(Win.closed) {
		clearInterval(newWinInterval);
		if($('video-player-small') && $('video-player-small-paused')) {
			$('video-player-small').style.left = '11px';
			$('video-player-small-paused').style.left = '-10000px';	
			Element.show($('video-player-small'));
		}
		else if($('video-player') && $('video-player-paused')) {
			$('video-player').style.left = '24px';
			$('video-player-paused').style.left = '-10000px';
			Element.show($('video-player'));
		}
	}
}

	
function tinyMCEFocusHandler() {
	var theFrame = $('mce_editor_0');
	if(theFrame.contentDocument) {
	    Event.observe(theFrame.contentDocument, 'focus', displayRedboxLogin);
	} else {
	    Event.observe(theFrame, 'focus', displayRedboxLogin);
	}
}

// FastInit.addOnLoad(tinyMCEFocusHandler);

function addQuotesToForumReply(index, new_post_path) {
	$('topic-quotes').innerHTML = $('reply-' + index).innerHTML; 
	$('topic-quotes').style.display = 'block'; 
	$('topic-submit-form').action = new_post_path;
	$('forum_topic_quote_first').value = (index == 0) ? 1 : 0;
	$('comment_body').focus();
}

function quoteLastPostOnly(index) {
    if(typeof(last_post_checkbox) == 'undefined') {
        last_post_checkbox = 'checked';
    }
    if(last_post_checkbox == 'checked') {
       $('topic-quotes').innerHTML = $('post-' + index).innerHTML; 
       last_post_checkbox = 'unchecked';
    }
    else {
        $('topic-quotes').innerHTML = $('reply-' + index).innerHTML; 
        last_post_checkbox = 'checked';
    } 
}

function toggleLogin() {
	toggleStatus = typeof(toggleStatus) == 'undefined' || toggleStatus == 0 ? 1 : 0;
	if(toggleStatus == 1) {
		oldLoginText = $('user-menu').innerHTML;
		$('user-menu').innerHTML = $('login_toggle').innerHTML;
		new Effect.Scale( $('user-menu') , 180, {duration:.2,scaleContent:false,scaleY:false});
		setTimeout("$('user_login').focus()", 500);
	}
	else {
		new Effect.Scale( $('user-menu') , 56, {duration:.2,scaleContent:false,scaleY:false});
		setTimeout("$('user-menu').style.height = ''; $('user-menu').style.width = '';", 250)
		$('user-menu').innerHTML = oldLoginText;
	}
	return false;
}

function promptLogin(return_id, return_url) {
	if(typeof(return_id) == 'undefined') {
		window.location.href = '/login';
	}
	else {
		if(typeof(return_url) == 'undefined') {
			return_url = encodeURIComponent(window.location.href);
		}
		window.location.href = '/login?return_to=' + return_url + '#' + encodeURIComponent(return_id);
	}
}

function redirectParent() {
	setTimeout("window.close()", 250);
	opener.document.location = opener.document.getElementById('form-return-data').innerHTML;
	
}

function displayStarRatings(star_number, item_id) {
	if(typeof(already_rated) == 'undefined') {
		var stars = $('star_ratings_' + item_id).getElementsByTagName('a');
		for(i=0; i<stars.length; i++) {
			stars[i].className = "off";
		}
		for(i=0; i<star_number; i++) {
			stars[i].className = "on";		
		}
	}
}

function clearCommentTextarea(textarea) {
	if(textarea.className == 'visitor') {
		textarea.value = ''; 
		textarea.className = '';
	}
	return false;
}

function ReadCookie(cookieName) {
 var theCookie=""+document.cookie;
 var ind=theCookie.indexOf(cookieName);
 if (ind==-1 || cookieName=="") return ""; 
 var ind1=theCookie.indexOf(';',ind);
 if (ind1==-1) ind1=theCookie.length; 
 return unencodeURIComponent(theCookie.substring(ind+cookieName.length+1,ind1));
}

function prepareFeaturedContentManageDelete(item) {
	item.firstChild.toggleClassName('delete_me');
	element = $('delete[' + item.firstChild.id + ']')
	if(item.firstChild.className == 'delete_me') {
		element.value = 1;
	}
	else {
		element.value = 0;
	}
	return false;
}

function vitalStat(type, id) {
	url = '/' + type + '/bump/' + id
	new Ajax.Request(url, {method: 'post'})
}

function checkAllPendingFriends() {
  if(typeof(check_uncheck) == 'undefined') {
    check_uncheck = 'uncheck';
  }
  fields = $$('table#member_friends input');
  if(check_uncheck == 'check') {
    for(i=0;i<fields.length;i++) {
      fields[i].checked = '';
    }
    $('check_all_link').innerHTML = 'Check All';
    check_uncheck = 'uncheck';
  }
  else {
    for(i=0;i<fields.length;i++) {
      fields[i].checked = 'checked';
    }
    $('check_all_link').innerHTML = 'Uncheck All';
    check_uncheck = 'check';
  }
}
//Event.observe(window, 'load', function() { $$('form').each(function(f) { f.reset(); }); });
