﻿function ShowVPlayer(id, hid) {
    $(id).show("slide", { direction: "left" }, 450);

    $(hid).hide();

}

$(document).ready(function () {

    //show more show less code

    var up = "/Media/wsj_graphics/wsj_artExpand.gif";
    var down = "/Media/wsj_graphics/wsj_artCollapse.gif";

    $(".message_head").click(function (event) {

        var icon = $('.message_icon', this);
        $(this).next(".message_body").slideToggle();
        //icon.attr("src", this.attr("src") == up ? down : up);

        if (icon.attr("src") == up) {
            icon.attr("src", down);
            $(".today-we-asked-slidetabs").data("slideshow").stop();
        }
        else {
            icon.attr("src", up);
            $(".today-we-asked-slidetabs").data("slideshow").play();
        }
    });


    $('.message_head2').live('click', function () {

        var icon = $('.message_icon', this);
        $(this).next(".message_body").slideToggle();
        //icon.attr("src", this.attr("src") == up ? down : up);

        if (icon.attr("src") == up) {
            icon.attr("src", down);
        }
        else {
            icon.attr("src", up);
        }
    });

    //for college page and expert page


    //Track school or topic

    $('.trackclassforjquery').click(function (event) {
        event.preventDefault();
        var ajaxUrl = $(this).attr("href");
        //$.post(ajaxUrl);
        $("#tracking-content").load(ajaxUrl);
        //$("#tracking-content").html("<p>good</p><p>not good</p>");
        var popup = $("#tracking");
        popup.show();
        popup.css("top", $(window).scrollTop() + ($(window).height() - popup.height()) / 3);
        return false;
    });

    /*
    $(".tabs").each(function () {
    var thisTabber = $(this);

    if (thisTabber.find("li.selected").length == 0) {
    thisTabber.find("li:first").addClass("selected");
    $(thisTabber.find("li:first a").attr("href")).show();
    }
    else {
    $(thisTabber.find("li.selected a").attr("href")).show();
    }

    thisTabber.find("li a").click(function () {
    thisTabber.find("li.selected").removeClass("selected");
    $(this).parent().addClass("selected");

    thisTabber.parent().find(".tab").hide();
    $($(this).attr("href")).show();

    return false;
    });
    });
    
    $(".af-tabber-tabs").each(function () {
    var thisTabber = $(this);

    if (thisTabber.find(".af-tabs li.selected").length == 0) {
    thisTabber.find(".af-tabs li:first").addClass("selected");
    $(thisTabber.find(".af-tabs li:first a").attr("href")).show();
    }
    else {
    $(thisTabber.find(".af-tabs li.selected a").attr("href")).show();
    }

    thisTabber.find(".af-tabs li a").click(function () {
    thisTabber.find(".af-tabs li.selected").removeClass("selected");
    $(this).parent().addClass("selected");

    thisTabber.parent().find(".af-tab").hide();
    $($(this).attr("href")).show();

    $(".tabs ul:visible").jcarousel({
    scroll: 1
    });

    return false;
    });

    $(".tabs ul:visible").jcarousel({
    scroll: 1
    });
    });
    */

    $(".panel").each(function () {
        var thisTabber = $(this);

        if (thisTabber.find(".left li.selected").length == 0) {
            thisTabber.find(".left li:first").addClass("selected");
            $(thisTabber.find(".left li:first a").attr("href")).show();
        }
        else {
            $(thisTabber.find(".left li.selected a").attr("href")).show();
        }

        thisTabber.find(".left li a").click(function () {
            thisTabber.find(".left li.selected").removeClass("selected");
            $(this).parent().addClass("selected");

            thisTabber.parent().find(".panel-content").hide();
            $($(this).attr("href")).show();

            return false;
        });
    });

    $(".activatePopup").live("click", function () {
        var popup = $($(this).attr("href"));


        $("#overlayScreen").show();
        popup.show();



        popup.css("top", $(window).scrollTop() + ($(window).height() - popup.height()) / 2);

        return false;
    });

    $(".popups .close, .popups .popup-ok").live("click", function () {
        $("#overlayScreen").hide();
        $(".popups").hide();

        return false;
    });

    $(".popups").each(function () {
        var popup = $(this);
        if (popup.find(".popup-tabs li.selected").length == 0) {
            popup.find(".popup-tabs li:first").addClass("selected");
            $(popup.find(".popup-tabs li:first a").attr("href")).show();

            //$('.block-scroll').jScrollPane();
        }

        popup.find(".popup-tabs li a").click(function () {
            popup.find(".tab").hide();
            popup.find(".popup-tabs li.selected").removeClass("selected");

            $($(this).attr("href")).show();

            $(this).parent().addClass("selected");

            //$('.block-scroll').jScrollPane();

            return false;
        });
    });
});

function activatePlaceholders() {
    var detect = navigator.userAgent.toLowerCase();
    if (detect.indexOf("safari") > 0) return false;
    var inputs = document.getElementsByTagName("input");
    for (var i = 0; i < inputs.length; i++) {
        if (inputs[i].getAttribute("type") == "text") {
            if (inputs[i].getAttribute("placeholder") && inputs[i].getAttribute("placeholder").length > 0) {
                inputs[i].value = inputs[i].getAttribute("placeholder");
                inputs[i].onclick = function () {
                    if (this.value == this.getAttribute("placeholder")) {
                        this.value = "";
                    }
                    return false;
                }
                inputs[i].onblur = function () {
                    if (this.value.length < 1) {
                        this.value = this.getAttribute("placeholder");
                    }
                }
            }
        }
    }
}
window.onload = function () {
    activatePlaceholders();
}





jQuery.fn.customSelect = function () {

    //Change Layout
    jQuery(this).each(function (e) {
        jQuery(this).not("[multiple=multiple]").hide();
        newSelect = '<span class="custom-select ' + jQuery(this).attr("class") + '"><span class="custom-select-inner"><a href="#" class="custom-select-current">';
        newSelect += jQuery(this).find(":selected").text() + "</a><ul>";
        currentElement = jQuery(this).find("option").index(jQuery(this).find("option:selected"));
        jQuery(this).children().each(function (i) {
            if (i == currentElement)
                newSelect += "<li class='current'>" + jQuery(this).text() + "</li>";
            else
                newSelect += "<li>" + jQuery(this).text() + "</li>";
        });
        newSelect += "</ul></span></span>";
        jQuery(this).before(newSelect);

        //Fix for IE6 max-height bug
        if (jQuery(this).is(":disabled")) {
            jQuery(this).prev().addClass("disabled");
            jQuery(this).prev().find("a.custom-select-current").removeAttr("href");
        }

        //Toggles dropdown on click
        jQuery(this).prev().click(
			function () {
			    jQuery(this).not(".disabled").find("ul").toggle(
					0,
					function () {
					    if (jQuery(this).is(':visible')) {
					        if (jQuery(this).hasClass("focused")) {
					            //Scrolls to the current element
					            jQuery(this).scrollTop(0);
					            var scrollingOffset = jQuery(this).find("li.current").offset().top - jQuery(this).offset().top;
					            jQuery(this).scrollTop(scrollingOffset - 50);

					            var current = jQuery(this);
					            jQuery(".custom-select ul:visible").each(function () {
					                if (jQuery(this) != current) {
					                    jQuery(this).hide();
					                }
					            });
					        }
					        else {
					            var current = jQuery(this);
					            jQuery(".custom-select ul:visible").each(function () {
					                if (jQuery(this) != current) {
					                    jQuery(this).hide();
					                }
					            });
					        }
					    }
					}
				);
			    return false;
			}
		);

        //Adds .focused to the focusted element
        jQuery(this).prev().find(".custom-select-current").blur(
			function () {
			    jQuery(this).next().removeClass("focused");
			}
			).focus(function () {
			    jQuery(this).next().addClass("focused");
			}
		);


        //Hover Event		
        jQuery(this).prev().find("ul li").hover(
			function () {
			    jQuery(this).parent().find(".hovered").each(function () { jQuery(this).removeClass("hovered"); });
			    jQuery(this).addClass("hovered");
			},
			function () {
			    jQuery(this).removeClass("hovered");
			}
		);

        //Click Event
        jQuery(this).prev().find("ul li").each(function (i) {
            jQuery(this).click(
				function () {
				    jQuery(this).parent().parent().find(".custom-select-current").html(jQuery(this).html());
				    jQuery(this).parent().find("li").each(function () {
				        jQuery(this).removeClass("current");
				    });
				    jQuery(this).addClass("current");
				    jQuery(this).parent().parent().parent().next().find("option:eq(" + i + ")").attr("selected", "selected");
				}
			);
        });
    });

    //Hides the dropdowns on click somewhere on the webpage
    jQuery(document).click(function (e) {
        jQuery(".custom-select ul").each(function () {
            jQuery(this).hide();
        });
    });

    //Keyboard events
    jQuery(document).keydown(function (e) {
        //If the dropdown is opened
        jQuery(".custom-select ul.focused").each(function () {
            var object = jQuery(this);
            var objectParent = object.parent();
            switch (e.which) {
                case 37: case 38:
                    //Up arrow
                    object.find("li.current").each(function () {
                        var previousElement = jQuery(this).prev();

                        if (previousElement.length > 0) {
                            previousElement.addClass("current");
                            jQuery(this).removeClass("current");

                            if (jQuery(this).is(":visible")) {
                                var scrollingOffset = jQuery(this).offset().top - object.offset().top;
                                var scrollingOffsetTotal = (scrollingOffset - object.height() < -jQuery(this).height() * 2) ? scrollingOffset - object.height() + jQuery(this).height() * 4 : 0;
                                jQuery(this).parent().scrollTop(scrollingOffsetTotal + jQuery(this).parent().scrollTop());
                            }
                            objectParent.find(".custom-select-current").html(object.find("li.current").html());
                            objectParent.next().find("option:eq(" + object.find("li").index(object.find("li.current")) + ")").attr("selected", "selected");
                        }
                    });
                    e.preventDefault();
                    break;
                case 39: case 40:
                    //Down arrow
                    object.find("li.current").each(function () {
                        var nextElement = jQuery(this).next();

                        if (nextElement.length > 0) {
                            nextElement.addClass("current");
                            jQuery(this).removeClass("current");

                            if (jQuery(this).is(":visible")) {
                                var scrollingOffset = nextElement.offset().top - object.offset().top;
                                var scrollingOffsetTotal = (scrollingOffset - object.height() > -jQuery(this).height() * 4) ? scrollingOffset - object.height() + jQuery(this).height() * 4 : 0;
                                jQuery(this).parent().scrollTop(scrollingOffsetTotal + jQuery(this).parent().scrollTop());
                            }
                            objectParent.find(".custom-select-current").html(object.find("li.current").html());
                            objectParent.next().find("option:eq(" + object.find("li").index(object.find("li.current")) + ")").attr("selected", "selected");
                        }
                    });
                    e.preventDefault();
                    break;
                case 13:
                    //Enter
                    objectParent.find(".custom-select-current").html(object.find("li.current").html());
                    objectParent.next().find("option:eq(" + object.find("li").index(object.find("li.current")) + ")").attr("selected", "selected");
                    object.toggle();
                    e.preventDefault();

                    if (jQuery(this).is(":visible")) {
                        //Scrolls to the current element
                        jQuery(this).scrollTop(0);
                        var scrollingOffset = jQuery(this).find("li.current").offset().top - jQuery(this).offset().top;
                        jQuery(this).scrollTop(scrollingOffset - 50);
                    }

                    return false;
                    break;
                case 27:
                    //Escape
                    object.find("li.hovered").removeClass("hovered");
                    object.hide();
                    break;
                case 9:
                    //Tab
                    object.find("li.hovered").removeClass("hovered");
                    object.hide();
                    break;
            }
        });
    });
}

