function is_video(extension){
	var video_types = ['mov', 'mp4', 'm4v', 'flv'];

	var types = "|" + video_types.join("|") + "|";

	return types.indexOf("|" + extension + "|");
}

$(document).ready(function(){

	$("a[rel='external']").bind('click', function(e){
		e.preventDefault();
		var h = 512;
		var w = 800;
		var win_l = (screen.width - w) / 2;
		var win_h = (screen.height - h) / 2;

		obj_win = window.open(this.href,this.href,'width='+w+',height='+h+',location=yes,status=no,resizable=yes,scrollbars=yes,top='+win_h+','+'left='+win_l+',dependent=yes,alwaysRaised=yes');
		obj_win.opener = window;
		obj_win.focus();		
	})

	$(".click-me-for-calendar").bind('click', function(e){
        e.preventDefault();
		$('#calendar').show();        
   	});
   	
   	if( $('div#calendar').length > 0 ){
   		$("body").bind('mousedown', function(e){
			$('#calendar').hide();        
	   	});
	   	
	   	$('#close-calendar').hide();
   	}
   
    /*
     * Modal windows
     */
     
   	$.nyroModalSettings({ closeButton 		: '<a href="#" class="nyroModalClose" id="closeBut" title="close"><img src="' + root + 'img/close.png" alt="close" /></a>',
   						  showContent		: showContent,
   						  css				: { wrapper : {border : 'solid 1px #fff' }},
   						  minHeight			: 50,
   						  processHandler	: function(settings) {
								var url = settings.url;
								var format = url.substr(-3);

								if( is_video( format ) > -1 ) {

									$.nyroModalSettings({
										type: 'swf',
										height: 288,
										width: 512,
										url: url.replace(new RegExp("watch\\?v=", "i"), 'v/')
									});
									
									
									setTimeout($.bind(function(){
                
						                LbVideoPlace('nyroModalContent', {
						                                        height      : 288,
						                                        width       : 512,
						                                        skin		: window.location.toString().indexOf('/interactive/') > -1 ? root + 'swf/interactive.swf' : root + 'swf/blip.swf',
						                                        videoFile   : url
						                                      });
						
						            }, this), 1150);

								}else if( format == 'png' || format == 'jpg' || format == 'gif' ){

						        }else if( format == 'swf' ){
									$.nyroModalSettings({
							            type: 'swf',
										width: 512,
										url: url
									});						
						        } else if ( format == 'pdf' ){
									$.nyroModalSettings({
							            type: 'iframe',
							            height	: 512,
										width: 512,
										url: url
									});						
						        }
						        	

							}
						});
	$('.modal').nyroModal();
/*
    $('.modal').click(function (e) {
        
        var html = "invalid content";

        if( is_video($(this).attr('type') ) > -1 ){
            html = "<div id='modalcontentcontainer'></div>";
            
            setTimeout($.bind(function(){
                
                LbVideoPlace('modalcontentcontainer', {
                                        height      : 288,
                                        width       : 512,
                                        skin		: window.location.toString().indexOf('/interactive/') > -1 ? root + 'swf/interactive.swf' : root + 'swf/blip.swf',
                                        videoFile   : $(this).attr('href')
                                      });

            }, this), 700);

        }else if( $(this).attr('type') == 'png' || $(this).attr('type') == 'jpg' || $(this).attr('type') == 'gif' ){
            html = "<img src=" + $(this).attr('href') + " alt=\"not found\" />";
        }else if( $(this).attr('type') == 'swf' ){

            html = "<div id='modalcontentcontainer'></div>";            

            setTimeout($.bind(function(){
            	var flashvars = {};
			    var params = { wmode: "opaque" };
                swfobject.embedSWF( $(this).attr('href'), 'modalcontentcontainer', '400', '264', "8", flashvars, params);
            }, this), 700);

        }
        
        e.preventDefault();
        $(html).modal({
            onOpen  : simplemodal_open,
            onClose : simplemodal_close,
            position: ["25%","25%"]});
    });*/
});
/**
* When the close event is called, this function will be used to 'close'
* the overlay, container and data portions of the modal dialog.
*
* The SimpleModal close function will still perform some actions that
* don't need to be handled here.
*
* onClose callbacks need to handle 'closing' the overlay, container
* and data.
*/
function simplemodal_open (dialog) {
    dialog.overlay.slideDown(300, function () {
        dialog.container.show(300, function () {
            dialog.data.fadeIn(0);
        });
    });
}

function simplemodal_close (dialog) {
   dialog.data.fadeOut(300, function () {
       dialog.container.hide(300, function () {
           dialog.overlay.slideUp(300, function () {
               $.modal.close();
           });
       });
   });
}
function showContent(elts, settings, callback) {
  elts.contentWrapper
    .css({ // Reset the CSS at the start position
      marginTop: (-100/2 + settings.marginScrollTop)+'px',
      marginLeft: (-100/2 + settings.marginScrollLeft)+'px',
      height: '100px',
      width: '100px',
      opacity: 0
    })
    .show()
    .animate({ // Set the width
      width: settings.width+'px',
      marginLeft: (settings.marginLeft)+'px',
      opacity: 0.5
    }, {duration: 300})
    .animate({ // Set the height
      height: settings.height+'px',
      marginTop: (settings.marginTop)+'px',
      opacity: 1
    }, {complete: callback, duration: 400});
}