﻿$(document).ready(function() {
    ShowPages();
    //new win link
    $('a[target="_blank"][class="newwin"]').addClass('newwin');

    //fmcomment_init
    fmcomment_init();

    //images
    if ($('.entryimage img').length > 0) {
        if ($('.entryimage img').length > 1) {
            var html = '';
            html += '<div class="entryimage_th">' + $('.entryimage').html() + '<\/div>';
            html += '<div class="entryimage_main"><img id="imgMain" src="' + $('.entryimage img').eq(0).attr('src').replace('/th_', '/med_') + '" alt="' + $('.entryimage img').eq(0).attr('alt') + '" /><\/div>';

            $('.entryimage').html(html);
            $('.entryimage_th img').click(function() {
                $('.entryimage_th div').removeClass('on');
                $(this).parent().addClass('on');
                $('#imgMain').attr('src', $(this).attr('src').replace('/th_', '/med_'));
                $('#imgMain').attr('alt', $(this).attr('alt'));
            });
            //$('.entryimage_th div:eq(0)').css('border', '1px solid #999');
        }
    }

    //tr01
    $('.tr01 tr:odd').addClass('tr1');
    $('.tr01 tr:even').addClass('tr0');

    $('.tr10 tr:odd').addClass('tr0');
    $('.tr10 tr:even').addClass('tr1');

    //shippingtype
    $('#drpShippingType').change(function() {
        ShowTotal();
    });
    ShowTotal();

    //htext
    htext_init();

    //plgtcf
    $('.plgtcf').click(function() { location.href = '/contact_form.aspx?subject=' + document.title });
});

function fmcomment_init() {
    var arr = $.trim($('.plfmComment').text());
    if (arr.length > 0) {
        arr = arr.split(',');
        //[EntryID],[CookieAuthor],[Nickname],[CookieLink],[Homepage or Email],[Comments here],[Submit]
        $('.plfmComment').html('<fo' + 'rm act' + 'ion="/co' + 'mment.aspx" met' + 'hod="po' + 'st" id="fmComment"><label><input name="author" type="text" value="' + getCookie("Author") + '" title="' + arr[1] + '" class="htext" /></label><label><input name="link" type="text" value="' + getCookie("Link") + '"  title="' + arr[2] + '" class="htext"  /></label><label><textarea cols="50" rows="4" name="content" id="txtContent" title="' + arr[3] + '" class="htext" ></textarea></label><input name="submit" value="' + arr[4] + '" type="submit" class="clearhtext" /><input name="entryid" value="' + arr[0] + '" type="hidden" /><input name="quoteid" id="txtQuoteID" value="0" type="hidden" /></form>');
        //comment form
        $('#txtContent').focus(txtContent_focus);
    }

    //comments
    if ($.trim($('.comments').html()).length > 0) { $('.comments').css('border-top', '1px solid #bdbdbd'); }
    $('.lblquote').click(function() { lblquote_click(this) });
    $('.liauthors div').click(function() { liauthors_click(this) });
}


function getCookie(arg_CookieName) {
    var fromN = document.cookie.indexOf(arg_CookieName + "=") + 0;
    if ((fromN) != -1) {
        fromN += arg_CookieName.length + 1;
        var toN = document.cookie.indexOf(";", fromN) + 0;
        if (toN == -1) {
            toN = document.cookie.length
        }
        var re = document.cookie.substring(fromN, toN);

        re = decodeURI(re);

        re = unescape(re);

        re = re.replace(/\+/g, ' ');
        return re; //
    }
    return '';
}

function htext_init() {
    $('.htext').each(function(i) { htext_blur($('.htext').eq(i)); });
    $('.htext').focus(function() { htext_focus(this); });

    $('.htext').blur(function() { htext_blur(this); });
    $('.clearhtext').click(function() {
        $('.htext').each(function(i) {
            htext_clear($('.htext').eq(i));
        });
    });
}


function htext_clear(obj) {
    var val = $.trim($(obj).val());
    if (val == $(obj).attr('title')) {
        $(obj).val('');
    }
}
function htext_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 htext_focus(obj) {
    var val = $.trim($(obj).val());

    $(obj).removeClass('cblur');
    $(obj).addClass('cfocus');
    if (val == $(obj).attr('title')) {
        $(obj).val('');
    }
}


function liauthors_click(obj) {
    var color = $(obj).attr('class').substr(3);

    $('.ulcomments .ca_' + color).parents('li').toggleClass('ht1');

    if ($(obj).css('font-weight') == 'bold') {
        $(obj).css({ 'font-weight': 'normal', 'font-style': 'normal' });
    } else {
        $(obj).css({ 'font-weight': 'bold', 'font-style': 'italic' });
    }
}
function lblquote_click(obj) {
    htext_clear($('#txtContent'));
    var id = parseInt($(obj).attr('id').substr(('lblquote_').length)) - 1;
    var cid = $(obj).attr('class').substr(10);
    $('#txtQuoteID').val(cid);

    var text = $('.ulcomments li .comment_author').eq(id).text() + ': ' + $.trim($('.ulcomments li .comment_content').eq(id).text().replace(/\[.*?\]/g, '').replace(/\]|\[/g, ''));

    if (text.length > 100) {
        text = text.substr(0, 100);
        if (text.lastIndexOf(' ') > (text.length - 10)) {
            text = text.substr(0, text.lastIndexOf(' '));
        }
        if (text.lastIndexOf('.') > (text.length - 10)) {
            text = text.substr(0, text.lastIndexOf('.'));
        }
        if (text.lastIndexOf(',') > (text.length - 10)) {
            text = text.substr(0, text.lastIndexOf(','));
        }
        if (text.lastIndexOf('?') > (text.length - 10)) {
            text = text.substr(0, text.lastIndexOf('?'));
        }

        text = text + ' ...';
    }

    text = '[' + text + ']';

    $('#txtContent').val(text + '\n' + $('#txtContent').val());
    $('#txtContent').focus();
}

function ShowTotal() {
    var value = $('#drpShippingType option[value=' + $('#drpShippingType').val() + ']').attr('title');
    if (value) {
        if (value.length > 0) {
            $('#txtTotal').val($('#lblSubtotal').html() + " + " + value);
        }
        else {
            $('#txtTotal').val('');
        }
    }
}

function InitContactForm() {
    $(document).ready(function() {
        var form_contact = $("#form_contact").validate({
            rules: {
                Name: "required",
                Email: {
                    required: true,
                    email: true
                },
                Code: "required"
            }
        });
    });
}

function InitCheckout() {
    $(document).ready(function() {
        var form_checkout = $("#form_checkout").validate({
            rules: {
                Fullname: "required",
                Phone: "required",
                Email: {
                    required: true,
                    email: true
                },
                ShippingType: "required",
                Shipping: "required",
                CardHolder: "required",
                CardNumber: "required",
                CardType: "required",
                ExpairedYear: "required",
                ExpairedMonth: "required",
                SecurityCode: "required"
            }
        });
    });
}


function HighLight() {
    $(document).ready(function() {
        try {
            var arr = HightlightStr.split(",");
            for (var i = 0; i < arr.length; i++) {
                if (arr[i].length > 0) {
                    if ($('.c_' + arr[i]).html()) {
                        $('.c_' + arr[i]).attr('class', 'on');
                        return;
                    }
                }
            }
        } catch (e) { }
        $('.c_home').attr('class', 'on');
    });
}

function txtContent_focus() {
    if ($('#fmComment').html().toLowerCase().indexOf('<img') == -1) {
        $('#txtContent').parent().after('<label><input name="code" type="text" value="" size="3" /><img src="/code.aspx" alt="" /></label>');
    }
}



function ShowPages() {
    var loc = location.href.toLowerCase();
    loc = loc.substr(loc.lastIndexOf('/') + 1);
    loc = loc.substr(0, loc.indexOf('.'));
    if (loc.length == 0) loc = 'default';
    var curi = -1;
    var len = $('.ulpages_fm li').length;
    if (len == 0) {
        return;
    }

    var curli = $('.ulpages_fm a[href$=/' + loc + '.aspx]').parent();
    if (loc == 'default') {
        curli = $('.ulpages_fm a[href$=/]').parent();
    }

    $(curli).html('<strong>' + $(curli).text() + '</strong>');
    curi = $('.ulpages_fm li').index(curli);

    var padding = 3;
    var posb = curi - padding;
    var pose = curi + padding;

    if (posb <= 0) {
        pose = pose - posb + 1;

    }
    if (pose >= len - 1) {
        posb = posb - (pose - len) - 2;
    }

    for (var i = 0; i < len; i++) {
        if ((i >= posb && i <= pose) || i == 0 || i == len - 1) {
            $('.ulpages_fm li').eq(i).addClass('s');
        }
    }
    $('.ulpages_fm li[class!=s]').remove();

    if (curi > padding + 1) {
        $('.ulpages_fm li:first').after('<li class="s">...</li>');
    }
    if (curi < len - padding - 1) {
        $('.ulpages_fm li:last').before('<li class="s">...</li>');
    }
    return;
}


function ShowPages0() {
    var loc = location.href.toLowerCase();
    loc = loc.substr(loc.lastIndexOf('/') + 1);
    loc = loc.substr(0, loc.indexOf('.'));
    if (loc.length == 0) loc = 'default';
    var curi = -1;

    if ($('.ulpages_fm li').length == 0) {
        return;
    }

    var curli = $('.ulpages_fm a[href$=/' + loc + '.aspx]').parent();

    $(curli).html('<strong>' + $(curli).text() + '</strong>');
    curi = $('.ulpages_fm li').index(curli);

    var hidel = false;
    var hider = false;

    var posl = 3;
    var posr = 3;

    if (curi < posl + 2) {
        posr = posr + posl - curi + 1;
    }

    if (curi > ($('.ulpages_fm li').length - posr - 3)) {
        posl = posl + posr - $('.ulpages_fm li').length + curi + 2;
    }

    if ((curi - posl) > 1) {
        hidel = true;
    }

    if (($('.ulpages_fm li').length - 1) > (curi + posr + 1)) {
        hider = true;
    }

    $('.ulpages_fm li:first').addClass('s');
    $('.ulpages_fm li:last').addClass('s');

    for (var i = curi - posl; i < curi + posr + 1; i++) {
        $('.ulpages_fm li').eq(i).addClass('s');
    }

    if (hidel) {
        $('.ulpages_fm li:first').after('<li>...</li>');
    }
    if (hider) {
        $('.ulpages_fm li:last').before('<li>...</li>');
    }
    $('.ulpages_fm li').eq(1).addClass('s');
    $('.ulpages_fm li').eq($('.ulpages_fm li').length - 2).addClass('s');

    $('.ulpages_fm li[class!=s]').remove();
}