/* ******************************
	global javscript for AMHA
********************************/

function getYouTubeThumb(url, size) {
    if (url != null) {    
        size = (size === null) ? "big" : size;
        var vid;
        var results;
        results = url.match("[\\?&]v=([^&#]*)");
        vid = ( results === null ) ? url : results[1];
        
        if(size == "small") {
            return "http://img.youtube.com/vi/"+vid+"/2.jpg";
        } else {
            return "http://img.youtube.com/vi/"+vid+"/0.jpg";
        }
    }
}

$(document).ready(function() { 

	/**********
	pop up external links for valid
	**********/
	$('a[rel="external"]').click( function() {
        window.open( $(this).attr('href') );
        return false;
    });

	/**********
	redirect youtube links and open in lightbox
	**********/
	$('a[@href^="http://www.youtube"]').each(function() {
        // check for withimg class, then lookup youtube thumbnail
        if ( $(this).hasClass('withimg') )
            $(this).text('').append('<img src="' + getYouTubeThumb($(this).attr('href')) + '" />');
        var newHref = '/_common/video/video_player.php?v=' + $(this).attr('href').replace(/^[^v]+v.(.{11}).*/,"$1");
        $(this).attr('href', newHref).addClass('videobox');
    });

	$('a.videobox').fancybox({ 
		'imageScale': false,
		'zoomSpeedIn': 0,
		'zoomSpeedOut': 0,
		'frameHeight': 360,
		'frameWidth': 430,
		'overlayShow': true,
        'navigation': false,
        'hideOnContentClick': false
	});
    
	/**********
	font style swticher - bind to links
	**********/
	$('#style-switcher ul li').each(function(){
	    $(this).children('a').click(function(){
	        var thisSize = $(this).attr('rel');
	        setSize(thisSize);
            return false;
        });
    });

	/*************
	=AMH table striping
	*************/
	//apply table striping to Order an Issue table
	$('table#issue-order tbody tr:even').addClass('odd');
	
	//apply table striping to pop up contact table
	$('table#contact-detail tbody tr:odd').not('.controls').addClass('odd');
	
	$('table#amh-classifeds tbody tr:odd').not('.controls').addClass('odd');
	$('table#amh-classifeds tbody tr').hover(function(){
		$(this).addClass('hover');
	}, function(){
		$(this).removeClass('hover');
	});
	
	/*************
	AMH homepage slideshow using jQuery cycle plugin @ http://www.malsup.com/jquery/cycle/options.html
	hide all images except for first, then load first image, then show all other images, then call cycle animation
	*************/
	//cycle call with preload
/*	$('#main-home img').not('.first').hide();
	$('#main-home img.first').load(function(){
		$('#main-home img').show();
		$('#main-home').cycle({ 
		    fx:     'fade',
			speed: 3000,
			timeout: 3000,
			delay: -1000
		});
	});
*/

	//cycle call without preload
	$('#main-home').cycle({ 
		fx:     'fade',
		speed: 3000,
		timeout: 3000,
		delay: -1000
	});	
	
});
