var hideList = new Array();
hideList.push('#ctl00_FooterLang');
hideList.push('#ctl00_FooterModelsBox');
hideList.push('.menu-overlay');
hideList.push('#rechercher-liste-univers-box');
hideList.push('#rechercher-liste-modeles-box');
hideList.push('.list-box');
hideList.push('#iframeOverlay');
var hideInList;
var iframe;
/*04082009*/
$(document).ready(function() {
$('#ctl00_MainContent_produit_scroll_box').jScrollPane();
});

function createIFrame(overlay) {
    if ( !iframe) {
        iframe = document.createElement('iframe');
        $(iframe).attr('id', 'iframeOverlay');
        $(iframe).attr('allowtransparency', 'true');
        $(iframe).attr('frameborder', 'no');
        $(iframe).addClass('iframe');
    }
    
    if ($(overlay).position() != undefined) {
        $(iframe).css({
            'position': 'absolute',
            'border': '0 none',
            'left': $(overlay).position().left,
            'width': $(overlay).outerWidth(),
            'height' : $(overlay).outerHeight()
        });
        $('#content').append(iframe);
    }
}

function sleep(milliseconds) {
    var start = new Date().getTime();
    while (true) {
        if ((new Date().getTime() - start) > milliseconds) {
            break;
        }
    }
}

$(document).ready(function() {
    $('#page').css('marginTop', $(document).height() / 2 - $('#page').height() / 2);
    $('#page').css('visibility', 'visible');
    sleep(200);

    hideInList = function() {
        $(hideList).each(function(i) {
            $(hideList[i]).hide();
        });
    }
    
    // Elements positionning
    var ujaElements = function() {
        //if ( $(document).height() > 550 ) {
            $('#page').css('marginTop', $(document).height() / 2 - $('#page').height() / 2);
            $('#page').css('visibility', 'visible');
            $('#ctl00_FooterModelsBox').css({
                'left': $('#footer-models').position().left,
                'top': $('#ctl00_footer').position().top - $('#ctl00_FooterModelsBox').outerHeight() + 12
            });
            $('#ctl00_FooterLang').css({
                'left': $('#ctl00_AutresLanguesLink').position().left,
                'top': $('#ctl00_footer').position().top - $('#ctl00_FooterLang').outerHeight()
            });
        //}
    };
    
    jQuery(function( $ ){
	    $('#page').hide();
	    $.preload('#page img', {//the first argument is a selector to the images
		    onFinish:finish,
			threshold: 2 //'2' is the default, how many at a time, to load.
		});
	    function finish(){//hide the summary
		    $('#page').show();
		    $('#page img').pngfix();
		    ujaElements();
	    };
	});

    $('#page').show();

    ujaElements();
    $(window).resize(function() {
        ujaElements();                          
    });
    
    // Footer models / lang event
    $('#footer-models').click(function() {
        hideInList();
        var box = $('#ctl00_FooterModelsBox');
        var isHidden = $(box).is(':hidden');

        if ( isHidden ) { $(box).show(); }

        return false;
    });
    $('#ctl00_AutresLanguesLink').click(function() {
        hideInList();
        var box = $('#ctl00_FooterLang');
        var isHidden = $(box).is(':hidden');

        if ( isHidden ) { $(box).show(); }
        
        return false;   
    });
    $().click(function() {
        hideInList();
    });

    $('#footer-models' + '-box a').each(function() {
        $(this).click(function() {
            $('#footer-models').text($(this).text());
        })
    });
        
    // Menu events
    $('#menu li a').bind('mouseover', function() {
        hideInList();
        
        var menu = $(this).attr('id');
        var overlay = '#' + menu + 'Over';
        
        $(overlay).css({
            'left': $('#' + menu).position().left,
            'width': $('#' + menu).outerWidth()
        });
        $(overlay).show();
        createIFrame(overlay);
        
        $(overlay).hover(function() {},
        function() {
            $(this).hide();
            $('.iframe').remove();
        });
    });
    
    // Show/Hide lignes texts
    $('.lignes-text').hide();
    $('#ctl00_MainContent_Slider a').each(function(i) {
        $(this).hover(
            function() {
                $('.lignes-text').hide().eq(i).css('padding-left', $('#ctl00_MainContent_Slider a').width()*i + $('#ctl00_MainContent_Slider a').width()/2).show();
            },
            function() {
                $('.lignes-text').hide();
            }
        );
    });
    
    // Show/Hide product elements
    //$('.detail').hide().eq(0).show();
    $('.detail').css('visibility','visible');
    $('#ctl00_MainContent_Slider img').each(function(i) {
        $(this).click(function() {
            $('.detail').css('visibility','visible');
            //$('.detail').hide().eq(i).show();
            //return false;
        });
    });
    
    // Show/Hide product elements
    $('.elements').hide().eq(0).show();
    $('.produit-elements a').each(function(i) {
        $(this).click(function() {
            $('.elements').hide().eq(i).show();
            //return false;
        });
    });
});

/**
* Slideshow function
*/
(function($) {
    $.fn.ujaSlider = function(options) {
        var cfg = $.extend({}, $.fn.ujaSlider.defaults, options);
        // var slides = $(cfg.slider + ' img').length;
        var slides = $(cfg.slider + ' > *').length;
        var current = 0;
        
        // Style or restyle slides
        var ujaStyle = function() {
            $(cfg.slider).css({
                width: cfg.slidesWidth * slides + 'px',
                left: '-' + (current * $(cfg.slider).find(':first-child').width() + 'px')
            });    
        };
        
        // Initialize sliders style
        ujaStyle();
        $(window).resize(function() {
            ujaStyle();                          
        });
        
        // Animation management
        var ujaAnimate = function(idx) {
            var e = $(cfg.slider).find(":first-child");
            var left_val =  '-' + (idx * (e.outerWidth()) + 'px');

            $(cfg.slider).animate({
                left: left_val
            }, cfg.duration, 'easeInOutCubic');
        };
        
        $(cfg.prev).hide();

        if ( current >= slides - cfg.stepBy )
            $(cfg.next).hide();
        
        // Prev & next events
        $(cfg.prev).click(function() {
            $('.list-box').hide();
        
            if ( current > 0 ) {
                current -= cfg.stepBy;
                $(cfg.next).show();
                if ( current == 0 )
                    $(this).hide();
            } else {
                current = slides - cfg.stepBy;
            }
            ujaAnimate(current);
            return false;
        });
        
        $(cfg.next).click(function() {
            $('.list-box').hide();
            
            if ( current < slides - cfg.stepBy ) {
                current += cfg.stepBy;
                if ( current >= slides - cfg.stepBy )
                    $(this).hide();
                if ( current > 0 )
                    $(cfg.prev).show();
            }
            else {
                current = 0;
            }
            ujaAnimate(current);
            return false;
        });
    };
    
    // Slider defaults options
    $.fn.ujaSlider.defaults = {
        'slider': '#ctl00_MainContent_Slider',
        'slidesWidth': 141,
        'stepBy': 5,
        'prev': '#prev a',
        'next': '#next a',
        'duration': 1500
    };
})(jQuery);

/* List */
(function($) {
    $.fn.ujaList = function(options) {
        var cfg = $.extend({}, $.fn.ujaList.defaults, options);
        var box = $(cfg.id + '-box');

        $(box).css({
            'left': $(cfg.id).position().left,
            'top': $(cfg.id).position().top + $(cfg.id).outerHeight()
        });
        $(box).hide();

        $(cfg.id).click(function() {
            var list = $(this).attr('id');
            var box = $('#' + list + '-box');
            
            var isHidden = $(box).is(':hidden');
            
            if ( isHidden ) { 
                hideInList();
                $(box).show(); 
                $(cfg.id + '-box .scroll-pane').jScrollPane();
                $(cfg.id + '-box .jScrollPaneTrack').click(function() {
                    $(box).show(); 
                    
                    return false;
                });
            }
            
            return false;
        });
        
        $(cfg.id + '-box a').each(function() {
            $(this).click(function() {
                $(cfg.id).text($(this).text());
            })
        });
    };

    // List defaults options
    $.fn.ujaList.defaults = {
        'id': '#list'
    };
})(jQuery);

/**
* Easing functions
*/
jQuery.extend(jQuery.easing,
{
    easeInOutCubic: function (x, t, b, c, d) {
        if ((t/=d/2) < 1) return c/2*t*t*t + b;
        return c/2*((t-=2)*t*t + 2) + b;
    }
});

// initialize product picture zoom
(function($) {
    $.fn.ujaPrettyPhoto = function(options) {
        var cfg = $.extend({}, $.fn.ujaPrettyPhoto.defaults, options);
        
        $(cfg.src).lightBox({ //'a[@rel*=zoom]'
            overlayBgColor: cfg.overlayBgColor,
            overlayOpacity: cfg.overlayOpacity,
            imageLoading: cfg.imageLoading,
            imageBtnClose: cfg.imageBtnClose,
            imageBtnPrev: cfg.imageBtnPrev,
            imageBtnNext: cfg.imageBtnNext,
            imageBlank: cfg.imageBlank,
            containerResizeSpeed: cfg.containerResizeSpeed,
            txtImage: cfg.txtImage,
            txtOf: cfg.txtOf
        });
    };
    
    // Pretty Photo defaults options
    $.fn.ujaPrettyPhoto.defaults = {
        'src': "a[rel^='zoom']",
        'overlayBgColor': '#000000',
        'overlayOpacity': 0.3,
        'imageLoading': '/App_Themes/ThemeUJA/img/lightbox/loading.gif',
        'imageBtnClose': '/App_Themes/ThemeUJA/img/lightbox/close.gif',
        'imageBtnPrev': '/App_Themes/ThemeUJA/img/lightbox/prev.gif',
        'imageBtnNext': '/App_Themes/ThemeUJA/img/lightbox/next.gif',
        'imageBlank': '/App_Themes/ThemeUJA/img/lightbox/blank.gif',
        'containerResizeSpeed': 350,
        'txtImage': 'Produit ',
        'txtOf': 'sur'
    };
})(jQuery);

// Show or hide Answers
$(document).ready(function() {
    $('#reponses p').hide().eq(0).show();
    $('#questions p').each(function(i) {
        $(this).click(function() {
            $('#reponses p').hide().eq(i).show();
            return false;
        });
    });
});

/**
 * Slide chunks of text
 */
(function($) {
    $.fn.ujaTextSlider = function(options) {
        var cfg = $.extend({}, $.fn.ujaTextSlider.defaults, options);
        var current = 0;
        var max;
        max = Math.ceil($(cfg.slider).height() / cfg.stepBy) - 1;
        $(cfg.prev).css('visibility', 'hidden');
        if (max <= 0)
            $(cfg.next).css('visibility', 'hidden');

        $(cfg.prev).click(function() {
            $('.list-box').hide();
            
            current--;
            if (current < 0)
                current = 0;
            $(cfg.slider).animate({bottom:cfg.stepBy*current+'px'}, cfg.duration, 'easeInOutCubic');
            $(cfg.next).css('visibility', 'visible');
            if (current == 0)
                $(cfg.prev).css('visibility', 'hidden');
            else
                $(cfg.prev).css('visibility', 'visible');
            return false;
        });

        $(cfg.next).click(function() {
            $('.list-box').hide();
            
            current++;
            if (current > max) {
                current = max;
            }
            $(cfg.slider).animate({bottom:cfg.stepBy*current+'px'}, cfg.duration, 'easeInOutCubic');
            $(cfg.prev).css('visibility', 'visible');
            if (current == max)
                $(cfg.next).css('visibility', 'hidden');
            else
                $(cfg.next).css('visibility', 'visible');
            return false;
        });

    };

    // Text Slider defaults options
    $.fn.ujaTextSlider.defaults = {
        'slider': '#slider',
        'stepBy': 132,
        'prev': '#prev a',
        'next': '#next a',
        'duration': 1500
    };
})(jQuery);

/* Pager */
(function($) {
    $.fn.ujaPager = function(options) {
        var cfg = $.extend({}, $.fn.ujaPager.defaults, options);
        var current = 1;
        var max = $(cfg.items).length;

        $(cfg.confirm).hide();
   			$(cfg.prev).hide();

        if (max > 1) {
            $(cfg.max_place).text(max);
            $(cfg.current_place).text(current);
      			$(cfg.items).hide().eq(current - 1).show();
  			}
  			else {
      			$(cfg.next).hide();
  			}

        $(cfg.prev).click(function() {
            current--;
            
            if (current >= 1) {
          			$(cfg.next).show();
                $(cfg.confirm).hide();
          			$(cfg.items).hide().eq(current - 1).show();
          			$(cfg.current_place).text(current);
            }
            else
                current = 1;
                
            if (current == 1) {
                $(cfg.prev).hide();
            }
            return false;    
        });
        
        $(cfg.next).click(function() {
            current++;

            if (current <= max) {
          			$(cfg.prev).show();
          			$(cfg.items).hide().eq(current - 1).show();
          			$(cfg.current_place).text(current);
            }
            else
                current = max;

            if (current == max) {
                $(cfg.next).hide();
                $(cfg.confirm).show();
            }
            return false;
        });

    };

    // Pager defaults options
    $.fn.ujaPager.defaults = {
        'items': '.page',
        'current_place': '#current_place',
        'max_place': '#max_place',
        'prev': '#prev',
        'next': '#next',
        'confirm': '#confirm'
    };
})(jQuery);


/**
* Easing functions
*/
jQuery.extend(jQuery.easing,
{
    easeInOutCubic: function (x, t, b, c, d) {
        if ((t/=d/2) < 1) return c/2*t*t*t + b;
        return c/2*((t-=2)*t*t + 2) + b;
    }
});

// Selection popup
function selectionOpen() {
    //$('.produit-selection .produit-image').attr('src', $('.produit-element-image').eq(i).attr('src'));
    $('<div></div>').addClass('overlay').css({'opacity': 0.33}).click(selectionClose).appendTo($('body'));
    
    var isMSIE6 = false /*@cc_on || @_jscript_version < 5.7 @*/;
    if (isMSIE6) {
        $('.produit-selection').css({
            'visibility': 'visible'
        });
        $('.overlay').css({
            'height': $(document).height()
        });
    }
    $('.produit-selection').show();
}
function selectionClose() {
    $('.overlay').remove();
    $('.produit-selection').hide();
}
(function($) {
    $.fn.ujaPopup = function(options) {
        var cfg = $.extend({}, $.fn.ujaPopup.defaults, options);

        $('.produit-selection').each(function () {
            $(this).css({
                'left': $(document).width() / 2 - $(this).width() / 2,
                'top': $(document).height() / 2 - $(this).height() / 2
            });
        }).hide();
        
        /*$('.add-selection').each(function(i) {
            $(this).click(function() {
                selectionOpen();
                //return false;
            });
        });*/

        $('.poursuivre-visite').click(selectionClose);
    };
    
    // Popup defaults options
    $.fn.ujaPopup.defaults = {};
})(jQuery);

(function($) {
    $.fn.initFileUploads = function(options) {
        var cfg = $.extend({}, $.fn.initFileUploads.defaults, options);
        var W3CDOM = (document.createElement && document.getElementsByTagName);

        if (!W3CDOM) return;
        
        var fakeFileUpload = document.createElement('div');
        fakeFileUpload.className = cfg.fakeFileClass;
        fakeFileUpload.appendChild(document.createElement('input'));
        
        var image = document.createElement('img');
        image.src = cfg.src;
        fakeFileUpload.appendChild(image);
        
        var x = $(cfg.fileInput);
        for (var i=0;i<x.length;i++) {
	        if (x[i].type != 'file') continue;
	        if (x[i].parentNode.className != cfg.parentClass) continue;
	        x[i].className = cfg.fileClass;
	        
	        var clone = fakeFileUpload.cloneNode(true);
	        x[i].parentNode.appendChild(clone);
	        x[i].relatedElement = clone.getElementsByTagName('input')[0];
	        x[i].onchange = x[i].onmouseout = function () {
		        this.relatedElement.value = this.value;
	        }
        }
    };
    
    // InitFileUploads defaults options
    $.fn.initFileUploads.defaults = {
        'fakeFileClass': 'fakefile', 
        'fileClass': 'file hidden', 
        'fileInput': '#FileUpload', 
        'parentClass': 'fileinputs', 
        'src': '../../App_Themes/ThemeUJA/img/fr/bt_parcourir.gif'
    };
})(jQuery);

/* ujaLimitCheckbox */
(function($) {
    $.fn.ujaLimitCheckbox = function(options) {
        var cfg = $.extend({}, $.fn.ujaLimitCheckbox.defaults, options);
        
        var node = $(cfg.idPrefix+cfg.id).parent();
        for (var i=0; i<cfg.rowSearchLimit; i++) {
            if (node[0].tagName.toLowerCase() == cfg.searchNode) {
                break;
            } else {
                node = node.parent();
            }
        }
        
        if (node[0].tagName.toLowerCase() == cfg.searchNode) {
            var row = node[0];
            var inputs = row.getElementsByTagName("input");
            for (var j=0;j<inputs.length;j++) {
                if (inputs[j].type == "checkbox") {
                    inputs[j].checked = ('#'+inputs[j].id == cfg.idPrefix+cfg.id) ? true : false;
                }
            }
        }
    };

    // Limit Checkbox defaults options
    $.fn.ujaLimitCheckbox.defaults = {
        'rowSearchLimit': 5,
        'id': '',
        'idPrefix': '#ctl00_MainContent_',
        'searchNode': 'tr'
    };
})(jQuery);
