$(document).ready(function() {

    //detect Mac/Win OS (default is Windows)
    var OSName = "Windows";

    if (navigator.appVersion.indexOf("Win")!=-1) OSName="Windows";
    if (navigator.appVersion.indexOf("Mac")!=-1) OSName="MacOS";

    //update dp8 button to windows if necessary
    if (OSName == "MacOS"){
        $('.win-display').css("display", "none");
        $('.mac-display').css("display", "inline");
        $('a.mac-display').css("display", "block");
        $('p.mac-display').css("display", "block");

	//update arrow link (mac is selected by default as installer target)
	$("[id$='installer-dl-btn']").each(function(){
	    $($(this).children("a")).attr('href', $($(this).parent().siblings(".mac-win-installer-dl-btns").children("[id$='installer-mac-target']")).children("a").attr("href"));
	});
    }

    if (OSName == "Windows"){
        $('.win-display').css("display", "inline");
        $('a.win-display').css("display", "block");
        $('p.win-display').css("display", "block");
        $('.mac-display').css("display", "none");
	
	//select windows installer + update arrow link
	$("[id$='installer-win-target']").removeClass("installer-os-icon-default");
	$("[id$='installer-win-target']").addClass("installer-os-icon-selected");

	$("[id$='installer-mac-target']").removeClass("installer-os-icon-selected");
	$("[id$='installer-mac-target']").addClass("installer-os-icon-default");

	$("[id$='installer-dl-btn']").each(function(){
	    $($(this).children("a")).attr('href', $($(this).parent().siblings(".mac-win-installer-dl-btns").children("[id$='installer-win-target']")).children("a").attr("href"));
	});
    }


    //hover behavior for mac/win targets within installer icons
    $("[id$='-target']").hover(
        function() {
            id_prefix = '#' + $(this).attr("id").split(/-mac-target$|-win-target$/)[0];

        
	    //highlight installer download buttond, update dl arrow link 
	    $($(id_prefix+'-dl-btn').children().children("a")).toggleClass("hovered");
	    $($(id_prefix+'-dl-btn').children().children("a")).attr('href', $($(this).children().children("a")).attr('href'));

	    if ($(this).attr("id").indexOf("mac") >=0){ //if we're hovered over mac
		//display OS specific Installer notes 
		$($(id_prefix+'-notes').children(".mac-display")).css("display", "block");
		$($(id_prefix+'-notes').children(".win-display")).css("display", "none");
		//display corresponding version info
		$($(id_prefix+'-icon-group').siblings(".installer-version-info")).children(".win-display").css("display", "none");
		$($(id_prefix+'-icon-group').siblings(".installer-version-info")).children(".mac-display").css("display", "block");
	    }
	    else{ //hovered over windows
		//display OS specific Installer notes 
		$($(id_prefix+'-notes').children(".mac-display")).css("display", "none");
		$($(id_prefix+'-notes').children(".win-display")).css("display", "block");
		//display corresponding version info
		$($(id_prefix+'-icon-group').siblings(".installer-version-info")).children(".mac-display").css("display", "none");
		$($(id_prefix+'-icon-group').siblings(".installer-version-info")).children(".win-display").css("display", "block");
	    }

	    
            if ($(this).hasClass("installer-os-icon-default")){
		//switch to (highlight) selected os
		$(this).removeClass("installer-os-icon-default");
		$(this).addClass("installer-os-icon-selected");

		//update arrow link
		


		//de-select highlighted-os.
                if ($(this).attr("id").match(/-mac-target$/)){
      		    $(id_prefix+'-win-target').removeClass("installer-os-icon-selected");
		    $(id_prefix+'-win-target').addClass("installer-os-icon-default");
		}
		else if ($(this).attr("id").match(/-win-target$/)){
		    $(id_prefix+'-mac-target').removeClass("installer-os-icon-selected");
		    $(id_prefix+'-mac-target').addClass("installer-os-icon-default");
		}
	    }
	},
	function(){
            id_prefix = '#' + $(this).attr("id").split(/-mac-target$|-win-target$/)[0];
            $($(id_prefix+'-dl-btn').children().children("a")).toggleClass("hovered");

	}
    );	



    $(".product-download-link-title").hover(
        function(){
            if ($($($(this).parent()).children('.open_product')).is(":hidden")){
                $(this).addClass("active-blue-arrow");
            }
        },
        function(){$(this).removeClass("active-blue-arrow")}
    );  

    //collapses open product link.
    //takes instance of .product-link-title as an argument
    function collapseSelected(product){
        $(product).removeClass("selected");
        /*$($(product).children('#product-list-blue-collapse-arrow')).rotate({
            angle:90, 
            animateTo:0,
            duration: 1,
            callback:function(){
                $(this).fadeOut('fast', function(){$(this).remove();});
            }
        });*/
        
        $($($(product).parent()).children('.open_product')).slideUp("medium");
    }

/*
    //toggle visibility of product links for boxed section via click and update plus/minus sign in section heading.
    jQuery(".additional-resources-header a").click(function(event){
	event.preventDefault();
	if ($($($(this).parent()).parent('.product-download-link-group')).children('.product-download-all-links').is(":hidden")){
	    //reveal list of product-download-links
	    $($($(this).parent()).parent('.product-download-link-group')).children('.product-download-all-links').slideDown("slow");
	    //update plus to minus
	    $(this).children('.more-options-plus').css("display", "none");
	    $(this).children('.more-options-minus').css("display", "inline");

	}
	else{
	    //close list of product-download links
	    //close open_product_link if it exists in this box (there's at most 1 open product link at any time).
	    if (open_product_link != null){
		var closing_group_id = $($(this).parent()).parent('.product-download-link-group').attr("id");
		$('#' + open_product_link.id).parents(".product-download-link-group").each(function(){
		    if ($(this).attr("id") == closing_group_id){
			collapseSelected(open_product_link);
		    }
		});
	    }
	//return product download links list to its original state 
	$($($(this).parent()).parent('.product-download-link-group')).children('.product-download-all-links').slideUp("slow");

	//update minus to plus
	$(this).children('.more-options-minus').css("display", "none");
	$(this).children('.more-options-plus').css("display", "inline");
	}
	
    });

*/
    //toggle visibility of product links for boxed section via click and update plus/minus sign in section heading.
    jQuery(".additional-resources-header").click(function(event){
	event.preventDefault();

	if ($(this).parent().parent('.product-download-link-group').children('.product-download-all-links').is(":hidden")){
	    //reveal list of product-download-links
	    $(this).parent().parent('.product-download-link-group').children('.product-download-all-links').slideDown("slow");
	    //update plus to minus
	    $(this).children('.more-options-plus').css("display", "none");
	    $(this).children('.more-options-minus').css("display", "inline");

	}
	else{
	    //close list of product-download links
	    //close open_product_link if it exists in this box (there's at most 1 open product link at any time).
	    if (open_product_link != null){
		var closing_group_id = $(this).parent().parent('.product-download-link-group').attr("id");
		$('#' + open_product_link.id).parents(".product-download-link-group").each(function(){
		    if ($(this).attr("id") == closing_group_id){
			collapseSelected(open_product_link);
		    }
		});
	    }
	//return product download links list to its original state 
	$(this).parent().parent('.product-download-link-group').children('.product-download-all-links').slideUp("slow");

	//update minus to plus
	$(this).children('.more-options-minus').css("display", "none");
	$(this).children('.more-options-plus').css("display", "inline");
	}
	
    });


/*
    //toggle visibility of product links for boxed section via hover.
    jQuery(".product-download-link-group").hover(
	function(){
	    if ($(this).children('.product-download-all-links').is(":hidden")){
		//reveal list of product-download-links
		$(this).children('.product-download-all-links').slideDown("slow");
	    }
	    else{
		//close open_product_link if it exists in this box (there's at most 1 open product link at any time).
		if (open_product_link != null){
		    var closing_group_id = $(this).attr("id");
		    $('#' + open_product_link.id).parents(".product-download-link-group").each(function(){
			if ($(this).attr("id") == closing_group_id){
			    collapseSelected(open_product_link);
			}
		    });
		}
		//return product download links list to its original state 
		$(this).children('.product-download-all-links').slideUp("slow");
	    }
	    return false;
	},
	function(){}
/*
	function(){
	    //close open_product_link if it exists in this box (there's at most 1 open product link at any time).
	    if (open_product_link != null){
		var closing_group_id = $(this).attr("id");
		$('#' + open_product_link.id).parents(".product-download-link-group").each(function(){
		    if ($(this).attr("id") == closing_group_id){
			collapseSelected(open_product_link);
		    }
		});
	    }
	    //return product download links list to its original state 
	    $(this).children('.product-download-all-links').slideUp("slow");
	    
	    return false;

	}

    );
*/



    
    //toggle visibility of open product image and links and animate arrow flip
    var open_product_link;

    jQuery(".product-download-link-title").click(function(event){
	event.preventDefault();

        if ($($($(this).parent()).children('.open_product')).is(":hidden")){
            //close already open product-download-link-title
            if (open_product_link != null){
                collapseSelected(open_product_link);
            }
            open_product_link = this;
            $(this).removeClass("active-blue-arrow"); //remove hovering blue arrow
            $(this).addClass("selected"); //specific to product-download-link body
            $($($(this).parent()).children('.open_product')).slideDown("medium"); 
            
            //add collapse arrow element if there's not one already
            if ($($(this).children('#product-list-blue-collapse-arrow')).length == 0){ 
                $(this).prepend('<img id="product-list-blue-collapse-arrow" src="http://cdn-data.motu.com/site/images/list-arrow6x11.png" />');
                //$($(this).children()).rotate({angle:0, animateTo:90});
            }
        }
        else{
            collapseSelected(this);
        }

    });
   
    //highlights adjacent download button when product-specific download title is hovered.
    $(".download-resource-header").hover(
	function(){
             $($(this).parent().siblings(".open-product-right").children(".additional-resource-btn").children().children("a")).toggleClass("hovered");
	},
	function(){
           $($(this).parent().siblings(".open-product-right").children(".additional-resource-btn").children().children("a")).toggleClass("hovered");
	}
    );


    //underlines product-specific download title when download button is hovered.
    $(".additional-resource-btn").hover(
	function(){
            $($(this).parent().siblings(".open-product-left").children(".download-resource-header").children().children().children("a")).toggleClass("hovered");
	},
	function(){
            $($(this).parent().siblings(".open-product-left").children(".download-resource-header").children().children().children("a")).toggleClass("hovered");
	}
    );


    //underlines installer OS that is currently selected (blue) when installer download button is hovered.
    $(".additional-resource-btn, .installer-dl-btn").hover(
	function(){
	    $($(this).parent().siblings(".mac-win-installer-dl-btns").children(".installer-os-icon-selected").children().children("a")).toggleClass("hovered");
	},
	function(){
	    $($(this).parent().siblings(".mac-win-installer-dl-btns").children(".installer-os-icon-selected").children().children("a")).toggleClass("hovered");
	}
    );




    /*
    //toggle visibility of more-info link on audio installer.
    jQuery(".installer-details-more-info-btn").click(function(event){
    if ($(this).children(".installer-details-more-info").is(":hidden")){
    $(this).children(".installer-details-more-info").slideDown("medium");
    $($(this).children("a")[0]).css("display", "none");
    $($(this).children("a")[1]).css("display", "block");
    }
    else{
    $(this).children(".installer-details-more-info").slideUp("medium");
    $($(this).children("a")[1]).css("display", "none");
    $($(this).children("a")[0]).css("display", "block");
    }
    return false;
    });
    */

    

});
