﻿var $IntenixAjaxOptions = {
    type: "POST",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    error: function(result) {
        //  alert("Error in server:" + result.status + ' ' + result.statusText);
    }
};
var ajaxQueue = $.manageAjax.create('AjaxQueue', { queue: true, cacheResponse: false });
var asmxImageGallery = "/WebService/WSAjaxImageGallery.asmx/";
var autoPlay = false;
var showPopup = false;
var showPreview = false;
var carouselSize = 0;
var slideDelay = 0;
var carouselApi;
var eventBind = false;
var incrementCounter = true; ;

var this1 = $(this);
$(function() {

    GetDefaultGallerySettings();

    if (showPopup) {
        //        $(".ancGalleryimages").fancybox({
        //            'titleShow': false,
        //            'padding': 10,
        //            'autoScale': false,
        //            'transitionIn': 'elastic',
        //            'transitionOut': 'elastic',
        //            'width': '800px',
        //            'height': '800px',
        //            'scrolling': 'no'

        //        });

        $("a[rel=ImagePopup_Gallery]").fancybox({
            'titleShow': false,
            'padding': 10,
            'autoScale': false,
            'transitionIn': 'elastic',
            'transitionOut': 'elastic',
            'width': '800px',
            'height': '800px',
            'scrolling': 'no',
            onComplete: function() {
                var ancTitle = $(this).attr("title");
                $("#divPageContents").html();
                //var ancTitle = $(this).next().attr("title");
                //var ancTitle = $(this).attr("id");
                //var $anc = $(this);
                //var ancTitle = $anc.get(0).getAttribute("id");

                var split = ancTitle.split("_");

                carouselApi.click(split[0]);
                if (incrementCounter) {
                    UpdateHtCounter(split[1]);
                }
                ShowSelectedPreview(split[1], split[0]);
                incrementCounter = true;

            }

        });
    }

    /*** 
    Simple jQuery Slideshow Script
    Released by Jon Raasch (jonraasch.com) under FreeBSD license: free to use or modify, not responsible for anything, etc.  Please link out to me if you like it :)
    ***/


    function GetDefaultGallerySettings() {
        autoPlay = ConvertToBoolean($("#hdnGalleryAutoPlay").val());
        showPreview = ConvertToBoolean($("#hdnGalleryShowPreview").val());
        showPopup = ConvertToBoolean($("#hdnGalleryShowPopup").val());
        carouselSize = $("#hdnCarouselSize").val();
        slideDelay = $("#hdnSlideDelay").val();
        if (slideDelay == 0)
            slideDelay = 500;
    }

    function ConvertToBoolean(value) {
        if (value == undefined || value == null || value == "False" || value == "false")
            return false;
        else
            return true;
    }
    //this is for on click the scroller image
    function slideSwitch2(id) {
        //alert(setClickRet);
        setClickRet = true;
        // setTimeout("ClearEntry", 5000);
        var $previewactive = $('#slideshow DIV.previewactive');
        var $next = $("#" + id);
        if ($next.attr("id") == $previewactive.attr("id"))
            return;

        $previewactive.animate({ opacity: 0.0 }, 1300, function() {
            $previewactive.removeClass('previewactive previewlast-activ');
            $previewactive.addClass('previewlast-activ');
            if ($nextAuto != null) {
                if ($nextAuto.is('.previewactive')) {
                    $nextAuto.removeClass('previewactive previewlast-activ');
                    $nextAuto.addClass('previewlast-activ');
                }
            }
            $next.css({ opacity: 0.0 }).addClass('previewactive').animate({ opacity: 1.0 }, 2300, function() {
                setClickRet = false;
            });

            //setTimeout("ClearEntry", 4000);
        });

    }


    carouselApi = $("div.scrollable").scrollable({ size: carouselSize, api: true, time: 0 });
    var interval;
    var setClickRet = false;
    if (autoPlay) {
        this1.everyTime(parseInt(slideDelay), 'controlled', function() {
            slideSwitch();
        });
        $(this).stopTime('controlled');
    }

    function ClearEntry() {
        setClickRet = false;
    }
    function ShowSelectedPreview(id, index) {
        this1.stopTime('controlled');
        slideSwitch2("pnlPreview_" + id + "_" + index);
        if (autoPlay) {
            this1.everyTime(parseInt(slideDelay), 'controlled', function() {
                slideSwitch();
            });
        }
        return false;
    }

    $('img[title*=ImageGallery]').click(function() {
        if (setClickRet) {
            //alert(setClickRet);
            return;
        }
        if (showPopup || showPreview) {
            var id = $(this).attr("id");
            var split = id.split("__");
            UpdateHtCounter(split[1]);
            ShowSelectedPreview(split[1], split[2]);
            incrementCounter = false;
        }
    });

    $(".ancPreviewLeft").click(function() {
        var $previewactive = $('#slideshow DIV.previewactive');
        var id = $previewactive.prev().attr("id");
        if (id != null) {
            var split = id.split("_");
            carouselApi.click(split[2]);
            UpdateHtCounter(split[1]);
            ShowSelectedPreview(split[1], split[2]);
        }

    });
    $(".ancPreviewNext").click(function() {

        var $previewactive = $('#slideshow DIV.previewactive');
        var id = $previewactive.next().attr("id");
        if (id != null) {
            var split = id.split("_");
            carouselApi.click(split[2]);
            UpdateHtCounter(split[1]);
            ShowSelectedPreview(split[1], split[2]);
        }
    });

    var $nextAuto = null;
    //get called after the interval  for auto calling 
    function slideSwitch() {
        var $previewactive = $('#slideshow DIV.previewactive');
        if ($previewactive.length == 0) $previewactive = $('#slideshow DIV:last');
        // use this to pull the divs in the order they appear in the markup
        $nextAuto = $previewactive.next().length ? $previewactive.next()
        : $('#slideshow DIV:first');
        // uncomment below to pull the divs randomly
        // var $sibs  = $previewactive.siblings();
        // var rndNum = Math.floor(Math.random() * $sibs.length );
        // var $next  = $( $sibs[ rndNum ] );
        $previewactive.animate({ opacity: 0.0 }, 1300, function() {


            $previewactive.addClass('previewlast-activ');
            $previewactive.removeClass('previewactive previewlast-activ');
            $nextAuto.css({ opacity: 0.0 }).addClass('previewactive').animate({ opacity: 1.0 }, 2300, function() {
                // $(".ancPreviewNext").css('disabled', 'false');
//                alert("out: " + $(".ancPreviewNext"));
            });
        });
    }


});

function GetUpdateHitCountResult(result) {
}

function UpdateHtCounter(id) {
    var q = $IntenixAjaxOptions;
    q.url = asmxImageGallery + "UpdateGalleryImageHitCount";
    q.data = "{imageID:" + id + "}";
    q.success = GetUpdateHitCountResult;
    ajaxQueue.add(q);
}
