﻿Type.registerNamespace('CustomExtenders.ImageCarousel');

CustomExtenders.ImageCarousel.ImageCarouselBehavior = function(element) {

    CustomExtenders.ImageCarousel.ImageCarouselBehavior.initializeBase(this, [element]);

    this._targetImageIDValue = null;
    this._ImageFolderValue = null;
    this._FileMasksValue = null;
    this._PrevButtonIDValue = null;
    this._NextButtonIDValue = null;
    this._FirstButtonIDValue = null;
    this._LastButtonIDValue = null;
    this._RandomButtonIDValue = null;
}

CustomExtenders.ImageCarousel.ImageCarouselBehavior.prototype = {

    initialize : function() {
        CustomExtenders.ImageCarousel.ImageCarouselBehavior.callBaseMethod(this, 'initialize');

        // Initalization code        
        var prevButton = document.getElementById(this._PrevButtonIDValue);
        if (prevButton != undefined){
            prevButton.onclick = Function.createDelegate(this, this._onprevclick);
        }
        
        var nextButton = document.getElementById(this._NextButtonIDValue);
        if (prevButton != undefined){
            nextButton.onclick = Function.createDelegate(this, this._onnextclick);
        }

        var firstButton = document.getElementById(this._FirstButtonIDValue);
        if (firstButton != undefined){
            firstButton.onclick = Function.createDelegate(this, this._onfirstclick);
        }
        
        var lastButton = document.getElementById(this._LastButtonIDValue);
        if (lastButton != undefined){
            lastButton.onclick = Function.createDelegate(this, this._onlastclick);
        }
        
        var randomButton = document.getElementById(this._RandomButtonIDValue);
        if (randomButton != undefined){
            randomButton.onclick = Function.createDelegate(this, this._onrandomclick);
        }
        
    },

    dispose : function() {
        // Cleanup code 
        CustomExtenders.ImageCarousel.ImageCarouselBehavior.callBaseMethod(this, 'dispose');
    },

    // Property accessors 
    get_TargetImageID : function() {
        return this._TargetImageIDValue;
    },

    set_TargetImageID : function(value) {
        this._TargetImageIDValue = value;
    },

    get_PrevButtonID : function(){
        return this._PrevButtonIDValue;
    },
    
    set_PrevButtonID : function(value) {
        this._PrevButtonIDValue = value;
    },

    get_NextButtonID : function(){
        return this._NextButtonIDValue;
    },
    
    set_NextButtonID : function(value) {
        this._NextButtonIDValue = value;
    },

    get_FirstButtonID : function(){
        return this._FirstButtonIDValue;
    },
    
    set_FirstButtonID : function(value) {
        this._FirstButtonIDValue = value;
    },

    get_LastButtonID : function(){
        return this._LastButtonIDValue;
    },
    
    set_LastButtonID : function(value) {
        this._LastButtonIDValue = value;
    },

    get_RandomButtonID : function(){
        return this._RandomButtonIDValue;
    },
    
    set_RandomButtonID : function(value) {
        this._RandomButtonIDValue = value;
    },

    get_ImageFolder : function() {
        return this._ImageFolderValue;
    },
    
    set_ImageFolder : function(value) {
        this._ImageFolderValue = value;
    },

    get_FileMasks : function() {
        return this._FileMasksValue;
    },

    set_FileMasks : function(value) {
        this._FileMasksValue = value;
    },

    // delegates
    _onprevclick : function(){
        var e = this.get_element();
        this.callCarouselService(1, e.src, this._FileMasksValue, this._ImageFolderValue, e.id  );
    },
    
    _onnextclick : function(){
        var e = this.get_element();        
        this.callCarouselService(0, e.src, this._FileMasksValue, this._ImageFolderValue, e.id );
    },
    
    _onfirstclick : function(){
        var e = this.get_element();        
        ImageCarousel.CarouselFunctions.First(this._FileMasksValue, this._ImageFolderValue, this.callSuccess, this.callFail, e.id );
    },

    _onlastclick : function(){
        var e = this.get_element();        
        ImageCarousel.CarouselFunctions.Last(this._FileMasksValue, this._ImageFolderValue, this.callSuccess, this.callFail, e.id );
    },

    _onrandomclick : function(){
        var e = this.get_element();        
        ImageCarousel.CarouselFunctions.GetRandom(this._FileMasksValue, this._ImageFolderValue, this.callSuccess, this.callFail, e.id );
    },

    callCarouselService : function(direction, currFile, FileMasks, folderPath, id){
        ImageCarousel.CarouselFunctions.Get(direction, currFile, FileMasks, folderPath, this.callSuccess, this.callFail, id);
    },
    
    // callback targets
    callSuccess : function(result, context){
        //alert(result);
        var e = document.getElementById(context);
        if (e != undefined){
            e.src = result;
        } else {
            alert('Request succeeded but could not find target (image) object in which to place result');
        }
    },

    callFail : function(result){
        alert('Call failed: ' + result._message);
    }       
}

CustomExtenders.ImageCarousel.ImageCarouselBehavior.registerClass('CustomExtenders.ImageCarousel.ImageCarouselBehavior', AjaxControlToolkit.BehaviorBase);

if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();