$.isEmail = function (email){
	var result = email.search(/^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z]{2,3})+$/);
	if(result > -1){return true;} else {return false;}
}

$(document).ready(function(){
    $.bindFancybox();
    $('.readmore').live('click',function(e){
            e.preventDefault()
            $(this).parent().css('display','none');
            $(this).parent().next().css('display','block');
    });
    $('.readless').click(function(e){
            e.preventDefault()
            $(this).parent().css('display','none');
            $(this).parent().prev().css('display','block');
    });


    $('#fldsearch').live('keyup',function(e){
        $.fancybox.showActivity();
        data = {ajax:1,query:$('#fldsearch').val()};
        $.post(root+'/products.html',data,function(data){
            $.fancybox.hideActivity();
            
            $('#content').html(data.content);
        },'json')
    });
    $('#showaccessoriesmenu').live('mouseover',function(e){
        $('.arrows',$(this).parent().parent()).addClass('hover');
    }).live('mouseout',function(e){
        if(!$(this).hasClass('active'))
            $('.arrows',$(this).parent().parent()).removeClass('hover');
    });
    $('#showaccessoriesmenu').live('click',function(e){
        e.preventDefault();
        $('#menubar .item a').removeClass('active');
        $(this).addClass('active');        
        if($('#submenuaccessoires').is(':visible')){
            $('#submenuaccessoires').hide();
            $('.arrows',$(this).parent().parent()).removeClass('active');
            $('.arrows',$(this).parent().parent()).addClass('hover');
        }else{
            $('#submenuaccessoires').show();
            $('.arrows',$(this).parent().parent()).removeClass('hover');
            $('.arrows',$(this).parent().parent()).addClass('active');
        }
    });
       
    $('select').each(function(id,field){
        if($(field).attr('val'))
            $(field).val($(field).attr('val'));
    });   
    $('.firstmenuitem').mouseover(function(){
        if(!$(this).hasClass('active')){
            $('.first img.visible',$(this).parent().parent()).css('display','block');
            $('.first img.invisible',$(this).parent().parent()).css('display','none');
        }
    }).mouseout(function(){
        if(!$(this).hasClass('active')){
            $('.first img.visible',$(this).parent().parent()).css('display','none');
            $('.first img.invisible',$(this).parent().parent()).css('display','block');
        }
    });

    
    $('#email').focus(function(e){
        $('#validemailmsg').hide(300);
    });
    $('#password,#password_confirm').focus(function(e){
        $('#validpassword').hide(300);
    });
    $('.paymethod').focus(function(e){
        $( '#selpaymethod').hide(300);
    });
    showAddress = function(){
        $('#billing_addr').css('display',$(this).is(':checked')?'none':'block');
        if($(this).is(':checked'))
            $('.ship').val('');
    }
    showPassword = function(){
        $('#set_password').css('display',$(this).is(':checked')?'block':'none');
    }
    $('#fldsameasbilling').live('click',showAddress).live('focus',showAddress).live('blur',showAddress);
    $('#create_account').live('click',showPassword).live('focus',showPassword).live('blur',showPassword);

    $('input[type=text],input[type=radio],input[type=password],select').focus(function(){
        if(!$(this).hasClass('nofocusborder')){
            $(this).css({
                'background-color':'white',
                'border-style':'solid',
                'border-color':'#ABC3D7',
                'border-width':'1px;'
            });
        }
    });
    isEmpty = function(elementName){
        if($.trim($("#"+elementName).val())=='')
            return true;
    }
    markInvalid = function(elementId){
        $("#"+elementId).css({
            'border-style':'dashed',
            'border-color':'red',
            'border-width':'1px',
            'background-color':'#FAEBE7'
        });
    }
    $('.news_signup').live('click',function(e){
        e.preventDefault();
        if($('.news_signup:selected').val())
            state = $('.news_signup:checked').val();
        else if($('.news_signup:checked').val())
            state = $('.news_signup:checked').val();
        else
            state = $('#signup_state').val();             
        
        if(($('#fld_newsname').val()==$('#fld_newsname').attr('title')||$.trim($('#fld_newsname').val()=='')) && state=='in'){            
                $('.news_signup').attr('checked',false);
                markInvalid('fld_newsname');
                return alert('Geef alstublieft uw naam op.');
        }if($('#fld_newsemail').val()==$('#fld_newsemail').attr('title')){
            $('.news_signup').attr('checked',false);
            markInvalid('fld_newsemail');
            return alert('Geef alstublieft uw e-mailadres op.');
        }if(!$.isEmail($('#fld_newsemail').val())){
            $('.news_signup').attr('checked',false);
            markInvalid('fld_newsemail');
            return alert('Geef alstublieft een geldig e-mailadres op.');
        }
        $.fancybox.showActivity();
        send = {    _do     :   'newsletter',
                    action  :   state,
                    email   :   $('#fld_newsemail').val(),
                    name    :   $('#fld_newsname').val(),
                    inout   :   state,
                    ajax    :   1
                };
        $.post(root+'/ajax.php',send,function(data){
            if(data.inout=='in')
                $('#signedinmsg').css('display','block');
            else
                $('#signedoutmsg').css('display','block');            
            $('#news_signupform').css('display','none');
            $.fancybox.hideActivity();
        },'json');
        
    });

    $(".accordion" ).accordion();

    $('.add_basket').live('click',function(e){
        e.preventDefault();
        $.fancybox.showActivity();
        if($('#fld_quantity').val()==undefined)
            quantity = 1;
        else            
            quantity = $('#fld_quantity').val()?$('#fld_quantity').val():1;
            
        id = $('#fld_id').val()?$('#fld_id').val():$(this).attr('rel');
        optioncount = 0;
        $('.options').each(function(i,e){            
            if($(e).val()){
                optioncount = optioncount + 1;
                optsend = {quantity:1,id:$(e).val(),_do:'order_product',ajax:1};
                $.post(root+'/ajax.php',optsend,function(data){
                    $.fancybox.hideActivity();
                    $('#cart_items').html(data.cart_items);
                },'json');
            }
        });
        send = {quantity:quantity,id:id,_do:'order_product',ajax:1,optioncount:optioncount};
        
        $.post(root+'/ajax.php',send,function(data){
            $.fancybox.hideActivity();            
            $('#cart_items').html(data.cart_items);
            window.location = root+'/cart.html';
            /*
            if(data.optioncount==1){
                alert('Wij hebben 1 product en '+data.optioncount+' optie toegevoegd aan uw winkelmandje');
            }else if(data.optioncount>1){
                alert('Wij hebben 1 product en '+data.optioncount+' opties toegevoegd aan uw winkelmandje');
            }else{
                alert('Het product is toegevoegd aan uw winkelmandje');
            }
            */
        },'json');       
    });
   $('.focusblur').live('focus',function(e){
      if($(this).val()==$(this).attr('title'))
          $(this).val('');
   }).live('blur',function(e){
      if($.trim($(this).val())=='')
          $(this).val($(this).attr('title'));
   });
});
