﻿var testFacilityID = "#movieContent";
var facilityID = "#movieContent";
var testimonialID = "#testimonialDiv";

var testimonialVidID = 1;
var testimonialParent;
var testFacilityMovieParent;
var facilityMovieParent;

var relativeRoot = "";

$(document).ready(function() {
    $('.rollover').mouseover(function() {
        var src = $(this).attr('src');
        $(this).attr('src', src.replace('_off', '_on'));
    });

    $('.rollover').mouseout(function() {
        var src = $(this).attr('src');
        $(this).attr('src', src.replace('_on', '_off'));
    });
	
	// Technology Left Nav
	$("#navlist").children("li").children("a").hover(
		/* Hover over */
		function() {
			$(this).removeClass("cufonTechNav_out").addClass("cufonTechNav_over");
			Cufon.replace($(this), {fontfamily: 'Trade Gothic LT Std'});
		},
		
		/* Hover Out */
		function() {
			$(this).removeClass("cufonTechNav_over").addClass("cufonTechNav_out");
			Cufon.replace($(this), {fontfamily: 'Trade Gothic LT Std'});
		});
    

    // Popup menu event attachments
    $('.languageSelector').mouseleave(function() {
        closeLanguageSelector();
    });
    $('.truckFilter').mouseleave(function() {
        closeTruckFilter();
    });
    $('.nav_engines').mouseenter(function() {
        openEngineRollover();
    });
    $('.engineRollover').mouseleave(function() {
        closeEngineRollover();
    });
    $('.engineRolloverCutout').mouseenter(function() {
        closeEngineRollover();
    });

    // Subsections
    $('.sub_action').mouseenter(function() {
        subSectionRollover($(this));
    });
    $('.sub_action').mouseleave(function() {
        subSectionRollout($(this));
    });
    $('.sub_action').click(function() {
        subSectionClick($(this));
    });

    // NEWS Subsection
    $('.sub_action_news').mouseenter(function() {
        subSectionNewsRollover($(this));
    });
    $('.sub_action_news').mouseleave(function() {
        subSectionNewsRollout($(this));
    });
    $('.sub_action_news').click(function() {
        subSectionNewsClick($(this));
    });

    // Companies Rollovers
    $('.companies_action').mouseenter(function() {
        companiesRollover($(this));
    });
    $('.companies_action').mouseleave(function() {
        companiesRollout($(this));
    });
    $('.companies_action').click(function() {
        companiesClick($(this));
    });



    // Engine page rollover - change 2 buttons on/off
    $('.mxImgRollover, .px8ImgRollover, .px6ImgRollover').each(
        function() {
            var thisItem = $(this);
            var src = thisItem.attr('src');
            var link = '.' + thisItem.attr('linkto');
            var srcLink = $(link).attr('src');

            thisItem.mouseover(function() {
                thisItem.attr('src', src.replace('_off', '_on'));
                $(link).attr('src', srcLink.replace('_off', '_on'));
            });

            $(link).mouseover(function() {
                thisItem.attr('src', src.replace('_off', '_on'));
                $(link).attr('src', srcLink.replace('_off', '_on'));
            });

            thisItem.mouseout(function() {
                thisItem.attr('src', src.replace('_on', '_off'));
                $(link).attr('src', srcLink.replace('_on', '_off'));
            });

            $(link).mouseout(function() {
                thisItem.attr('src', src.replace('_on', '_off'));
                $(link).attr('src', srcLink.replace('_on', '_off'));
            });
        });

    ////movie popup
    initMoviePopups();
});

function initMoviePopups()
{
    $("img[rel],div[rel]").each(
            function() {
                $(this).overlay({
                    left: 162,
                    expose:
                    {
                        color: '#000',
                        loadSpeed: 200,
                        opacity: 0.7
                    },
                    onClose: function() {
                        $(testimonialID).remove();
						$(testFacilityID).remove();
						$(facilityID).remove();
                    }
                });
                if ($(this).hasClass('testimonial')) {
                    // call flash js function here
                    $(this).bind("click", function() {
                        var classid = $(this).attr('id').toString()
                        testimonialVidID = classid.substring(classid.length - 1, classid.length);

                        setTestimonialSWF();
                    });
                };
				
                if ($(this).hasClass('testfacility')) {
                    // call flash js function here
                    $(this).bind("click", function() {
                        setTestFacilitySWF();
                    });
                };	
                
                if ($(this).hasClass('facility')) {
                    // call flash js function here
                    $(this).bind("click", function() {
                        setFacilitySWF();
                    });
                };				
            });
	
    if (testimonialParent == null) {
        testimonialParent = $(testimonialID).parent().get(0); 
    }
	
    if (testFacilityMovieParent == null) {
        testFacilityMovieParent = $(testFacilityID).parent().get(0); 
    }
    
    if (facilityMovieParent == null) {
        facilityMovieParent = $(facilityID).parent().get(0); 
    }
    
}

function setTestFacilitySWF() {
    var contentDiv = testFacilityID;

    $(contentDiv).remove();

    var newElement = document.createElement('div');
    newElement.setAttribute('id', contentDiv.replace("#", ""));
    newElement.setAttribute('class', 'simple_overlay_video');

    testFacilityMovieParent.appendChild(newElement);

	var flashVars = { "videoPath": relativeRoot + "/Movies/PTC_Tour_Video_SHORT_Version.flv", "title": "State-of-the-art Engine Testing Facilities", "close": "close", "isLatin": "true" };
	var params = { "allowFullScreen": "true" };
	swfobject.embedSWF( relativeRoot + "/Flash/PaccarPlayer.swf", contentDiv.replace("#", ""), "644", "430", "10.0.45", null, flashVars, params);
}

function setTestimonialSWF() {
    var contentDiv = testimonialID;
    if (testimonialParent == null) {
        
        testimonialParent = $(contentDiv).parent().get(0);
        
    }

    $(contentDiv).remove();

    var newElement = document.createElement('div');
    newElement.setAttribute('id', contentDiv.replace("#", ""));
    newElement.setAttribute('class', 'testimonial_overlay_video');

    testimonialParent.appendChild(newElement);

    var flashVars = { "data_api": relativeRoot + "/TestimonialsAPI.aspx", "videoID": "0", "close": "close", "isLatin": "true" };
    var params = { "allowFullScreen": "true" };
    swfobject.embedSWF(relativeRoot + "/Flash/TestimonialPlayer.swf", contentDiv.replace("#", ""), "644", "546", "10.0.45", null, flashVars, params);
}

function setFacilitySWF() {
    var contentDiv = facilityID;

    $(contentDiv).remove();

    var newElement = document.createElement('div');
    newElement.setAttribute('id', contentDiv.replace("#", ""));
    newElement.setAttribute('class', 'simple_overlay_video');

    testFacilityMovieParent.appendChild(newElement);

	var flashVars = { "videoPath": relativeRoot + "/Movies/Facilities.flv", "title": "Engine Development and Testing", "close": "close", "isLatin": "true" };
	var params = { "allowFullScreen": "true" };
	swfobject.embedSWF( relativeRoot + "/Flash/PaccarPlayer.swf", contentDiv.replace("#", ""), "644", "430", "10.0.45", null, flashVars, params);
}

function playTestimonialVideo() {
    return testimonialVidID;
}

function closeTestimonialVideo() {
    $('.close').click();
}

// Language selection
function openLanguageSelector() {
    $('.languageSelector').css('visibility', 'visible');
}
function closeLanguageSelector() {
    $('.languageSelector').css('visibility', 'hidden');
}

// Truck filter selection
function openTruckFilter() {
    var p = $('#truckFilterLink').position();
    $('.truckFilter').css('visibility', 'visible');
    $('.truckFilter').css('left', p.left-21);
}
function closeTruckFilter() {
    $('.truckFilter').css('visibility', 'hidden');
}

// Nav Engine popup selection
function openEngineRollover() {
    $('.engineRolloverContainer').css('display', 'block');
}
function closeEngineRollover() {
    $('.engineRolloverContainer').css('display', 'none');
}

// Subsections
function subSectionRollover(elem) {
    elem.addClass('sub_action_on');
    elem.parent().addClass('sub_module_on');
}
function subSectionRollout(elem) {
    elem.removeClass('sub_action_on');
    elem.parent().removeClass('sub_module_on');
}
function subSectionClick(elem) {
    var href = elem.attr('href');
    if (href != '') {
        window.location = href;
    }
}

// News subsection 
function subSectionNewsRollover(elem) {
    elem.addClass('sub_action_news_on');
    elem.parent().addClass('sub_module_on');
}
function subSectionNewsRollout(elem) {
    elem.removeClass('sub_action_news_on');
    elem.parent().removeClass('sub_module_on');
}
function subSectionNewsClick(elem) {
    var href = elem.attr('href');
    if (href != '') {
        window.open( href,"_blank" );
    }
}

// Companies rollover
function companiesRollover(elem) {
    elem.addClass('companies_action_on');
}
function companiesRollout(elem) {
    elem.removeClass('companies_action_on');
}
function companiesClick(elem) {
    var href = elem.attr('href');
    window.open(href, "_blank");
}
