var articlesFlashPlayer = null;


$(document).ready(function(){

    if (navigator.appName == 'Microsoft Internet Explorer') {
        position = (document.documentElement && document.documentElement.scrollTop) ? document.documentElement.scrollTop : document.body.scrollTop;
    }
    else {
        position = window.pageYOffset;
    }
    
    articlesFlashPlayer = new SWFObject('/toyota/vehicles/' + vehicleSection + '/video/videoPlayer_light.swf', 'videoPlayer', '650', '335', '9.0.0', '');
    articlesFlashPlayer.addParam('allowfullscreen', 'true');
    articlesFlashPlayer.addParam('menu', 'false');
    articlesFlashPlayer.addParam('loop', 'false');
    articlesFlashPlayer.addParam('scale', 'noscale');
    articlesFlashPlayer.addParam('quality', 'best');
    articlesFlashPlayer.addParam('wmode', 'opaque');
    articlesFlashPlayer.addParam('allowScriptAccess', 'always');
    articlesFlashPlayer.addVariable('autoPlay', 'true');
    articlesFlashPlayer.addVariable('carModel', vehicleSection);
    articlesFlashPlayer.addVariable('location', vehicleSection + ' ' + pageType);
    /*articlesFlashPlayer.addVariable('thumbnail','/toyota/vehicles/images/videoThumbnail.jpg');*/
    
    
    $('.gallery_popup .btn-close, .gallery_popup .btn-close a').click(function(){
        $('.black_overlay').fadeOut('slow');
        $('.gallery_popup').fadeOut('slow');
        $('body').css('width', '100%'); //revert the width change when browser vertical scroll bar is back on
        $('html').css('overflow-y', 'auto'); //insert browser scroll bar back when pop up closed
        $('#videoPlayerDiv').html('');
        
        return false;
    });
    
    $('.three_col_articles .three_col_b').each(function(i, n){
        $(n).find('a').click(function(){
            if (navigator.appName == "Microsoft Internet Explorer") {
                position = (document.documentElement && document.documentElement.scrollTop) ? document.documentElement.scrollTop : document.body.scrollTop;
            }
            else {
                position = window.pageYOffset;
            }
            var topPos = (document.documentElement.clientHeight - $('.gallery_popup').height()) / 2 + position;
            var leftPos = ($('.container_main').width() - $('.gallery_popup').width()) / 2;
            $('.gallery_popup').css({
                left: leftPos + 'px',
                top: topPos + 'px'
            });
            
            var title = $(n).find('.video-thumb-title').text();
            var rel = $(this).attr('rel').split(';');
            var thumbnail = $(n).find('img').attr('src');
            
            if (rel.length > 0) {
                $('.gallery_popup .gp_header_title').text(title);
                articlesFlashPlayer.addVariable('video', rel[0]);
                articlesFlashPlayer.addVariable('thumbnail', thumbnail);
                articlesFlashPlayer.setAttribute('width', rel[1])
                articlesFlashPlayer.setAttribute('height', rel[2])
                articlesFlashPlayer.write('videoPlayerDiv');
                $('.black_overlay').fadeIn('slow');
                $('body').css('width', pageWidth + 'px'); //to accomodate the width change when browser vertical scroll bar dissapears
                $('html').css('overflow-y', 'hidden'); //remove browser scroll bar
                $('.gallery_popup').fadeIn('slow');
            }
            return false;
        });
    });
    
    $('.two_col_articles .articles .video').each(function(i, n){
        if (navigator.appName == "Microsoft Internet Explorer") {
            position = (document.documentElement && document.documentElement.scrollTop) ? document.documentElement.scrollTop : document.body.scrollTop;
        }
        else {
            position = window.pageYOffset;
        }
        var topPos = (document.documentElement.clientHeight - $('.gallery_popup').height()) / 2 + position;
        var leftPos = ($('.container_main').width() - $('.gallery_popup').width()) / 2;
        $('.gallery_popup').css({
            left: leftPos + 'px',
            top: topPos + 'px'
        });
        
        var containerId = $(n).attr('id');
        var rel = $(n).attr('rel').split(';');
        var thumbnail = $(n).find('img').attr('src');
        if (rel.length > 0) {
            var videoPlayer = new SWFObject('/toyota/vehicles/' + vehicleSection + '/video/videoPlayer_light.swf', 'videoPlayer', rel[1], rel[2], '9.0.0', '');
            videoPlayer.addParam('allowfullscreen', 'true');
            videoPlayer.addParam('menu', 'false');
            videoPlayer.addParam('loop', 'false');
            videoPlayer.addParam('scale', 'noscale');
            videoPlayer.addParam('quality', 'best');
            videoPlayer.addParam('wmode', 'transparent');
            videoPlayer.addParam('allowScriptAccess', 'always');
            videoPlayer.addVariable('autoPlay', 'false');
            videoPlayer.addVariable('thumbnail', thumbnail);
            videoPlayer.addVariable('sizeToContainer', 'true');
            videoPlayer.addVariable('video', rel[0]);
    		videoPlayer.addVariable('carModel', vehicleSection);
    		videoPlayer.addVariable('location', vehicleSection + ' ' + pageType);
            videoPlayer.write(containerId);
        }
    });
    
    $('.two_col_articles .articles .image').each(function(i, n){
        $(n).find('a').click(function(){
        
            $('.gallery_popup').addClass('image-overlay');
            
            if (navigator.appName == "Microsoft Internet Explorer") {
                position = (document.documentElement && document.documentElement.scrollTop) ? document.documentElement.scrollTop : document.body.scrollTop;
            }
            else {
                position = window.pageYOffset;
            }
            
            var topPos = (document.documentElement.clientHeight - $('.gallery_popup').height()) / 2 + position;
            var leftPos = ($('.container_main').width() - $('.gallery_popup').width()) / 2;
            
            $('.gallery_popup').css({
                left: leftPos + 'px',
                top: topPos + 'px'
            });
            
            var title = $(n).find('.image-title').text();
            var img = document.createElement('img');
            img.src = $(this).attr('href');
            
            $('.gallery_popup .gp_header_title').text(title);
            $('#videoPlayerDiv').html(img);
            
            $('.black_overlay').fadeIn('slow');
            $('body').css('width', pageWidth + 'px'); //to accomodate the width change when browser vertical scroll bar dissapears
            $('html').css('overflow-y', 'hidden'); //remove browser scroll bar
            $('.gallery_popup').fadeIn('slow');
            
            
            return false;
        });
    });
    
    var docHeight;
    var pageHeight = document.documentElement.clientHeight;
    var pageWidth = document.documentElement.clientWidth; //viewable document width
    /* measuring viewport height & page height after flash elements loaded */
    if (document.compatMode && document.compatMode != 'BackCompat') {
        docHeight = document.documentElement.scrollHeight;
    }
    else 
        if (document.body && typeof document.body.scrollHeight != 'undefined') {
            docHeight = document.body.scrollHeight;
        }
    
    if (docHeight < pageHeight) {
        docHeight = pageHeight;
    }
    
    /* then set the measurements for the black overlay */
    var viewPortHeight = self.innerHeight || (document.documentElement && document.documentElement.clientHeight) || document.body.clientHeight;
    if (viewPortHeight > pageHeight) {
        $('.black_overlay').css({
            height: 100 + '%',
            opacity: .80,
            width: '960px'
        });
    }
    else {
        $('.black_overlay').css({
            height: docHeight + 'px',
            opacity: .80,
            width: '960px'
        });
    }
    
});
