﻿
jQuery.noConflict();
(function($K)
{
var sourceImageID,sourceLinkID;
var intImageCount;
var curImage;
var curImageIndex;
var dbBack;
var popupDivID,closeButtonID,nextButtonID,prevButtonID,playButtonID;
var intTopAxis=0;
var fadeTime;
var nextImgPath,nextImgPathDesable,prevImgPath,prevImgPathDesable,closeImgPath,playImgPath,pauseImgPath;
var destImagePath,sourceImagePath;
var arrCurImageIndex= new Array();
var arrIntImageCount= new Array();
var arrSourceImageID= new Array();
var arrSourceLinkID= new Array();
var arrSourceDivID= new Array();
var instanceCount=0;
var curInstance=0;
var isFirst=true;
var isPlay=false;
var mode,zoomLinkClassName,sourceImageClassName,backgroundCSS;
var timeInterval=3000;
var timer;
var isDrag=false,ischangecallback=false,isclickcallback=false,isInitCallback=false,isExitCallback=false;
var onChangeCallBack,exitCallback;

$K.fn.initZoom= function(options)
    {
        
        //timerTest();  \              
        popupDivID = "#" + options.zoomControlID;
        nextButtonID = "#" + options.nextButtonID;
        playButtonID = "#" + options. playButtonID;
        prevButtonID = "#" + options.prevButtonID;
        closeButtonID = "#" + options.closeButtonID;
        zoomImageID = "#" + options.zoomImageID;

        if(options.mode)
                    {
                        mode= options.mode;
                        if (mode == "both" || mode=="link" )
                            {
                                zoomLinkClassName=options.zoomLinkClassName;
                                sourceLinkID= "#" + $K(this).attr("id") + " a." + zoomLinkClassName;
                            }
                    }
                else
                    {
                        mode="image";
                    }                
        var parentDVID=$K(this).attr("id");
        sourceImageClassName=options.sourceImageClassName;
        sourceImageID = "#" + $K(this).attr("id") + " img." + sourceImageClassName;
        
        $K(sourceImageID).addClass("zoomCursor");
        
        intImageCount=$K(sourceImageID).length;
        
        //Generate Arrays STARTs
        instanceCount= instanceCount + 1;
        arrCurImageIndex[arrCurImageIndex.length]=0;
        arrIntImageCount[arrIntImageCount.length]=intImageCount;
        arrSourceImageID[arrSourceImageID.length]=sourceImageID;
        if (mode != "image")
            arrSourceLinkID[arrSourceLinkID.length]=sourceLinkID;
        arrSourceDivID[arrSourceDivID.length]=parentDVID;        
        //Generate Arrays ENSs       
        
        if(options.imageClickCallBack)
            {
                isclickcallback=true;
            }
        if(options.onChangeCallBack)
            {
                ischangecallback=true;
                onChangeCallBack=options.onChangeCallBack;
            }
        if(options.exitCallback)
            {
                isExitCallback=true;
                exitCallback=options.exitCallback;
            }
        
        if(mode == "both")
            {                  
                $K(sourceImageID).click(function()
                {                    
                    sourceImageClick(parentDVID,$K(this),options.imageClickCallBack);
                });
                $K(sourceLinkID).click(function()
                {
                    sourceLinkClick(parentDVID,$K(this),options.imageClickCallBack);
                });
            }
        else if(mode == "link")
            {
                $K(sourceLinkID).click(function()
                {
                    sourceLinkClick(parentDVID,$K(this),options.imageClickCallBack);
                });
            }
        else
            {
                $K(sourceImageID).click(function()
                {
                    sourceImageClick(parentDVID,$K(this),options.imageClickCallBack());
                });
            }
        
        if(isFirst)
            {
               if(options.initCallback)
                    {
                        isInitCallback=true;
                        options.initCallback();
                    }                
                destImagePath = options.destImagePath;
                sourceImagePath = options.sourceImagePath;
                    
                fadeTime= options.fadeTime;
                nextImgPath=options.nextImgPath;
                nextImgPathDesable=options.nextImgPathDesable;
                prevImgPath=options.prevImgPath;
                prevImgPathDesable=options.prevImgPathDesable;
                closeImgPath = options.closeImgPath;
                playImgPath=options.playImgPath;
                pauseImgPath=options.pauseImgPath;
                $K(closeButtonID).click(closeButtonClick);
                $K(nextButtonID).click(nextButtonClick);
                $K(playButtonID).click(playButtonClick);
                $K(prevButtonID).click(prevButtonClick);
                backgroundCSS=options.backgroundCSS;                
                
                initBackGround(backgroundCSS);
                
                $K(nextButtonID).attr("src",nextImgPath); 
                $K(playButtonID).attr("src",playImgPath); 
                $K(prevButtonID).attr("src",prevImgPath);
                $K(closeButtonID).attr("src",closeImgPath);
                $K(nextButtonID).css("cursor","pointer");
                $K(prevButtonID).css("cursor","pointer");
                $K(playButtonID).css("cursor","pointer");
                $K(closeButtonID).css("cursor","pointer");
                
                isFirst=false;
            }
    };
    
sourceImageClick = function(parent,obj,callBack)
    {        
        getCurrentInstance(parent);        
        curImage=obj;
        ShowBackGround();        
                
        $K(popupDivID).css({"left":(($K(window).width())/2)-($K("div#dvZoom").width()/2),"top":(($K(window).height())/2)-($K("div#dvZoom").height()/2)+parseInt($K(window).scrollTop())}).fadeIn("slow").fadeIn(fadeTime);
        //alert((($K(window).width())/2)-($K("div#dvZoom").width()/2))
        $K(zoomImageID).attr("src",curImage.attr("src").replace(sourceImagePath,destImagePath));
        getCurrentImageIndex(curImage);
        ShowHideButtons();        
        
        if(isclickcallback)
            {
                callBack(obj);
            }
    }
sourceLinkClick = function(parent,obj,callBack)
    {
        getCurrentInstance(parent);
        curImage=obj;
        getCurrentImageIndexByLink(obj);                
        
        $K(popupDivID).css({"left":(($K(window).width())/2)-($K("div#dvZoom").width()/2),"top":(($K(window).height())/2)-($K("div#dvZoom").height()/2)+parseInt($K(window).scrollTop())}).fadeIn("slow").fadeIn(fadeTime);
        ShowBackGround();
        var xSRC= $K(arrSourceImageID[curInstance]).eq(arrCurImageIndex[curInstance]).attr("src").replace(sourceImagePath,destImagePath);        
        $K(zoomImageID).attr("src",xSRC);
        ShowHideButtons();
        
        if(isclickcallback)
            {
                callBack($K(arrSourceImageID[curInstance]).eq(arrCurImageIndex[curInstance]));
            }
    }
    
getCurrentInstance=function(dvID)
    {
        var xInd=0;
        $K.each(
            arrSourceDivID,
            function( intIndex, objValue )
                {
                    if(objValue == dvID)
                        {
                            xInd = intIndex;
                            curInstance=intIndex;
                        }
                }
        );
        return xInd;
    }
    
getCurrentImageIndex = function(objImg)
    {   
        var xInd;
        $K(arrSourceImageID[curInstance]).each(function(i)
            {
                if(objImg.attr("id") == $K(this).attr("id") && objImg.attr("src") == $K(this).attr("src"))
                    {
                        xInd = i;
                        arrCurImageIndex[curInstance]=i;
                    }
            });        
        return xInd;
    }
getCurrentImageIndexByLink = function(objLnk)
    {   
        var xInd;        
        $K(arrSourceLinkID[curInstance]).each(function(i)
            {
                if(objLnk.attr("id") == $K(this).attr("id"))
                    {
                        xInd = i;
                        arrCurImageIndex[curInstance]=i;
                    }
            });
        return xInd;
    }
    
closeButtonClick = function()
    {
        $K(popupDivID).fadeOut(fadeTime);
        HideBackGround();
        clearTimeout(timer);
        pauseSlideShow();
      //  exitCallback();
    }
playButtonClick=function()
    {
        if(isPlay)
            {
                pauseSlideShow();
            }
        else
            {
                playSlideShow();
            }        
    }
    
playSlideShow=function()
    {
        $K(playButtonID).attr("src",pauseImgPath);
        isPlay=true;
        timer = setInterval("nextButtonClick();",timeInterval);
    }
pauseSlideShow=function()
    {
        $K(playButtonID).attr("src",playImgPath); 
        isPlay=false;
        clearTimeout(timer);
    }
nextButtonClick= function()
    {
        if(arrCurImageIndex[curInstance] < arrIntImageCount[curInstance]-1)
            {
                arrCurImageIndex[curInstance] = arrCurImageIndex[curInstance]+1;
                changeImage();
            }
        else
            {
                if(isPlay)
                    {
                        arrCurImageIndex[curInstance]=0;
                        changeImage();
                    }                
            }
        ShowHideButtons();        
    }    
prevButtonClick= function()
    {
       if(arrCurImageIndex[curInstance] > 0)
            {
                arrCurImageIndex[curInstance] = arrCurImageIndex[curInstance]-1;
                changeImage();
            }        
       ShowHideButtons();
    }
changeImage=function()
    {
        $K(zoomImageID).fadeOut(fadeTime,function()
            {                
                $K(zoomImageID).attr("src",$K(arrSourceImageID[curInstance]).eq(arrCurImageIndex[curInstance]).attr("src").replace(sourceImagePath,destImagePath)).fadeIn(fadeTime);
            });
        if(ischangecallback)            
            {
                onChangeCallBack($K(arrSourceImageID[curInstance]).eq(arrCurImageIndex[curInstance]));
            }
    }
ShowHideButtons=function()
    {
        if(arrCurImageIndex[curInstance] <= 0)
            {                
                $K(nextButtonID).attr("src",nextImgPath);
                $K(prevButtonID).attr("src",prevImgPathDesable);
            }
        else if(arrCurImageIndex[curInstance] >= arrIntImageCount[curInstance]-1)
            {
                $K(nextButtonID).attr("src",nextImgPathDesable);
                $K(prevButtonID).attr("src",prevImgPath);
            }
        else
            {
                $K(prevButtonID).attr("src",prevImgPath);
                $K(nextButtonID).attr("src",nextImgPath);                
            }        
    }
initBackGround= function(className)
    {        
//        if($K.browser.msie)
//            dbBack = $K("<div></div>").attr("class",className).css("height",$K(document).height()).hide();
//        else
//            dbBack = $K("<div></div>").attr("class",className).css("height",$K(window).height()).hide();            
        dbBack = $K("<div></div>").attr("class",className).css("height",$K(document).height()).hide();
        $K("body").append(dbBack);
        
        //DragDrop STARTs
        var addX,addY;
        
        var prevLeft,prevTop;
            $K(popupDivID).mousedown(function(e)
            {
                isDrag= true;
                $K(this).addClass("moveClass");                                
                addX=e.pageX - parseInt($K(popupDivID).css("left"));
                addY=e.pageY - parseInt($K(popupDivID).css("top"));      
                //alert(e.pageX)
               //alert($K(popupDivID).css("left"));          
                //alert(addY);
                prevLeft=e.pageX;
                    prevTop=e.pageY;

            });
            $K(popupDivID).mouseup(function(e)
            {  
                if(isDrag)
                {
                    isDrag=false;
                    $K(this).removeClass("moveClass");
                    prevLeft=e.pageX;
                    prevTop=e.pageY;
                }
            });
            $K("div." + backgroundCSS ).mouseup(function(e)
            {
                if(isDrag)
                {   
                    isDrag=false;
                    $K(popupDivID).removeClass("moveClass");
                     prevLeft=e.pageX;
                    prevTop=e.pageY;
                }
            });
                
                
            $K("div." + backgroundCSS).mousemove(function(e)
            {
          //  alert("asf")    ;
                if(isDrag)
                {
                    var posX=0,posY=0;
                    //alert(e.pageX)
                    if(e.pageX>0)
                        posX=e.pageX;
                    if(e.pageY>0)
                        posY=e.pageY;                                
                    var dragX,dragY;
                    //dragX=posX-
                   
                    var left1,top1;
                   // alert(prevLeft-e.pageX)
                    left1=parseInt($K(popupDivID).css("left")) - (prevLeft-e.pageX);
                    top1=parseInt($K(popupDivID).css("top")) - (prevTop-e.pageY); //posX-addY;
                    
                    //top1=parseInt($K(popupDivID).css("top")) - addY
                    //alert(left1)
//                    addX=posX - parseInt($K(popupDivID).css("left"));
//                    addY=posY- parseInt($K(popupDivID).css("top"));   
                    $K(popupDivID).css({left:left1,top:top1});
                    //  alert(addX)
                        prevLeft=e.pageX
                        prevTop=e.pageY
                }
            });
            
            $K(popupDivID).mousemove(function(e)
            {
                //alert("asf");
                if(isDrag)
                {
                    var posX=0,posY=0;
                    //alert(e.pageX)
                    if(e.pageX>0)
                        posX=e.pageX;
                    if(e.pageY>0)
                        posY=e.pageY;                                
                    var dragX,dragY;
                    //dragX=posX-
                   
                    var left1,top1;
                  //  alert(prevLeft-e.pageX)
                    left1=parseInt($K(popupDivID).css("left")) - (prevLeft-e.pageX);
                    top1=parseInt($K(popupDivID).css("top")) - (prevTop-e.pageY); //posX-addY;
                    
                    //top1=parseInt($K(popupDivID).css("top")) - addY
                    //alert(left1)
//                    addX=posX - parseInt($K(popupDivID).css("left"));
//                    addY=posY- parseInt($K(popupDivID).css("top"));   
                    $K(popupDivID).css({left:left1,top:top1});
                    //  alert(addX)
                        prevLeft=e.pageX
                        prevTop=e.pageY
                }
            });
    
        // ======== DragDrop ENDs ============
        
        $K(document).keypress(function(e)
            {   
                if(e.which == 27 || e.which == 0)
                    {                        
                        closeButtonClick();
                    }
                if(e.which == 32)
                    {
                        nextButtonClick();
                    }
                if(e.which == 13)
                    {
                        playButtonClick();
                    }
            });
    }
    
ShowBackGround = function()
    {
        dbBack.show();
        intTopAxis= parseInt(($K(window).height())/2)-($K(popupDivID).height()/2);        
        if (arrIntImageCount[curInstance] <=1 )
            {
                $K(nextButtonID).hide();
                $K(prevButtonID).hide();
                $K(playButtonID).hide();
            }        
        else
            {
                $K(nextButtonID).show();
                $K(prevButtonID).show();
                $K(playButtonID).show();                
            }
    }
    
HideBackGround = function()
    {
        dbBack.hide();        
    }
    
$K(window).scroll(function()
    {   
        var xTop= parseInt($K(window).scrollTop()) + intTopAxis ;
        $K(popupDivID).animate({top:xTop+ "px"},{queue: false, duration: 350});
    });
    
})(jQuery);