// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

/*  
 * Unobtrusive image rollover with Prototype library, v1.1
 * 
 * Created by Herryanto Siatono
 * Copyright (c) 2007 Pluit Solutions <www.pluitsolutions.com>
 * 
 * This script is freely distributable under the terms of an MIT-style license.
/*------------------------------------------------------------------------------*/

var Rollover= Class.create();
Rollover.prototype = {
  // provide the container id containing image links to be rolled over.
  initialize: function(theclass, options) {
    this.theclass = theclass;
	this.theid = "test";
    this.images = {};
	this.submit_tags = {}
    this.setOptions(options);
    this.observeLinks();
  },
  
  setOptions: function(options) {
    this.options = {
      rolloverSuffix: 'on',
      selectedSuffix: 'sel',
      suffixSeperator: '-'
    };
    Object.extend(this.options, options || {});
  },

  observeLinks: function() {
	this.links = $$('.' + this.theclass);
    for (i=0; i<this.links.length; i++) {
      this.links[i].observe('mouseover', this.rolloverImage);
      this.links[i].observe('mouseout', this.rollbackImage);
    }
    
	submit_tags = $$('input.' + this.theclass)
	
	for (i=0; i<submit_tags.length; i++) {
		tagId = submit_tags[i].id 
	     if (!tagId) {
        	tagId  = this.id + i;
        	submit_tags[i].id = tagId;
      	}

      this.submit_tags[tagId] = submit_tags[i].src;
      submit_tags[i].imageRollover = this;

      // preload rollover image
      (new Image()).src = this.parseRolloverImage(submit_tags[i]);
    
	}
	
    images = $$('.' + this.theclass + ' img');

    for (i=0; i<images.length; i++) {
	  
      imageId = images[i].id 
      if (!imageId) {
        imageId  = this.id + i;
        images[i].id = imageId;
      }
      
      this.images[imageId] = images[i].src;
      images[i].imageRollover = this;

      // preload rollover image
      (new Image()).src = this.parseRolloverImage(images[i]);
    }
  },
  
  rolloverImage: function(e) {
    image = Event.element(e);
    if (image.imageRollover) {
      image.imageRollover.images[image.id] = image.src;
      image.src = image.imageRollover.parseRolloverImage(image);  
    }
  },

  rollbackImage: function(e) {
    image = Event.element(e);
    if (image.imageRollover) {
      image.src = image.imageRollover.images[image.id];
    }
  },
  
  parseRolloverImage: function(image) {
    ext = image.src.substr(image.src.lastIndexOf('.'));
    path = image.src.match(/(.*)\/(.*\.(png|gif|jpg))/)[1]
    filename = image.src.gsub(path, '')
    basename = this.parseBasename(filename);
    basename = this.parseBasename(basename, this.options.suffixSeperator + this.options.selectedSuffix);
    return path + basename + this.options.suffixSeperator + this.options.rolloverSuffix + ext;
  },
  
  parseBasename: function(name, seperator) {
    seperator = seperator || '.';
    found = name.lastIndexOf(seperator);
    if (found > 0) {
      return name.substr(0, found);
    } else {
      return name;
    }
  }
}

function replace_tab(target) {
	//search for the tab with the class active
	active_tab = $$('#tabnav .active')[0];
	//mark the old tab and tab_content as hidden
	active_tab.removeClassName("active");
	//window.alert($(target + \"_li\").hasClassName(\"active\"));
	//$(target + "_li").removeClassName('active');
	$(target).addClassName('active');
	$('tab_inner').addClassName('dimmed');
	Element.show('tabspinner');
	//window.alert($(target + \"_li\").hasClassName(\"active\"));
}

function sendReaderReplyGA() {
	pageTracker._trackPageview("/requests/more_info_new");
}

function sendReaderReplyAdvisorGA() {
	pageTracker._trackPageview("/requests/more_info_advisor");
}

function reply_to_comment(id, username) {
	$('comment_parent_id').value = id;
	$('new_comment').scrollTo();
	$('username').innerHTML = username;
	$('in_reply_to').show();
    new Effect.Highlight('in_reply_to');
}

function reset_reply() {
	$('comment_parent_id').value = null;
	$('in_reply_to').hide();
	$('username').innerHTML = '';
}

function free_mag_extend() {
	thestate = (typeof thestate == 'undefined') ?
	     "closed" : thestate;
	if(thestate != "open" && thestate != "closing") {
	$('free_mag_floater').absolutize();
	$('fm_placeholder').show();
	new Effect.Fade('free_mag_initial', { 
		duration: 0.3,
		queue: 'end'
	 });
	
	new Effect.Morph('free_mag_floater', {
	  style: {
		width: '400px',
		left: '-216px'
	  },
	  duration: 0.3,
	  queue: 'end'
	});
	
	new Effect.Fade('red-tagged', {
	  duration: 0.3
	});
	
	//$('red-tagged').setStyle({
	  //backgroundImage: 'none'
	//});
	
	new Effect.Appear('free_mag_form', { 
		duration: 0.5,
		queue: 'end'
	 });
	thestate="open";
	}
}

function free_mag_retract() {
	thestate = "closing";
	new Effect.Fade('free_mag_form', { 
		duration: 0.3,
		queue: 'end'
	 });
	
	new Effect.Morph('free_mag_floater', {
	  style: {
		width: '170px',
		left: '14px'
	  }, // CSS Properties
	  duration: 0.3,
	  queue: 'end'
	});
	
	new Effect.Appear('red-tagged', {
	  duration: 0.1,
	  queue: 'end'
	});
	
	new Effect.Appear('free_mag_initial', {
		duration: 0.3,
		queue: 'end'
	});
	

	
	$('red-tagged').setStyle({
	  backgroundImage: 'url(/rimages/red-tag.gif)'
	});
	
	setTimeout('thestate = "closed"', 800);
}

function funkyForm(form, fields) {
	fields.each(function(field) {
	  	Event.observe(field['name'], 'focus', function(e) {
		  if($F(this) == field['value']) {
			this.value = '';
		}
		});
		Event.observe(field['name'], 'blur', function(e) {
		  if($F(this) == '') {
			this.value = field['value'];
		  }
		});
	});
	
	Event.observe(form, 'submit', function(e) {
		resetFields(fields);
	});
}

function resetFields(fields) {
	fields.each(function(field) {
  		if($F(field['name']) == field['value']) {
			$(field['name']).value = '';
		}
	});
}

function logFloatBox() {
	pageTracker._trackPageview("/flowchart");
}

function closeAndLogFloatBox() {
	var dir = location.href.substring(0,location.href.indexOf('/', 8));
	var url = location.href.substring(dir.length,location.href.length+1);
	//window.alert(url);
	pageTracker._trackPageview(url);
	fb.end()
}