
/*
 * The Episcopal Diocese of Long Island
 * Events page
 *
 * Javascripts and functionality
 *
 */


var trace = function( message ) {
    try {
        console.log( message );
    }
    catch (e) {}
};


// handle family photo hover/click
var photo, photoTimer, photoViewLink;


/*
 * a method that can be called from anywhere, which replaces
 * the innerHTML content of the 'player' area with a new
 * player and the selected video.
 * NOTE: this may cause flickering - i think there's a way to just
 *       switch the flashvars instead..?
 * @param url = video source
 * @param (hidden) autoStart = automatically play video
 * @param (hidden) thumbnail image name
 */
var playVideo = function( url ) {
    var thumb = '';
    var autoStart = 'false';
    if ( arguments.length >= 2 ) {
        autoStart = ( arguments[1] ? 'true' : 'false' );
    }
    if ( arguments.length == 3 ) {
        thumb = arguments[2];
    }
    trace('autostart is set to '+ autoStart);
    var so = new SWFObject('flash/jwplayer/player-viral.swf', 'player', '640', '380', '9');
    so.addParam('allowfullscreen', 'true');
    so.addParam('allowscriptaccess', 'always');
    so.addParam('wmode', 'opaque');
    so.addVariable('file', url);
    so.addVariable('bufferlength', '8');
    so.addVariable('volume', '50');
    so.addVariable('autostart', autoStart);
    //so.addVariable('mute', 'true');
    so.addVariable('stretching', 'fill');
    //so.addVariable('skin', 'flash/jwplayer/skins/nacht.swf')
    //so.addVariable('image','http://www.dioceselongisland.org/images/dio_ord_tn.jpg');
    so.addVariable('image', thumb);
    so.addVariable('title','Ordination and Consecration of a Bishop');
    so.addVariable('author','The Diocese of Long Island');
    so.addVariable('date','September 19, 2009');
    so.addVariable('description','The Ordination and Consecration of Bishop Larry Provenzano');
    so.write('player');
};



//if ( (typeof document.getElementsByClassName).toString() !== 'function' ) {
//    document.getElementsByClassName = function( className ) {
//        var e = [];
//        var a = document.getElementsByTagName('body')[0].childNodes;
//        for ( var c in a ) {
//            if ( a[c].className == className ) {
//                try {
//                    e.push( a );
//                }
//                catch (e) {}
//            }
//        }
//        return e;
//    };
//}



function getStyle( el, styleProp ) {
    if ( el.currentStyle ) {
        var y = el.currentStyle[styleProp];
    }
    else if ( window.getComputedStyle ) {
        var y = document.defaultView.getComputedStyle(el, null).getPropertyValue( styleProp );
    }
    return y;
}

// just a wrapper for the on/off functions (b = boolean)
function dimTheLights( b ) {
    if ( b ) {
        dimTheLightsOn();
    }
    else {
        dimTheLightsOff();
    }
}

function dimTheLightsOn() {
    var dim = document.getElementById('dim');
    //dim.style.width = getStyle('body', 'width');
    dim.style.height = getStyle(document.getElementsByTagName('body')[0], 'height');
    dim.style.display = 'block';
}

function dimTheLightsOff() {
    var dim = document.getElementById('dim');
    dim.style.display = 'none';
}





window.onload = function() {
    
    // disable all hash links for now
    // TODO: TURN THIS OFF IN PRODUCTION
    //var links = document.getElementsByTagName('a');
    //for ( var i in links ) {
    //    try {
    //        if ( links[i].getAttribute('href') == '#' ) {
    //            links[i].onclick = function() {
    //                return false;
    //            };
    //        }
    //    }
    //    catch (e) {}
    //}
    //links = null;
    
    
    //var k = document.getElementById('k');
    //k.onclick = function() {
    //    var d = document.getElementById('dim');
    //    if ( getStyle(document.getElementById('dim'), 'display') == 'none' ) {
    //        dimTheLights(true);
    //    }
    //    else {
    //        dimTheLights(false);
    //    }
    //    return false;
    //};
    
    
    
    /*
     * play initial video
     *
     */
    playVideo( 'http://web26.streamhoster.com/mediasource/bishop_ws10_cbr_600k.f4v', false, 'http://www.dioceselongisland.org/images/dio_ord_tn.jpg');
    
    
    /*
     * click handling for video links
     *
     */
    var videos = document.getElementById('content');
    videos = videos.getElementsByTagName('a');
    for ( var v in videos ) {
        try {
            if ( videos[v].getAttribute('href') != '#' ) {
                videos[v].onclick = function() {
                    
                    // play the video clicked
                    var url = this.getAttribute('href');
                    // TODO: THIS MIGHT NOT WORK in IE b/c of their stupid handling of "this"
                    //debug
                    if ( (url.substring(0, 7) != 'http://') && (url.substring(0, 1) != '/') ) {
                        // this is a local video, probs on the MT box for testing!
                        url = '../../' + url;
                    }
                    
                    var thumb = this.getElementsByTagName('img')[0];
                    
                    playVideo( url, true, thumb.getAttribute('src').replace('_tn', '') );
                    
                    // #! this stuff is long-winded without jQuery!
                    
                    // remove the active state from the old vid
                    var li = document.getElementById('currentItem');
                    var link = document.getElementById('currentTitle');
                    li.removeAttribute('id');
                    link.removeAttribute('id');
                    link.className = 'title';
                    
                    // add the active state to the clicked vid
                    this.id = 'currentTitle';
                    this.parentNode.id = 'currentItem';
                    
                    link.blur();
                    this.blur();
                    return false;
                };
            }
        }
        catch (e) {
            trace(e);
        }
    }
    
    
    // handle family photo hover/click
    photo = document.getElementById('familyPhoto'); //anchor
    photoTimer = -1, photoPop = null;
    photoViewLink = document.getElementById('familyPhotoView');
    
    // show on hover
    photo.onmouseover = function() {
        var full = document.getElementById('familyPhotoFull'); //image
        full.style.display = 'block';
    };
    
    // hide on hover off
    photo.onmouseout = function() {
        var full = document.getElementById('familyPhotoFull'); //image
        full.style.display = 'none';
    };
    
    // pop cropped sized window on click
    var popPhoto = function(url) {
        var photoPop = window.open(
            url, 'provenzanoFamilyPhoto',
            'width=640,height=435,status=1,toolbar=0,location=0,scrollbars=0'
        );
        photoPop.moveTo( 100, 100 );
        return false;
    };
    
    photo.onclick = photoViewLink.onclick = function() {
        popPhoto(this.href);
        return false;
    };
    
};