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;
    }
    
    
    $('.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
        if ($.browser.msie){
			$('html').css('overflow-y', 'auto'); //insert browser scroll bar back when pop up closed
		}
		else {
			$('body').css('overflow-y', 'auto'); //insert browser scroll bar back when pop up closed - Firefox does not like html overflow-y (positions the page back up to the top)
		}
		
        $('#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
                if ($.browser.msie) {
					$('html').css('overflow-y', 'hidden'); //remove browser scroll bar
				}
				else {
					$('body').css('overflow-y', 'hidden'); //remove browser scroll bar - Firefox does not like html overflow-y (positions the page back up to the top)
				}
                
                $('.gallery_popup').fadeIn('slow');
            }
            return false;
        });
    });
    
    $('.two_col_articles .articles .video').each(function(i, n){
    		
    		//check vehicle name to choose the correct swf player
				var cssName = vehicleSection.split('-');
				var vehicleNameLen = cssName[0].length;
				var vehicleName = vehicleSection.substr(0,(vehicleNameLen-4));
				//prado css name is pradong, need to be Modified
				if(vehicleName == "pradong"){
					vehicleName = "prado";
				}
    		
    		//vehicleSection is set on the template from css name
				var articlesFlashPlayer = new SWFObject("/toyota/vehicles/"+vehicleName+"/videoSmall/swf/smallPlayer"+vehicleName+".swf", "playerwidget", "246", "177", "9.0.0.0"); 
        
        tpSetCommManagerID("communicationwidget", true);

				tpRegisterID("playerwidget");
        
        var containerId = $(n).attr('id');
        var rel = $(n).attr('rel').split(';');
        if (rel.length > 0) {
          //vehicleName is set from the freetext content
					var articlesFlashPlayer = new SWFObject("/toyota/vehicles/"+vehicleName+"/videoSmall/swf/smallPlayer"+vehicleName+".swf", "playerwidget", "246", "177", "9.0.0.0"); 
					articlesFlashPlayer.addParam("wmode", "transparent");
					articlesFlashPlayer.addParam("scale", "noscale");
					articlesFlashPlayer.addParam("salign", "tl");
					articlesFlashPlayer.addParam("menu", "true");
					articlesFlashPlayer.addParam("bgcolor", "#202930");
					articlesFlashPlayer.addParam("allowFullScreen", "true");
					articlesFlashPlayer.addParam("allowScriptAccess", "always");
					articlesFlashPlayer.addVariable("ID", "playerwidget");
					articlesFlashPlayer.addVariable("logLevel", "warn");	
					articlesFlashPlayer.addVariable("feedPID", rel[0]);	
					articlesFlashPlayer.addVariable("directPID", rel[1]);		
					articlesFlashPlayer.addVariable("pageLocation", rel[2]); //Webtrends tracking
					//glossaryFlashPlayer.addVariable("subCategory", webTrendsSubCategory); Webtrends tracking
					articlesFlashPlayer.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
            if ($.browser.msie) {
				$('html').css('overflow-y', 'hidden'); //remove browser scroll bar
			}
			else {
				$('body').css('overflow-y', 'hidden'); //remove browser scroll bar - Firefox does not like html overflow-y (positions the page back up to the top)
			}
            $('.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'
        });
    }
    
});
