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

document.hasFlash = function(reqVersion) {
	var p = navigator.plugins['Shockwave Flash'];
	if (typeof p == 'object') 
	{
		for (var i=16; i >= 3; i--) 
		{			
			if (p.description && p.description.indexOf(' ' + i + '.') != -1) {
				flashVersion = i; break;
			}
		}
		
		alert(flashVersion);
		
		if(flashVersion < reqVersion) {
			return false;
		}
		
		return true;
	}
	else {
		return false;
	}
}

function flashCheck() {
	/*if(!document.hasFlash(7)){
		if(confirm("Sorry, but Adobe Flash Player 8 or later is required to view rich media content on Business POV.\nYou can get the latest version of Flash Player from http://adobe.com/.\nTo go to Adobe's web site right now, please click OK.")) {
			location.href = 'http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash&promoid=BIOW';};	
		}*/
}

function updateRowShading(selector) {
	rows = $$(selector);
	x = 0;

	rows.each(function(row){

		if(x % 2 == 0) {
			row.removeClassName('odd');
			row.addClassName('even');
		}
		else {
			row.removeClassName('even');
			row.addClassName('odd');
		}

		x += 1;
	});
}

var GhettoInlineEditor = {
	create: function(id) {
		$(id+'_edit').hide();
		
		if(!$(id).getAttribute('ghettonoclick')) {
			Event.observe(id,'mouseover',function(){$(id).addClassName('inlineedit')},false);
			Event.observe(id,'mouseout',function(){$(id).removeClassName('inlineedit')},false);
			Event.observe(id,'click',function(){GhettoInlineEditor.activate(id)},false);
		}
	},
	activate: function(id){
		$(id).hide();
		$(id+'_edit').show();
		$(id+'_field').focus();
	},
	cancel: function(id){
		$(id).show();
		$(id+'_edit').hide();
	},
	cancelAndClear: function(id){
		this.cancel(id);
		$(id+'_field').value = '';
	}
}

function createLiveEditor() {
	$('PreviewLink').onclick = switchToPreview;
	$('EditLink').onclick = switchToEditor;
	
	
	Event.observe(document, 'keypress',function(event){
		
		kc = event.which ? event.which : event.keyCode;
		
		if(event.ctrlKey && event.shiftKey){
			
			switch(String.fromCharCode(kc)) {
				case 'P':
					switchToPreview();
					break;
				case 'E':
					switchToEditor();
					break;
				case 'S':
					$('ArticleForm').submit();
					break;
			}
		}
		
	},false);
}

function switchToEditor() {
	$('PreviewBox').hide();
	$('EditorBox').show();
	Element.removeClassName('PreviewTab','on');
	Element.addClassName('EditTab','on');
	$('EditLink').blur();
	return false;
}

function switchToPreview() {		
	refreshPreview();		
	$('EditorBox').hide();
	$('PreviewBox').show();
	Element.removeClassName('EditTab','on');
	Element.addClassName('PreviewTab','on');
	$('PreviewLink').blur();
	return false;
}

function refreshPreview() {
	
	et = $F('EditorText');
	et = et.replace(/\&/,"%26");
	et = et.replace(/\;/,"%3B");
	
	new Ajax.Updater('PreviewBoxContents',$F('preview_ajax_url'),{
		asynchronous: true,
		evalScripts: true,
		method: 'post',
		parameters: 'text=' + et
	});
}

function resetSwitcherHighlights(id) {
	links = $$('#MediaTypeSwitcher .SwitcherLink')
	links.each(function(link){
		link.removeClassName('on');
	})
	$(id + 'SwitcherLink').addClassName('on')
}


function doGraphResize() {
	elems = $$('.GraphBar');
	
	dims = Element.getDimensions('TrafficGraph');
	w = parseInt(((dims.width - 20) / elems.length) - 2);
	
	x = 0;
	elems.each(function(elem){
		elem.style.width = w + 'px';
		elem.style.left = (((w * x) + (x * 2)) + 10) + 'px';
		
		$('TrafficGraphInner').style.right = '0px';
		x += 1;
	});
	
	elems = $$('.GraphBar .UniqueBar');
	
	elems.each(function(elem){
		elem.style.width = w + 'px';
	});
	
	elems = $$('.GraphBar .date');
	
	elems.each(function(elem){
		elem.style.width = w + 'px';
		elem.style.textAlign = 'center';
	});
	
	$('TrafficGraphInner').style.width = (((w + 2) * elems.length) + 25) + 'px'
}