﻿/*

Option Rotator(ORC) Function Calls

-When supplying image paths, you should create .gif and .png versions of all images to maximize
compatibility.  Also, do not include ".gif or .png" in the image path - this is
appended after checking the browser version.  If you do not have png versions, set ORC.force_gif = true


ORC.CustomizeSelectors(a, b) - Change Empty/Full images for the rotator selectors
a = (string) path to "empty" version of selector image (do not include '.' or file extension)
b = (string) path to "full" version of selector image (do not include '.' or file extension)

ORC.CustomizeArrows(a, b) - Change Previous and Next arrow images
a = (string) path to previous arrow image (do not include '.' or file extension)
b = (string) path to next arrow image (do not include '.' or file extension)

ORC.Load(a,b,c,d,e)
a = (string) wrapper div that controls will be appended into
b = (int) total number of options to cycle between
c = (int) delay in milliseconds between auto-cycle, enter 0 to disable auto-cycle
d = (int) start index(1-based), enter 0 for random (empty/out-of-range/NaN default to 1)
e = (string) name of javascript update function to call on each cycle - do not include '()'

*/

function ORC(prefix){

    this.wrapper = "";
    this.MIN_OPTION_NUMBER = 1;
    this.MAX_OPTION_NUMBER = 1;
    this.curOption = 1;
    this.delay = 0;
    this.timerId = "";
    this.updateFunction = "";

    this.stopped = false;
    this.imageExtension = '.PNG'
    this.force_gif = false

    // Selector and Arrow Images
    this.prev_arrow = '/Images/rotator/arrowNav-Left';
    this.selector_empty = '/Images/rotator/arrowNav-empty';
    this.selector_full = '/Images/rotator/arrowNav-full';
    this.next_arrow = '/Images/rotator/arrowNav-Right';
    this.prefix = prefix;

    // TO-DO: set access modifiers
    this.CustomizeSelectors = CustomizeSelectors;
    this.CustomizeArrows = CustomizeArrows;
    this.CreateControl = CreateControl;
    this.Update = Update;
    this.Wend = Wend;
    this.Navigate = Navigate;
    this.Select = Select;
    this.Calc = Calc;
    this.GetImageExtension = GetImageExtension;
    this.SetSelectors = SetSelectors;
    this.Hover = Hover;
    this.CreateControl = CreateControl;
    this.Load = Load;

    var self = this;

    function CustomizeSelectors(empty_img_path, full_img_path){
        self.selector_empty = empty_img_path;
        self.selector_full = full_img_path;
    }

    function CustomizeArrows(prev_img_path, next_img_path){
        self.prev_arrow = prev_img_path;
        self.next_arrow = next_img_path;
    }

    function Load(wrapper_div, numOptions, interval, start_index, update_function){
        
        self.wrapper = document.getElementById(wrapper_div);
        
        self.MAX_OPTION_NUMBER = numOptions;
        self.updateFunction = update_function;
        
        if(start_index == 0)
        { 
            self.curOption = Math.floor(Math.random() * self.MAX_OPTION_NUMBER + self.MIN_OPTION_NUMBER); 
        }
        else
        {
            self.curOption = parseInt(start_index);
        }
        
        if(self.curOption < self.MIN_OPTION_NUMBER || self.curOption > self.MAX_OPTION_NUMBER || isNaN(self.curOption)) { self.curOption = 1 }

        self.GetImageExtension();
        self.CreateControl();
        self.SetSelectors();
        
        self.Update();
        
        if(interval != 0)
        {
            self.delay = interval;
            self.timerId = setTimeout(function(){ self.Wend(1); }, self.delay);
        }
        if(interval == 0)
        {
            self.stopped = true;
        }

    }

    function Wend(dir) {
        self.curOption = self.Calc(self.curOption + dir);
        self.Update();
        if(!self.stopped)
        {
            self.timerId = setTimeout(function(){ self.Wend(1); }, self.delay);
        }
    }
    function Navigate(dir) {
        self.stopped = true;
        clearTimeout(self.timerId);
        self.Wend(dir);
    }

    function Update(){
        eval(self.updateFunction + "(" + self.curOption + ")");  
        self.SetSelectors();
    }
    
    function Select(dotNum){
        self.curOption = dotNum;
        self.stopped = true;
        clearTimeout(self.timerId);
        self.Update()
    }

    function Calc(check_num){
        if (check_num > self.MAX_OPTION_NUMBER) { return self.MIN_OPTION_NUMBER; }
        if (check_num < self.MIN_OPTION_NUMBER) { return self.MAX_OPTION_NUMBER; }
        return check_num;
    }

    function GetImageExtension() {
        if (navigator.appVersion.indexOf('MSIE 6.0') > 0 || self.force_gif == true)
            self.imageExtension = '.gif';
    }

    function SetSelectors(){
        for(var i = self.MIN_OPTION_NUMBER; i <= self.MAX_OPTION_NUMBER; i++)
        {
            var sel = document.getElementById(self.prefix + i);
            if (self.curOption == i) { sel.src = self.selector_full + self.imageExtension; }
            else { sel.src = self.selector_empty + self.imageExtension; }
        }
    }

    function Hover(img, state) {
        if(state == 'on') { img.src = self.selector_full + self.imageExtension; }
        if(state == 'off') 
        {
            if(img.id != self.prefix + self.curOption) { img.src = self.selector_empty + self.imageExtension; }
        }
    }


    function CreateControl(){
        // Previous Arrow
        var sel = $(document.createElement("img"))
        .attr('src', self.prev_arrow + self.imageExtension)
        .attr('id', self.prefix + 'prev')
        .click(function(){
            self.Navigate(-1);
        })
        .appendTo(self.wrapper);
        
        //Selectors
        for(var i = this.MIN_OPTION_NUMBER; i <= this.MAX_OPTION_NUMBER; i++)
        (function(i, self){
        var sel = $(document.createElement("img"))
        .attr('src', self.selector_empty + self.imageExtension)
        .attr('id', self.prefix + i )
        .mouseover(function(){ 
            self.Hover(this, 'on');
        })
        .mouseout(function(){ 
            self.Hover(this, 'off');
        })
        .click(function(){
            self.Select(i);
        })
        .appendTo(self.wrapper);
        //alert(t.prefix);
        })(i, self);
        
        // Next Arrow
        var sel = $(document.createElement("img"))
        .attr('src', self.next_arrow + self.imageExtension)
        .attr('id', self.prefix + 'next')
        .click(function(){
            self.Navigate(1);
        })
        .appendTo(self.wrapper);
    }
}