// 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($('editor_editor').contentDocument.body.innerHTML) }); return false;
}

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

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

function previewProductGuidePost() {
	//new Ajax.Updater('preview_area', '/manage/product_guides/preview', {asynchronous:true, evalScripts:true, method:'post', parameters:'body=' + encodeURIComponent($('comment_body').value) + '&nl2br=' + $('product_guide_nl2br').checked }); return false;
	new Ajax.Updater('preview_area', '/manage/product_guides/preview', {asynchronous:true, evalScripts:true, method:'post', parameters:'body=' + encodeURIComponent($('editor_editor').contentDocument.body.innerHTML) }); return false;
}

function previewHomepageProductBlog(id) {
	new Ajax.Updater('product-blog-text', '/manage/product_guides/preview_homepage', {asynchronous:true, evalScripts:true, method:'post', parameters:'body=' + encodeURIComponent($('product_guide_mini_body').value) + '&title=' + encodeURIComponent($('product_guide_mini_title').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', '/new_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';
  }
}

function showSurvey() {
  /*if (!Cookie.get('mx_survey')) {
    new Effect.Move('survey_floatover', {x:265, y:180, mode: 'absolute'});
  }*/
}

function showSearch() {
  if(typeof(searching) == 'undefined' || searching == false) {
    Effect.toggle('searchMenu','appear',{duration: 0.3});
    moveSearchMenu(30);
    setTimeout("$('googleSearchField').focus()", 800);
    searching = true;
  }
  else {
    Effect.toggle('searchMenu','appear',{duration: 0.3});
    moveSearchMenu(-30);
    searching = false;
  }
}

function moveSearchMenu(y) {
  if($('main-wrapper')) {
    new Effect.MoveBy('main-wrapper', y, 0, {duration: 0.3,transition: Effect.Transitions.sinoidal});
  }
  else if ($('main-content-detail')) {
    new Effect.MoveBy('main-content-detail', y, 0, {duration: 0.3,transition: Effect.Transitions.sinoidal});
    new Effect.MoveBy('right-sidebar-detail', y, 0, {duration: 0.3,transition: Effect.Transitions.sinoidal});
  }
}

function spotlightMouseover(n, manual) {
  $$('#mainSpotlight div img').each(function(t) { t.className = 'none'; });
  $('spotlightRolloverA' + n).className = 'rollover';
  if(typeof(currentCycle) == 'undefined') {
    currentCycle = 1;
  }
  if(currentCycle == n) {
    return;
  }
  else {
    currentCycle = n;
  }
  
  spotlightMousedown(n);
}
function spotlightMouseoff(n) {
  if(typeof(currentCycle) != 'undefined' && currentCycle == n) {
    return;
  }
  currentCycle = n;
  cycleSpotlightA();
  //startSpotlightA();
}
function spotlightMousedown(n) {
  if(typeof(lastSpotlight) == 'undefined') {
    lastSpotlight = 'spotlightPhotoA1';
  }
  $('mainSpotlight').style.backgroundImage = 'url(' + $(lastSpotlight).src + ')';
  new Effect.Morph('spotlightPhotoA' + n,{
    style:'top: 0;', duration:0.5, transition: Effect.Transitions.sinoidal
  });
  lastSpotlight = 'spotlightPhotoA' + n;
  $$('#mainSpotlight > a.spotlightAImgLink img').each(function(t) { 
    t.style.top = '400px'; 
  });
  $$('#spotlightCaption span').each(function(t) { t.className = 'none'; });
  $('spotlightTitleA' + n).className = 'rollover';
  $('spotlightCaptionA' + n).className = 'rollover';
}

function cycleSpotlightA() {
  if(typeof(currentCycle) == 'undefined') {
    currentCycle = 1;
  }
  //currentCycle = (currentCycle > 5) ? 1 : (currentCycle);
  spotlightMouseover(currentCycle, false);
  spotlightMousedown(currentCycle);
}

function startSpotlightA() {
  spotlightInterval = setInterval(cycleSpotlightA, 5000);
}
function swapButton(type, status, button) {
  if(status == 'over') {
    button.src = '/images/buttons/' + type + '_OVER.png';
  }
  else {
    button.src = '/images/buttons/' + type + '.png';
  }
}

function createSpotlightBAjaxPagination() {
  var b_pages_span = $('secondSpotlightPagination').getElementsByTagName('span');
  var b_pages_a = $('secondSpotlightPagination').getElementsByTagName('a');
  
  function updatePage(page) {
    new Ajax.Updater('secondSpotlightWrapper', '/new_homepage/update_spotlight_b/?page=' + page, { evalScripts: true });
  }
  function toInt(element) {
    return parseInt(element.innerHTML, 10)
  }
  function isPrevious(element) {
    return element.innerHTML.include('Previous');
  }
  function isNext(element) {
    return element.innerHTML.include('Next');
  }
  /*
  function setHref(element) {
    if(isPrevious(element)) {
      element.href = "/?page=" + prev;
    }
    else if(isNext(element)) {
      element.href = "/?page=" + next;
    }
    else {
      element.href = "/?page=" + toInt(element);
    }
  }
  */
  function respondToClick(e) {
    var element = e.element();
    if(isPrevious(element)) {
      updatePage(prev);
    }
    else if(isNext(element)) {
      updatePage(next);
    }
    else {
      updatePage(toInt(element));
    }
  }
  
  for(i=0;i<b_pages_span.length;i++) { 
    var c = toInt(b_pages_span[i]);
    if(!isNaN(c)) {
      prev = c - 1;
      next = c + 1;
    }
  }

  for(i=0;i<b_pages_a.length;i++) { 
    var element = b_pages_a[i];
    Event.observe(element, 'click', respondToClick, false);
    /*
    if(window.addEventListener) {
      element.addEventListener("click", respondToClick, false); 
    } 
    else {
      element.attachEvent('onclick', respondToClick);
    }*/
    //element.observe('click', respondToClick);
    //element.onclick = function(){ return false; };
    element.href = "/#secondSpotlight";
  }
}

function set_lightboxes() {
  var post_images = $$('div.forum-post-body img');
  for(i=0;i<post_images.length;i++) {
    if (post_images[i].width == 400) {
      var div = new Element('a', { 'rel': 'lightbox', href: post_images[i].src });
      post_images[i].wrap(div);
    }
  }
}

//Event.observe(window, 'load', function() { $$('form').each(function(f) { f.reset(); }); });