﻿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");
                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;
    }

    function slideSwitch2(id) {
        var $previewactive = $('#slideshow DIV.previewactive');
        var $next = $("#" + id);

        if ($next.attr("id") != $previewactive.attr("id")) {
            $previewactive.addClass('previewlast-activ');

            $next.css({ opacity: 0.0 })
        .addClass('previewactive')
          .animate({ opacity: 1.0 }, 0, function() {
              $previewactive.removeClass('previewactive previewlast-activ');
          });

        }
        else {
            // alert("They are same!!! Cause nothing")


        }
    }



    carouselApi = $("div.scrollable").scrollable({ size: carouselSize, api: true, time: 0 });

    var interval;

    if (autoPlay) {
       
        this1.everyTime(parseInt(slideDelay), 'controlled', function() {

            slideSwitch();
        });
        $(this).stopTime('controlled');

    }



    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 (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]);
        }



    });





    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
        var $next = $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.addClass('previewlast-activ');

        $next.css({ opacity: 0.0 })
        .addClass('previewactive')
        .animate({ opacity: 1.0 }, 0, function() {
            $previewactive.removeClass('previewactive previewlast-activ');
        });
    }




});

function GetUpdateHitCountResult(result) {
}

function UpdateHtCounter(id) {
    var q = $IntenixAjaxOptions;
    q.url = asmxImageGallery + "UpdateGalleryImageHitCount";
    q.data = "{imageID:" + id + "}";
    q.success = GetUpdateHitCountResult;
    ajaxQueue.add(q);
}



   

    

  
