var EDCommon = {};

EDCommon.Categories = {
	bindTitleAccordion: function(){
		jQuery('.block-layered-nav a.layered-accordion').bind('click', function(ev){
			EDCommon.Categories.toggleAccordion(this);
		});
	},
	
	toggleAccordion: function(elm){
		jQuery(elm).parent().next().slideToggle(500).parent().toggleClass('yes-buttons');
	}
};

EDCommon.Slider = {
	current: '',
	display: function(intIndex, obj){
		jQuery('#home-slider').cycle(intIndex).cycle('pause');
		var objAnchor = jQuery(obj);
		objAnchor.parent().siblings().removeClass('active');
		objAnchor.parent().addClass('active');
	}
};

EDCommon.OnePageCheckout = {
	getSameAsBilling: function(objInput){
		var objEl = jQuery(objInput);
		
		if(objEl.val() == ''){
			objEl.val('1');
			return true;
		}else{
			objEl.val('');
			return false;	
		}
	},
	
	dropdownTheme: function(){
		jQuery('.dropdown').dropdownchecklist();
	}
};


function addNewItem(){
	
	var objItem = jQuery('<li />')
					.append(
						jQuery('<a/>').attr('href', '#')
					);
	
	jQuery('.change-photo ul').append(objItem);
}


/*** OVERRIDES/EXTENDS ***/
(function(jQuery) {
		
	jQuery.extend(jQuery.ui.dropdownchecklist.prototype, {
		// Creates the control that will replace the source select and appends it to the document
        // The control resembles a regular select with single selection
        _appendControl: function() {
            var self = this, options = this.options, sourceSelect = this.sourceSelect;

            // the controls is wrapped in a span with inline-block display
            var wrapper = jQuery("<span/>");
            wrapper.addClass("ui-dropdownchecklist-wrapper");
            wrapper.css({
                display: "inline-block",
                cursor: "pointer"
            });

            // the actual control, can be styled to set the border and drop right image
            var control = jQuery("<span/>");
            control.addClass("ui-dropdownchecklist");
            control.css({
                display: "inline-block"
            });
			control.attr("tabIndex", 0);
			control.keyup(function(e) {self._handleKeyboard(e)});
            wrapper.append(control);

            // the text container keeps the control text that is build from the selected (checked) items
            var textContainer = jQuery("<span/>");
            textContainer.addClass("ui-dropdownchecklist-text")
            textContainer.css({
                display: "inline-block",
                overflow: "hidden"
            });
            control.append(textContainer);

            // add the hover styles to the control
            wrapper.hover(function() {
                if (!self.disabled) {
                    control.toggleClass("ui-dropdownchecklist-hover")
                }
            }, function() {
                if (!self.disabled) {
                    control.toggleClass("ui-dropdownchecklist-hover")
                }
            });

            // clicking on the control toggles the drop container
            wrapper.click(function(event) {
                if (!self.disabled) {
                    event.stopPropagation();
                    self._toggleDropContainer();
                }
            })

            wrapper.insertAfter(sourceSelect);

            return wrapper;
        },
		// Creates a drop item that coresponds to an option element in the source select
        _createDropItem: function(index, value, text, checked, indent) {
            var self = this;
            // the item contains a div that contains a checkbox input and a span for text
            // the div
            var item = jQuery("<div/>");
            item.addClass("ui-dropdownchecklist-item");
            item.css({whiteSpace: "nowrap"});
            var checkedString = checked ? ' checked="checked"' : '';
			var idBase = (self.sourceSelect.attr("id") || "ddcl");
			var id = idBase + index;
            var checkBox;
            if (self.initialMultiple) {
                // the checkbox
                checkBox = jQuery('<input type="checkbox" id="' + id + '"' + checkedString + '/>');
            } else {
                // the radiobutton
                checkBox = jQuery('<input type="radio" id="' + id + '" name="' + idBase + '"' + checkedString + '/>');
            }
            checkBox = checkBox.attr("index", index).val(value);
            item.append(checkBox);
            // the text
            var label = jQuery("<label for=" + id + "/>");
            label.addClass("ui-dropdownchecklist-text")
                .css({
                    cursor: "pointer",
                    width: "100%"
                })
                .text(text);
			if (indent) {
				item.addClass("ui-dropdownchecklist-indent");
			}
            item.append(label);
            item.hover(function() {
                item.addClass("ui-dropdownchecklist-item-hover")
            }, function() {
                item.removeClass("ui-dropdownchecklist-item-hover")
            });
            // clicking on the checkbox synchronizes the source select
            checkBox.click(function(e) {
                e.stopPropagation();
                self._syncSelected(jQuery(this));
                self.sourceSelect.trigger("change");
                jQuery(document).trigger("click");
            });
            // check/uncheck the item on clicks on the entire item div
            var checkItem = function(e) {
                e.stopPropagation();
                var checked = checkBox.attr("checked");
                checkBox.attr("checked", !checked)
                self._syncSelected(checkBox);
                self.sourceSelect.trigger("change");
                jQuery(document).trigger("click");
            }
            label.click(function(e) {e.stopPropagation()});
            item.click(checkItem);
			item.keyup(function(e) {self._handleKeyboard(e)});
            return item;
        }
	});
})(jQuery);