$(document).ready( function() {
    
    $('.bullets li a').each(function(i, elem) {
        $(elem).html(function(i, html) {
            return html.replace(/(®)/, '<sup class="reg">$1</sup>');
     	});
    });

    
    /**
     * Home Page Featured Rotator
     */
    $('#home-featured').cycle({
        fx: 'fade', // choose your transition type, ex: fade, scrollUp, shuffle, etc...
        slideExpr: '.featured-item',
        timeout: 5000,
        pager: '#featured-nav',
        cleartypeNoBg: true
    });

    $(".side-nav-link").hover( function() {
        $(this).find('.arrow').show();
    }, function() {
        if ( $(this).hasClass('active') == true) {

        } else {
            $(this).find('.arrow').hide();
        }
    });
    /**
     * Equal Heights of Homepage Columns & Site Map
     */
    $('.eq').equalHeightColumns();
    $('.sitemap-section ul').equalHeightColumns();
    /**
     * Standard Image Popups
     */
    $('.enlarge').colorbox({
        maxWidth:'980px'
    });
    /**
     * Company Team Manager Bios
     */
    $('.read-more').live('click', function() {
        var t = $(this).attr('rel');

        if (t == 'more') {
            $(this).parent().find('.hide').show();
            $(this).hide();
        } else if (t == 'less') {
            $(this).closest('.hide').hide();
            $(this).parents('p.excerpt').find('.read-more').show();

        }
        return false;
    });
    /**
     * Use Case Tool Tip
     */
    function case_tip() {

        $('.text-link').each( function() {

            $(this).qtip({
                content: {
                    text: function(api) {
                        var customContent = '<h5>' + $(this).text() + '</h5>';
                        customContent += '<p>' + $(this).attr('title') + '</p>';
                        customContent += '<a class="view-full-case" href="'+ $(this).attr('href') + '">&nbsp;</a>';
                        return customContent;
                    }
                },
                position: {
                    at: 'left center', // Position the tooltip to the right of link
                    my: 'right center', //The tip goes to the right and center of the box
                    adjust: {
                        screen: true,
                        x: -45
                    } // Keep the tooltip on-screen at all times
                },
                hide: {
                    event: 'mouseleave', //Closes on Mouseleave, with a 1/2 second delay, and fixed for allowing users to click content
                    delay:2000,
                    fixed:true
                },
                show: {
                    delay:0,
                    solo:true
                },

                style: {
                    classes: 'ui-tooltip-wiki ui-tooltip-integri ui-tooltip-shadow',
                    width: 300
                },
                events: {
                    render: function(event, api) {
                        // Grab the BGIFrame element
                        var elem = api.elements.bgiframe;
                    }
                }

            })

        });
    }
	/**
     * Logged In Download Resource Toolip
     */
    function resource_tip() {

        $('.bullets .download-documents-loggedin').each( function() {

            $(this).qtip({
                content: {
                    text: function(api) {
                        var customContent = '<h5>' + $(this).text() + '</h5>';
                        customContent += '<p class="download-p">' + $(this).attr('title') + '</p>';
                        customContent += '<a class="download-link" href="'+ $(this).attr('href') + '">&nbsp;</a>';
                        return customContent;
                    }
                },
                position: {
                    at: 'left center', // Position the tooltip to the right of link
                    my: 'right center', //The tip goes to the right and center of the box
                    adjust: {
                        screen: true,
                        x: -25
                    } // Keep the tooltip on-screen at all times
                },
                hide: {
                    event: 'mouseleave', //Closes on Mouseleave, with a 1/2 second delay, and fixed for allowing users to click content
                    delay:2000,
                    fixed:true
                },
                show: {
                    delay:0,
                    solo:true
                },

                style: {
                    classes: 'ui-tooltip-wiki ui-tooltip-integri ui-tooltip-shadow',
                    width: 300
                },
                events: {
                    render: function(event, api) {
                        // Grab the BGIFrame element
                        var elem = api.elements.bgiframe;
                    }
                }

            })

        });
    }
	/**
     *  Not Logged in Register Tool Tip
     */
    function download_tip() {

        $('.bullets .download-documents').each( function() {
	
            $(this).qtip({
                content: {
                    text: function(api) {
                        var customContent = '<h5>' + $(this).text() + '</h5>';
                        customContent += '<p class="download-p">' + $(this).attr('title') + '</p>';
                        customContent += '<a class="register-link" href="'+ $(this).attr('href') + '">&nbsp;</a>';
                        return customContent;
                    }
                },
                position: {
                    at: 'left center', // Position the tooltip to the right of link
                    my: 'right center', //The tip goes to the right and center of the box
                    adjust: {
                        screen: true,
                        x: -25
                    } // Keep the tooltip on-screen at all times
                },
                hide: {
                    event: 'mouseleave', //Closes on Mouseleave, with a 1/2 second delay, and fixed for allowing users to click content
                    delay:2000,
                    fixed:true
                },
                show: {
                    delay:0,
                    solo:true
                },

                style: {
                    classes: 'ui-tooltip-wiki ui-tooltip-integri ui-tooltip-shadow',
                    width: 300
                },
                events: {
                    render: function(event, api) {
                        // Grab the BGIFrame element
                        var elem = api.elements.bgiframe;
                      
                    }
                }

            })

        });
    }
    //For Case Studies and White Papers (Non Logged In)
	function download_resource_notloggedin() {

        $('#docContainer .download-documents').each( function() {
	
            $(this).qtip({
                content: {
                    text: function(api) {
                        var customContent = '<h5>' + $(this).text() + '</h5>';
                        customContent += '<p class="download-p">' + $(this).attr('title') + '</p>';
                        customContent += '<a class="register-link" href="'+ $(this).attr('href') + '">&nbsp;</a>';
                        return customContent;
                    }
                },
                position: {
                    at: 'right center', // Position the tooltip to the right of link
                    my: 'left center', //The tip goes to the right and center of the box
                    adjust: {
                        screen: true,
                        x: +5
                    } // Keep the tooltip on-screen at all times
                },
                hide: {
                    event: 'mouseleave', //Closes on Mouseleave, with a 1/2 second delay, and fixed for allowing users to click content
                    delay:2000,
                    fixed:true
                },
                show: {
                    delay:0,
                    solo:true
                },

                style: {
                    classes: 'ui-tooltip-wiki ui-tooltip-integri ui-tooltip-shadow',
                    width: 300
                },
                events: {
                    render: function(event, api) {
                        // Grab the BGIFrame element
                        var elem = api.elements.bgiframe;
                      
                    }
                }

            })

        });
    }
	 //For Case Studies and White Papers (Logged In)
	function download_resource() {

        $('#docContainer .download-documents-loggedin').each( function() {
	
            $(this).qtip({
                content: {
                    text: function(api) {
                        var customContent = '<h5>' + $(this).text() + '</h5>';
                        customContent += '<p class="download-p">' + $(this).attr('title') + '</p>';
                        customContent += '<a class="download-link" href="'+ $(this).attr('href') + '">&nbsp;</a>';
                        return customContent;
                    }
                },
                position: {
                    at: 'right center', // Position the tooltip to the right of link
                    my: 'left center', //The tip goes to the right and center of the box
                    adjust: {
                        screen: true,
                        x: +5
                    } // Keep the tooltip on-screen at all times
                },
                hide: {
                    event: 'mouseleave', //Closes on Mouseleave, with a 1/2 second delay, and fixed for allowing users to click content
                    delay:2000,
                    fixed:true
                },
                show: {
                    delay:0,
                    solo:true
                },

                style: {
                    classes: 'ui-tooltip-wiki ui-tooltip-integri ui-tooltip-shadow',
                    width: 300
                },
                events: {
                    render: function(event, api) {
                        // Grab the BGIFrame element
                        var elem = api.elements.bgiframe;
                      
                    }
                }

            })

        });
    }
    case_tip();
    resource_tip();
    download_tip();
    download_resource();
	download_resource_notloggedin();
	
	/**
     * Tooltip Registration Button to Call Lightbox Form to Register
     */
    $('.register-link').live('click', function() {
		//Hides the Tooltip that you just hovered over
		$('.download-documents').qtip('hide');
        //Call Colorbox for the Registration form.
        $.fn.colorbox({
            width:500,
            href:$(this).attr('href'),
            'onComplete': function() {

                $('#registration').validate({
                    highlight: function(element, errorClass, validClass) {
                        $(element).addClass(errorClass).removeClass(validClass);
                        $(element.form).find("label[for=" + element.id + "]")
                        .addClass(errorClass);
                        $(element.form).parent('.row').find("label[for=" + element.id + "]").addClass(errorClass);
                    },
                    unhighlight: function(element, errorClass, validClass) {
                        $(element).removeClass(errorClass).addClass(validClass);
                        $(element.form).find("label[for=" + element.id + "]")
                        .removeClass(errorClass).addClass(validClass);
                    },
                    rules: {
                        firstName: "required",
                        email: {
                            required: true,
                            email:true
                        },
                        lastName: "required"
                    },
                    errorElement:"em",
                    errorClass: "error",
                    validClass: "success",

                    submitHandler: function(form) {
                        $(form).ajaxSubmit({
                            success: successAction
                        });
                        return false;
                    }
                });
                function successAction(responseText, statusText, xhr, $form) {
					
                    $.colorbox({
                        html:responseText,
                        'onClosed': function() {
                        	resource_tip();
                        	download_resource();
                        }
                    });
                }

            }
        });
        return false;
    });
    /**
     * Download Documents Registration Form Validation
     */
	
    $('.download-documents').colorbox({
        width:500,
        'onOpen': function(){
        //Hides the Tooltip that you just hovered over
		$('.download-documents').qtip('hide');
        },
        'onComplete': function() {

            $('#registration').validate({
                highlight: function(element, errorClass, validClass) {
                    $(element).addClass(errorClass).removeClass(validClass);
                    $(element.form).find("label[for=" + element.id + "]")
                    .addClass(errorClass);
                    $(element.form).parent('.row').find("label[for=" + element.id + "]").addClass(errorClass);
                },
                unhighlight: function(element, errorClass, validClass) {
                    $(element).removeClass(errorClass).addClass(validClass);
                    $(element.form).find("label[for=" + element.id + "]")
                    .removeClass(errorClass).addClass(validClass);
                },
                rules: {
                    firstName: "required",
                    email: {
                        required: true,
                        email:true
                    },
                    lastName: "required"
                },
                errorElement:"em",
                errorClass: "error",
                validClass: "success",

                submitHandler: function(form) {
                    $(form).ajaxSubmit({
                        success: successAction
                    });
                    return false;
                }
            });
            function successAction(responseText, statusText, xhr, $form) {

                $.colorbox({
                    html:responseText
                });
            }

        }
    });
  


    /**
     * Home Page Custom Lightbox Navigation
     */

    $('.market-hp').click( function() {
    	//Hides the Tooltip that you just hovered over
		$('.download-documents,.download-documents-loggedin').qtip('hide');
        var url = $(this).attr('href');
        var myThis = $(this);
        var jqxhr = $.ajax({
            url: url
        })
        .success( function(html) {
            $('.bg-overlay').fadeTo('fast','0.75');
            $('.overlay-wrap').show();
            $('#overlay-container').append(html);
            $('<span id="close-modal">&nbsp;</span>').appendTo('.lightbox');

            case_tip(); //Call Tooltip Function

            $('.bg-overlay').click( function() { //Click on the Background to close it
                $(this).fadeOut('fast');
                $('.overlay-wrap').hide();

                $('#overlay-container').find('div.lightbox').remove(); //Removes from DOM
                $('#overlay-container').find('.clear').remove(); //Removes from DOM

            });
            $('#close-modal').live('click', function() { //Function for Close Button
                $('.bg-overlay').fadeOut('fast');

                $('#overlay-container').find('div.lightbox').remove();
                $('#overlay-container').find('.clear').remove(); //Removes from DOM

            });
        })
        .error( function() {
            alert("error");
        })
        .complete( function() {
        });
        return false;
    });
    /*Product Page Tabs
     *
     */

    function setTabs() {
        //Gets Hidden Value of Tab
        var getTab = $('#whichTab').val();
        if(getTab != '') {
            var selectedTab = getTab;

        } else {
            var selectedTab = 0;
        }

        $("#dna-tabs").tabs({
            selected:selectedTab
        });
    }

    setTabs();

    /* Contact Form Validation
     *
     */

    $('#contact-form').validate({
        highlight: function(element, errorClass, validClass) {
            $(element).addClass(errorClass).removeClass(validClass);
            $(element.form).find("label[for=" + element.id + "]")
            .addClass(errorClass);
            $(element.form).parent('.row').find("label[for=" + element.id + "]").addClass(errorClass);
        },
        unhighlight: function(element, errorClass, validClass) {
            $(element).removeClass(errorClass).addClass(validClass);
            $(element.form).find("label[for=" + element.id + "]")
            .removeClass(errorClass).addClass(validClass);
        },
        rules: {
            firstName: "required",
            email: {
                required: true,
                email:true
            },
            lastName: "required",
            company:"required",
            privacy:"required"
        },
        errorElement:"em",
        errorClass: "error",

        submitHandler: function(form) {
            form.submit();
            // $(form).ajaxSubmit({
            //     success: successAction
            // });
            return false;
        }
    });
    // function successAction(responseText, statusText, xhr, $form) {
    //
    //
    // }

    /* Event Registration Form Validation
     *
     */
    $('#event-form').validate({
        highlight: function(element, errorClass, validClass) {
            $(element).addClass(errorClass).removeClass(validClass);

        },
        unhighlight: function(element, errorClass, validClass) {
            $(element).removeClass(errorClass).addClass(validClass);

        },
        rules: {
            firstName: "required",
            email: {
                required: true,
                email:true
            },
            phone: "required",
            'tracks[]': {
                required: "#trainingYes:checked"
            },
            lastName: "required",
            company:"required",
            privacy:"required"
        },
        errorElement:"em",
        errorClass: "error",

        submitHandler: function(form) {
            form.submit();
            // $(form).ajaxSubmit({
            //     success: successAction
            // });
            return false;
        }
    });
    // function successAction(responseText, statusText, xhr, $form) {
    //
    //
    // }

    //Fancy Checkbox
    $('input[name="training"]').change( function() {

        var $input = $(this);

        if($input.val() == 'yes') {
            $('#training-options').toggle();
        } else {
            $('#training-options').hide();
        }

    });
});
