var emailDef = 'E-mail cím...';
var pwdDef = 'Jelszó...';
var searchDef = 'Keresés...';

String.prototype.trim = function() {  return this.replace(/^\s+|\s+$/g, '');  }
String.prototype.strip_tags = function() { return this.replace(/<\/?[^>]+>/gi, ''); }
String.prototype.stripNonNumeric = function() {
    var str = this;
    str += '';
    var rgx = /^\d|\.|-$/;
    var out = '';
    for( var i = 0; i < str.length; i++ ) {
        if( rgx.test( str.charAt(i) ) ){
            if( !( ( str.charAt(i) == '.' && out.indexOf( '.' ) != -1 ) || ( str.charAt(i) == '-' && out.length != 0 ) ) ){
                out += str.charAt(i);
            }
        }
    }
    return out;
}
Number.prototype.format = function(format) {
	var hasComma = -1 < format.indexOf(','),
        psplit = format.stripNonNumeric().split('.'),
        that = this;
    if (1 < psplit.length) {
	    that = that.toFixed(psplit[1].length);
    } else if (2 < psplit.length) {
	    throw('NumberFormatException: invalid format, formats should have no more than 1 period: ' + format);
	} else {
	    that = that.toFixed(0);
	}
	var fnum = that.toString();
	if (hasComma) {
	    psplit = fnum.split('.');
	    var cnum = psplit[0],
            parr = [],
            j = cnum.length,
            m = Math.floor(j / 3),
            n = cnum.length % 3 || 3;
	    for (var i = 0; i < j; i += n) {
            if (i != 0) { n = 3;}
            parr[parr.length] = cnum.substr(i, n);
            m -= 1;
	    }
	    fnum = parr.join(' ');
	    if (psplit[1]) { fnum += '.' + psplit[1]; }
	}
	return format.replace(/[\d,?\.?]+/, fnum)
};

function addCommas(nStr) {
    nStr += '';
    x = nStr.split('.');
    x1 = x[0];
    x2 = x.length > 1 ? '.' + x[1] : '';
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(x1)) {
        x1 = x1.replace(rgx, '$1' + ',' + '$2');
    }
    return x1 + x2;
}

function strip_tags($text){
    return $text.replace(/<\/?[^>]+>/gi, '');
}

function submitFormByUrl(_id) {
/*    var params = [];
    var i = 0;
    var f = $('#' + _id);
    $('input[type="text"], input[type="hidden"]', f).each(function(){
        if ($(this).val().trim() != "" && $(this).val().trim() != '0') {
            params[i] = $(this).attr('id') + '/' + $(this).val().trim();
            ++i;
        }
    });
    $('select option:selected', f).each(function(){
        if ($(this).val().trim() != "" && $(this).val().trim() != '0') {
            params[i] = $(this).parent('select').attr('id') + '/' + $(this).val().trim();
            ++i;
        }
    });
    window.location = f.attr('action') + '/' + params.join('/');
    return false;*/
    return true;
}

function checkDefValue(id, def, colordef, colorother) {
    var text = $('#'+id).val();
    if (text == '') {
        $('#'+id).val(def);
        text = def;
    }
    if (text == def) {
        $('#'+id).css('color', '#'+colordef);
    } else {
        $('#'+id).css('color', '#'+colorother);
    }
}

function checkFocus(id, def, colorother) {
    var text = $('#'+id).val();
    if (text == def) {
        $('#'+id).val('');
        $('#'+id).css('color', '#'+colorother);
    }
}

function bindDefValue(id, def) {
    $('#'+id).change(function(){ checkDefValue(id, def, 'AAAAAA', '800000'); });
    $('#'+id).focus(function(){ checkFocus(id, def, '800000'); });
    $('#'+id).blur(function(){ checkDefValue(id, def, 'AAAAAA', '800000'); });
    checkDefValue(id, def, 'AAAAAA', '800000');
}

function handleLogin() {
    if ($('#login_email').val() == '' || $('#login_email').val() == emailDef) {
        showUserMessage('Nem töltötte ki az E-mail cím mezőt!', 'error', 5);
        return false;
    }
    if ($('#login_passwd').val() == '' || $('#login_passwd').val() == pwdDef) {
        showUserMessage('Nem töltötte ki a Jelszó mezőt!', 'error', 5);
        return false;
    }
    return true;
}

function handleSearch() {
    if ($('#search').val() == '' || $('#search').val() ==  searchDef) {
        showUserMessage('Kérem adja meg a keresőkifejezést!', 'error', 5);
        return false;
    }
    if ($('#search').val().length < 3) {
        showUserMessage('A megadott keresőkifejezés túl rövid! (Legalább 3 karakter szükséges.)', 'error', 5);
        return false;
    }
    return true;
}

function toogleMenu(id) {
    if ($('#cat_menu_'+id).is(':visible')) {
        $('#cat_menu_'+id).hide();
    } else {
        $('[id*="cat_menu_"]').hide();
        $('#cat_menu_'+id).show();
    }
}

function bindHover(id) {
    var butt = $('#' + id);
    butt.hover(function(){
        var pic = $(this).attr('src');
        butt.attr('src', pic.replace('out', 'over'));
    },function(){
        var pic = $(this).attr('src');
        butt.attr('src', pic.replace('over', 'out'));
    });
}
function loadTAProduct() {
    $.ajax({
        url: baseUrl + '/product/randomajax',
        dataType: "xml",
        success: function(xml){
            if ($('product', xml).size() > 0) {
                var url = $('url', xml).text();
                var linkStart = '<a href="' + url + '" title="Termék megtekintése">';
                var linkEnd = '</a>';
                $('#ta_name').html(linkStart + $('producteur', xml).text() + " " + $('name', xml).text() + linkEnd);
                $('#ta_link').html(linkStart + 'bővebben &raquo;' + linkEnd);
                $('#ta_pic').html(linkStart + $('picture', xml).text() + linkEnd);
                $('#ta_price').html($('brutto', xml).text() + ' Ft');
            } else {
                $('#ta_name').html('Az oldal ezen szolgáltatása jelenleg nem elérhető.');
                $('#ta_link').html('');
                $('#ta_pic').html('');
                $('#ta_price').html('');
            }
            setTimeout('loadTAProduct()', 15000);
        }
    });
}

$(document).ready(function(){
    bindDefValue('login_email', emailDef);
    bindDefValue('login_passwd', pwdDef);
    bindDefValue('search', searchDef);
    
    $('.cat_show').click(function(){
        var id = $(this).attr('id').substring(9);
        toogleMenu(id);
    });
    $('.cat_menu').hide();
    
    loadTAProduct();
    
    var isCtrl = false;
    $('#specifikacio, #galeria, .productDiv .product_desc, #leiras').keyup(function (e){
        if(e.which == 17){
            isCtrl=false;
        }
    }).keydown(function (e){
        if(e.which == 17){
            isCtrl=true;
        }
        if( (e.which == 65 || e.which == 67) && isCtrl == true){
            return false;
        }
    });
    
    /**
     * LOCK RIGHT CLICK
     */
    /*
    var message="";
    function clickIE() {
        if (document.all) {
            return false;
        }
    }
    function clickNS(e) {
        if (document.layers||(document.getElementById&&!document.all)) {
            if (e.which==2||e.which==3) {
                return false;
            }
        }
    }
    if (document.layers) {
        document.captureEvents(Event.MOUSEDOWN);
        document.onmousedown=clickNS;
    } else {
        document.onmouseup=clickNS;
        document.oncontextmenu=clickIE;
    }
    document.oncontextmenu=new Function("return false");
    */
});
