$(document).ready(function() {

    //common
    //submitbutton
    $('.submitbutton').click(function() {
        $(this).parents('form').submit()
    });

    //lbtntellafriend
    $('.lbtntellafriend').attr('href', '/tell-a-friend.asp#' + location.href);

    //autofields
    InitFormFields();

    InitValidations();
    //common

    UpdateTime();
    //hovertext    
    $('.hovertext').each(function(i) {
        hovertext_blur($('.hovertext').eq(i));
    });
    $('.hovertext').focus(function() {
        hovertext_focus(this);
    });

    $('.hovertext').blur(function() {
        hovertext_blur(this);
    });
    $('.clearhovertext').click(function() {
        $('.hovertext').each(function(i) {
            hovertext_clear($('.hovertext').eq(i));
        });
    })

    //sub-nav


    $('#subnav h2').click(function() {
        var id = $(this).attr('id').replace('h2', '');
        //$('#subnav ul[id!="ul' + id + '"]').slideUp('normal');
        $('#subnav ul[class=' + $('#ul' + id).attr('class') + '][id!="ul' + id + '"]').slideUp('normal');
        $('#ul' + id).slideToggle('normal');
        // $('#ul' + id).parents('ul').slideToggle('normal');
    });
    //InitSub()

    //pageinfo_content

    if ($('.pageinfo_content').attr('class')) {
        $('.pageinfo').html($('.pageinfo_content').html());
    }

    //btnSearch
    $('#btnSearch').click(function() {
        SearchDo();
    });

    $('#txtKeywords').change(SearchDo);


    //frmForward
    if ($('#txtURL').attr('type')) {
        var url = location.href;
        if (url.indexOf('#') != -1) {
            url = url.substr(url.indexOf('#') + 1);
            $('#txtURL').val(url);
        } else {
            $('#txtURL').val('http://mattermatters.com/');
        }
    }
    var frmForward = $("#frmForward").validate({
        rules: {
            ToEmail: "required",
            FromEmail: { required: true, email: true }
        }
    });

    //select IE
    if (!document.height) {
        $('.noselect').css({ 'margin-top': '-4px', 'margin-left': '-4px' });
    }

    //tbproducts_width
    tbproducts_width();

    //plalter
    $('.plalter img').click(function() { plalter_img_click(this); });
});


function InitSub() {

   // $('#subnav ul').hide();
    try {
        $('#subnav a[class=on]').parents('ul').show();
        $('#subnav a[class=on]').parents('li').children('ul').show();
        // $('#subnav a[class=on]').parents('ul').find('h2').eq(0).css('font-weight','bold');;
        if ($('#subnav a[class=on]').parent().parent().attr('class') == 'uls') {
            $('#subnav a[class=on]').parent().parent().parent().prev().find('a:not(:contains("ALL"))').addClass('on');
        }
    } catch (e) {}
}
function SearchDo() {
    var keywords = $.trim($('#txtKeywords').val());
    if (keywords.length > 0 && keywords != $('#txtKeywords').attr('title')) {
        location.href = '/products.asp?key=' + keywords;
    }
}
function plalter_img_click(obj) {
    var m = $('.mainimg img').attr('src');
    $('.mainimg img').attr('src', $(obj).attr('src').replace('/th_', '/lg_'));
    $(obj).attr('src', m.replace('/lg_', '/th_'));
}

var alerted = 0;
function tbproducts_width() {
    //alert($('.body_fm').width());
    // $('.body_fm').css('background','red')
    if ($('#tbproducts').attr('id')) {
        var wl = 200 + 20;
        var wt = $('#tbproducts').width();
        if (wt < 598) {
            $('#tbproducts').width(598);
            wt = 598;
        }
        /* if(alerted<2)
        alert('1) '+wl + '-' + wt + '-' + $('.body_fm').width());
        */
        if ($('.body_fm').width() < (wl + wt)) $('.body_fm').width(wl + wt);
        /*
        if (alerted <2) alert('2) ' + wl + '-' + wt + '-' + $('.body_fm').width());
        alerted ++;
        */
        setTimeout("tbproducts_width()", 300);
    }
}


function InitValidations() {
    var fmnewuser = $("#fmnewuser").validate({
        rules: {
            Email: { required: true, email: true },
            ConfirmEmail: { required: true, email: true, equalTo: "#txtEmail" },
            Password: { required: true },
            ConfirmPassword: { required: true, equalTo: "#txtPassword" },
            Firstname: "required",
            Lastname: "required"
        }
    });

    var fmmailing = $("#fmmailing").validate({
        rules: {
            Email: { required: true, email: true }
        }
    });

    var fmlogin = $('#fmlogin').validate({
        rules: {
            Email: { required: true, email: true },
            Password: { required: true }
        }
    });

    $('#chkSaveAsShipping').click(chkSaveAsShipping_click);
    var fmedituser = $('#fmedituser').validate({
        rules: {
            Phone: { required: true },
            FirstName: "required",
            LastName: "required",
            Address1: "required",
            City: "required",
            State: "required",
            Postal: "required",
            ShipFirstName: "required",
            ShipLastName: "required",
            ShipAddress1: "required",
            ShipCity: "required",
            ShipState: "required",
            ShipPostal: "required"
        },
        errorPlacement: function(error, element) { }
    });

    var payment_form = $('#payment_form').validate({
        rules: {
            FirstName: "required",
            LastName: "required",
            CCNumber: "required",
            CCType: "required",
            CSC: "required",
            strCCExpiresMonth: "required",
            strCCExpiresYear: "required"
        },
        errorPlacement: function(error, element) {
            /*
            var f = (error.attr('for'));
            if ($('#msg label[for=' + f + ']').length == 0)

                error.prependTo('#msg');
            // alert($('#msg').html());*/
        }

    });

}


function chkSaveAsShipping_click() {
    if ($('#chkSaveAsShipping').attr('checked')) {
        $(document.fmedituser.FirstName).val($(document.fmedituser.ShipFirstName).val());
        $(document.fmedituser.LastName).val($(document.fmedituser.ShipLastName).val());
        $(document.fmedituser.Address1).val($(document.fmedituser.ShipAddress1).val());
        $(document.fmedituser.Address2).val($(document.fmedituser.ShipAddress2).val());
        $(document.fmedituser.State).val($(document.fmedituser.ShipState).val());
        $(document.fmedituser.City).val($(document.fmedituser.ShipCity).val());
        $(document.fmedituser.Postal).val($(document.fmedituser.ShipPostal).val());
    }
}

function hovertext_clear(obj) {
    var val = $.trim($(obj).val());
    if (val == $(obj).attr('title')) {
        $(obj).val('');
    }
}
function hovertext_blur(obj) {
    var val = $.trim($(obj).val());

    if (val.length == 0 || val == $(obj).attr('title')) {
        $(obj).val($(obj).attr('title'));
        $(obj).removeClass('cfocus');
        $(obj).addClass('cblur');

    }
}

function hovertext_focus(obj) {
    var val = $.trim($(obj).val());


    $(obj).removeClass('cblur');
    $(obj).addClass('cfocus');
    if (val == $(obj).attr('title')) {
        $(obj).val('');

    }
}


function UpdateTime() {

    //  return;
    //FRIDAY, 15 MAY, 2009 10:32AM
    var days = new Array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
    var months = new Array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
    months = new Array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
    var d = new Date();
    $('.pltime').html(days[d.getDay()] + ' ' + d.getDate() + ' ' + months[d.getMonth()] + ', ' + d.getFullYear() + ' ' + d.toLocaleTimeString());

    if ((new Date()).getSeconds() % 59 == 0) {
        $('.pltime').fadeOut(500, function() {
            $('.pltime').fadeIn(200);
        });
    }

    setTimeout('UpdateTime()', 1000);
}


function FillFields(box) {
    if (box.checked == false) { return; }

    document.FrontPage_Form1.ShipFirstName.value = document.FrontPage_Form1.FirstName.value;
    document.FrontPage_Form1.ShipLastName.value = document.FrontPage_Form1.LastName.value;

    document.FrontPage_Form1.ShipAddress1.value = document.FrontPage_Form1.Address1.value;
    document.FrontPage_Form1.ShipAddress2.value = document.FrontPage_Form1.Address2.value;
    document.FrontPage_Form1.ShipCity.value = document.FrontPage_Form1.City.value;
    document.FrontPage_Form1.ShipState.value = document.FrontPage_Form1.State.value;
    document.FrontPage_Form1.ShipPostalCode.value = document.FrontPage_Form1.PostalCode.value;

    document.FrontPage_Form1.ShipCountry.value = document.FrontPage_Form1.Country.value;
}

function ChangeCountry(index, drpid) {
    if (index > 57) {
        $('#' + drpid).val('Canada');
    } else {
        $('#' + drpid).val('USA');
    }
}


function InitFormFields() {
    if ($('.autofields').html()) {
        if (!$('.autofields input[name=_fieldOrder]').attr('type')) {
            var fields = ',';
            //alert($('.autofields :input').length);
            $('.autofields :input').each(function(i) {
                var fname = $('.autofields :input').eq(i).attr('name');
                if (fname) {
                    if (fname.substr(0, 1) != '_' && fields.indexOf(',' + fname + ',') == -1 && (',form_redirect,submit,submitbutton,FORM_ID,NEXT_PAGE,').indexOf(',' + fname + ',') == -1) {
                        fields = fields + fname + ',';
                    }
                }
            });
            fields = fields.substr(1);
            if (fields.substr(fields.length - 1) == ',') {
                fields = fields.substr(0, fields.length - 1);
            }

            $('.autofields').append('<input type="hidden" name="_fieldOrder" value="' + fields + '" /> ');
        }
    }
}


$(document).ready(function(){
	if($("#homeslideshow").length>0)
	{
		
		setTimeout(function(){
			$('#homeslideshow img:first').fadeIn(1500);
			$('#homeslideshow').cycle();
		}, 1500);
	}
});