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

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

function updateProductType(idCategory, idProductType){
    
    updateElement($(idProductType), sBaseUrl + 'ajax/product-type-options/id/' + $(idCategory).val());
}

function toCatalog(idBrand, idCategory, idProductType){
    
    var iBrandId = $(idBrand).val();
    var iProductTypeId = $(idProductType).val();
    var iCategoryId = $(idCategory).val();
    
    var sUrl = sBaseUrl + 'shop/by';
    
    if (!isPositiveInt(iBrandId)){     
        alert('Please select brand');
        return false;
    }
    
    if (!isPositiveInt(iCategoryId)){
        alert('Please select category');
        return false;
    }
    
    sUrl += '/category/' + iCategoryId;
    sUrl += '/brand/' + iBrandId;
    
    if (isPositiveInt(iProductTypeId)){
        sUrl += '/type/' + iProductTypeId;
    }
    
    window.location = sUrl;
    return true;
}

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('');
    }
}

function ajaxSubscribe(eForm){
   eForm = $(eForm);

   $.post(eForm.attr('action'),
      eForm.serialize(),
      function(data){

         if (data.result) {
         	eForm.prev('p').text(data.message);
            eForm.html('');            
         }
         else {
            alert(data.message);
         }
      },
      "json"
   );

   return false;
}

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