function registrationUpdateRetailerTip(){
    
    if ($('#reg_retailer').is(':checked')) {
        $('#reg_retailer_tip').slideDown();
    }
    else {
        $('#reg_retailer_tip').slideUp();
    }
}

function updateElement(oElement, sUrl) {
    
    oElement.attr('disabled', 'disabled');

    $.get(sUrl, {},
        function(sResponse){
            oElement.html(sResponse);
            oElement.removeAttr('disabled');
        }
    );
}

function updateProductType(oElement){

    $(oElement).next().children('option[value=0]').attr('selected', true);
    updateElement($(oElement).next(), sBaseUrl + 'ajax/product-type-options/id/' + $(oElement).val());
    updateBrand($(oElement).next());
}

function updateBrand(oElement){
    
    oElement = $(oElement);
    var sUrl = sBaseUrl + 'ajax/brand-options';
    
    var iCategoryId = oElement.prev().val();
    if (isPositiveInt(iCategoryId)){
        sUrl += '/category_id/' + iCategoryId;
    }
    
    var iProductTypeId = oElement.val();
    if (isPositiveInt(iProductTypeId)){
        sUrl += '/type_id/' + iProductTypeId;
    }
    
    updateElement(oElement.next(), sUrl);
}

function toCatalog(eButton){
    
    var eBrandId = $(eButton).prev();
    var eProductTypeId = $(eBrandId).prev();
    var eCategoryId = $(eProductTypeId).prev();
    var eRootCategoryId = $(eCategoryId).prev();
    
    iBrandId = eBrandId.val();
    iProductTypeId = eProductTypeId.val();
    iCategoryId = eCategoryId.val();
    iRootCategoryId = eRootCategoryId.val();
    
    var sUrl = sBaseUrl + 'shop'; 
    var sUrlParams = '';
    
    if (isPositiveInt(iCategoryId)){
        sUrlParams += '/category/' + iCategoryId;
    }
    else if (isPositiveInt(iRootCategoryId)) {
        sUrlParams += '/category/' + iRootCategoryId;
    }
        
    if (isPositiveInt(iProductTypeId)){
        sUrlParams += '/type/' + iProductTypeId;
    }
    
    if (isPositiveInt(iBrandId)){
        sUrlParams += '/brand/' + iBrandId;
    }
    
    if (sUrlParams != ''){    
        sUrl += '/by' + sUrlParams;
    }
    
    window.location = sUrl;
    return false;
}

function isPositiveInt(sString){
    
    if (typeof sString != 'string'){
        return (sString > 0);
    }
    
    var iString = parseInt(sString);
    
    if (isNaN(iString)){
        return false;
    }
    
    return (iString > 0); 
}

function checkAdressesForm(eForm) {
    
    if (!isPositiveInt($(eForm).find("input[name=billing_address_id]:checked").val())){
        alert('Please select billing address');
        return false;
    }
    
    if (!isPositiveInt($(eForm).find("input[name=shipping_address_id]:checked").val())){
        alert('Please select shipping address');
        return false;
    }
    
    return true;
}

function slideToggle(el, bShow){
    var $el = $(el), height = $el.data("originalHeight"), visible = $el.is(":visible");

    // if the bShow isn't present, get the current visibility and reverse it
    if( arguments.length == 1 ) bShow = !visible;

    // if the current visiblilty is the same as the requested state, cancel
    if( bShow == visible ) return false;

    // get the original height
    if( !height ){
        // get original height
        height = $el.show().height();
        // update the height
        $el.data("originalHeight", height);
        // if the element was hidden, hide it again
        if( !visible ) $el.hide().css({height: 0});
    }

    // expand the knowledge (instead of slideDown/Up, use custom animation which applies fix)
    if( bShow ){
        $el.show().animate({height: height}, {duration: 250});
    } else {
        $el.animate({height: 0}, {duration: 250, complete:function (){
                $el.hide();
            }
        });
    }
}

function paymentShowProperFields(sCardType){
    
    if (sCardType == 'VISA' || sCardType == 'MC' || sCardType == 'AMEX') {
        slideToggle('#pay_cvn_container', true);
    }
    else {
        slideToggle('#pay_cvn_container', false);
        $('#pay_cvn').val('');
    }
    
    if (sCardType == 'SWITCH') {
        slideToggle('#pay_issueno_container', true);
    }
    else {
        slideToggle('#pay_issueno_container', false);
        $('#pay_issueno').val('');
    }
}


$(document).ready(function(){
    
    var mapsElement = document.getElementById('googlemaps');
    
    if (mapsElement) {        
        var map = new GMap2(mapsElement);            
        var center = new GLatLng(52.2590038569466, -7.111823558807373);                            
        map.setUIToDefault();        
        map.setCenter(center, 12);
        map.addOverlay(new GMarker(center));
    }
});
