/*
 * ISTEP SlideView - jQuery JavaScript Library v1.4.2
 *
 * http://www.istep.fr/
 *
 * istep_slideView.1.2.js
 *
 * Copyright (c) 2010-2011 Etienne Bailly - ISTEP.FR
 *
 * Date: 2010-08-13
 *
 * Based on	"SlideView Component"
	developled by Robin at ELFVision and PageTalks
	used mainly in the WUSESKY Project	
	Visit Robin's:
	http://www.elfvision.com/
	http://pagetalks.com/ 
 */


(function ($) {
    $.iSlideView = {
        build: function (user_options) {
            var defaults = {
                thumb: true,
                thumbPrefix: "thumb_",
                frame: {
                    isExist: false
                },
                slideBy: 1,
                speed: 350,
                easeOut: "swing",
                easeIn: "swing",
                easeThumb: "swing",
                loop: false,
                interval: 6000
            },
			options = $.extend(defaults, user_options),
			getThumbName = function (n, prefix) {
			    var arr = n.split("/");
			    var tl = n.length;
			    var w = arr[arr.length - 1].length;
			    return n.substr(0, tl - w - 1) + "/" + prefix + arr[arr.length - 1];
			};

            return $(this).each(function () {
                var x = $(this),
				list = x.find("ul"),
				links = list.find("a"),
				images = list.find("img"),
				slidebar,
				thumbList,
				thumbCon,
				desc,
				arrow,
				back = options.leftArrow ? $(options.leftArrow) : $("<div class='btn_previous'><a href='#' alt='< Précédent' title='< Précédent'><</a></div>"),
				next = options.rightArrow ? $(options.rightArrow) : $("<div class='btn_next'><a href='#' alt='Suivant >' title='Suivant >'>></a></div>"),
				xH,
				xW,
				li,
				liW,
				totalW,
				xTotalH,
				Pointer,
				total,
				tLink,
				isBusy = false,
				descFull,
				tabdesc,
				descTitle1,
				descTitle2,
				descLink,
                descLink2,
				focusTo = function (p) {
				    var offsetT = -(xH * p);
				    if (isBusy) {
				        list.stop();
				    }
				    isBusy = true;
				    if (options.desc) {
				        desc.html(options.desc[p]);
				    } else {
				        descFull = images.eq(p).attr("alt");
				        tabdesc = descFull.split(" | ");
				        descTitle1 = tabdesc[0];
				        descTitle2 = tabdesc[1];
				        if ((tabdesc[2] != null) && (tabdesc[3] != null)) {
				            descLink = tabdesc[2];
				            descLink2 = tabdesc[3];
				            desc.html("<div class='module_resume'><p><span class='text_vert_bold_16'>" + descTitle1 + "</span><br/><span class='text_blanc_13'>" + descTitle2 + "</span><br/><span class='text_13_blanc'><a href='" + descLink2 + "' target='_self' class='text_gris_13'>" + descLink + "</a></span></p></div>");
				        }
				        else {
				            desc.html("<div class='module_resume'><p><span class='text_vert_bold_16'>" + descTitle1 + "</span><br/><span class='text_blanc_13'>" + descTitle2 + "</span><br/></p></div>");
				        }
				    }
				    list.css("left", "700px");
				    list.css("top", offsetT + "px");
				    list.animate({
				        left: "0px"
				    }, options.speed, options.easeOut, function () {
				        isBusy = false;
				    });
				};

                // Méthode qui désactive les liens
                /*links.click(function () {
                    return false;
                });*/

                //Insert a frame
                var xW = x.width();
                var xH = x.height();
                if (options.frame.isExist) {
                    $("<div class='slideFrame' />").appendTo(x).css({
                        width: x.width() - options.frame.width * 2,
                        height: x.height() - options.frame.width * 2,
                        border: options.frame.width + "px solid " + options.frame.color,
                        position: "absolute",
                        top: 0,
                        left: 0,
                        zIndex: 4
                    });
                }
                desc = $("<div class='desc' />").appendTo(x);
                if (options.thumb || (!options.leftArrow || !options.rightArrow)) {
                    slidebar = $("<div class='slidebar' />").appendTo(x); /*.fadeTo("fast",0.9);*/
                }
                if (options.thumb) {
                    thumbList = $("<ul class='thumbList' />");
                    thumbCon = $("<div class='wrap' />").append(thumbList).appendTo(slidebar);
                    //prepare sidebar thumbs
                    $.each(images, function (index, img) {                        
                        thumbList.append("<li><a href='#' id='" + (index + 1) + "'><div class='vignette'><div class='vignette_image'><img src='" + getThumbName(img.src, options.thumbPrefix) + "' alt='" + img.alt + "' /></div><div class='vignette_reflet'></div></div></a></li>");                       
                    });

                    li = thumbList.find("li");
                    liW = li.width();
                    totalW = li.length * liW;
                    thumbList.css("width", totalW);

                    tLink = thumbList.find("a").each(function (i) {
                        $(this).hover(function () {
                            var id = this.id - 1;
                            (function (index) {
                                /*if(index!=Pointer) {*/
                                focusTo(id);
                                /*}*/
                            })(i);
                        }, function () {
                            /*focusTo(Pointer);*/
                            // 
                        });
                    });
                }
                if (!options.leftArrow || !options.rightArrow) {
                    slidebar.append(next).prepend(back);
                }

                focusTo(0);

                xTotalH = images.length * xH;
                list.css("height", xTotalH);

                Pointer = 0;
                total = images.length;

                if (options.loop) {//loop the slide
                    runDiapo = setInterval(function () {
                        next.click();
                    }, options.interval);
                }

                // Sur le survol des miniatures, on bloque le défilement
                $('div.wrap').hover(
				function () {
				    clearInterval(runDiapo);
				},
				function () {
				    runDiapo = setInterval(function () {
				        next.click();
				    }, options.interval);
				}
			 );


                // Sur le survol de l'image principale, on bloque le défilement
                $('li.slide').hover(
				function () {
				    clearInterval(runDiapo);
				},
				function () {
				    runDiapo = setInterval(function () {
				        next.click();
				    }, options.interval);
				}
			 )


                next.click(function () {
                    if (total - Pointer > options.slideBy) {
                        if (options.thumb) {
                            var offsetL = parseInt(thumbList.css("left")) - liW * options.slideBy - 8; // -8 pour le padding-right
                            thumbList.animate({
                                left: offsetL + "px"
                            }, options.speed, options.easeThumb);
                        }

                        Pointer += options.slideBy;
                        focusTo(Pointer);

                    } else {
                        if (options.loop) {
                            if (options.thumb) {
                                thumbList.animate({
                                    left: "0px"
                                }, options.speed, options.easeThumb);
                            }
                            Pointer = 0;
                            focusTo(0);
                        }
                    }
                    return false;
                });

                back.click(function () {
                    if (Pointer != 0) {
                        if (options.thumb) {
                            var offsetL = parseInt(thumbList.css("left")) + liW * options.slideBy + 8; // +8 pour le padding-right
                            thumbList.animate({
                                left: offsetL + "px"
                            }, options.speed, options.easeThumb);
                        }

                        Pointer -= options.slideBy;
                        focusTo(Pointer);
                    }
                    return false;
                });
            });
        }
    };
    $.fn.slideView = $.iSlideView.build;
})(jQuery);
