
//GENERIC CLASSES

var Tabs = Class.create();
Tabs.prototype = {
	initialize: function(options) {
		this.options = Object.extend({
			tab_container_id:'tab_container',
			tab_button_controls_class:'img.tab_button_control',
			tab_button_off_class:'table.tab_button_off',
			tab_button_on_class:'table.tab_button_on',
			tab_content_class:'div.tab_content'
		}, options || { });
				
		this.create();
	},
	create: function() {
		this.tab_container = $(this.options.tab_container_id);
		if(this.tab_container) {
			var me = this;
			me.tab_button_controls = this.tab_container.getElementsBySelector(this.options.tab_button_controls_class);
			me.tab_button_off = this.tab_container.getElementsBySelector(this.options.tab_button_off_class);
			me.tab_button_on = this.tab_container.getElementsBySelector(this.options.tab_button_on_class);
			me.tab_content = this.tab_container.getElementsBySelector(this.options.tab_content_class);
			me.tab_button_controls.each(function(h,i) {
				var index = i;
				var tab_button_control = h;
				h.onmousedown = function(){
					me.switch_tab(index);
				}
			});
			this.switch_tab(0);
		}
	},
	switch_tab: function(index) {
		var me = this;
		me.before_switch_tab(index);
		me.tab_button_on.each(function(h,i) {
			if(i==index) {
				me.tab_button_on[i].setOpacity(1.0);
				me.tab_content[i].show();
				me.tab_button_controls[i].hide();
			}
			else {
				me.tab_button_on[i].setOpacity(0.0);
				me.tab_content[i].hide();
				me.tab_button_controls[i].show();
			}
		});
		me.after_switch_tab(index);
	},
	before_switch_tab: function(index) {
		/*OVERRIDABLE*/
	},
	after_switch_tab: function(index) {
		/*OVERRIDABLE*/
	}
};

var AccessoriesFeatureExpandable = Class.create();
AccessoriesFeatureExpandable.prototype = {
	initialize: function(options) {
		this.options = Object.extend({
			container_id:'accessories_expandable',
			expandable_control:'tr.expandable_control',
			expandable_content:'div.expandable_content'
		}, options || { });
		
		this.create();
	},
	create: function() {
		this.container = $(this.options.container_id);
		if(this.container) {
			var me = this;
			me.expandable_controls = this.container.getElementsBySelector(this.options.expandable_control);
			me.expandable_contents = this.container.getElementsBySelector(this.options.expandable_content);
			me.expandable_controls.each(function(h,i) {
				var index = i;
				h.onmousedown = function(){
					me.open(index);
				}
			});
		}
	},
	open: function(index) {
		var me = this; 
        if(me.expandable_contents[index].visible()) {
            me.expandable_contents[index].hide();
        }
        else {
            var url = '/AjaxAccessoryFeatures.aspx';
            var pars = 'specitemid=' + me.expandable_contents[index].id;
            new Ajax.Updater(me.expandable_contents[index], url,{
	            method: 'get',
	            parameters: pars,
	            evalScripts:true,
	            onComplete: function() {
		            me.expandable_contents.each(function(h,i) {			    
			            if(index==i) {
			                h.show();
			            }
			            else {
			                h.hide();
			            }
		            });
	            }
            });
        }
	}
};

var HomePageAutoPlay = Class.create();
HomePageAutoPlay.prototype = {
	initialize: function(options) {
		this.options = Object.extend({
			container_id:'news_item_container'
		}, options || { });
						
		this.create(options);
	},
	create: function(options) {
		this.index = 0;
		this.auto_play = true;
		this.container = $(this.options.container_id);
		if(this.container) {
		    this.news_items = this.container.getElementsBySelector('div.news_item_link');
		    this.flash_items = this.container.getElementsBySelector('object');
		    if(0 == this.flash_items.length)
		       this.flash_items = this.container.getElementsBySelector('embed');
		    		    
		    var me = this;
		    me.news_items.each(function(h,i) {
				h.onmouseover = function(){
				    me.play(i);
				    me.auto_play = false;
				}
				h.onmouseout = function(){
				    me.auto_play = true;
				}
			});
			
			me.flash_items.each(function(h,i) {
			    if(me.index == i) {
			        h.show();
			        if(h.animation_control!=null)
				        h.animation_control('start');
			    } else {
			        if(h.animation_control!=null)
				        h.animation_control('stop');
				    h.hide();
			    }
			});
		}
	},
	stop: function() {
	    var me = this;
	    me.auto_play = false;
	    me.flash_items.each(function(h,i) {
		    if(h.animation_control!=null)
                h.animation_control('stop');
			h.hide();
		});
	},
	start: function() {
	    var me = this;
	    me.auto_play = true;
	    me.flash_items.each(function(h,i) {
		    if(me.index == i) {
		        h.show();
		        if(h.animation_control!=null)
			        h.animation_control('start');
		    }
		});
	},
	play: function(ix) {
		this.index = ix;
		if(this.news_items.length <= this.index)
		    this.index = 0;
		var me = this;
	    me.news_items.each(function(h,i) {
			if(me.index == i) {
			    h.removeClassName('HomeNewsItem');
			    h.addClassName('HomeNewsItemHover');			    
			} else {
			    h.removeClassName('HomeNewsItemHover');
			    h.addClassName('HomeNewsItem');
			}
		});
		me.flash_items.each(function(h,i) {			
			if(me.index == i) {
			    h.show();
			    if(h.animation_control!=null)
				    h.animation_control('start');
			} else {
			    if(h.animation_control!=null)
				    h.animation_control('stop');
				h.hide();
			}
		});
	},
	play_next_animation: function() {
		if(this.auto_play) {
		    this.index++;
		    this.before_play(this.index);
		    this.play(this.index);
		    this.after_play(this.index);
		}
	},
	before_play: function(index) {
		/*OVERRIDABLE*/
	},
	after_play: function(index) {
		/*OVERRIDABLE*/
	}
};

/*This should be rewritten so that the Auto Play mechanism is the base class of this one.
That way all of the index accounting could be handled in one easy to understand place.
But there is no time.*/
var HomePageMachine = Class.create();
HomePageMachine.prototype = {
	initialize: function(options) {			
		this.create();
	},
	create: function() {
		var me = this;
		
		//Create the auto play object
		me.top_news_auto_play = new HomePageAutoPlay();
		me.top_news_auto_play.before_play = function(ix) {
		    if(me.top_news_auto_play.news_items.length <= ix)
		        ix = 0;
		    if(ix % 4 == 0) {
		        var index = Math.floor(ix/4.0);
		        me.news_sub_tabs.switch_tab(index); 
		    }
		}
		
		//Create the Top News / Top Search Tabs object. Override the before_switch_tab member
		//to stop the animation
		var tab_options = Object.extend({
			tab_container_id:'tab_container',
			tab_button_controls_class:'img.tab_button_control',
			tab_button_off_class:'div.tab_button_off',
			tab_button_on_class:'div.tab_button_on',
			tab_content_class:'div.tab_content'
		}, { });
		me.news_and_search_tabs = new Tabs(tab_options);
		me.news_and_search_tabs.before_switch_tab = function(index) {
		    if(index==2) {
	            me.top_news_auto_play.stop();
	            Element.show('top_searches_cube');
	        } else {
	            me.top_news_auto_play.start();
	            Element.hide('top_searches_cube');
	        }
		}
		
		//Create the Sub Tabs (paging) object. Override the before_switch_tab member
		//to start the first animation on this tab.
		var sub_tab_options = Object.extend({
			tab_container_id:'tab_container',
			tab_button_controls_class:'img.sub_tab_button_control',
			tab_button_off_class:'div.sub_tab_button_off',
			tab_button_on_class:'div.sub_tab_button_on',
			tab_content_class:'div.sub_tab_content'
		}, { });
		me.news_sub_tabs = new Tabs(sub_tab_options);
		me.news_sub_tabs.before_switch_tab = function(index) {
		    var ix = index*4;
		    me.top_news_auto_play.play(ix);
		}
	},
	play_next_animation: function() {
		this.top_news_auto_play.play_next_animation();
	}
};

/*This is the TextBoxControl object. It manages all text box events and calls its
submit member function when the provided text is not the default value and is not
empty. The default value is defined in the title attribute.*/
var TextBoxControl = Class.create();
TextBoxControl.prototype = {
	initialize: function(text_box) {
		var me = this;
		me.text_box = $(text_box);
		if(me.text_box) {
			me.input= me.text_box.getElementsBySelector('input.search_box_field');
			me.down_button= me.text_box.getElementsBySelector('div.go_button');
			if(me.input && me.down_button) {
				if(0 < me.input.length && 0 < me.down_button.length) {
					me.input = me.input[0];
					me.down_button = me.down_button[0];
					me.input.onfocus = function() {
						if(me.input.value==me.input.title) {
							me.input.value='';
						}
					}
					me.input.onblur = function() {
						if(me.input.value=='') {
							me.input.value=me.input.title;
						}
					}
					me.input.onkeydown = function(event){
						var keycode = null;
						if (window.event)
							keycode = window.event.keyCode;
						else if (event)
							keycode = event.which;
						//sumbit on enter
		                if (keycode == 13) {
							if(me.input.value!='') {
								me.submit(me.text_box.id, me.input.value);
							}
							return false;
						}
					}
					me.down_button.onclick = function() {
						if(me.input.value!='' && me.input.value!=me.input.title) {
							me.submit(me.text_box.id, me.input.value);
						}
						else {
							me.input.value='';
							me.input.focus();
						}
					}
					me.submit();
				}
			}
		}
	},
	reset: function() {
		this.input.value=this.input.title;
	},
	focus: function() {
		this.input.focus();
	},
	submit: function() {
		var me = this;
		if(me.input.value!=me.input.title) {
		    me.execute(me.text_box.id, me.input.value);
	    }
	},
	execute: function(id, value) {
		//alert(id + ':' + value);
	}
};

/*This is the DropDownControl object. It manages all dropdown events and calls its
submit member function when the selection is not the default value and is not empty.
The default value is defined in the title attribute.*/
var DropDownControl = Class.create();
DropDownControl.prototype = {
	initialize: function(dropdown_control) {
		var me = this;
		me.dropdown_control = $(dropdown_control);
		if(me.dropdown_control) {
			me.is_dropdown_event=false;
			me.input= me.dropdown_control.getElementsBySelector('input.inputfield');
			me.select=me.dropdown_control.getElementsBySelector('a.select');
			me.dropdown=me.dropdown_control.getElementsBySelector('div.dropdown');
			me.down_button = me.dropdown_control.getElementsBySelector('div.down_button');
			me.options = me.dropdown_control.getElementsBySelector('a.option');
			if(me.select && me.down_button && me.dropdown) {
				if(0 < me.select.length && 0 < me.down_button.length && 0 < me.dropdown.length) {
					me.input = me.input[0];
					me.select = me.select[0];
					me.dropdown = me.dropdown[0];
					me.down_button = me.down_button[0];
					me.select.onfocus = function() {
						me.select.addClassName('selected');
						me.dropdown.show();
					}
					me.select.onblur = function() {
						me.select.removeClassName('selected');
					}
					me.select.onmousedown = function() {
						me.is_dropdown_event=true;
						me.dropdown.toggle();
					}
					me.select.onkeydown = function(event){
						if(me.select.innerText!= me.select.getAttribute('title')) {
							me.onkeydown(event);
						}
					}
					me.down_button.onmousedown = function() {
					    me.is_dropdown_event=true;
						me.dropdown.toggle();
					}
					me.options.each(function(h,i) {
						var ix = i;
						var o = h;
						o.onfocus = function() {
							o.addClassName('selected');
							me.select.innerHTML=o.innerHTML;
							me.input.value=o.innerHTML;
						}
						o.onblur = function() {
							o.removeClassName('selected');
						}
						o.onmouseover = function() {
							o.addClassName('selected');
						}
						o.onmouseout = function() {
							o.removeClassName('selected');
						}
						o.onmousedown = function(){
							me.dropdown.hide();
							me.select.innerHTML=o.innerHTML;
							me.input.value=o.innerHTML;
							me.submit(me.dropdown_control.id, me.input.value);
						}
						if(i == me.options.length-1) {
							o.onblur = function(){
								o.removeClassName('selected');
								me.dropdown.hide();
							}
						}
						o.onkeydown = function(event){
							me.onkeydown(event);
						}
					});
					
					me.eventHideDropdown = me.hide_dropdown.bindAsEventListener(me);
					Event.observe($('body') , 'click', me.eventHideDropdown);
					
					me.submit();
				}
			}
		}
	},
	hide_dropdown: function(event) {
		if(!this.is_dropdown_event)
			this.dropdown.hide();
		this.is_dropdown_event = false;
	},
	onkeydown: function(event) {
		var me = this;
		var keycode = null;
		if (window.event)
			keycode = window.event.keyCode;
		else if (event)
			keycode = event.which;
		//sumbit on enter
		if (keycode == 13) {
			if(me.select.innerHTML!='') {
				me.dropdown.hide();
				me.input.value=me.select.innerHTML
				me.submit(me.dropdown_control.id, me.input.value);
			}
			return false;
		}
	},
	reset: function() {
		this.select.innerHTML=this.select.title;
	},
	submit: function() {
		var me = this;
		if(me.select.innerHTML!= me.select.getAttribute('title')) {
			me.input.value=me.select.innerHTML;
			me.execute(me.dropdown_control.id, me.input.value);
		}
	},
	execute: function(id, value) {
		//alert(id + ':' + value);
	}
};