/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

if(typeof(SOFTSCRUB) === 'undefined') {
    var SOFTSCRUB = {

    };
}

/* Document Ready Function */
$(document).ready(function(){

    $('ul.product-sf-menu').superfish(
        {
            speed:'fast',
            onShow: function(){
                $("a#menu-products").addClass("drop-down-selected");
            },
            onHide: function(){
                $("a#menu-products").removeClass('drop-down-selected');
            }
        });

    //Open external links in new window
    $('a.external').attr('target', '_blank');



    /* http://code.google.com/p/jquery-dynamic-form/ */
    /* http://sroucheray.org/blog/demos/jquery-dynamic-form/ */

    /* Social Network Add*/
    $("#social-share").dynamicForm(
        "#share-plus", "#share-minus", {
            limit:3
        }
        );
    /* Blog add */
    $("#blog-add").dynamicForm(
        "#blog-plus", "#blog-minus", {
            limit:3
        }
        );

    /* Friend add */
    $("#share-friend").dynamicForm(
        "#friend-share-plus", "#friend-share-minus", {
            limit:3
        }
        );

    $("div#social-visibility,div#blog-visibility").hide();

    $('div#social-network input[type="radio"]').bind('click',function(){
        SOFTSCRUB.clickedSocialVal = $(this).val();

        if ( 0 == SOFTSCRUB.clickedSocialVal ){
            $("div#social-visibility").show();
        }else{
            $("div#social-visibility").hide();
        }

    }

    );
    $('#blog input[type="radio"]').bind('click',function(){
        SOFTSCRUB.clickedBlogVal = $(this).val();
        if ( 0 == SOFTSCRUB.clickedBlogVal ){
            $("div#blog-visibility").show();
        }else{
            $("div#blog-visibility").hide();
        }

    });

    /* Captain toolkit */
    $('div.download-image input').click( function(){
        $(this).select();
    });

    /** Home Page Product Tile Roll-Over */

    $('div#product-tile li.prod-link a').hover(
        function(){

            $('#product-tile ul').addClass( $(this).attr('name') );  //get the name attribute for
            var targetDetail = $(this).html();
            $('#product-tile p.product-detail-link span').html( targetDetail.toUpperCase() );
            $('#product-tile p.product-detail-link').show();
        },

        function(){
            $('#product-tile p.product-detail-link').hide();
            $('#product-tile ul').removeClass( $(this).attr('name') );

        }
        );


    /**
      * Products Index (Overview) Page
      * Tooltips box for product information.
      */
	/* handling info ballons on products page using event delegation */

	var toggleBalloonVisibility = function( event ) {
		var target = $(event.target),
			balloon;
		if ( target.is('a') ) {
			balloon = $( '#' + target.attr('name') + '-balloon');
			if ( (event.type == 'mouseover') && ( balloon.css('display') != 'block') ) {
				balloon.css('display', 'block');
			}
			if ( (event.type == 'mouseout') && ( balloon.css('display') != 'none') ) {
				balloon.css('display', 'none');
			}
		}
	};

    $('#product-index-page').bind( 'mouseover mouseout', toggleBalloonVisibility );


    /** FAQ Section */
    $('dl#prod-faq dt').bind("click",

        function(){
            var faqdetail = 'dl#prod-faq';
            var enableid = 'dd'+$(this).attr('id');
            $(faqdetail + ' dd').removeClass('showsection');
            $(faqdetail + ' dd#'+enableid).addClass('showsection');
            return false;
        }
        );

	/* TODO - code too brittle, depends upon naming conventions that could break */
    /* Individual product pages - elements with detail content */
    $('.product-detail-section ul#info-nav li a').click( function(){
		var link = $(this),
			sectionName = "#" + link.html() + '-section';
			var section = $('.product-detail-section .section' + sectionName);
			if ( section.css( 'display' ) == 'none' ) {
				$('.product-detail-section .section').hide();
				$('.product-detail-section ul li a.sectionactive').removeClass('sectionactive');
				link.addClass('sectionactive');
				section.show();
			}
			return false;
	});

	/* It would be nice to refactor this, make it DRY  */
    /* for the forum section */
	$("#site-search-wrapper input[type='text']" ).focus( function() {
		var that = $(this);
		var value =  that.val();
		if ( (value === 'Search') ) {
			that.val('').css('color', '#333333');
		}
	}).blur( function() {
		$(this).val('Search').css('color', '#aaa');
	});

	$("#forum-search-wrapper input[type='text']" ).focus( function() {
		var that = $(this);
		var value =  that.val();
		if ( (value === 'Search Forums') ) {
			that.val('').css('color', '#333333');
		}
	}).blur( function() {
		$(this).val('Search Forums').css('color', '#aaa');
	});


	/* generial hover state for all products on the surface guide */
    $("div#guide-products li.prod-link").hover(
        function(){
            $("div#guide-products li.prod-link").addClass('dimmed');
            $(this).removeClass('dimmed')

        },
        function(){
            $("div#guide-products li.prod-link").removeClass('dimmed');

        });

	/* hover states for when some has chose a product and certain products do not
       clean this surface...those products are considered 'inactive' and they appeare in blue */
    $("div#guide-products li.prod-link.inactive").hover(
        function(){
			$(this).removeClass('inactive');

        },
        function(){
            $(this).addClass('inactive');

        });

	/*
       this hover states deals when the case where the user has actually clicked on a bottle (rather
       than using the select menu.  In that case, the bottle is considered selected, and the others are not.
       This handler makes the hovers states behave like the the 'dimming' handler, with the exception that
       this handle remembers the state of the currently selected product - the product the user clicked on
       to see the surfaces it treats.
     */
    $("div#guide-products li.prod-link.not-selected").hover(
        function(){
			$('div#guide-products li.prod-link.current-select').removeClass('current-select').addClass('previous-current');
			$(this).addClass('current-select').removeClass('not-selected');

        },
        function(){
			$('div#guide-products li.prod-link.previous-current').addClass('current-select').removeClass('previous-current');
			$(this).addClass('not-selected').removeClass('current-select');
        });

	$('#main-menu ul li ul').bgiframe(
		{
			width: 136
		}
	);

	$('#bread-crumb-nav').superfish( {
		delay: 100,
		speed: 100,
		dropShadows: false
	}
	);

});
