var URL = getURL();
function getURL() {
    var metas = document.getElementsByTagName('meta');
    for (var i = 0; i < metas.length; i++) if (metas[i].getAttribute('name') == 'url') break;
    return metas[i].getAttribute('content');
}
function mt(name, domain, subject, body) {
    location.href = 'mailto:' + name + '@' + domain + '?subject=' + subject + '&body=' + body;
}

/* PNG */
if (typeof(DD_belatedPNG) == 'object') DD_belatedPNG.fix('#menuFlash li, .ct, .cb, #logo img, .article .img em, .articles .img em, .articles-h .img em, .recipes .icon em, .product .img img, .recipes .icon img, .products li img, #tooltip h3');

/* SWF */
var distributorsVars = {
    content: URL+'swf/distributors.swf',
    lang:  $('#curLang').val(),
    data: URL+'modules/xml/ajax/public.xml.php?a=gen_distributors',
    dataFolder: URL+'swf/data/',
    ajaxFolder: URL+'modules/'
};
swfobject.embedSWF(URL+'swf/loader.swf', 'distributorsFlash', '830', '570', '9.0.0', URL+'swf/expressInstall.swf', distributorsVars, {wmode: 'opaque'});

var validateMessages = {};

$(function(){
// lightBox
    $('.awards .i a').lightBox({
        overlayBgColor: '#000',
        overlayOpacity: 0.6,
        imageBlank: URL+'img/px.gif',
        imageLoading: URL+'img/lb/loading.gif',
        imageBtnClose: URL+'img/lb/close.gif',
        imageBtnPrev: URL+'img/lb/prev.gif',
        imageBtnNext: URL+'img/lb/next.gif',
        containerResizeSpeed: 350,
        txtImage: lb.txtImage,
        txtOf: lb.txtOf
    });

// tooltip
    $('.products a').tooltip({
        showURL: false,
        track: true,
        fixPNG: true,
        delay: 0
    });

// search
    var search_input = $('#bottom #search input:text, #footer #search input:text');
    var search_holder = search_input.val();
    search_input.addClass('holder').focus(function(){
        if ($(this).val() == search_holder) {
            $(this).val('').removeClass('holder');
        }
    }).blur(function(){
        if ($(this).val() == '') {
            $(this).addClass('holder').val(search_holder);
        }
    });

// feedback
    $('.feedback input:text, .feedback textarea').focus(function(){
        $(this).prev().hide();
    }).blur(function(){
        if ($(this).val() == '') $(this).prev().show();
    });
    $('.feedback select').focus(function(){
        $(this).removeClass('holder');
    }).blur(function(){
        if ($(this).val() == '') $(this).addClass('holder');
    });

    $('.feedback').validate({
        messages: validateMessages,
        highlight: function(element, errorClass) {
            $(element).addClass(errorClass).prev('label').addClass('holder-error');
        },
        unhighlight: function(element, errorClass) {
            $(element).removeClass(errorClass).prev('label').removeClass('holder-error');
        }
    });


// quick jump
    $('#navi1').change(function(){
        quickJump(this, 2, URL+'modules/navigation/ajax/public.navigation.php?a=getsub');
    });
    $('#navi2').change(function(){
        quickJump(this, 3, URL+'modules/catalog/ajax/public.catalog.php?a=getitems');
    });
    $('#navi3').change(function(){
        quickJump(this, 0);
    });

// rate
    $('#setMark a').click(function(){
        postMark($(this).attr('title'), $(this).attr('rel'));
        $('#setMarkWrap').fadeOut('fast');
        return false;
    });
});


function quickJump(selectBox, nextNum, scriptUrl) {
    var form = $(selectBox.form);
    var selected = $(selectBox).find(':selected');
    var selectedId = selected.attr('id').substr(3);
    var selectedUrl = selected.val();
    var goToUrl = selectedUrl;
    var submitButton = $('#quickWrap input:image');
    var nextSelectBox = $('#navi'+nextNum);

    if (nextNum == 0) {
        if (selectedUrl == '')
            goToUrl = $('#navi2').find('input:selected').val();
        else
            goToUrl = selectedUrl + 'id' + selectedId + '/';
    } else {
        $.ajax({
            type: 'POST',
            url: scriptUrl,
            dataType: 'text',
            data: 'id='+selectedId,
            dataType: 'xml',
            success: function(xml) {
                var resultHTML = '<option></option>';
                $(xml).find('nav').each(function(){
                    var id = $(this).find('id').text();
                    var url = $(this).find('url').text();
                    var title = $(this).find('title').text();
                    resultHTML += '<option value="'+url+'" id="nav'+id+'">'+title+'</option>';
                });
                nextSelectBox.html(resultHTML);
            }
        });
        if (selectedUrl == '') {
            if (nextNum == 2) {
                $('#navi2wrap, #navi3wrap').hide('fast');
                submitButton.hide('fast');
            } else if (nextNum == 3) {
                $('#navi3wrap').hide('fast');
                goToUrl = $('#navi1').find('input:selected').val();
            }
        } else {
            submitButton.show('fast');
            nextSelectBox.parent().show('fast');
        }
    }

    form.unbind().submit(function(){
        window.location = goToUrl;
        return false;
    });
}


function postMark(mark, id) {
    $.ajax({
        type: 'POST',
        url: URL+'modules/catalog/ajax/public.catalog.php?a=count_rating',
        dataType: 'text',
        data: 'c='+mark+'&id='+id,
        dataType: 'text',
        success: function(msg) {
            if (msg != '') {
                $('#rateValue').html(msg);
            }
        }
    });
}