

$(document).ready(function() {
				
    var myMenu = new SDMenu("my_menu");
    myMenu.init();
    
    skiptrip.core.onLoad();
    
    var visibleItem = null;

    $("#venue-1-select").click(function() {
        setToggle("#venue-1");
    });
				
    $("#venue-2-select").click(function() {
        setToggle("#venue-2");
    });
				
    $(".close-venue").click(function() {
        setHide("#venue-1");
        setHide("#venue-2");
    });
				
    $(".close-calendar").click(function() {
        setToggle(".calendar");
    });
				
    $("#date").click(function() {
        setToggle(".calendar");
    });
				
    $(".studio-link-1").click(function() {
        $("#venue-1-select").html($("span", this).text());
        setHide("#venue-1");
    });
				
    $(".studio-link-2").click(function() {
        $("#venue-2-select").html($("span", this).text());
        setHide("#venue-2");
    });
				
    $(".participate1").click(function() {
        setToggle(".participate1-dropdown");
    });
				
    $(".participate2").click(function() {
        setToggle(".participate2-dropdown");
    });
				
    $("#start-time").click(function() {
        setToggle(".start-time-dropdown");
    });
				
    $("#time-length").click(function() {
        setToggle(".time-length-dropdown");
    });
    
    $(".time-delta").click(function() {
        setToggle(".time-delta-dropdown");
    });
    
    // toggles the slickbox on clicking the noted link and changing the class 
    $("a.info-toggle").click(function(){
        $("div", this).toggleClass("icon-info2");  
        toggleInfo(this);
        return false;
    });
    
    $(".alert-toggle").click(function(){
        $("div", this).toggleClass("alert-icon-on");  
        var parentTr = this.parentNode.parentNode;
        var nextTr = $(parentTr).next('tr');
        $(nextTr).find('.alert-box').slideToggle('fast');
        return false;
    });
    
    $(".time-select").autocomplete(skiptrip.util.timeData, {
        highlight: false,
        minChars: 0,
        max: 48
    });
    $(".time-select").blur(function() {
        var v = $(this).attr("value");
        if (v === null) {
            return;
        }
        if (v.length == 3) {
            v = "0" + v.substring(0, 1) + ":" + v.substring(1, 3);
            $(this).attr("value", v);
        }
        if (v.length == 4) {
            if (v.indexOf(":") < 0) {
                v = v.substring(0, 2) +":"+ v.substring(2, 4);
            } else {
                v = "0"+ v;
            }
            $(this).attr("value", v);
        }
        
    });
    
    function toggleInfo(e) {
        var parent = e.parentNode.parentNode;
        var yourChild= $(parent).children(".infobox");
        $(yourChild).slideToggle('fast');
        return false;
    }
    
    function setHide(id) {
        if (id == visibleItem) {
            visibleItem = null;
        }
        if (id.charAt(0) != '#' && id.charAt(0) != '.') {
            id = "#" +id;
        }
        $(id).hide();
    }    
    
    function setShow(id) {
        if (visibleItem !== null && id != visibleItem) {
            setHide(visibleItem);
        }
        visibleItem = id;
        if (id.charAt(0) != '#' && id.charAt(0) != '.') {
            id = "#" +id;
        }
        $(id).show();
    }
    
    function setToggle(id) {
        if (id.charAt(0) != '#' && id.charAt(0) != '.') {
            id = "#" +id;
        }
        if ($(id).is(":visible")) {
            visibleItem = null;
        }
        else { 
            if (visibleItem !== null) {
                setHide(visibleItem);
            }
            visibleItem = id;
        }
        $(id).toggle();
    } 
});

$.fn.selectRange = function(start, end) { 
        return this.each(function() { 
                if(this.setSelectionRange) { 
                        this.focus(); 
                        this.setSelectionRange(start, end); 
                } else if(this.createTextRange) { 
                        var range = this.createTextRange(); 
                        range.collapse(true); 
                        range.moveEnd('character', end); 
                        range.moveStart('character', start); 
                        range.select(); 
                } 
        }); 
}; 
    


