// JavaScript Document
function initCufon() {
    Cufon.replace('h1, h2, h3');
	Cufon.replace('.trip-main h2, .trip-main h3, ul.points-use h3, div.col-left h2, div.col-right h2, div.show-directions h3, div.terms-tab h3, div.image-info h3, h2.explanation', {
		fontFamily: 'GothamBold'
	});
	Cufon.replace('ul.nav li, a.input-btn-wrap, h2.service, ul.faq h2, h3.service', {
		fontFamily: 'GothamBold',
		hover: {
			color: '#fffc00'
		}
    });
    Cufon.replace('ul.destinations strong');
	Cufon.now();
}

function initMyAccount() {
    var mysec = $("div.primary");
    if (mysec.length > 0) {
        if ($("div.primary").height() < $("div.secondary").height()) {
            $(mysec[0]).height($("div.secondary").height());
        }
		/*else {
			alert("hi");
			$(mysec[0]).css("height", $(mysec[0]).height());
		}*/
    }
}

$(document).ready(function() {
    //Font replace
    initCufon();

    //Register modal
    //	$("a.register").click(function() {
    //		Cufon.refresh();
    //		$("div.register").modal();
    //	});
    //Code position modal
    $("a.code-position").click(function() {
        Cufon.refresh();

        $("div#code-position").modal();
        return false;
    });
    $("a#code-position").click(function() {
        Cufon.refresh();
        $("div#code-position").modal();
        return false;
    });
    $("a.popup").click(function() {
        Cufon.refresh();
        $("div.popup").modal();
    });

    //
    $("a.code").click(function() {
        Cufon.refresh();

        $("div#code").modal();
        return false;
    });
    $("a#code").click(function() {
        Cufon.refresh();
        $("div#code").modal();
        return false;
    });
    //

    //IE fixes

    var conts = $("div.main");
    if (conts.length > 1) {
        $('div.main:first').addClass('below');
    }

    //Mapviw show/hide list
    $("div.section li.show-list a").toggle(function() {
        $(this).addClass("opened");
        $(this).html("Hide list");
        $("div.show-list").slideDown();
    }, function() {
        $(this).removeClass("opened");
        $(this).html("Show list");
        $("div.show-list").slideUp();
    });

    //Get/hide directions in Trip Details
    $("div#map-details li.show-list a").toggle(function() {
        var text = $("div#culture").html();
        $(this).addClass("opened");
        //$(this).html("Hide directions");
        if (text == "1")
            text = "Hide directions";
        else
            text = "Cacher itin&#233;raires";
        $(this).html(text);
        $("div.show-directions").slideDown();
    }, function() {
        var text = $("div#culture").html();
        if (text == "1")
            text = "Get directions";
        else
            text = "itin&#233;raires";    
        $(this).removeClass("opened");
        //$(this).html("Get directions");
        $(this).html(text);
        $("div.show-directions").slideUp();
    });

    //Custom selects
    if ($("select").length > 0) {
        $("select").each(function() { $(this).customSelect(); });
    }

    //Custom tooltips for Help
    if ($("a.help").length > 0) {
        simple_tooltip("a.help", "tooltip");
    }
    if ($("span.help").length > 0) {
        simple_tooltip("span.help", "tooltip");
    }

    //Custom checkboxes
    if ($("input:checkbox").length > 0) {
        $("input:checkbox").checkbox();
    }

    if ($("input:radio").length > 0) {
        $("input:radio").checkbox();
    }


    //Sliders
    $(function() {
        if (document.getElementById("slider-distance")) {
            $("#slider-distance").slider({
                change: function(event, ui) {
                    $("#ctl00_ContentPlaceHolder1_SideSearchBar_SliderDst").val($("#slider-distance").slider('option', 'value'));
                    $("#slider-data-distance").text($("#slider-distance").slider('value') + " " + $("#slider-data-distance").attr("title"));

                },
                min: 0,
                max: 60
            });
        }
    });
    $(function() {
        if (document.getElementById("slider-pts")) {
            $("#slider-pts").slider({
                change: function(event, ui) {
                    $('#ctl00_ContentPlaceHolder1_SideSearchBar_SliderPts').val($("#slider-pts").slider('option', 'value'));
                    $("#slider-data-pts").text($("#slider-pts").slider('value') + " " + $("#slider-data-pts").attr("title"));

                },
                min: 0,
                max: 50
            });
        }
    });

    //Giving enough height to my-sectin Div in My Account
    initMyAccount();

    //FAQ toggle
    $("ul.faq").css("display", "none");
    $("div.info").css("display", "none");
    // $("div.info:first").css("display", "block");
    // $("ul.faq h2 a:first").addClass("opened");
    var curInfo = ""; //$("div.info:first");

    $("ul.faq h2 a").click(function() {
        if (this.className != "opened") {
            $("ul.faq h2 a").removeClass("opened");
            $(this).addClass("opened");
            var temp = $(this).parent().parent().find("div.info");
            $(this).parent().parent().find("div.info").slideDown(300, function() {
                if (curInfo != "") {
                    $(curInfo).slideUp(300);
                }
                curInfo = temp;
            });

        } else {
            //$(this).removeClass("opened");
            //$(this).parent().parent().find("div.info").slideUp();
        }
        return false;
    });
    $("div.faq-sections h3 a:first").addClass("opened");
    $("ul.faq:first").css("display", "block");
    var curFaq = $("ul.faq:first");
    $("div.faq-sections h3 a").click(function() {
        if (this.className != "opened") {
            $("ul.faq h2 a").removeClass("opened");
            $("div.info").css("display", "none");
            $("div.faq-sections h3 a").removeClass("opened");
            $(this).addClass("opened");
            var temp = $(this).parent().next();
            $(this).parent().next().slideDown(300, function() {
                $(curFaq).slideUp(300);
                curFaq = temp;
            });

        } else {
            //$(this).removeClass("opened");
            //$(this).parent().parent().find("div.info").slideUp();
        }
        return false;
    });
});

function focusNext(sender, after) {
    if (sender.val().length == after)
        sender.parent().next().children(':first-child').focus();
}
