/*

	Class to create a circular carousel
	Author: George Blazer

*/
var LnCarousel = new Class({
	Implements: [Events, Options, Chain],
	options: {
		startIndex: 0,
		slides: [],
		sliderWidth: 99999,
		next_link: false,
		prev_link: false,
		container: null
	},
	initialize: function(container, options){
		this.setOptions(options);
		this.steps_on_click = this.options.steps_on_click;
		this.starting_element = this.options.starting_element;
		this.next_link = this.options.next_link;
		this.prev_link = this.options.prev_link;
		this.now = this.options.startIndex;
		this.container = $(container);
		this.container.setStyles({
			width: this.options.sliderWidth,
            position: 'relative'
		});
        /* life is beautiful, ie sucks! */
        this.container.getParent('div').setStyle('position', 'relative');
		this.fx = this.options.fx;
		this.handleListeners();
		this.init_slides();
		this.num_of_slides = this.container.getChildren('div').length;
	},
	reset_slides: function(type) {
		/* 
			set initial slide to 1 
			currently hardcoded, needs to be a configurable value
		*/
		
		if (type == 'prev') {
			this.container.setStyle('left', -(this.num_of_slides-2)*this.container.getChildren('div')[0].getSize().x);
			this.now = this.num_of_slides-2; /* -2 is to the exclude first and last clones */
		} else {
			this.container.setStyle('left', -this.container.getChildren('div')[0].getSize().x);
			this.now = 1;
		}	
	},
	log: function() {
		console.log('now#' + this.now);
		console.log('now: ' + this.container.getChildren('div')[this.now].getProperty('alt'));
		console.log('last one: ' + this.last_clone.getProperty('alt'));
		console.log(this.container);
	},
	init_slides: function() {			
		/* 
			set ALT tags dynamically
		*/
		this.container.getChildren('div').each(function(item, index) {
			item.setProperty('alt', 'slide_' + (index+1));
		})

		/* 
			clones the last slide and sticks it in front of the first one 
			so that if you click prev slide, you have something to go to 
		*/
		this.last_clone = this.container.getChildren('div').getLast().clone();
		this.first_clone = this.container.getChildren('div')[0].clone();
		
		this.first_clone.injectAfter(this.container.getChildren('div').getLast());
		this.last_clone.injectBefore(this.container.getChildren('div')[0]);
		
		this.reset_slides();
		
	},
	handleListeners: function() {
		// attaching event handler so the next button
		this.next_link.addEvent('click', function(e) {		
		e.stop();
				
		// if ((e.target.getProperty('class') != 'carousel') && (e.target.get('tag') != 'a')) {
		// 			e.target = e.target.getParent('div.carousel')
		// 		}
		// 		
		// 		num_of_steps = ($defined(e.target.retrieve('steps'))) ? e.target.retrieve('steps') : 1;
		
		num_of_steps = 1;

		this.fx.start($(this.container).getStyle('left').toInt()-this.container.getChildren('div')[0].getSize().x*num_of_steps).chain(
			function() {
				this.now++;				

				if ( this.container.getChildren('div').getLast().getProperty('alt') == this.container.getChildren('div')[this.now].getProperty('alt')) {
					this.reset_slides('next');
				} 
			}.bind(this));
		}.bind(this));
		
		this.prev_link.addEvent('click', function(e) {					
		
		this.fx.start($(this.container).getStyle('left').toInt()+this.container.getChildren('div')[0].getSize().x).chain(
			function() {
				this.now--;		
				if ( this.last_clone.getProperty('alt') == this.container.getChildren('div')[this.now].getProperty('alt') ) {
					this.reset_slides('prev');
				} 
			}.bind(this));
		}.bind(this));
	},
	status: function() {
		console.log(this);
	}
});
/* including flash here */
var the_movie = new Swiff(LIVENATION.config.static_server_path + 'pages/vipnation/vip_flash_fade.swf', {
    id: 'myBeautifulMovie',
    width: 743,
    height: 288,
    container: $('flash_container'),
    params: {
        wmode: 'opaque',
        bgcolor: '#000000'
    }
});

if ($('large_slideshow') != null) {
    var main_carousel = new LnCarousel('large_slideshow', {
        next_link: $('next_link'),
        prev_link: $('prev_link'),
        steps_on_click: 1,
        fx: new Fx.Tween($('large_slideshow'), {
            property: 'left'
        })
    });
    teaser_carousel = new LnCarousel('small_slideshow', {
        next_link: $('next_link'),
        prev_link: $('prev_link'),
        steps_on_click: 1,
        fx: new Fx.Tween($('small_slideshow'), {
            property: 'left'
        })
    });

    second_teaser_carousel = new LnCarousel('small_slideshow_2', {
        next_link: $('next_link'),
        prev_link: $('prev_link'),
        steps_on_click: 2,
        fx: new Fx.Tween($('small_slideshow_2'), {
            property: 'left'
        })
    });
        
    /* attaching events */
    $('small').cloneEvents($('next_link'));
    $('small2').cloneEvents($('next_link'));

    /* sets the number of steps to take */
    $('small').retrieve('steps', 1);
    $('small2').retrieve('steps', 1);
}

if ($('vip_select_city') != null) {
    $('vip_select_city').addEvent('click', function(e) {
        $('module.ln_box.select_another_city').setStyles({
            'display' : 'block',
            'top' : e.target.getCoordinates().top + 15,
            'left' : e.target.getCoordinates().left
        }); 
        $('vip_select_another_city').addEvent('change', function(e) {
            e.target.getParent('form').submit();
        });
    });
    $('city_selector_close').addEvent('click', function(e) {
        $('module.ln_box.select_another_city').setStyle('display', 'none');
    });
}

if ($$('table.comparison_chart tbody tr').length) {
    $$('table.comparison_chart tbody tr').addEvents({
        'mouseenter' : function(e) {
            e.stopPropagation();
            $(e.target).getParent('tr').addClass('active');
        },
        'mouseleave' : function(e) {
            e.stopPropagation();
            $(e.target).getParent('tr').removeClass('active');
        }
    });

    $$('div.prg').set('tween', {duration: 'long'});
    var expand = false;
    $$('a.expand').addEvent('click', function(e) {
        e.preventDefault();
        if (Boolean(expand)) {
            $$('div.prg').tween('height', '40px');
            expand = false;
            $$('a.expand').set('html', expand_str);
        } else {
            $$('div.prg').tween('height', '350px');
            expand = true;
            $$('a.expand').set('html', collapse_str);
        }
    });

}

if ($('vipnation.select_another_city') != null) {
    $('vipnation.select_another_city').addEvent(
        'change',
        function(a) {
            $('vipnation.request_information').submit()
        }
    )
}
LIVENATION.module.vip_contact_us = function(item_data) {
    var module_id = item_data.module_id;
    
    var attach_form_listeners = function() {
        $$('div.submit input[alt=Cancel]').addEvent('click', function(e) {
            e.preventDefault();
            detach_form_listeners();
        });
        $$('div.vip_contact_us_header img.close').cloneEvents($$('div.submit input[alt=Cancel]')[0]);

        $$('div.submit input[alt=Submit]').addEvent('click', function(e) {
            e.preventDefault();
            $('vip_contact_us_form').set('send', {
                onSuccess: function(response) {
                    var _response = JSON.decode(response);
                    if ($defined(_response.errors) && (_response.errors.length > 0)) {
                        $$('div.vip_contact_us_error').setStyle('display', 'block');
                    } else {
                        $$('div[class=module.vip_thank]').setStyles({
                            'display' : 'block',
                            'left' : (document.getSize().x - $(module_id).getSize().x)/2,
                            'top' : 150
                        });
                        detach_form_listeners();
                    }
                },
                onFailure: function(response) {
                    var _response = JSON.decode(response);
                }   
            });
            $('vip_contact_us_form').send();
        });
    }

    
    var detach_form_listeners = function() {
        mySticky.hide();
        $$('div.vip_contact_us_error').setStyle('display', 'none');

        $$('div.submit input[alt=Submit]').removeEvents();
        $$('div.submit input[alt=Cancel]').removeEvents();
    }

    $$('a.vip_contact_us_link').addEvent('click', function() {
        $('vip_contact_us_form').reset();
        mySticky = new StickyWin.Modal({
            content: $('module.ln_box.vip_contact_us'),
            relativeTo: $('top'),
            zIndex: 80,
            position: 'center',
            offset: {
                x: -($('module.ln_box.vip_contact_us').getSize().x/2),
                y: 200
            },
            modalOptions: {
                hideOnClick: false,
                modalStyle:{
                    'background-color':'#000',
                    'opacity':.3,
                    'z-index':15
                }
            }
        });

        attach_form_listeners();

    });

}

window.addEvent('domready', function() {
    if (LIVENATION.instances.vip_contact_us !== 'object') {
        LIVENATION.instances.vip_contact_us = new Array();
    }
    for (i = 0; i < LIVENATION.registry.vip_contact_us.length; i++) {
        LIVENATION.instances.vip_contact_us.push(new LIVENATION.module.vip_contact_us(LIVENATION.registry.vip_contact_us[i]));
    }
});
LIVENATION.module.vip_faq = function(item_data) {
    var module_id = item_data.module_id;

    $$('a.vip_faq_link').addEvent('click', function() {
        mySticky = new StickyWin.Modal({
            content: $('module.ln_box.vip_faq'),
            relativeTo: $('top'),
            zIndex: 80,
            position: 'center',
            offset: {
                x: -($('module.ln_box.vip_faq').getSize().x/2),
                y: 200
            },
            modalOptions: {
                hideOnClick: false,
                modalStyle:{
                    'background-color':'#000',
                    'opacity':.3,
                    'z-index':15
                }
            }
        });

        $$('div.vip_faq_header img.close').addEvent('click', function(e) {
            mySticky.hide();
        });
    });
}

window.addEvent('domready', function() {
    if (LIVENATION.instances.vip_faq !== 'object') {
        LIVENATION.instances.vip_faq = new Array();
    }
    for (i = 0; i < LIVENATION.registry.vip_faq.length; i++) {
        LIVENATION.instances.vip_faq.push(new LIVENATION.module.vip_faq(LIVENATION.registry.vip_faq[i]));
    }
});
LIVENATION.module.vip_programs = function(item_data) {
    var module_id = item_data.module_id;

    $$('div.vip_programs ul li[class!=active]').addEvents({
        'mouseenter' : function(e) {
            e.stopPropagation();
            e.target = ($(e.target).get('tag') == 'li') ? e.target : $(e.target).getParent('li');
            e.target.addClass('active');
         },
        'mouseleave' : function(e) {
            e.stopPropagation();
            e.target = ($(e.target).get('tag') == 'li') ? e.target : $(e.target).getParent('li');
            e.target.removeClass('active');
         }
    });
}

window.addEvent('domready', function() {
    if (LIVENATION.instances.vip_programs !== 'object') {
        LIVENATION.instances.vip_programs = new Array();
    }
    for (i = 0; i < LIVENATION.registry.vip_programs.length; i++) {
        LIVENATION.instances.vip_programs.push(new LIVENATION.module.vip_programs(LIVENATION.registry.vip_programs[i]));
    }
});
LIVENATION.module.vip_more_info = function(item_data) {
    var module_id = item_data.module_id;

    $(module_id + '_select').addEvent('change', function(e) {
        e.target.getParent('form').submit();
    });

}

window.addEvent('domready', function() {
	if (LIVENATION.instances.vip_more_info !== 'object') {
		LIVENATION.instances.vip_more_info = new Array();
	}

	if ($type(LIVENATION.registry.vip_more_info) == 'array') {
		for (i = 0; i < LIVENATION.registry.vip_more_info.length; i++) {
			LIVENATION.instances.vip_more_info.push(new LIVENATION.module.vip_more_info(LIVENATION.registry.vip_more_info[i]));
		}
	}

})
/*
LIVENATION.module.vip_venues = function(item_data) {
    var module_id = item_data.module_id;

}

window.addEvent('domready', function() {
    if (LIVENATION.instances.vip_venues !== 'object') {
        LIVENATION.instances.vip_venues = new Array();
    }
    for (i = 0; i < LIVENATION.registry.vip_venues.length; i++) {
        LIVENATION.instances.vip_venues.push(new LIVENATION.module.vip_venues(LIVENATION.registry.vip_venues[i]));
    }
});
*/
LIVENATION.module.vip_thank = function(item_data) {
    var module_id = item_data.module_id;

    $$('.thank_close').addEvent('click', function() {
        $(module_id).setStyle('display', 'none');
    });
}

window.addEvent('domready', function() {
    if (LIVENATION.instances.vip_thank !== 'object') {
        LIVENATION.instances.vip_thank = new Array();
    }
    for (i = 0; i < LIVENATION.registry.vip_thank.length; i++) {
        LIVENATION.instances.vip_thank.push(new LIVENATION.module.vip_thank(LIVENATION.registry.vip_thank[i]));
    }
});
LIVENATION.module.vip_already_member = function(item_data) {
    var module_id = item_data.module_id;

    $(module_id + '_vip_sign_in').addEvent('click', function(e) {
        e.preventDefault();
        if ($(module_id + '_site_selector').selectedIndex > 0)
            window.location = $(module_id + '_site_selector').options[$(module_id + '_site_selector').selectedIndex].value;
    });   
}

window.addEvent('domready', function() {
    
    if (LIVENATION.instances.vip_already_member !== 'object') {
        LIVENATION.instances.vip_already_member = new Array();
    }
    for (i = 0; i < LIVENATION.registry.vip_already_member.length; i++) {
        LIVENATION.instances.vip_already_member.push(new LIVENATION.module.vip_already_member(LIVENATION.registry.vip_already_member[i]));
    }
});
