/* +----------------------------------------------------+ */
/* | JavaScript for Hextractor.com                      | */
/* +----------------------------------------------------+ */

/**
 * Call onLoad functions
 */
$(document).ready(function() {
	tab_observer();
	contact_link_handler();
	field_observer();
	file_input_observer();
	get_palette();
	reset_observer();
});

/**
 * Kill parent framesets
 */
if (top.location != self.location) {
	top.location.replace(self.location.href);
}

/**
 * Displays the form for the selected tab
 */
function tab_observer()
{
	$('#tab_list li a').click(function(event) {
		// Hide current form and switch tab focus 
		$('div.input_area').hide();
		$('#tab_list li a').removeClass('current');
		$('#tab_list li a').addClass('inactive');
		$('#' + this.id).addClass('current');
		// Show selected form 
		var form = this.id.replace('_tab', '_form');
		$('#' + form).show();
		event.preventDefault();
	});
}

/**
 * Watch form fields and take any necessary actions
 */
function field_observer()
{
	$('#url_field').click(function() {
		if ($('#url_field').get(0).value == $('#url_field').get(0).defaultValue) {
			$('#url_field').attr('value', '');
		}
	});
}

/**
 * Triggers Helpspot contact form
 */
function contact_link_handler()
{
	$('a.contact_link').click(function(event) {
		HelpSpotWidget.Tab.open();
		event.preventDefault();
	});
}

/**
 * Gets color palette from provided CSS 
 */
function get_palette()
{
	$('#form').submit(function(event) {
		// Figure out which form is active
		var input_id    = $('div.input_area:visible').get(0).id.replace('_form', '');
		var input_field = input_id + "_field";
		
		//pageTracker._trackPageview('/extract/' + input_id);
		$('#reset').show();
		
		// Don't use Ajax for file uploads; allow form to submit normally
		if (input_id == "upload") return true;
		
		var form = $('#form').serialize();		
		jQuery.post('/extract', form, function(data) {
			$('#palette').empty().append(data);
		});
		
		$('#' + input_field).attr('value', $('#' + input_field).get(0).defaultValue);
		event.preventDefault();
	});
}

/**
 * Display path and filename of selected file to upload.
 */
function file_input_observer()
{
	$('#upload_field').change(function() {
		var file_info       = $('#file_info').get(0);
		var file_info_label = $('#file_info_label').get(0);
		var file_path       = $('#upload_field').get(0).value;
				
		// Split path on *nix or Windows slashes
		var delimiter_regex = /(\/|\\)/;
		var path_parts   = file_path.split(delimiter_regex);
		var file_pos_num = path_parts.length - 1;
		var file_name    = path_parts[file_pos_num];
		
		$(file_info_label).show();
		$(file_info).empty().append(file_name).show();
	});
}

/**
 * Sorts color table by column header
 */
function sort_color_table()
{
	$('#colortable th a').click(function(event) {
		var column = this.id.replace('sort_', '');
		$('#colortable').load('/colormap/?sort=' + column);
		event.preventDefault();
	});
}

/**
 * Toggles visibility of palette source HTML
 */
function toggle_source()
{
	$('div.sourcelink').click(function() {
		$('#source_text').toggle('slow');
		$('#show_source').toggle();
		$('#hide_source').toggle();
	});
	$('#source_text').click(function() {
		$('#source_text textarea').select();
	});
}

/**
 * Clear submitted styles and display example again
 */
function reset_observer()
{
	$('#reset').click(function(event) {
		$('#palette').load('/reset');
		$('#reset').hide();
		event.preventDefault();
	});
}


/* +----------------------------------------------------+ */
/* | Third party JavaScript                             | */
/* +----------------------------------------------------+ */

/* Helpspot contact form */
if(!HelpSpotWidget){var HelpSpotWidget={}}HelpSpotWidget.Util={render:function(a,b){return a.replace(/\#{([^{}]*)}/g,function(d,c){var e=b[c];return typeof e==="string"||typeof e==="number"?e:d})},toQueryString:function(a){var b=[];for(key in a){if(a[key]!=null&&a[key]!=""&&key.substring(0,2)!="on"){b.push([key,escape(a[key])].join("="))}}return b.join("&")}};HelpSpotWidget.Tab={id:"helpspot-widget-tab",css_template:"body a##{id}, body a##{id}:link { #{alignment}: 0; top: #{top} !important; margin-#{alignment}: -5px !important; background-position: top left !important; background-color: #{background_color}; background-image: url(#{tabimage}); } body a##{id}:hover { background-color: #{hover_color};margin-#{alignment}: -3px !important; } * html a##{id}, * html a##{id}:link { filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='#{tabimage}'); } #helpspot_widget_tab_close img { filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='#{closeimage}'); cursor:pointer;} #helpspot_widget_tab_overlay {top:0; left:0; width: 100%; height: 100%; position: fixed; _position: absolute; z-index: 100002;}  #helpspot_widget_tab_overlay * { margin: 0; padding: 0; font-family: 'Lucida Grande', 'Lucida Sans Unicode', sans-serif; font-style:normal; font-variant:normal;}  #helpspot_widget_mask { top:0; left:0; z-index:1; width: 100%; position: absolute; background-color: #{overlay_color}; opacity: 0.45; filter:alpha(opacity=45); -moz-opacity: 0.45;} #helpspot_widget_tab_loading {background-color: #{popup_background_color}; border-style: solid; border-color: #{popup_border_color}; border-width: #{popup_border_size}; }",setupOptions:function(a){this.options={alignment:"left",tabtype:"questions",tabtype_custom_img:"",top:"30%",width:600,host:"/",color:"white",background_color:"#222222",hover_color:"#222",popup_background_color:"#fff",popup_border_color:"#ccc",popup_border_size:"10px",overlay_color:"#000",default_note:"",default_name:"",default_email:"",use_field_name:false,text_header:"How can we help?",text_intro:"Submit your question/comment for a member of our team.",text_note:"Question",text_note_er:"Please provide some information on your request",text_email:"Your Email",text_email_er:"Please provide your email address",text_name:"Your Name",text_name_er:"Please provide your name",text_submit:"Submit",text_msg_submit:"Message sent, thank you.",text_msg_submit_error:"Sorry, there was an error.",text_msg_submit_error_link:"Please try this form",text_msg_submit_error_url:"",text_loading:"Loading...",text_special:"",onLoad:false,onClose:false};for(attr in a){this.options[attr]=a[attr]}this.options.id=this.id;this.options.closeimage=this.options.host+"/widgets/images/close.png";this.options.width=(this.options.width?this.options.width:600);this.options.text_msg_submit_error_url=(this.options.text_msg_submit_error_url?this.options.text_msg_submit_error_url:this.options.host+"/index.php?pg=request");if(this.options.tabtype_custom_img){this.options.tabimage=this.options.tabtype_custom_img}else{this.options.tabimage=this.options.host+"/widgets/images/tab/"+this.options.tabtype+"-"+this.options.color+".png"}},show:function(a){this.setupOptions(a||{});document.write('<a id="'+this.id+'" onclick="HelpSpotWidget.Tab.open(); return false;" href=""></a>');document.write('<style type="text/css">'+HelpSpotWidget.Util.render(this.css_template,this.options)+"</style>")},open:function(){if(!document.getElementById("helpspot_widget_tab_overlay")){var a=document.createElement("div");a.id="helpspot_widget_tab_overlay";a.style.display="none";a.innerHTML='<div id="helpspot_widget_tab_wrapper"><div id="helpspot_widget_tab_close"><img src="'+this.options.closeimage+'" onclick="HelpSpotWidget.Tab.close();return false" /></div><div id="helpspot_widget_tab_loading">'+this.options.text_loading+'</div><iframe src="'+this.options.host+"/widgets/tab.php?"+HelpSpotWidget.Util.toQueryString(this.options)+"&rand="+Math.floor(Math.random()*1000)+'" name="helpspot_widget_tab_iframe" id="helpspot_widget_tab_iframe" allowTransparency="true" scrolling="no" frameborder="0" class=""></iframe></div><div id="helpspot_widget_mask" onclick="HelpSpotWidget.Tab.close();return false"></div>';document.body.appendChild(a);if(document.getElementById("helpspot_widget_tab_iframe").attachEvent){document.getElementById("helpspot_widget_tab_iframe").attachEvent("onload",HelpSpotWidget.Tab.ready)}else{if(document.getElementById("helpspot_widget_tab_iframe").addEventListener){document.getElementById("helpspot_widget_tab_iframe").addEventListener("load",HelpSpotWidget.Tab.ready,false)}}document.getElementById("helpspot_widget_mask").style.height=HelpSpotWidget.page.theight()+"px";document.getElementById("helpspot_widget_mask").style.width=HelpSpotWidget.page.twidth()+"px";document.getElementById("helpspot_widget_tab_wrapper").style.top=(0.1*HelpSpotWidget.page.height())+"px"}document.getElementById("helpspot_widget_tab_overlay").style.display="block"},ready:function(){document.getElementById("helpspot_widget_tab_loading").style.display="none";document.getElementById("helpspot_widget_tab_iframe").style.display="block";if(HelpSpotWidget.Tab.options.onLoad){HelpSpotWidget.Tab.options.onLoad()}},close:function(){HelpSpotWidget.Tab.reset();if(HelpSpotWidget.Tab.options.onClose){HelpSpotWidget.Tab.options.onClose()}},reset:function(){document.body.removeChild(document.getElementById("helpspot_widget_tab_overlay"))}};HelpSpotWidget.page=function(){return{top:function(){return document.body.scrollTop||document.documentElement.scrollTop},width:function(){return self.innerWidth||document.documentElement.clientWidth},height:function(){return self.innerHeight||document.documentElement.clientHeight},theight:function(){var f=document,a=f.body,c=f.documentElement;return Math.max(Math.max(a.scrollHeight,c.scrollHeight),Math.max(a.clientHeight,c.clientHeight))},twidth:function(){var f=document,a=f.body,c=f.documentElement;return Math.max(Math.max(a.scrollWidth,c.scrollWidth),Math.max(a.clientWidth,c.clientWidth))}}}();


