window.addEvent('domready', function() {
	
	$$('.brand-cb-wrapper').each(function(e) {
		var img = e.getElement('.img');
		e.getElement('label').addEvent('mouseenter', function() {
			img.setStyle('display', 'block');
		});
		e.getElement('label').addEvent('mouseleave', function() {
			img.setStyle('display', 'none');
		});		
	});
	
	$$('a.popout, a[href^="http://"]').addEvent('click', function(e) {
		e.stop();
		window.open(this.get('href'));
	})
		
});

window.addEvent('domready', function() {
	$$('a.modal').addEvent('click', function(e) {
		e.stop();
		e.preventDefault();
		
		var size = {width: 700, height: 250};
		
		if (this.get('rel')) {
			var relAttribs = this.get('rel').split(';');
			
			for (var i = 0; i < relAttribs.length; i ++) {
				var tmpAttrib = relAttribs[i].split(':');
				switch (tmpAttrib[0]) {
					case 'size':
						var sizeVals = tmpAttrib[1].split('x');
						size = {width: parseInt(sizeVals[0]), height: parseInt(sizeVals[1])};
						break;
				}
			}
		}
		
		var req = new MooDialog.Request(this.get('href'), {}, {
			size: size,
		    scroll: false
		});
		
		return false;
	});
});

// teamcards needs special confirmation window
window.addEvent('domready', function() {
	
	var form = $('cardAddForm');
	if(form && !confirmed_license) {
		form.addEvent('submit', function(event) {
			var form = this;
			var code = document.id('CardCode');
			var val = code.get('value');

			if(val.substr(0,1) == 6 && val.length == 6) {
				var req = new Request.HTML({ 
					url: '/pages/teamcard_agree',
					onSuccess: function(html, resp, raw) {
						var confirm_action = function() {
							form.removeEvent('submit');
							form.submit();
						}
						var raw_html = new Element('div').set('html', raw);
						var dialog = new MooDialog.Agree(raw_html.get('html'), confirm_action, function() {}, {
								useScrollBar: false,
								okText: AgreeTxt,
								size: {
									width: 700,
									height: 450
								}
						});	
					}
				}).get();
				event.stop();
			}
		});
	}
});

// checkbox stuff for brands edit page
window.addEvent('domready', function() {
	$$('div.brandlist input.checkall').addEvent('click', function() {
		var boxes = this.getParent('.country-col').getElements('.brands-col input[type=checkbox]');
		if(this.get('checked')) {
			boxes.set('checked','checked');
		} else {
			boxes.set('checked', '');
		}
	});
	var uncheckBrands = $$('.uncheckbrands');
	var allBoxes = $$('div.brandlist .brands-col input[type=checkbox]');
	allBoxes.addEvent('click', function() {
		if(uncheckBrands.get('checked')) {
			uncheckBrands.set('checked', '');
		}
	});
	uncheckBrands.addEvent('click', function() {
		if(this.get('checked')) {
			allBoxes.set('checked', '');
		}
	});
	
	
	if($('branchbox-move')) {
		$('branchbox-move').addEvent('click', function() {
			$('branchbox').setStyle('display','block');
		});
		$('branchbox-remove').addEvent('click', function() {
			$('branchbox').setStyle('display','none');
			$('BranchDeactivateTeamcards').set('checked','checked');
		});
	}
	
});

window.addEvent('domready', function() {
	if($('companyform') && $('CompanyNoCompany')) {
		var wrapper = $$('#companyform .company-wrapper');
		if($('CompanyNoCompany').get('checked')) {
			wrapper.setStyle('display', 'none');
		}
		
		$$('input[type=checkbox].disabled').addEvent('click', function() {
			if(this.get('checked')) {
				wrapper.getElements('input, select').each(function(e) { e.set('value', '') });
				wrapper.setStyle('display', 'none');
			} else {
				wrapper.setStyle('display', 'block');
			}
		});
	}
});


window.addEvent('domready', function() {
	if($('carousel')) {
		mySlideShow2 = new SlideShow('carousel', {
			delay:  3000,
			transition: 'fade',
			duration: 500,
			autoplay: true
		});
	}
});

window.addEvent('domready', function() {
	if(typeof Password == 'undefined' && !$('UserPasswordFirst')) {
		return false;
	}
	new Password($('UserPasswordFirst'), {
		onChange: function() {
			var score = "medium";
			
			if (this.score == 0) {
				this.controlField.set('html', '');
				return;
			}
			
			if (this.score >= 1 && this.score <= 50) {
				score = "weak";
			}
			else if (this.score >= 80) {
				score = "strong";
			}
			
			this.controlField.set('class', score);
			this.controlField.set('html', passwordscores[score]);
		}
	});
});

