$(function () { /* DOMREADY */
    /** page variables **/
    var pageid = $("body").attr("id").split("page_")[1];
    var subid = "";
    var body_class = $("body").attr("class");
    var subid_idx = body_class.indexOf("subpage_");
    if (subid_idx > -1) {
        var subid_idx2 = (body_class.indexOf(" ", subid_idx) > -1) ? body_class.indexOf(" ", subid_idx) : 999;
        subid = body_class.substring(subid_idx + 8, subid_idx2);
    }


    /** Site Nav **/
    $("#navigation .nav_item").hover(
        function () {
            var navimg = $(this).find("img");
            navimg.attr("src", navimg.attr("src").replace("_off", "_on"));
        },
        function () {
            var navid = $(this).attr("id").split("nav_")[1];
            if (navid != pageid) {
                var navimg = $(this).find("img");
                navimg.attr("src", navimg.attr("src").replace("_on", "_off"));
            }
        }
    ); // end nav hover

    $("#subnav .subnav_item").hover(
        function () {
            var navimg = $(this).find("img");
            navimg.attr("src", navimg.attr("src").replace("_off", "_on"));
        },
        function () {
            var subnavid = $(this).attr("id").split("subnav_")[1];
            if (subnavid != subid) {
                var navimg = $(this).find("img");
                navimg.attr("src", navimg.attr("src").replace("_on", "_off"));
            }
        }
    ); // end subnav hover

    if ($("#nav #nav_" + pageid + " img").length>0) {
        $("#nav #nav_" + pageid + " img").attr("src", $("#nav #nav_" + pageid + " img").attr("src").replace("_off", "_on"));
    }

    /** **/

    //item filtering
    $("#page_apparel #subnav li").click(function(){
        
        if ($(this).hasClass('active')) { return; }
        
        $(this).siblings().removeClass('active');
        $(this).addClass('active');

        var filter = (this.id).replace("i_filter", "if");

        if (filter == 'if_all') {
            $(".itemGrid li").show();
            return;
        } else {
            $(".itemGrid li").hide();
			$(".itemGrid li." + filter).show();
        }

    });

    //shoe filtering
    $("#page_shoes #subnav li").click(function(){
        
        if ($(this).hasClass('active')) { return; }
        
        $(this).siblings().removeClass('active');
        $(this).addClass('active');


        var filter = (this.id).replace("s_filter", "sf");

        if (filter == 'sf_all') {
            $("#shoeGrid li").show();
            return;
        } else {
            $("#shoeGrid li").hide();
            $("#shoeGrid li." + filter).show();
        }

    });

    //shoe detail
    var video;
    $("#detailImageNav li").click(function(e){

        if ($(this).hasClass('active')) { return; }
        video = $("#sdMovie").children("video")[0];

		$("#detailImageNav li").removeClass('active');
		$(this).addClass('active');

        if ($(this).hasClass('video')) {
			$("#detailImageStage").fadeOut(200, function(){
				$("#shoeDetailVideo").fadeIn(200, function(){
                    if (video != undefined && video.paused) { video.play(); }
				});
            });
        }
        else
        {
			var lastimg;
            var imgsrc = $(this).find('img').attr('src').replace('/thumbnails', '');

			if ($("#shoeDetailVideo").is(":visible"))
			{
				if (video != undefined && !video.paused) { video.pause(); }
				$("#shoeDetailVideo").hide();
				$("#detailImageStage").css({'background': 'url(' + imgsrc + ') no-repeat center center'});
				$("#detailImageStage").fadeIn(200);
			}
			else
			{
				$("#detailImageStage").stop(true, true).hide('slide', { direction: 'up'}, 200, function(){
					$("#detailImageStage").css({'background': 'url(' + imgsrc + ') no-repeat center center'});
					$("#detailImageStage").show('slide', { direction: 'down'}, 200);
				});
			}
        }
    });
});




function mvalidate(ftype, val, req) {
    if ((ftype == null) || (ftype == "")) { return false; }
    switch (ftype) {
        case "text":
            var re = /[^a-zA-Z\- ]/g;
            if ((req) && (val == "")) {
                return false;
            } else if (re.test(val)) {
                return false;
            } else {
                return true;
            }
            break;
        case "int":
            var re = /[\D]/g;
            if (re.test(val)) { return false; } else { return true; }
            break;
        case "email":
            var re = /^[^\s()<>@,;:\/]+@\w[\w\.-]+\.[a-z]{2,}$/i;
            return re.test(val);
            break;
        case "alpha":
            var re = /[^a-zA-Z]/g;
            if (re.test(val)) { return false; } else { return true; }
            break;
        case "phone":
            var re = /^\(?[2-9]\d{2}[\)\.-]?\s?\d{3}[\s\.-]?\d{4}$/;
            return re.test(val);
            break;
        case "date":
            var re = /^(\d{1,2})[\s\.\/-](\d{1,2})[\s\.\/-](\d{4})$/;
            if (!re.test(val)) return false;
            var result = val.match(re);
            var m = parseInt(result[1]);
            var d = parseInt(result[2]);
            var y = parseInt(result[3]);
            if (m < 1 || m > 12 || y < 1900 || y > 2100) return false;
            if (m == 2) {
                var days = ((y % 4) == 0) ? 29 : 28;
            } else if (m == 4 || m == 6 || m == 9 || m == 11) {
                var days = 30;
            } else {
                var days = 31;
            }
            return (d >= 1 && d <= days);
            break;
        case "pass":
            return true;
            break;
        default:
            return false;
    }
}
