/* Author: 
	Xandor Schiefer (xandor [ at ] wetpaint.co.za)
*/

$(document).ready(function () {
	// Deactivate text-shadow for @font-face on Chrome Windows because of lame 18-month old rendering bug
	if (BrowserDetect.browser == 'Chrome' && BrowserDetect.OS == 'Windows') {
		$('.flexi-block h1, .flexi-block h2, .logged-in h1').css('text-shadow', 'none');
	}

	// Placeholder support for older browsers.
	if (!Modernizr.input.placeholder) {
		$('input, textarea').placeholder();
	}

	// Set up the testimonial ticker.
	$('#ticker ul').newsticker(8000);
	// Reposition some elements
	$('img[src="img/gloss-finish.png"]').css('top', '5px');
	$('footer h1').css('top', '30px');
	$('footer h1').css('left', '82px');
	$('.index footer, .services footer, .media-planning footer, .graphic-design footer, .web-design footer, .email-marketing footer, .production footer, .television-commercials footer, .radio-advertising footer, .printing footer, .copy-writing footer, .photography footer, .event-management footer, .public-relations footer, .bee footer, .press-office footer, .contact-us footer, .login footer, .register footer, register-done footer, .not-found footer, .under-construction footer').each(function () {
		$(this).css('top', '-30px');
		$(this).css('z-index', '-300');
	});

	// Suckerfish dropdowns
	if(window.addEventListener) window.addEventListener('load', mcAccessible, false); // gecko, safari, konqueror and standard
	else if(document.addEventListener) document.addEventListener('load', mcAccessible, false); // opera 7
	else if(window.attachEvent) { // win/ie
		window.attachEvent('onload', sfHover);
		window.attachEvent('onload', mcAccessible);
	} else { // mac/ie5
		if(typeof window.onload == 'function') {
			var existing = onload;
			window.onload = function() {
				existing();
				sfHover();
				mcAccessible();
			}
		} else {
			window.onload = function() {
				sfHover();
				mcAccessible();
			}
		}
	}
	
	// Change the sidebar submit button to a text link. Petra wants it to be a link.
	$('#newsletter-submit').replaceWith('<a href="javascript:document.getElementById(\'newsletter-form\').submit();">BE IN THE KNOW</a>');

	// Initialize dock with options.
	if ($('#menu').length) {
		$('#menu').jqDock({
			align:	'middle'
		});
		$('#menu').children().click(function() {
			$('#menu').each(function() {
				$(this).jqDock($(this).jqDock('get').Frozen ? 'nudge' : 'freeze');
			});
		});
	}
	if ($('.portfolio').length) {
		$('#menu').css('margin-top', '1em');
	}

	// Newsticker for client names.
	if ($('#webticker').length) {
		$('#webticker').webTicker();
	}

	// Lightbox for portfolio images.
	$('.cmc, .credit-guarantee, .epson, .huawei, .john-craig, .miss-south-africa, .philips, .quickbooks, .rocla, .sony, .sunbird').each(function () {
		$(this).find('.flexi-block a').lightBox();
	});

	// Autofocus on login page
	if ($('#username').length && !('autofocus' in document.createElement('input'))) {
		document.getElementById('username').focus();
	}

	// Smart forms for registration
	if ($('label[for="registered"]').length) {
		// Toggle info requirements
		$('#registered').change(function () {
			if ($('.register-info').hasClass('hidden')) {
				$('.register-info').removeClass('hidden');
			} else {
				$('.register-info').addClass('hidden');
			}
		});
		// Just in case they checked the box before page load finished.
		if ($('#registered').attr('checked') == 'checked') {
			$('.register-info').addClass('hidden');
		}
	}

	// Indeterminate state for checkboxes 'inactive' value;
	$('.admin .indeterminate').each(function () {
		$(this).prop('indeterminate', true);
	});

	// When the form is reset, make sure that any checkboxes
	// that started out in the indeterminate state are put
	// back to it.
	$('.admin input[type="reset"]').click(function () {
		$('.admin .indeterminate').each(function () {
			$(this).prop('indeterminate', true);
		});
	});
	
	$('#admin-form').submit(function () {
		var submit = false;
		// Check when the admin form is submitted whether
		// there are any indeterminate checkboxes left.
		var wait = false;
		$('input[type="checkbox"]').each(function () {
			if ($(this).prop('indeterminate') == true) {
				wait = true;
				submit = false;
			}
		});
		// Don't submit straight away if there are indeterminate checkboxes left.
		if (wait == true) {
			// Ask the user if they do not want to change the checkboxes.
			var confirmed = confirm('There are checkboxes in the indeterminate (pending) state, indicating that a user has requested this feature. Are you sure you want to submit changes without activating or deactivating the user\'s requested features?');
			// If they don't want to change the checkboxes.
			if (confirmed) {
				$('input[type="checkbox"]').each(function () {
					if ($(this).prop('indeterminate') == true) {
						// Change each indeterminate checkbox's value to -1,
						// remove the indeterminate property, and check it.
						$(this).attr('value', '-1');
						$(this).prop('indeterminate', false);
						$(this).val(['-1']);
					}
				});
				// Submit form now.
				submit = true;
			} else {
				// Cancel form submission.
				submit = false;
			}
		} else {
			// Submit the form.
			submit = true;
		}


		// When the admin form is submitted, check whether there are any
		// press office drop-down boxes that have 'pending' selected left.
		var wait = false;
		$('select').each(function () {
			if ($(this).val() == '-1') {
				wait = true;
				submit = false;
			}
		});
		// Don't submit straight away if there are 'pending' values left.
		if (wait == true) {
			// Ask the user if they do not want to change the checkboxes.
			var confirmed = confirm('There are dropdown menus in the pending state, indicating that a user has requested this feature. Are you sure you want to submit changes without activating or deactivating the user\'s requested features?');
			// If they don't want to change the dropdowns.
			if (confirmed) {
				$('select').each(function () {
					if ($(this).val() == '-1') {
						// Remove the disabled attribute so
						// that the value will be passed to the
						// next page.
						$(this).children('option[disabled]').removeAttr('disabled');
						$(this).val(['-1']);
					}
				});
				// Submit form now.
				submit = true;
			} else {
				// Cancel form submission.
				submit = false;
			}
		} else {
			// Submit the form.
			submit = true;
		}


		// Send or not.
		if (submit) {
			return true;
		} else {
			return false;
		}
	});

	// Change preview form destination when asked to edit again. Then submit.
	$('#edit-again').click(function () {
		if ($('#source').val() == 'new') {
			$('#preview').attr('action', '/blog/new');
		} else {
			$('#preview').attr('action', $('#source').val());
		}
		$('#preview').submit();
	});

	// Pretty SEO'd urls function for preview.
	new urlBuilder('pTitle');
	$('#pTitle').keyup();

	// Initialize editor for blog posts. Keep this at the bottom, nothing else seems to execute below it for some reason.
	if ($('.wymeditor').length) {
		$('.wymeditor').wymeditor();
	}

});

