// JavaScript Document
$j=jQuery.noConflict(); // required in wordpress to avoid conflicts
$j(document).ready(function(){ 

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
	
	var myThis = this;

	/* Make it so the RSS button in the header has a mouseover */
	myThis.onMouseOverHeaderRSS = function(e)
	{
		var rssImg = $j('img[name=btn_rss]', this);
		if (rssImg.length < 1) return;
		
		rssImg.attr('mouseoutimagesrc', rssImg.attr('src'));
		rssImg.attr('src', rssImg.attr('mouseoverimagesrc'));
	}
	myThis.onMouseOutHeaderRSS = function(e)
	{
		var rssImg = $j('img[name=btn_rss]', this);
		if (rssImg.length < 1) return;
		
		rssImg.attr('src', rssImg.attr('mouseoutimagesrc'));
	}
	
	$j('#btn_rssfeed').bind('mouseover.onMouseOverHeaderRSS', myThis.onMouseOverHeaderRSS);
	$j('#btn_rssfeed').bind('mouseout.onMouseOutHeaderRSS', myThis.onMouseOutHeaderRSS);

	/* Slider Code */
    var slideOutTimeout = 3500;
    // add slider to subscribe form

    var slider1Info =   {
							myID:'slider1',
                            button:{id:'#btn_subscribe',OnClickSubmit:false},
                            slider:{id:'#subscribeslider',state:{over:{width: 300, opacity: 1},out:{width: 0,opacity: 0,marginLeft: 109}}},
                            textBox:{id:'input[name=gsom_email_field]',state:{over:{width: 200, opacity: 1},out:{width: 0,opacity: 1,marginLeft: 0}}},
                            form:{id:'#subscribeform'},
                            slideOutTimeout: 3500,
                            blankTextTimeout: 7000
                        };
                        

    var slider2Info =   {
							myID:'slider2',
                            button:{id:'#tab_search',OnClickSubmit:true},
                            slider:{id:'#searchslider',state:{over:{width: 300,opacity: 1,marginLeft:-273},out:{width: 0,opacity: 0,marginLeft:30}}},
                            textBox:{id:'#txt_search',state:{over:{width: 300},out:{width: 0,opacity: 1,margin: 0}}},
                            form:{id:'#searchform'},
                            slideOutTimeout: 3500,
                            blankTextTimeout: 7000
                        };
                        
    var slider1 = new VerbatimSlider(slider1Info);
    var slider2 = new VerbatimSlider(slider2Info);
	
	slider1.setSliderState(1);
	slider1.doClose();
	
	// Setting a property 'window.slider1' on the window object
	// so we can get to it in timeout calls
	window.slider1 = slider1;
	window.slider2 = slider2;
	
	// Check to see if the Focus Events are already 
	// bound during initialization
	if(!slider1.boundTextBoxFocusEvents)
	{
		// try to bind subscribe events if
		// input objects have been rendered
		slider1.bindTextBoxFocusEvents();
		// if this bind was not successful 
		if(!slider1.boundTextBoxFocusEvents)
			// set a timeout to bind again.
			setTimeout("slider1.bindTextBoxFocusEvents();", 10);
	}
	
	/* This method will handle the when the user
	 * hits the enter key while in the subscribe
	 * textbox
	 */
	this.subscribeTextBoxKeyPressDelegate = function(e)
	{
		// jquery identity of the go button
		var go_button_id = 'input[name="gsomsubscribe"]';
		// check to see if the button actually exists on the page
		if ($j(go_button_id).length < 1) return;
		// Check to see if the user actually clicked on the enter button
		if(e.which != 13) return;
		// Submit the email by clicking on the go button
		$j(go_button_id).trigger('click');
		// Returning False will stop the form containg the 
		// textbox from submiting and allow the go button to 
		// do the actual submission.
		return false;
	}
	// setting delegate handler in 'window' scope'
	window.slider1.onTextBoxKeyPress = function(e) { return myThis.subscribeTextBoxKeyPressDelegate (e); };
	// Check to see if the binding has been done already
	if (!slider1.boundTextBoxKeyPressEvents)
	{
		// try to bind the key press event to subscribeTextBoxKeyPressDelegate()
		slider1.bindTextBoxKeyPressEvents( myThis.subscribeTextBoxKeyPressDelegate );
		// Check if the bind was successful
		if(!slider1.boundTextBoxKeyPressEvents)
			// the bind was not successful so set a timeout to attempt to 
			// binding the subscribe text box keypress event
			setTimeout("slider1.bindTextBoxKeyPressEvents(slider1.onTextBoxKeyPress);", 10);
	}
	// This function will set the default text of the
	// subscribe text box "Please Enter Email."
	window.slider1.setDefaultSubscribeText = function()
	{
		var textBoxJQ = $j( slider1Info.textBox.id );
		if(textBoxJQ.length < 1) 
		{
			setTimeout("slider1.setDefaultSubscribeText();", 10);
			return;
		}
		
		textBoxJQ.val("Please enter email address.");
	}
	
	window.slider1.clearDefaultSubscribeText = function()
	{
		var textBoxJQ = $j( slider1Info.textBox.id );
		if(textBoxJQ.length < 1) return false;
		
		textBoxJQ.val("");
		textBoxJQ.unbind("focus.clearDefaultSubscribeText");
	}
	
	slider1.setDefaultSubscribeText();
	
	window.slider1.bindClearDefaultSubscribeText = function()
	{
		var textBoxJQ = $j(slider1Info.textBox.id);
		if(textBoxJQ.length < 1) 
		{
			setTimeout('slider1.bindClearDefaultSubscribeText();', 10);
		}
		
		textBoxJQ.bind("focus.clearDefaultSubscribeText", slider1.clearDefaultSubscribeText);
	}
	slider1.bindClearDefaultSubscribeText();
	
	window.slider1.centerTextBox = function()
	{
		var textBoxJQ = $j(slider1Info.textBox.id);
		if(textBoxJQ.length < 1) 
		{
			setTimeout("slider1.centerTextBox();", 10);
		}
		textBoxJQ.css("line-height", 2.5);
	}
	
	slider1.centerTextBox();
	
	
	
	slider2.setSliderState(1);
	slider2.doClose();
	
	/* Share Link Code */
    var shareHTML = '<div class="share-dropdown"><a class="shareEmailLink">EMAIL</a><a class="shareFBLink">FaceBook</a><br /><a class="shareTweetLink">Twitter</a><a class="shareDeliLink">DELICIOUS</a></div>';
    
    $j('.share-link a').css('float', 'left');
    $j('.share-link').append(shareHTML);

    $j('.share-dropdown').hide();

    $j('.share-link').bind('mouseover', 
        function() { 
            if ($j(this).attr('dropdown') != 'open')
            {
                $j('.share-dropdown', this).show(); 
                $j(this).attr('dropdown', 'open');
                //$j('.btn_expand', this).addClass('btn_collapse').removeClass('btn_expand');
            }
        });
    $j('.share-link').bind('mouseout', 
        function() { 
            if ($j(this).attr('dropdown') != 'closed')
            {
                $j('.share-dropdown', this).hide(); 
                $j(this).attr('dropdown', 'closed');
                //$j('.btn_collapse', this).addClass('btn_expand').removeClass('btn_collapse');
            }
        });
        
//    $j('.shareDiggLink').bind('click', shareByDigg);
	
	shareLinks = $j('.share-link');
	
	for(var i=0; i<shareLinks.length; i++)
	{
		var slink = shareLinks[i];
		var title = "Verbatim: "+slink.attributes['verbatimShareTitle'].value;
		var shareLink = slink.attributes['verbatimShareLink'].value;
		var shorthref = (slink.attributes['shorthref']!= null) ? slink.attributes['shorthref'].value : shareLink;
		
		var emailLink = "mailto:?subject="+title+"&body=I thought you would find this interesting: "+shareLink;
		var facebookLink = "http://www.facebook.com/sharer.php?u="+shareLink+"&t="+title;
		var diggLink = "http://digg.com/submit?phase=2&amp;url="+shareLink+"&amp;title="+title;
		var twitterLink = "http://twitter.com/home?status="+encodeURI(title)+ " " + shorthref + " via @communispace";
				
		var deliLink = "http://del.icio.us/post?url="+shareLink+"&amp;title="+title;
		
		$j('.shareEmailLink', slink).attr('href', emailLink);
		$j('.shareFBLink', slink).attr('href', facebookLink);
		$j('.shareTweetLink', slink).attr('href', twitterLink);
	    $j('.shareDeliLink', slink).attr('href', deliLink);
		
		$j('.shareFBLink, .shareEmailLink, .shareTweetLink, .shareDeliLink', slink).attr('target', '_blank');
	}

    $j('.share-link').bind('click', function(e) { 
													var eventObject = e.srcElement;
													if ($j(e.target).hasClass('shareEmailLink') || 
														$j(e.target).hasClass('shareFBLink') ||
														$j(e.target).hasClass('shareTweetLink') ||
														$j(e.target).hasClass('shareDeliLink'))
													{
														return true;
													}
													return false; 
												} );
	
	/* Author Page */
	var currentAuthorID = 0;

	$j('.shadowHolder img.avatar, .shadowHolder .shadow').bind('click', function() 
												{
													var authorID = $j(this).parent().attr("authorDetailIcon");

													$j('.shadow').fadeIn();
													$j('.shadow').css('display', '');
													$j('.AuthorRightBox[authorid]').fadeOut();
													$j('.AuthorRightBox[authorid='+authorID+']').delay(300).fadeIn();
													currentAuthorID = authorID;
													window.scroll(0,0);
													$j('.shadow', $j(this).parent()[0]).hide();
												});
		
	// Clicking on Category on People Page handler						
	$j('.CategoryLeftHeaderBox').bind('click', function()
												{
													var category = this.attributes['category'].value;													
													
													if(category == 'other' && $j('.expandIcon', this).attr('state') != 'minus')
													{
														$j('.expandIcon[state=minus]').attr('state', 'cross');
														
														$j('.CategoryLeftHeaderBox[category='+category+'] .expandIcon').attr('state', 'minus');
														
														$j('.expandIcon[state=minus]').removeClass('cross').addClass('minus');
														$j('.expandIcon[state=cross]').removeClass('minus').addClass('cross');
														
														$j('.leftAuthorPicContainer[category!='+category+']').slideUp();	
														$j('.leftAuthorPicContainer[category='+category+']').slideDown();
													}
													else if ( $j('.expandIcon', this).attr('state') != 'minus')
													{
														$j('.expandIcon', this).attr('state', 'minus');
														$j('.expandIcon', this).removeClass('cross').addClass('minus');
														$j('.leftAuthorPicContainer[category='+category+']').slideDown();
													}
													else
													{
														$j('.expandIcon', this).attr('state', 'cross');
														$j('.expandIcon', this).removeClass('minus').addClass('cross');
														$j('.leftAuthorPicContainer[category='+category+']').slideUp();
													}
												});
    
  });
  
/* Slider Object */
function VerbatimSlider(sliderInfo)
{   
    //Get the Timeout
    var slideOutTimeout = sliderInfo.slideOutTimeout;
    // add slider to subscribe form

    var sliderState = 0;
	this.sliderElement = $j(sliderInfo.slider.id)[0];

    // Setting up the strings for the timeout calls to callapseTime()
    var collapseCallIgnoreFocus = 'collapseTime("'+ sliderInfo.slider.id + '", "'+ sliderInfo.textBox.id +'", "'+ sliderInfo.button.id +'", true);';
    var collapseCall = 'collapseTime("'+ sliderInfo.slider.id + '", "'+ sliderInfo.textBox.id +'", "'+ sliderInfo.button.id +'");';

    // IDs for elements which need to track their own mouseover state using an extended attribute
    var mouseoverIDs = sliderInfo.slider.id + ', ' + sliderInfo.button.id;

    // Tracking if mouse is over the slider and the button
    // mouseoverSliders = 'true'
    //      mouse IS OVER the slider or the button
    // mouseoverSliders = 'false'
    //      mouse IS NOT OVER the slider or the button
    $j(mouseoverIDs).mouseover(function() { $j(sliderInfo.slider.id).attr('mouseoverSliders', 'true');});
    $j(mouseoverIDs).mouseout(function() {$j(sliderInfo.slider.id).attr('mouseoverSliders', 'false');});
	
	this.bindTextBoxFocusEvents = function()
	{
		if ($j(sliderInfo.textBox.id).length < 1) 
		{ 
			this.boundTextBoxFocusEvents = false;
			return this.boundTextBoxFocusEvents;
		}

		// Tracking focus state of the text box
		// focus = 'true' 
		//      text box HAS FOCUS
		// focus = 'false'
		//      text box does NOT HAVE FOCUS
		$j(sliderInfo.textBox.id).bind('focusin', function(){$j(sliderInfo.textBox.id).attr('focus', 'true');});
		$j(sliderInfo.textBox.id).bind('focusout', function(){$j(sliderInfo.textBox.id).attr('focus', 'false');});
    
		// Set a longer time out to slide close 
		// if the text box is empty still empty even if there is focus (ingoring focus)
	
    	$j(sliderInfo.textBox.id).bind('focusin', function() { setTimeout(collapseCall, sliderInfo.blankTextTimeout); });
		   // set timeout collapse when the text box blur event is fired
		$j(sliderInfo.textBox.id).bind(
			'blur', 
			function() {setTimeout(collapseCall, slideOutTimeout);} );     
		
		this.boundTextBoxFocusEvents = true;
		return this.boundTextBoxFocusEvents;
	}
	this.bindTextBoxFocusEvents();
	
	var myThis = this;
	
	this.boundTextBoxKeyPressEvents = false;
	
	this.onKeyPressEvent = function(e)
	{
		return true;
	}
	
	this.bindTextBoxKeyPressEvents = function(submitDelegate)
	{
		if($j(sliderInfo.textBox.id).length < 1) return false;
		
		if(typeof(submitDelegate) == 'undefined' || submitDelegate == null)
			$j(sliderInfo.textBox.id).bind('keypress.VerbatimSlider', function(e) { return myThis.OnKeyPressEvent(e); });
		else
			$j(sliderInfo.textBox.id).bind('keypress.VerbatimSlider', function(e) { return submitDelegate(e); });
			
		this.boundTextBoxKeyPressEvents = true;
	}
	
    // Make sure that the text box is not empty before submitting the form
    $j(sliderInfo.form.id).bind('submit', function() { if ($j(sliderInfo.textBox.id).val() == '') return false; });
	

	this.delayedSlideOpen = function()
	{
		// Check if the user is still over the slider
		if($j(sliderInfo.slider.id).attr('mouseoverSliders') != 'true') return;
		
    	$j(sliderInfo.slider.id)[0].slideOpen();
	}

    // Bind sliding animation to mouse over button event
    $j(sliderInfo.button.id).mouseover(
        function()
        {	
			// Delay the sliding open so th user can brush over it without sliding open
			setTimeout(sliderInfo.myID+".delayedSlideOpen();", 300);
        } );

    // set timeout collapse when the mouse leaves the button and the textbox
    $j(mouseoverIDs).bind(
        'mouseout', 
        function() { setTimeout(collapseCall, slideOutTimeout);} );
     
    // handle the submit button click
    $j(sliderInfo.button.id).click(
        function() {
            if (sliderState == 1){ 

				var searchTxt = $j(sliderInfo.textBox.id).val();
				if (searchTxt == '')
				{
					$j(sliderInfo.slider.id)[0].slideClose();
				}
				else
				{
						
					if($j(sliderInfo.textBox.id).length < 1 || $j(sliderInfo.textBox.id)[0].form == undefined || !sliderInfo.button.OnClickSubmit)
						$j(sliderInfo.slider.id)[0].slideClose();
					else 
						$j(sliderInfo.textBox.id)[0].form.submit();
				}
		    }
			else
			{
				$j(sliderInfo.slider.id)[0].slideOpen();
			}
        } );

	this.setSliderState = function(newSliderState)
	{
		sliderState = newSliderState;
	}

	this.doClose = function() 
	{
		if (sliderState == 1)
		{ 
                $j(sliderInfo.slider.id).css(sliderInfo.slider.state.out);
			    $j(sliderInfo.textBox.id).css(sliderInfo.textBox.state.out);
				
				var sliderObject = $j(sliderInfo.slider.id)[0];
				
				var errDiv = $j('.gsom-required-msg', sliderObject);
				for(var i=0; i<errDiv.length; i++)
				{
					errDiv[i].style['display'] = 'none';
				}
			    sliderState = 0;
		}
	}	
	this.sliderElement.slideClose = function() 
	{
		if (sliderState == 1)
		{ 
                $j(sliderInfo.slider.id).animate(sliderInfo.slider.state.out);
			    $j(sliderInfo.textBox.id).animate(sliderInfo.textBox.state.out);
				$j(sliderInfo.textBox.id).blur();
				$j(sliderInfo.textBox.id).attr('focus', 'false');
				
				var sliderObject = $j(sliderInfo.slider.id)[0];
				
				var errDiv = $j('.gsom-required-msg', sliderObject);
				for(var i=0; i<errDiv.length; i++)
				{
					errDiv[i].style['display'] = 'none';
				}
			    sliderState = 0;
		}
	}
	this.sliderElement.slideOpen = function() 
	{
		if (sliderState == 0)
		{ 
                $j(sliderInfo.slider.id).animate(sliderInfo.slider.state.over);
			    $j(sliderInfo.textBox.id).animate(sliderInfo.textBox.state.over);
			    sliderState = 1;
		}
	}
	

	
}

VerbatimSlider.prototype.boundTextBoxFocusEvents = false;
  
/* Timeout Function to collapse a slider 
	Checks to make sure that the mouse isn't still over the slider
	
	It can ignore the focus
 */
function collapseTime( sliderID, textBoxID, triggerID, ignoreFocusIfBlank )
{
    // don't do anything ...
    //      if the sliders are already hidden
    if($j(sliderID).css('opacity') != 1) return;
    //      if the cusor is over the sliders
    if($j(sliderID+"[mouseoverSliders='true']").length > 0) return;
    
	/*
    // Check if we should ignore the Focus
    if(ignoreFocusIfBlank == undefined || !ignoreFocusIfBlank)
    {
    //      has focus
        if($j(textBoxID).attr('focus') == 'true') return;
        
        if((!$j.browser.msie || $j.browser.version > 7) && ($j(textBoxID + ":focus").length > 0)) return;
    }
    //  there is text in the text box
    if($j(textBoxID).length > 0 && $j(textBoxID).val() != '') return;
  	*/
	$j(sliderID)[0].slideClose();
    
}
