
var log = function(msg) {
	if(typeof(console)!='undefined') {
		console.log(msg);
	}
};

var findClass = function(_class, _needle) {
	var _classes = _class.split(" ");
	for(var i=0; i < _classes.length; i++) {
		if(_classes[i].indexOf(_needle)>-1) {
			return _classes[i];
		}
	}
};

var findNextClass = function(_class, _needle) {
	var _classes = _class.split(" ");
	var _next = false;
	var _target = false;
	for (var i = 0; i < _classes.length; i++) {
		if (_classes[i] == _needle) {
			_next = true;
		} else if (_next == true) {
			_target = _classes[i];
			_next = false;
		}
	}
	return _target;
};

var getBaseArg = function(args, arg) {
	var _default = arguments.length > 2 ? arguments[2] : '';
	var _value = args.length > arg ? args[arg] : _default;
	return _value;
}

var setupDefaultStates = function() {
	var _base = getBaseArg(arguments, 0);
	$(_base + " .has-default-state").css("color", "#7F7E74").focus(function() {
		if (typeof(this.defaults) == "undefined") {
			this.defaults = { default_text: $(this).attr("value") };
		}
		if ($(this).attr("value") === this.defaults.default_text && !$(this).hasClass("keep-default-state")) {
			$(this).attr("value", "");
		}
		$(this).css("color", "#000");
	}).blur(function() {
		if ($(this).attr("value") === "") {
			$(this).attr("value", this.defaults.default_text);
			$(this).css("color", "#7F7E74");
		}
	});
};

var positionAtMe = function(_target, me) {
	var _pos = me.offset();
	var _width = getTrueWidth(_target);
	_target.css("left", _pos.left - _width);
	_target.css("top", _pos.top - 42);
};

var setupScreen = function() {
	var _screen = document.createElement("div");
	_screen.id = "screen";
	$(_screen).height($(document).height());
	$(_screen).insertAfter(".wrapper");
};

/* this just shows the screen and puts the dialog in the center of the window */
var setupDialog = function(base) {
	//DAP
	//$("#screen").show();
	//$(base).css("left", ($(window).width() / 2) - ($(base).width() / 2));
	//$(base).css("top", (($(window).height() / 2) + ($(window).scrollTop())) - ($(base).height() / 2));
	$(base).show();
};

/* gets anything with an ".open-modal-dialog" class and triggers it to open the dialog of the following class name
   for instance: if an element has a class of "wide open-modal-dialog register small" it will open the "register" dialog
*/
var setupModalDialogTriggers = function() {
	function setupActions(_self) {
		var _dialog = findNextClass(_self.attr("class"), "open-modal-dialog");
		var _dialog_id = "#"+_dialog;
		var _args = '';
		$.get("modal-dialog-" + _dialog + ".ashx", _args, function(data) {
			$(data).appendTo($(".wrapper"));
			setupModalDialogTriggers(_dialog_id);
			setupDialog(_dialog_id);
			setupClose(_dialog_id, ".cancel");
			_dialog = _dialog.replace(/-/g, '');
			try {
				var _custom_setup = eval(_dialog+"Dialog");
			} catch(e) {}
			if (typeof(_custom_setup)=="function") {
				_custom_setup();
			}
		});
	};

	var _base = getBaseArg(arguments, 0);
	$(_base+" .open-modal-dialog").each(function() {
		if(this.tagName.toLowerCase()=='form') {
			$(this).submit(function() {
				setupActions($(this));
				return false;
			});
		} else {
			$(this).click(function() {
				setupActions($(this));
				return false;
			});
		}
	});
};

/* close button or any action with a ".cancel" class */
var setupClose = function(base, action) {
	$(base + " " + action).click(function() {
		$("#screen").hide();
		$(base).remove();
		return false;
	});
};

var setupTabGroup = function() {
	var _root = getBaseArg(arguments, 0);
	var _current_tab = $(_root + " .tab.selected");
	$(_root + " h4").click(function() {
		_current_tab.removeClass("selected");
		_current_tab = $(this).parent();
		_current_tab.addClass("selected");
		return false;
	});
};

var setupCarousel = function(_root) {
	var _items, _current, _count, _width, _cx, _offset, _content, _max, _root=_root;
		
	var getCount = function() {
		var _count = 0;
		_items.each(function() {
			if ($(this).is(":visible")) {
				_count++;
			}
		});
		return _count;
	};
	
	var animateCards = function(_page) {
		var _ref = $(_root + " .carousel-items ol").index($(_root + " ol.page-"+_page));
		var _to = (_ref * -(_width)) + parseInt(_offset,10)*(_ref+1);
		_content.animate({
			left: _to+"px",
			queue: false
		}, 250);
	};
	
	var setupCardNavigation = function() {
		$(_root + " .c-nav").unbind("click");
		$(_root + " .c-nav").click(function() {
			if ($(this).hasClass("previous") && _current > 0) {
				_current--;
				updateCards(1);
				animateCards(_current);
			} else if ($(this).hasClass("next") && _current < _max) {
				_current++;
				updateCards();
				animateCards(_current);
			}
			return false;
		});
	};
	
	var updateCards = function() {
		var _reverse = getBaseArg(arguments, 0);
		var _content_ol = $(document.createElement("ol"));
		if(_reverse == 1) {
			_page = parseInt(_current,10) - 1;
			if(_page<0) { return; }
		} else {
			_page = parseInt(_current,10) + 1;
		}
		if($(_root + " ol.page-"+_page).length>0) { return; }
		_content_ol.addClass("page-"+_page);
		_content_ol.attr("start", (_page*12+1));
		if(_reverse == 1) {
			_content.prepend(_content_ol);
		} else {
			_content.append(_content_ol);
		}
		_max++;
		
		$.get("Common/Handlers/update_carousel.ashx", {cid: _root, page: _page}, function(data) {
			_content_ol.html(data);
			_content.width(_width * (_max+1));
		});
	};
	
	var init = function() {
		
		_max = $(_root + " .carousel ol").length;
		_content = $(_root + " .carousel-items");
		_items = $(_root + " .carousel ol li");
		_current = (findClass($(_root + " .carousel ol").attr("class"), "page-").split("-")[1]);
		_count = getCount();
		_cx = $(_root + " .carousel");
		_width = _cx.width();
		_offset = _content.css("left");
		
		/*log(_root);
		log('_max: ' + _max);
		log('count: ' + _count);
		log('_width: ' + _width);
		log('_current: ' + _current);
		log(_cx);
		log('_offset: ' + _offset);
		log('-------------------');*/

		setupCardNavigation();
		updateCards();
		if(_current > 0) {
			updateCards(1);
			_max++;
		}
		animateCards(_current);
	};
	
	init();
	return {init: init, updateCards: updateCards};
};

/* ---------- [ ALL PAGE VIEWS ] ---------- */
$(document).ready(function() {
	setupModalDialogTriggers();
	//setupTabGroup();
	//setupScreen();
});
