/*
| -------------------------------------------------------------------------
| Global Vars
| -------------------------------------------------------------------------
*/

	var baseUrl = (window.location.host == 'www.lawclick.net') ? document.location.protocol+'//www.lawclick.net/':document.location.protocol+'//localhost/lawclick.net/';
	
	
	var timeout    = 500;
	var closetimer = 0;
	var ddmenuitem = 0;
	
	function jsddm_open()
	{  jsddm_canceltimer();
	   jsddm_close();
	   ddmenuitem = $(this).find('ul').css('visibility', 'visible');}
	
	function jsddm_close()
	{  if(ddmenuitem) ddmenuitem.css('visibility', 'hidden');}
	
	function jsddm_timer()
	{  closetimer = window.setTimeout(jsddm_close, timeout);}
	
	function jsddm_canceltimer()
	{  if(closetimer)
	   {  window.clearTimeout(closetimer);
	      closetimer = null;}}
	     
     
/*
| -------------------------------------------------------------------------
| Accordion Ad
| -------------------------------------------------------------------------
*/	
	function playerReady(obj) {
		player = document.getElementById(obj.id);
		player.addModelListener("STATE", "playerState");
		player.addModelListener('TIME','timeMonitor');
	}
	
	function adManager(){
		
		var ad = $('#adImage');
		if(player.state === 'PLAYING' && ( (player.position > 3 && player.position < 10) || (player.position > 60) ) ){
			if(ad.attr('state') === 'off'){
				ad.attr('state','on');
				ad.animate({left:'-350px',top:'0px'},1000,'swing');
			}
		}else if(ad.attr('state') === 'on'){
			ad.attr('state','off');
			ad.animate({left:'0px',top:'0px'},1000,'swing');
		}
	}		
	
	function playerState(obj){
		player.state = obj.newstate;
		adManager();
	}
	
	function timeMonitor(obj){
		player.position = obj.position;
		adManager();
	}
	
	/*
| -------------------------------------------------------------------------
| Sliding Survey 
| -------------------------------------------------------------------------
*/
(function ($) {  // the '$' is actually the jQuery object which is at the end after the last });  
    
$.fn.simpleSurvey = function () {
    
    return this.each(function () {  // this is the standard format for plugins
        var globalpos = "",
			movement = 0,
            running = true;
		
		$(this).bind('stop', function(){
			running=false;
		}).bind('start', function (){
			running=true;
		});
		
		function show_survey() {
			$('#annoying_survey').css('display', 'block');
			setTimeout(slide,4000);  
		}
		function closebox()	{
			$('#annoying_survey').css('display', 'none');
		}
		function slide() {
			if (running){
				dimension = $('#wrapper').width(); /* this is the width of the wrapper excluding the  outside blue margin */
				adjustment = ($(document).width() - dimension) / 2; 
				globalpos = $('#annoying_survey').offset().left;
				/* the tab should not include the adjustment we are having to make for the blue border */
				tab = (globalpos - adjustment) + 280;
				
				if (tab < dimension)	{  
			/*	if (movement < 45) {  */ /* was 42, this value allows image to slide across regardless of min or max window size */
				/*	movement = movement + 1; */
					$('#annoying_survey').css('left', globalpos + 1 + 'px', 
											  'width', '235px',
											  'height', '214px');
					setTimeout(slide,100);
				} else	{	/* if the box is finished sliding, give it 4 seconds and then close box  */
					setTimeout(closebox,4*1000);
				}
			} else {  /* if not running */
				setTimeout(slide,100); 
			}
		}
		setTimeout(show_survey,5000);
	});
};
    
})(jQuery);
		      
(function ($) {  // the '$' is actually the jQuery object which is at the end after the last });  
    
$.fn.simpleSpy = function (limit, interval) {
    limit = limit || 3;  // limit is the number of pictures to show at a time
					     // this syntax is same as if (typeof limit == 'undefined') { limit = 4;}
    interval = interval || 4000;  // interval is the speed at which the pictures transistion??
    
    return this.each(function () {  // this is the standard format for plugins
        // 1. setup
            // capture a cache of all the list items
            // chomp the list down to limit li elements
        var $list = $(this),  // this is your ul.spy list that you sent in when you called simpleSpy
            items = [], // uninitialised....this is the cache
            currentItem = limit,
            total = 0, // initialise later on
            height = $list.find('> li:first').height(),
			running = true;
            
        // capture the cache
        $list.find('> li').each(function () {
			  //items.push($(this).html());  // here we are getting the current li item and pushing its
			                               // html onto the cache, but if we look at the first item below in
										   // console.log(items[0])....we are getting the innerhtml, but not the <li> tags themselves
										   // so add these
            items.push('<li>' + $(this).html() + '</li>');  // now we get the <li>...</li> tags wrapped around the html to store in cache
        });
		// console.log(items[0]);
        
        total = items.length;  // now that we have our cache...we can update the total li's cached, which in this case we have 13
		
		
		
       // this wrap puts a div around the ul.spy element....this allows you to set the height so the box does jerk up and down....
	   // this is a problem in IE not firefox.  I tested it in IE without this line and the box shakes when you insert
        $list.wrap('<div class="spyWrapper" />').parent().css({ height : height * limit });
        
        $list.find('> li').filter(':gt(' + (limit - 1) + ')').remove();
		//  console.log(items[currentItem]);   // this would be item[4] which is the 5th item since we start at zero and this is the item we want
		$list.bind('stop', function(){
			running=false;
		}).bind('start', function (){
			running=true;
		});
		
       // 2. effect 
       function spy() {
		   if (running){
            	// insert a new item with opacity and height of zero
            	var $insert = $(items[currentItem]).css({  // by wrapping this item in a jQuery object '$', we have created a new jQuery instance of the html
              	  height : 0,
              	  opacity : 0,
               	 display : 'none'
            	}).prependTo($list);  // here we insert this new li item (item next in list of li's) into the beginning of the complete list of li's
                        
           	 	// fade the LAST item out
           	 	$list.find('> li:last').animate({ opacity : 0}, 1000, function () {
              	  	// increase the height of the NEW first item and after that is complete fade the first item in
               		 $insert.animate({ height : height }, 1000).animate({ opacity : 1 }, 1000);
                
                	// AND at the same time - decrease the height of the LAST item
              		 // $(this).animate({ height : 0 }, 1000, function () {
                    	// finally fade the first item in (and we can remove the last)   don't need this now that you have the height in the $list.wrap
                   	$(this).remove();
               		// });
           		 });
            
            	currentItem++;  // increment the currentItem so you can get next item in the cash, items[currentItem] to insert in the page
            	if (currentItem >= total) {   // if you get to the end of the cache, start at the beginning again
                	currentItem = 0;
           		}
		   }
            setTimeout(spy, interval) 
        }  
        
        spy();  // call the function spy once and then the setTimeout will continue to call it every 'interval' ms.  
    });
};
    
})(jQuery);	      
	
$(document).ready(function(){ 
		
	$('#jsddm > li').bind('mouseover', jsddm_open)
   	$('#jsddm > li').bind('mouseout',  jsddm_timer)
   	
   	$('#phone_icon').hover(
   		function(){
			$(this).attr('src','assets/images/connect_phone_over.gif');
		},function(){
			$(this).attr('src','assets/images/connect_phone.gif');
	}); 
	
	$('#xped_image').hover(
   		function(){
			$(this).attr('src','assets/images/xpedite_over.jpg');
		},function(){
			$(this).attr('src','assets/images/xpedite.jpg');
	}); 
	
	$('ul.spy').simpleSpy().bind('mouseenter', function () { // call the routine for webvideo page 
		$(this).trigger('stop');
	}).bind('mouseleave', function () {
		$(this).trigger('start');
	}); 
	
	/* $('#download').hover(  This was for downloading the 'Top 10 Marketing Mistakes' from the landing page...now, we are emailing, instead.
		function(){
			$(this).attr('src','assets/images/download_over.gif');
		},function(){
			$(this).attr('src','assets/images/download_up.gif');
	});  */
	
	$("a.iframe").fancybox({  /* web videos on web video page under services */
		'frameWidth':500,
		'frameHeight':320
	});
	
	$("a.iframe-ad").fancybox({  
		'frameWidth':865,
		'frameHeight':320
	});
	
	$("a.inline").fancybox({     /* used on performance page to enlarge graphics as well as social network page */
		'hideOnContentClick': true,
		'frameWidth':580,
		'frameHeight':500
	});
	
	$("a.fancy-image").fancybox({   /* used to enlarge website report card on the the report card page under services */
		'hideOnContentClick': true,
		'frameWidth':580,
		'frameHeight':500
	});
	
	$("a.inline-connect").fancybox({  /* used for the phone icon when you click on it  */
		'hideOnContentClick': false,
		'frameWidth':275,
		'frameHeight':320
	});
	
	$("a.survey-inline-connect").fancybox({
		'hideOnContentClick': false,
		'frameWidth':378,
		'frameHeight':368
	});
	
	$("#read_article").toggle(
		function(){
			$("#pres_column_full").slideDown();
			$(this).html('Hide Article');
		},
		function(){
			$("#pres_column_full").slideUp();
			$(this).html('Read Full Article');
		}
	);
	
	$("#pre_column_link").toggle(
		function(){
			$("#prev_columns_list").slideDown();
			$(this).html('Hide Previous Articles');
		},
		function(){
			$("#prev_columns_list").slideUp();
			$(this).html("Previous President's Columns");
		}
	);
	
	$('#annoying_survey').simpleSurvey().bind('mouseenter', function () {  
		$(this).trigger('stop');
	}).bind('mouseleave', function () {
		$(this).trigger('start');
	}); 
		
	$('#closesurveybox').click(function() {  /* if you click on the 'x' mark on the survey image, close the survey */
  			$('#annoying_survey').css('display', 'none'); 
	});
 	
	$('#select_survey').click(function() {	/* if you click on the link to fill out survey, close the orig. moving survey image 
									 		   and open fancybox (survey-inline-connect above) with the survey questions */
			$('#annoying_survey').css('display', 'none'); 
	});
	
	$('#select_survey').hover(
   		function(){
			$(this).attr('src','assets/images/site-survey-text-over.jpg');
		},function(){
			$(this).attr('src','assets/images/site-survey-text.jpg');
	});
	
});

document.onclick = jsddm_close;

