//**************************************************************
// jQZoom allows you to realize a small magnifier window,close
// to the image or images on your web page easily.
//
// jqZoom version 2.2
// Author Doc. Ing. Renzi Marco(www.mind-projects.it)
// First Release on Dec 05 2007
// i'm looking for a job,pick me up!!!
// mail: renzi.mrc@gmail.com
//**************************************************************
var $j = jQuery.noConflict();

(function($j){

        $j.fn.jqueryzoom = function(options){

        var settings = {
                xzoom: 600,     //zoomed width default width
                yzoom: 600,     //zoomed div default width
                offset: 10,     //zoomed div default offset
                position: "right" ,//zoomed div default position,offset position is to the right of the image
                lens:1, //zooming lens over the image,by default is 1;
                preload: 1

            };

        if(options) {
            $j.extend(settings, options);
        }

        var noalt='';

        function over() {

            $j("#teintList").css({ visibility: "hidden" });
            var imageLeft = $j(this).offset().left;
            var imageTop = $j(this).offset().top;


            var imageWidth = $j(this).children('img').get(0).offsetWidth;
            var imageHeight = $j(this).children('img').get(0).offsetHeight;


            noalt= $j(this).children("img").attr("alt");

            //var bigimage = $j(this).children("img").attr("longdesc");
            var bigimage = $j(this).children("span").attr("title");
            //alert(bigimage);

            $j(this).children("img").attr("alt",'');

            if($j("div.zoomdiv").get().length == 0){
                $j(this).after("<div class='zoomdiv'><img class='bigimg' src='"+bigimage+"'/></div>");
                $j(this).append("<div class='jqZoomPup'>&nbsp;</div>");
            }

            if(settings.position == "right"){
                if(imageLeft + imageWidth + settings.offset + settings.xzoom > screen.width){
                    leftpos = imageLeft  - settings.offset - settings.xzoom;
                }else{
                    leftpos = imageLeft + imageWidth + settings.offset;
                }
            }else{
                leftpos = imageLeft - settings.xzoom - settings.offset;
                if(leftpos < 0){
                    leftpos = imageLeft + imageWidth  + settings.offset;
                }
            }

            /*
            $j("div.zoomdiv").css({ top: imageTop,left: leftpos });
            $j("div.zoomdiv").width(settings.xzoom);
            $j("div.zoomdiv").height(settings.yzoom);*/
            $j("div.zoomdiv").show();

            if(!settings.lens){
                $j(this).css('cursor','crosshair');
            }

            $j(document.body).mousemove(function(e){
                mouse = new MouseEvent(e);
                var bigwidth = $j(".bigimg").get(0).offsetWidth;
                var bigheight = $j(".bigimg").get(0).offsetHeight;
                /*$j("div.jqZoomPup").hide();*/
                var scaley ='x';
                var scalex= 'y';

                if(isNaN(scalex)|isNaN(scaley)){
                    var scalex = (bigwidth/imageWidth);
                    var scaley = (bigheight/imageHeight);

                    $j("div.jqZoomPup").width((settings.xzoom)/scalex );
                    $j("div.jqZoomPup").height((settings.yzoom)/scaley);

                    if(settings.lens){
                        $j("div.jqZoomPup").css('visibility','visible');
                    }
                }

                xpos = mouse.x - $j("div.jqZoomPup").width()/2 - imageLeft;
                ypos = mouse.y - $j("div.jqZoomPup").height()/2 - imageTop ;

                if(settings.lens){
                    xpos = (mouse.x - $j("div.jqZoomPup").width()/2 < imageLeft ) ? 0 : (mouse.x + $j("div.jqZoomPup").width()/2 > imageWidth + imageLeft ) ?  (imageWidth -$j("div.jqZoomPup").width() -2)  : xpos;
                    ypos = (mouse.y - $j("div.jqZoomPup").height()/2 < imageTop ) ? 0 : (mouse.y + $j("div.jqZoomPup").height()/2  > imageHeight + imageTop ) ?  (imageHeight - $j("div.jqZoomPup").height() -2 ) : ypos;
                    $j("div.jqZoomPup").css({ top: ypos,left: xpos });
                }

                scrolly = ypos;
                $j("div.zoomdiv").get(0).scrollTop = scrolly * scaley;
                scrollx = xpos;
                $j("div.zoomdiv").get(0).scrollLeft = (scrollx) * scalex ;
            });
        }

        function out() {
            $j("#teintList").css({ visibility: "visible" });
            $j(this).children("img").attr("alt",noalt);
            $j(document.body).unbind("mousemove");
            if(settings.lens){
                $j("div.jqZoomPup").remove();
            }
            $j("div.zoomdiv").remove();
        }

        $j(this).unbind();
        //$j(this).unbind("mouseout", out);
        $j(this).hover(over, out);

        count = 0;

        if(settings.preload){
            //$j('div.jqPreload'+count+'').remove();
            //if($j('div.jqPreload'+count).get().length == 0){
                $j('body').append("<div style='display:none;' class='jqPreload"+count+"'>sdsdssdsd</div>");
            //}

            $j(this).each(function(){
                var imagetopreload= $j(this).children("span").attr("title");
                var content = jQuery('div.jqPreload'+count+'').html();
                jQuery('div.jqPreload'+count+'').html(content+'<img src=\"'+imagetopreload+'\">');
            });
        }
    }

})(jQuery);

function MouseEvent(e) {
    this.x = e.pageX;
    this.y = e.pageY;
}


function initZoom(img) {
    if(isIE) {
      //
      $j("#zoomText").css({ display: "none" });
    } else {
      document.getElementById('produitImage').setAttribute("longdesc", img);
      $j(".jqzoom").jqueryzoom({
                          xzoom: 330,
                          yzoom: 200,
                          offset: 1});
      $j("#zoomText").css({ display: "block" });
    }
 }
