//CAPTCHA
function setFieldValue(myFieldID, myFieldValue){ document.getElementById(myFieldID).value = myFieldValue; }

var newsItems = [ 
    '<a href="http://www.dts.com/Pro_Audio_Products/DTS_Products/Neural_Upmix_TM/Overview.aspx" target="_blank">Upmix stereo tracks to surround for DTS-HD encoding with Neural&trade; UpMix by DTS - only $449!</a>',
	'<a href="http://www.minnetonkaaudio.com/SurCodeBuy.html" target="_blank">Check out the SurCode Encoder for DTS-HD special deal, valid until September 1st, 2010.</a>'
    ];


var itemIndex = 0;

var footerPageList = [];
/*footerPageList.push({'name':'Overview','link':'/index.html','index':0});
footerPageList.push({'name':'BD Audio Made Easy','link':'/bdAudioMadeEasy.html','index':1});
footerPageList.push({'name':'Just Press Play','link':'/justPressPlay.html','index':2});
footerPageList.push({'name':'The Workflow','link':'/theWorkflow.html','index':3});
footerPageList.push({'name':'Test Drive','link':'/testDrive.html','index':4});
footerPageList.push({'name':'Tutorials','link':'/tutorials.html','index':5});
footerPageList.push({'name':'Just the FAQs','link':'/justTheFaqs.html','index':6});
footerPageList.push({'name':'About DTS-HD','link':'/aboutDTSHD.html','index':7});
footerPageList.push({'name':'Tech Specs','link':'/techSpecs.html','index':8});
footerPageList.push({'name':'Support','link':'/support.html','index':9});*/

footerPageList.push({'name':'Overview','link':'navOverview','index':0});
footerPageList.push({'name':'BD Audio Made Easy','link':'navAudioMadeEasy','index':1});
footerPageList.push({'name':'Just Press Play','link':'navPressPlay','index':2});
footerPageList.push({'name':'The Workflow','link':'navWorkflow','index':3});
footerPageList.push({'name':'Test Drive','link':'navTestDrive','index':4});
footerPageList.push({'name':'Tutorials','link':'navTutorials','index':5});
footerPageList.push({'name':'Just the FAQs','link':'navFAQs','index':6});
footerPageList.push({'name':'About DTS-HD','link':'navAbout','index':7});
footerPageList.push({'name':'Tech Specs','link':'navSpecs','index':8});
footerPageList.push({'name':'Support','link':'navSupport','index':9});

function setTickerItem(ind){
    news_text = '<span class="newsLabel">News: </span>';
    $('#newsTickerContent').html(news_text + newsItems[ind]);
}

function __findPageByAnchorId(id){
    for (var i=0; i < footerPageList.length; i++) {
        if(footerPageList[i].link == id){
            return footerPageList[i];
        }
    }
    return null;
}

function getCurrentPageAndBuildLowerNav(){
    var link_obj = null;
    var show_left = true;
    var show_right = true;
    $(".navCell").each(function(){
        link = $(this).find('a').children()[0];
        if($(link).hasClass('navActive')){
            l_id = $(link).parents('.navCell').attr('id');
            link_obj = __findPageByAnchorId(l_id);
        }
    });
    if(link_obj !== null){
        if(link_obj.index === 0){
            $("#footerNavLeft").hide();
            show_left = false;
        }else if(link_obj.index == footerPageList.length-1){
            $("#footerNavRight").hide();
            show_right = false;
        }
        if(show_left){
            p_obj = footerPageList[link_obj.index - 1];
            t_link = $("#"+p_obj.link).children('a')[0];
            
            $("#footerNavLeft").attr('href',$(t_link).attr('href'));
            $("#footerPrevLinkText").html(p_obj.name);
        }
        if(show_right){
            p_obj = footerPageList[link_obj.index + 1];
            t_link = $("#"+p_obj.link).children('a')[0];
            
            $("#footerNavRight").attr('href',$(t_link).attr('href'));
            $("#footerNextLinkText").html(p_obj.name);
        }
    }
}


/* nav effects */
$(function(){
    $("#"+active_nav+ " img").addClass('navActive');

    $(".navActive").each(function(){
        newp = $(this).attr('src').replace('up','selected');
        $(this).attr('src',newp);
    });
    $(".navImg").not(".navActive").hover(
        function(){
            newp = $(this).attr('src').replace('up','over');
            $(this).attr('src',newp);
        },
        function(){
            newp = $(this).attr('src').replace('over','up');
            $(this).attr('src',newp);
        }
    );
    getCurrentPageAndBuildLowerNav();
});

/* news ticker */

$(function(){
    $('#newsLeft').hover(
        function(){
            newp = $('#tickerLeft').attr('src').replace('up','over');
            $('#tickerLeft').attr('src',newp);
        },
        function(){
            newp = $('#tickerLeft').attr('src').replace('over','up');
            $('#tickerLeft').attr('src',newp);
        }
    );
    $('#newsRight').hover(
        function(){
            newp = $('#tickerRight').attr('src').replace('up','over');
            $('#tickerRight').attr('src',newp);
        },
        function(){
            newp = $('#tickerRight').attr('src').replace('over','up');
            $('#tickerRight').attr('src',newp);
        }
    );

    $("#newsRight").click(function(event){
        event.preventDefault();
        itemIndex = (itemIndex + 1) % newsItems.length;
        setTickerItem(itemIndex);
    });
    $("#newsLeft").click(function(event){
        event.preventDefault();
        itemIndex = (itemIndex - 1 + newsItems.length) % newsItems.length;
        setTickerItem(itemIndex);
    });
    setTickerItem(0); // init news ticker
});

/* workflow images and overlays */
$(function(){
    $(".workflowThumb a").hover(
        function(){
            id_r = $(this).attr('id').replace('thumb_','');
            id_b = "#btn_"+id_r;
            im_p = $(id_b+ " img").attr('src');
            im_n = im_p.replace('up','over');
            $(id_b+" img").attr('src',im_n);
        },
        function(){
            id_r = $(this).attr('id').replace('thumb_','');
            id_b = "#btn_"+id_r;
            im_p = $(id_b+ " img").attr('src');
            im_n = im_p.replace('over','up');
            $(id_b+" img").attr('src',im_n);
        }
    );
    $(".workflowButton a").hover(
        function(){
            i = $(this).children('img')[0];
            src = $(i).attr('src');
            $(i).attr('src',src.replace('up','over'));
        },
        function(){
            i = $(this).children('img')[0];
            src = $(i).attr('src');
            $(i).attr('src',src.replace('over','up'));
        }
    );

    $(".workflowOverlayClose a").hover(
        function(){
            i = $(this).children('img')[0];
            ip = $(i).attr('src');
            $(i).attr('src',ip.replace('up','over'));
        },
        function(){
            i = $(this).children('img')[0];
            ip = $(i).attr('src');
            $(i).attr('src',ip.replace('over','up'));
        }
    );

    var wfJqmOpts = {};

    wfJqmOpts.onShow = function(hash){ 
        hash.w.fadeIn('fast');
    };
    wfJqmOpts.onHide = function(hash){
        hash.w.fadeOut('fast');
        hash.o.remove();
    };
    $(".workflowImages a").click(function(event){
        event.preventDefault();
        imgid = $(this).attr('id').match(/^.+_([a-z]+)$/i)[1];
        divid = "#workflowOverlay_"+imgid;
        $(divid).jqm(wfJqmOpts);
        $(divid).jqmAddClose(".workflowOverlayClose");
        $(divid).center();
        $(divid).jqmShow();
        return false;
    });
});

/* test drive overlay */

/* Test Drive overlay animation functions */
var tdJqmOpts = {};
tdJqmOpts.onShow = function(hash){ hash.w.fadeIn('1',
            function(){
                $("#testDriveOverlayContainer").show();
            });
        }
/* end Test Drive overlay animation functions */

$(function(){
    $("#testDriveOverlay").jqm(tdJqmOpts);
    $("#testDriveOverlay").jqmAddClose("#tdOverlayClose");
    $("#tdOverlayClose").hover(
        function(){
            i = $(this).children('img')[0];
            ip = $(i).attr('src');
            $(i).attr('src',ip.replace('up','over'));
        },
        function(){
            i = $(this).children('img')[0];
            ip = $(i).attr('src');
            $(i).attr('src',ip.replace('over','up'));
        }
    );
    $(".testDriveImage a").hover(
        function(){
            i = $("#btn_test_drive img")[0];
            p = $(i).attr('src');
            $(i).attr('src',p.replace('up','over'));
        },
        function(){
            i = $("#btn_test_drive img")[0];
            p = $(i).attr('src');
            $(i).attr('src',p.replace('over','up'));
        }
    );

    $(".testDriveImage a").click(
        function(event){
            event.preventDefault();
            $("#testDriveOverlay").center();
            //$("#testDriveOverlayContainer").center();
            $("#testDriveOverlay").jqmShow(); 
        }
    );

});

/* tutorials */

$(function(){
    $(".videoOverlayClose a").hover(
        function(){
            i = $(this).children('img')[0];
            ip = $(i).attr('src');
            $(i).attr('src',ip.replace('up','over'));
        },
        function(){
            i = $(this).children('img')[0];
            ip = $(i).attr('src');
            $(i).attr('src',ip.replace('over','up'));
        }
    );
    $(".videoOverlay").jqm();
    $(".tutorialItemThumb a").click(
        function(event){
            event.preventDefault();
            /*$("#videoOverlayLogic").center(); 
            $("#videoOverlayLogic").jqmShow(); */
            id = $(this).attr('id').replace(/[^\d]*/,'');
            switch(id){
                case '1': // final cut
                    v = "#videoOverlayFinalCut";
                    break;
                case '2': // logic
                    v = "#videoOverlayLogic";
                    break;
                case '3': // qt
                    v = "#videoOverlayQt"; 
            }
            //alert('Show overlay video for ID '+id);
            $(v).jqmAddClose(".videoOverlayClose");
            $(v).center();
            $(v).jqmShow();
        }
    );
});

/* FAQs */

$(function(){
    $(".faqQuestion a").click(
        function(event){
            event.preventDefault();
            a_c = $(this).attr('id').replace('_q','_a');
            i = $(this).attr('id').replace('_q','_i');
            ip = $("#"+i).attr('src');
            if($("#"+a_c).is(':visible')){
                $("#"+a_c).slideUp('fast');
                $("#"+i).attr('src',ip.replace('minus','plus'));
            }else{
                $("#"+a_c).slideDown('fast');
                $("#"+i).attr('src',ip.replace('plus','minus'));
            }
        }
    );
    $(".faqQuestion img").click(
        function(event){
            event.preventDefault();
            a_l = $(this).attr('id').replace('i','q');
            $("#"+a_l).trigger('click');
        }
    );
});

/* Tech Specs */

$(function(){
    $('.specsImageContainer a').hover(
        function(){
            id = $(this).attr('id');
            root = id.replace(/[^\d]+/,'');
            iid = "#thumb_"+root;
            osrc = $(iid).attr('src');
            $(iid).attr('src',osrc.replace('up','over'));
        },
        function(){
            id = $(this).attr('id');
            root = id.replace(/[^\d]+/,'');
            iid = "#thumb_"+root;
            osrc = $(iid).attr('src');
            $(iid).attr('src',osrc.replace('over','up'));
        }
    );
    $('.specsImageContainer a').click(
        function(){
            id = $(this).attr('id');
            root = id.replace(/[^\d]+/,'');
            if(root == '1'){
                divid = "#workflowOverlay_ts1";
            }else if(root == '2'){
                divid = "#workflowOverlay_ts2";
            }

            var tsJqmOpts = {};

            tsJqmOpts.onShow = function(hash){ 
                hash.w.fadeIn('fast');
            };
            tsJqmOpts.onHide = function(hash){
                hash.w.fadeOut('fast');
                hash.o.remove();
            };

            $(divid).jqm(tsJqmOpts);
            $(divid).jqmAddClose(".workflowOverlayClose");
            $(divid).center();
            $(divid).jqmShow();
            return false;

            return false;
        }
    );
});

