/* JQUERY PREVENT CONFLICT */
(function($) {
/*  ------------------------------------------------------------------
    Load X-Cart Informations ----------------------------------------- */
    $.fn.loadXCart = function() {
        return this.each( function() {
            var $topnav = $('#topnav');

            $.ajax({
                url: "/shop/cart_contents.php",
                dataType: 'json',
                success: function(msg){ 
                    //Cart Items
                    var items = (msg.cartitems > 1) ? msg.cartitems + ' articles' : msg.cartitems + ' article';
                    $('#items', $topnav).html(items);

                    // bind popin on correct flag if any item in cart
                    if(msg.cartitems > 0) {
                        $("a.popin_bind","ul.currency").bind('click', function(e){

                            var href = $(this).attr("href");
                            // create a modal dialog with the data
                            $.get('/switch_currency?url='+escape(href),
                                function(msg){
                                    // create a modal dialog with the data
                                    $('<div id="popin"></div>').modal({
                                        closeClass: 'b_close',
                                        minWidth: 485,
                                        minHeight: 220,
                                        overlayId: 'overlay',
                                        onShow:function (dialog) {
                                            //Show overlay
                                            dialog.overlay.show();

                                            //Add content
                                            $("#popin").html(msg);
                                        }
                                    });
                                }
                            );
                            e.preventDefault();
                            //alert("test");
                        });
                    }

                    // Login/Logout Button
                    if(msg.loginstatus){
                        html = '<a href="/shop/include/login.php?mode=logout" rel="nofollow">D&eacute;connexion</a>';
                    }else{
                        html = '<a href="/shop/error_message.php?need_login" rel="nofollow">Identifiez-vous</a>';
                    }

                    $('#login', $topnav).html(html);

                    //Show Cart
                    $topnav.fadeIn();
                }
            });
        });
    };

	
    /*  -----------------------------------------------------------------------
        Validate Functions ---------------------------------------------------- */
                
        //Regular Expression
        var reg_email = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,5})+$/i;
        var reg_phone = /^((1)?(\s)?(\+)?(\s)?(\()?\d{3}(\))?\s?-?\d{3}\s?-?\d{4}((\s)+(ext|x|extension|#)?(\s)?(\d)+)?)?$/i;
        
        //Validate Function
        $.fn.isValid = function() {
            $form  = $(this);
            var rulesCache = {};
            var elements = $form
            .find(":text, textarea")
            .filter(function() {
                if ( this.name in rulesCache )
                    return false;
                
                rulesCache[this.name] = true;
                return true;
            });
            
            // Reset the validation
            var error=0;
            $(".error").removeClass('error');
            
                // Validate the form
                $(elements).filter(".v-required").each( function(){
                    if($.trim($(this).val()) == ''){ error++; $("label[for=" + this.id + "]").addClass("error"); $(this).addClass("error"); }
                });
                if(error == 0){
                    $(elements).filter(".v-email").each( function(){
                        if(!checkRegExp($(this).val(), reg_email)){ error++; $("label[for=" + this.id + "]").addClass("error"); $(this).addClass("error"); }
                    });
                    $(elements).filter(".v-phone").each( function(){
                        if(!checkRegExp($(this).val(), reg_phone)){ error++; $("label[for=" + this.id + "]").addClass("error"); $(this).addClass("error");  }
                    });
                    if(error != 0){
                        $form.find('fieldset:first').prev().addClass('error').html('Oh-oh. Ils sembleraient que certains champs obligatoires requi&egrave;rent votre attention.');
                    }
                }else{
                    $form.find('fieldset:first').prev().addClass('error').html("Oups! Tous les champs marqu&eacute;s d'un <span class='required'>*</span> sont obligatoires.");
                }
            
            return error == 0 ? true : false;
            
        };
        
        function checkRegExp(value, pattern){
            objRegExp = new RegExp(pattern);
            return objRegExp.test(value);
        };
		
	/*  -------------------------------------------------------------------
        SWFObject 2.1 ----------------------------------------------------- */
        var flashvars = {
            moviePath: "/flash/fr/"
        };
        var params = {
            wmode: "transparent",
            play:'true',
            allowFullScreen:'false',
            allowScriptAccess: "always"
        };
        var attributes = {};
        if (swfobject.hasFlashPlayerVersion("9.0.0")) {
            swfobject.embedSWF("/flash/preloader.swf", "swf_home", "960", "355", "9.0.0", null, flashvars, params, attributes);
            swfobject.embedSWF("http://www.youtube.com/v/s5P0lSVbpZo&hl=en_US&fs=1&rel=0&hd=1", "swf_youtube", "395", "254", "9.0.0", null, null, { wmode: "transparent" }, null);
        }else{
            //Alternate Flash Version
            $("#swf_youtube").show();
            $("#swf_home").show();
        }	
	
})(jQuery);


