﻿/*
Law School Homepage JS
by USF Law Webmaster jpschwinghamer@usfca.edu
*/

/* Initialize Google SWF Object for YouTube Embed */
google.load("swfobject", "2.2"); /* Get the SWF Object */
function onYouTubePlayerReady(playerId) {
	ytplayer = document.getElementById("usflawytplayer");
}

$(document).ready(function() {

	if( String(navigator.userAgent).search(/(iPad)|(iPhone)/i) > 0 )

	// add an onclick event handler function for all items that need it so the iPad/iPhone will pass it touch events as mouse events!
			
	$('#resources_dropdown').each(function(){
				
		this.onclick = function(){ return false; };

	});

	/* Open external hyperlinks in new window */
        $("a[href^='http']").attr('target','_blank');

	/* Delay Timer for loading content from 3rd Party Web Services */
	
	jQuery.timer = function(time,func,callback){
	var a = {timer:setTimeout(func,time),callback:null}
	if(typeof(callback) == 'function'){a.callback = callback;}
	return a;
	}; 
	/* End delay */
	
	
	/* AddThis */
	$('.custom_button, .hover_menu').mouseenter(function(){
     	$('.hover_menu').fadeIn('fast');
    	$('.custom_button').addClass('active');
      	$(this).data('in', true);
       	$('.hover_menu').data('hidden', false);
    }).mouseleave(function(){
        $(this).data('in', false);
        setTimeout(hideMenu, delay);
    });

    var delay = 400;
    function hideMenu(){
        if (!$('.custom_button').data('in') && !$('.hover_menu').data('in') && !$('.hover_menu').data('hidden'))
        {
        	$('.hover_menu').fadeOut('fast');
         	$('.custom_button').removeClass('active');
            $('.hover_menu').data('hidden', true);
        }
    } 
    /* End AddThis /*
    
    
    /* Small Slideshow image selector */
    if($('.slideshowsmall').length){
    	for(i=0; i < 5; i++){ 	/* Create a loop to select 5 images */
    		var rannumb = Math.floor(Math.random() * 57) + 1; /* Create a random rumber for each time through the loop */
			var imageurl = "/law/images/150x150/" + rannumb + ".jpg"; /* Grab the photo that corresponds to that number */
    		$('.slideshowsmall').append('<div class="image"><img src="' + imageurl + '"/></div>'); /* Append to small slideshow */
    	};
    }
    /* End Small Slideshow image selector */
    
    /* Wide Slideshow image selector */
    
    if($('.slideshowwide').length){
    	/* for(i=0; i < 1; i++){  */	/* Create a loop to select 5 images */
    		var rannumb = Math.floor(Math.random() * 54) + 1; /* Create a random rumber for each time through the loop */
			var imageurl = "/law/images/banners/560x150/" + rannumb + ".jpg"; /* Grab the photo that corresponds to that number */
    		$('.slideshowwide').append('<div class="image"><img src="' + imageurl + '"/></div>'); /* Append to wide slideshow */
    	/* }; */ 
    }
	/* End Wide Slideshow image selector */
	
	/* Interior Featured Movie and Teaser */
	var titleurl = $('#titleurl').text();
	$('#landing_teaser .landing_heading .landing_title').css({'background-image' : 'url(' + titleurl + ')'});
	
	/* Embed the teaser still */
	var stillid = $('#stillshotid').text();
	$('#landing_teaser').css({'background-image' : 'url(' + stillid + ')'});
   	
   	/* Embed the Youtube movie */
   	var params = { allowScriptAccess: "always", bgcolor: "#cccccc", wmode: "transparent" };
	var atts = { id: "usflawytplayer" }; 
	swfobject.embedSWF("http://www.youtube.com/apiplayer?enablejsapi=1&playerapiid=ytplayer&hd=1", 
	 	"featured", "560", "320", "8", null, null, params, atts); 
	
	$('#landing_teaser .landing_heading').click(function(){	/* When the caption is clicked */
		$('#landing_teaser').fadeOut(function(){	/* Fadeout the teaser, then... */
 		  	var video = $('#youtubeid').text();	/* Grab the youtubeid and set as the video variable */
 		   	ytplayer.loadVideoById(video);	/* Load the youtube video */
 		   	$('#landing_movie .close').fadeIn();
 		});
 	});
 	
 	/* Close the movie */
 	$('#landing_movie .close').click(function(){
 		ytplayer.pauseVideo();
 		$('#landing_teaser').fadeIn();
 		$(this).fadeOut();
 	}); 	
 	/* End Interior Featured Movie and Teaser */
 	
 	/* Graphs  */
 	if($('.graph').length){
		$('.graph').each(function(){
			$(this).find('.graph_item:last').css({'margin-bottom': '0px'});
		})
 		setTimeout(function(){
			$('.graph_item_value').each(function(){
				var width = $(this).find('.text').text();
				$(this).animate({'width' : width});
			});
		}, 1500);	
	}

	/* Expanding Text Feature */
	$('.text_expand .heading h4, .heading p, .heading h3, .heading h5').append(' &raquo;');
	$('.text_expand .heading').click(function(){
	    if($(this).siblings().is(':hidden')){
	    $(this).siblings().slideDown();
	    }
	    else{
	    $(this).siblings().slideUp();
	    }
	});	
	
	/* Hide Resources if non are present */
	if($('.resources_list li').length == 0){
		$('.resources.header').hide();	
	}
	
	/* IE 6 & 7 Error Bar */
	if($.browser.msie && $.browser.version < 7	){
		$('#errorbar').slideDown();
	}
	
	/* Image feature caption autosize */
 	
 	if($('.image_feature').length){
 	
 		$(window).load(function(){
			$('.image_feature').each(function(){
 			var imgwidth = $('.image_feature').find('img').attr('width');
 			$(this).find('.caption').css({width: imgwidth - 10})
 			}); 		
 		});
 	}
 	
	/* Section Select */
	
	if($('#section_select').length){	/* If there are any guide_select elements on the page */
	    $('#section_select').append('<option>Choose a Section</option>');	/* Add the option to the select element */
	    $('h3').each(function(){	/* Iterate through each H3 tag */
	    	var heading = $(this).html();	/* Set the H3 contents as the heading variable */
	    	var id_value = $(this).html().toLowerCase().split(" ").join("_");	/* Set a variable to contain a lowercase clean version */
	    	$(this).attr('id',id_value);		/* Set the currently selected H3 element's id to the clean variable */
	    	var hash = ($(this).attr('id')).toLowerCase().split(" ").join("_");	/* Set the new H3 id as the variable hash */
	    	$('#section_select').append('<option value="#' + hash + '">' + heading + '</option>');	/* Add the option to the select element */		
	    });
	
	    $('#section_select').change(function(){	/* When the value of the select changes */
	    	$('#section_go').attr('href', $(this).val()); /* Set the href value of the go button to the value of the select element */
	    })
	}
 	
 	
 	/* Connect Login */
 	
 	$('#connect_username').val('Username');
 	$('#connect_password').val('Password');
 	$('#connect_username').focus(function(){
		if($(this).val() == 'Username'){
			$(this).val('');
		}
	});
	
 	$('#connect_password').focus(function(){
		if($(this).val() == 'Password'){
			$(this).val('');
		}
	});

	/* On Blur Functions */
	$('#connect_username').blur(function(){
		if($(this).val() == ''){
			$(this).val('Username');
		}
	});
	
	$('#connect_password').blur(function(){
		if($(this).val() == ''){
			$(this).val('Password');
		}
	});
 	
	$('#connect_go').click(function(){
		$('#connect_login').submit();
	})
	
	$('#connect_login').keypress(function(e){
		if(e.which == 13){
	    	$('#connect_login').submit();
       	}
	});
	
	$('#search_go').click(function(){
	    	var query = $('#search_query').val();
	    	window.open('/law/search/?cx=008538038562022311527%3Aarsqx4un2dy&cof=FORID%3A11&ie=UTF-8&q=' + query + '&siteurl=rock.usfca.edu%2Flaw%2Fsearch%2F#1010');
	    	return false;
	})
	
	$('#search_query').keypress(function(e){
		if(e.which == 13){
	    	var query = $('#search_query').val();
	    	window.open('/law/search/?cx=008538038562022311527%3Aarsqx4un2dy&cof=FORID%3A11&ie=UTF-8&q=' + query + '&siteurl=rock.usfca.edu%2Flaw%2Fsearch%2F#1010');
	    	return false;
       	}
	});

	    

 	
});
