﻿var startMarquee=1;
jQuery.noConflict();
(function($K)
{
var sourceImageID,sourceLinkID;
var intImageCount;
var curImage;
var curImageIndex;
var dbBack;
var popupDivID,closeButtonID,nextButtonID,prevButtonID,playButtonID,closeButtonIDRight;
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();  \              
		resetPlugIn();
        popupDivID = "#" + options.zoomControlID;
        nextButtonID = "#" + options.nextButtonID;
        playButtonID = "#" + options. playButtonID;
        prevButtonID = "#" + options.prevButtonID;
        closeButtonID = "#" + options.closeButtonID;    closeButtonIDRight = "#" + options.closeButtonIDRight;
        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;
            }
            $K(zoomImageID).load(function()
                {                   
                    $K("#loader").removeClass("dv-loading"); 
                    $K(this).show();
                });
        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(closeButtonIDRight).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(closeButtonIDRight).attr("src",closeImgPath);
                
                $K(nextButtonID).css("cursor","pointer");
                $K(prevButtonID).css("cursor","pointer");
                $K(playButtonID).css("cursor","pointer");
                $K(closeButtonID).css("cursor","pointer");  $K(closeButtonIDRight).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(sourceImagePath);
        //alert((($K(window).width())/2)-($K("div#dvZoom").width()/2))
//        $K(zoomImageID).attr("src",curImage.attr("src").replace(sourceImagePath,destImagePath));
//        $K(zoomImageID).hide();
//        $K("#loader").addClass("dv-loading");

    $K('#loader').html("");
    $K("#loader").addClass("dv-loading");
        var img = new Image();
        $K(img).load(function () {
            //$(this).css('display', 'none'); // .hide() doesn't work in Safari when the element isn't on the DOM already
            $K(this).hide();            
            //alert(sourceImagePath);
            $K('#loader').removeClass('dv-loading').addClass('dv-loader').html('').append(this);            
            $K(this).fadeIn();
            $K(this).css("display","inline");
        }).error(function () {/*alert('error in image loading');*/
            // notify the user that the image could not be loaded
        }).attr('src', curImage.attr("src").replace(sourceImagePath,destImagePath ));

        getCurrentImageIndex(curImage);
        ShowHideButtons();        
        if(isclickcallback)
            {               
                callBack(obj);
            }
            startMarquee=0;
    }
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();
      document.getElementById('mrq1').start();
      startMarquee=1;
    }
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;
                //alert(curInstance);
                changeImage();
            }
        else
            {
                if(isPlay)
                    {
                        arrCurImageIndex[curInstance]=0;
                        changeImage();
                    }                
            }
        ShowHideButtons();        
    }    
prevButtonClick= function()
    {
       if(arrCurImageIndex[curInstance] > 0)
            {
                arrCurImageIndex[curInstance] = arrCurImageIndex[curInstance]-1;
                changeImage();
            }        
       ShowHideButtons();
    }
changeImage=function()
    {
     if(isPlay)
     {
        clearInterval(timer );
    }
        $K("#loader").html("");
        $K("#loader").addClass("dv-loading");
        var img = new Image();
        $K(img).load(function () {
            //$(this).css('display', 'none'); // .hide() doesn't work in Safari when the element isn't on the DOM already
            $K(this).hide();            
            //alert(sourceImagePath);
            $K('#loader').removeClass('dv-loading').addClass('dv-loader').html('').append(this);
            $K(this).fadeIn();
            $K(this).css("display","inline");
           if(isPlay)
            {
                timer = setInterval("nextButtonClick();",timeInterval);
            }
            
        }).error(function () {/* alert('error in image loading');*/
        // notify the user that the image could not be loaded
        }).attr('src', $K(arrSourceImageID[curInstance]).eq(arrCurImageIndex[curInstance]).attr("src").replace(sourceImagePath,destImagePath));
        
 //       $K(zoomImageID).fadeOut(fadeTime,function()
  //          {                
//                $K(zoomImageID).attr("src",$K(arrSourceImageID[curInstance]).eq(arrCurImageIndex[curInstance]).attr("src").replace(sourceImagePath,destImagePath)).fadeIn(fadeTime);
//                $K(zoomImageID).hide();
//                $K("#loader").addClass("dv-loading");

                   
                //alert('1');
//            });
            //alert($K(arrSourceImageID[curInstance]).eq(arrCurImageIndex[curInstance]).attr("src").replace(sourceImagePath,destImagePath));
            //alert($K('#loader').html());
        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;
        // ======== 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();        
    }
resetPlugIn= function()
	{
			intTopAxis=0;
			arrCurImageIndex= new Array();
			arrIntImageCount= new Array();
			arrSourceImageID= new Array();
			arrSourceLinkID= new Array();
			arrSourceDivID= new Array();
			instanceCount=0;
			curInstance=0;
			isFirst=true;
			isPlay=false;
			isDrag=false,ischangecallback=false,isclickcallback=false,isInitCallback=false,isExitCallback=false;
	}
$K(window).scroll(function()
    {   
        var xTop= parseInt($K(window).scrollTop()) + intTopAxis ;
        $K(popupDivID).animate({top:xTop+ "px"},{queue: false, duration: 350});
    });
})(jQuery);



//marquee speed
 var movefaster=false;

function togglespeed()
{
    if(movefaster)
    {   
    
        //normal
        movefaster=false;
        
        if(jQuery("#mrq1")!=null)
        {   
            jQuery("#mrq1").attr("scrollAmount",1);
            jQuery("#mrq1").attr("scrollDelay",20);
        }
    }
    else
    {
        //faster
        
        movefaster=true;
        if(jQuery("#mrq1")!=null)
        {   
        jQuery("#mrq1").attr("scrollAmount",5);
        jQuery("#mrq1").attr("scrollDelay",15);
        }
    }
}