/// <reference path="jquery-1.4.1-vsdoc.js" />
$(document).ready(function () {
    // current year replacement for footer copyright
    if ($('#currentyear').length) {
        var cdate = new Date();
        var cyear = cdate.getFullYear();
        $('#currentyear').html(cyear + '&nbsp;');
    }

    /*Jquery watermark Plugin ///Author: Amit Gaur, GB*/
    jQuery.fn.ws_watermark = function (options) {
        var settings = $.extend({
            message: "Search"
        }, options);

        this.each(function () {
            var id = $(this).attr("id");
            $("#" + id).val(settings.message);
            $("#" + id).focusin(function () {
                $(this).val("");
            });
            $("#" + id).focusout(function () {
                var count = $(this).val();
                if (count == 0) {
                    $(this).val(settings.message);
                }
            });
        });
        return this;
    };


    //    /*Tools DropDown*/
    function tools_hover_over() {
        $(this).find(".tools").fadeTo('normal', 1).slideDown(); //Find sub and fade it in
        $(this).find(".departments").fadeTo('normal', 1).slideDown();
        $(this).find(".uchealth").fadeTo('normal', 1).slideDown();
    }
    //On Hover Out
    function tools_hover_out() {
        $(this).find(".tools").slideUp();
        $(this).find(".departments").slideUp();
        $(this).find(".uchealth").slideUp();
    }
    //Set custom configurations
    var tools_config = {
        sensitivity: 6, // number = sensitivity threshold (must be 1 or higher)
        interval: 50, // number = milliseconds for onMouseOver polling interval
        over: tools_hover_over, // function = onMouseOver callback (REQUIRED)
        timeout: 500, // number = milliseconds delay before onMouseOut
        out: tools_hover_out // function = onMouseOut callback (REQUIRED)
    };
    $("ul#toolbox li .tools").css({ 'opacity': '0' }); //Fade sub nav to 0 opacity on default
    $("ul#toolbox li").hoverIntent(tools_config); //Trigger Hover intent with custom configurations
    /*==============================Tools DropDown End=============================================*/

    /*Main Navigation DropDown*/
    var is_nav_open = false;

    $(".top_nav_container ul li a").click(function (e) {
        e.preventDefault();
        if (is_nav_open) {
			//$("#media_container").css({'margin-top' : '18px'});
            $(".nav_container").animate({
                height: "30px"				
            }, 300);
            is_nav_open = false;
        } else {
			//$("#media_container").css({'margin-top' : '0px'});
            $(".nav_container").animate({
                height: "180px"
            }, 300);
            is_nav_open = true;
        }
    });

    /*==============================Main Navigation Dropdown End=============================================*/

    /*Slideshow Component*/
    /*Jquery Hover Plugin ///Author: Amit Gaur */
    jQuery.fn.active_effect = function () {
        return this.each(function () {
            $(this).hover(function () {
                var id = $(this).attr('id');
                $('#' + id).addClass('active');
            }, function () {
                var id = $(this).attr('id');
                $('#' + id).removeClass('active');
            });
        });
    }

    if ($("#slideshow_container").length) {
        // $("#slideshow_container").load("ContentSliderData.aspx");
        /* $.get("ContentSliderData.aspx#content_items", function (data) {
        alert(1);
        });
        */
        $(function () {
            $('#one').ContentSlider({
                width: '938px',
                height: '282px',
                speed: 800,
                easing: 'easeOutQuad'
            });
        });

        /*==============================Slideshow Component End=============================================*/

    }

    if(location.pathname.split("/")[2]=="FacultyProfile.aspx"){
        $("#site_left_nav a[href='Directory.aspx']").each(function(){
           $(this).addClass('active'); 
        });
    }
    //==================Left nav Highlight Logic=================================
    //Generic sublink highlight logic
    if ($("div.rtSelected").length) {
        var file_name = parseUri(window.location).file;
        $("div.rtSelected").siblings("ul").find("li.rtLI a").each(function () {
            if ($(this).attr("href") == file_name) {
                $("div.rtSelected").removeClass("rtSelected");
                $(this).addClass("active");
            }
        });
    }

    //Overview Page higlight logic
    /*if ($("div.COMTreeView").length) {
        var dir_name = parseUri(window.location).directory;

        //checking if you are on overview page and if you are then highlighting it
        var ov_page_url = $("div.COMTreeView ul li div a:first").attr("href");
        if (ov_page_url !== undefined) {
            var child_page_match_overview = /\.\.\/+/;
            //Removing unnecessary active for informationfor section            
        }

        //getting rid of unnecessary line at top on left menu 
        if (!$("div.COMTreeView ul").length) {
            $("div.COMTreeView").hide();
        }
        /*Highlighting left Menu Faculty Profile*/
        /*if ($("#left_nav_marketing_section ul li").length) {
            var query_name = parseUri(window.location).query;
            if (query_name == "filterby=department") {
                $("#left_nav_marketing_section ul li:first-child a").addClass("active");
            }
            else {
                $("#left_nav_marketing_section ul li:nth-child(2) a").addClass("active");
            }
            $("div.COMTreeView").show();
        }
    }*/

    if($("#site_left_nav").length){
        var file_name = parseUri(window.location).file.toLowerCase();
        var arr = ["summit.aspx","correctional.aspx","probate.aspx","rotationtable.aspx"];
        var parent_arr = ["curriculum/summit.aspx","curriculum/correctional.aspx","curriculum/probate.aspx","rotations/rotationtable.aspx"];
        $("#site_left_nav a").each(function() {
            var _href = $(this).attr('href').toLowerCase();
            if(_href == "http://med.uc.edu/gme/incoming/contracts_copy1.aspx"){$(this).attr('target','_blank');}            
            if($.inArray(_href,arr) != -1 || $.inArray(_href,parent_arr) != -1){
                $(this).parent().parent().hide();
                if($.inArray($(this).attr('href').toLowerCase(),parent_arr) == -1){
                    $(this).parent().parent().parent().siblings().find('a').addClass('active'); 
                }                                              
            }
        });
        
    }

    $("#search_box_toolbar_input").ws_watermark({ message: "Search" });

    /*==============================Watermark Component End=============================================*/

    /*==============================Adding Masking to  Phone Number in  contact form=============================================*/
      if($("#ctl00_page_content_section_SingleForm1_SingleForm14").length){
          $("#ctl00_page_content_section_SingleForm1_SingleForm14").mask("(999) 999-9999");
      }
    /*==============================END Adding Masking to  Phone Number in  contact form=============================================*/

    /*==============================Search Button Submit Logic=============================================*/

    $("#search_toolbar_submit").click(function (e) {
        e.preventDefault();
        submit_search(0);
    });

    $('#search_box_toolbar_input').keypress(function (e) {
        var query = $("#search_box_toolbar_input").val();
        if (e.which == 13) {
            e.preventDefault();
            submit_search(0);
        }
    });

    /* homepage addt'l search */
    $("#search_submit").click(function (e) {
        e.preventDefault();
        submit_search(1);
    });

    $('#search_box_input').keypress(function (e) {
        var query = $("#search_box_input").val();
        if (e.which == 13) {
            e.preventDefault();
            submit_search(1);
        }
    });

    function submit_search(inputsource) {
        var searchSource = '#search_box_toolbar_input';
        if (inputsource != 0) {
            searchSource = '#search_box_input';
        }

        var query = $(searchSource).val();
        if (query != "Search" && query != "Please enter a search term." && query != null && query != "") {
            window.location.href = "http://search.uc.edu/search?q=" + query + "&btnG=Google+Search&access=p&sort=date%3AD%3AL%3Ad1&output=xml_no_dtd&ie=UTF-8&oe=UTF-8&client=psychiatry_frontend&proxystylesheet=psychiatry_frontend";
        } else {
            e.preventDefault();
            $(searchSource).val("Please enter a search term.");
        }
    }

    /*=================================Secondary Page Navigation overlay effect*/
    $("#navigation_header_image img").hide();
    var secondary_page_navigation_image_url = $("#navigation_header_image img").attr("src");
    $("#navigation_image_container").css("background-image", 'url(' + secondary_page_navigation_image_url + ')');



    /*=================================Right Sidebar Height Logic================*/

    // If page contains right_content_section
    // equalize column height (insures right sidebar border stretches to meet footer)

    // need only to check if sidebar is shorter than content
    if ($('#page_content_container').length > 0) {
        var contentHeight = $('#site_content_text_container').height();
        var sbRightHeight = $('#right_content_section').height();
        
        if (sbRightHeight < contentHeight) {
            $('#right_content_section').height(contentHeight);
        }
    }

    /*=================Contact Us Submit Logic===================================*/
    if ($("#contact_us_form").length) {
        $("input#SubmitRequest").click(function () {
            var error_count = 0;
            error_count += validateitem("input#textBox1");
            error_count += validateitem("input#textBox2");
            error_count += validateitem("input#email");
            error_count += validateitem("input#phone1");
            error_count += validateitem("input#phone2");
            error_count += validateitem("input#phone3");
            var person = getValue("#dropdown");
            var firstname = getValue("input#textBox1");
            var lastname = getValue("input#textBox2");
            var email = getValue("input#email");
            var phonenumber = getValue("input#phone1") + getValue("input#phone2") + getValue("input#phone3");
            var faxnumber = getValue("input#Fax1") + getValue("input#Fax2") + getValue("input#Fax3");
            var message = getValue("#Message");
            if (error_count == 0) {
                $.post("http://med.uc.edu/sitefinitycoldfusionhelpers/radoncologyhelpers/emailer/contactusemailer.cfm?Person=" + person + "&firstName=" + firstname + "&lastName=" + lastname + "&Email=" + email + "&phoneNumber=" + phonenumber + "&faxNumber=" + faxnumber + "&Message=" + message);
                $("#contact_us_form").hide("slow");
                $("#contact_us_form").replaceWith('<h3>Email sent successfully, you will be contacted soon.</h3>');
            }
            else {
                $("#contact_us_form").append('<tr><td colspan="2"><h4>Please fill out the required fields.</h4></td></tr>');
            }

            return false;
        });
    }

    function validateitem(id) {
        if ($(id).val().length == 0) {
            $(id).css("background-color", "yellow");
            return 1;
        }
        else {
            return 0;
        }
    }
    function getValue(id) {
        var inputvalue = $(id).val();
        return inputvalue;
    }
}); 
 /*=================END Contact Us Submit Logic END===================================*/
/*URl Parsing Function*/
function parseUri(str) {
    var o = parseUri.options,
        m = o.parser[o.strictMode ? "strict" : "loose"].exec(str),
        uri = {},
        i = 14;

    while (i--) uri[o.key[i]] = m[i] || "";

    uri[o.q.name] = {};
    uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) {
        if ($1) uri[o.q.name][$1] = $2;
    });

    return uri;
};

parseUri.options = {
    strictMode: false,
    key: ["source", "protocol", "authority", "userInfo", "user", "password", "host", "port", "relative", "path", "directory", "file", "query", "anchor"],
    q: {
        name: "queryKey",
        parser: /(?:^|&)([^&=]*)=?([^&]*)/g
    },
    parser: {
        strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
        loose: /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
    }
};
/*=============================================*/


