/* 
 * 
 * Textarea Maxlength Setter JQuery Plugin 
 * Version 1.0 
 * 
 * Copyright (c) 2008 Viral Patel 
 * website : http://viralpatel.net/blogs 
 * 
 * Dual licensed under the MIT and GPL licenses: 
 * http://www.opensource.org/licenses/mit-license.php 
 * http://www.gnu.org/licenses/gpl.html 
 * 
 */  
jQuery.fn.maxlength = function() {
	$("textarea[maxlength]").keypress(function(event) {
		var key = event.which;  
		//all keys including return.  
		if (key >= 33 || key == 13) {
			var maxLength = $(this).attr('maxlength');
			var length = this.value.length;  
			if (length >= maxLength) {  
				event.preventDefault();  
			}
		}  
	} );
}

/*
 * Plentitube-specific JavaScript
 * @requires  DD_belatedPNG 0.0.7a
 * @requires jQuery v1.2.3
 * @requires TableSorter 2.0
 *
 * Copyright 2009 Jeffrey Barke
 * Licensed under the MIT license:
 * http://www.opensource.org/licenses/mit-license.php
 */
$(document).ready( function() { 

	// authenticated

	// bookmarks
	$('.bookmarkSP, .star').click( function() {
		var id = this.rel;
		if ($(this.parentNode).hasClass('true')) {
			$(this.parentNode).removeClass('true');
			var bm = false;
		} else {
			$(this.parentNode).addClass('true');
			var bm = true;
		}
		$.post('rfps-process.php', { action:'bookmark', id:id, bm:bm }, function(data) { 
			if (data.result) {
			}
		}, 'json' );
		return false;
	 } );
	$('.bookmarkEPK, .starEPK').click( function() {
		var id = this.rel;
		if ($(this.parentNode).hasClass('true')) {
			$(this.parentNode).removeClass('true');
			var bm = false;
		} else {
			$(this.parentNode).addClass('true');
			var bm = true;
		}
		$.post('epks-process.php', { action:'bookmark', id:id, bm:bm }, function(data) { 
			if (data.result) {
			}
		}, 'json' );
		return false;
	 } );

	// date picker
	Date.format = 'mm/dd/yyyy';
	$('.datePick').datePicker(
		{
			clickInput:true
		}
	).dpSetOffset(35, 0);

	// delete
	$('th .delete').click( function() {
		var arr = [];
		var count = 0;
		var id = document.getElementById('deleteType').value;
		$('td.delete input').each ( function() {
			if (this.checked) {
				arr.push(this.value);
				count++;
			}
		} );
		if (count > 0 && confirm('Are you sure you want to delete all ' + count + ' ' + id.toUpperCase() + 's?')) {
			for (i = 0; i < arr.length; i++) {
				switch (id) {
					case 'announcement':
						$.post('announcements-process.php', { action:'delete', id:arr[i] }, function(data) { 
							if (data.result) {
								$('#row-' + data.id).fadeOut('slow', function() { this.parentNode.removeChild(this); } );
							}
						}, 'json' );
						break;
					case 'cc':
					case 'mb':
						$.post('users-manage-process.php', { action:'delete', id:arr[i] }, function(data) { 
							if (data.result) {
								$('#row-' + data.id).fadeOut('slow', function() { this.parentNode.removeChild(this); } );
							}
						}, 'json' );
						break;
					case 'epk':
						$.post('epks-process.php', { action:'delete', id:arr[i] }, function(data) { 
							if (data.result) {
								$('#row-' + data.id).fadeOut('slow', function() { this.parentNode.removeChild(this); } );
							}
						}, 'json' );
						break;
					case 'forwarded':
						var ids = arr[i].split('-');
						$.post('epks-process.php', { action:'forwarded', id:ids[0], epk:ids[1] }, function(data) { 
							if (data.result) {
								$('#row-' + data.id).fadeOut('slow', function() { this.parentNode.removeChild(this); } );
							}
						}, 'json' );
						break;
					case 'invite':
						$.post('invites-process.php', { action:'delete', id:arr[i] }, function(data) { 
							if (data.result) {
								$('#row-' + data.id).fadeOut('slow', function() { this.parentNode.removeChild(this); } );
							}
						}, 'json' );
						break;
					case 'response':
						var ids = arr[i].split('-');
						$.post('rfps-process.php', { action:'deleteResponse', rfp:ids[0], epk:ids[1] }, function(data) { 
							if (data.result) {
								$('#row-' + data.id).fadeOut('slow', function() { this.parentNode.removeChild(this); } );
							}
						}, 'json' );
						break;
					case 'rfp':
						$.post('rfps-process.php', { action:'delete', id:arr[i] }, function(data) { 
							if (data.result) {
								$('#row-' + data.id).fadeOut('slow', function() { this.parentNode.removeChild(this); } );
							}
						}, 'json' );
						break;
				}
			}
		}
		return false;
	} );

	// epk types
	$('#rdoIndividual,#rdoSeries').click( function() {
		updateEpkType(this.value);
	} );

	// dropdowns
	$('.dropdown ul li a').click( function() {
		window.location = this.href;
		return false;
	} );
	$('.dropdown .close').click( function() {
		$('#' + this.parentNode.id + ' ul').slideToggle('fast');
		return false;
	} );
	$('#chkSelectAll').click( function() {
		var check = this.checked;
		$('.sendEpk').each( function() {
			if (check) {
				this.checked = true;
			} else {
				this.checked = false;
			}
		} );
	} );
	$('.ddContainSend .dropdown .close').click( function() {
		$('#dropSend .ddBox').slideToggle('fast');
		return false;
	} );
	$('#epkSend').click( function() {
		var sel = document.getElementById('sendTo');
		var id = sel.options[sel.selectedIndex].value;
		if (id) {
			$('.sendEpk').each( function() {
				if (this.checked) {
					$.post('epks-process.php', { action:'send', rfp:id, epk:this.value }, function(data) { 
						if (data.result) {
							var obj = document.getElementById('findThis');
							var p = document.createElement('P');
							p.className = 'success';
							p.innerHTML = 'The EPK has successfully been sent to the media buyer.';
							obj.parentNode.insertBefore(p, obj);
						}
					}, 'json' );
				}
			} );
		}
		$('#dropSend .ddBox').slideToggle('fast');
	} );

	// genres
	$('#selGenre1').change( function() { 
		updateGenre(this);
	} );

	// table sorting
	$('.sortable').tablesorter();

		// attach reset events
		$('a.reset').click( function() {
			if (confirm('Are you sure you want to reset this form?')) {
				document.getElementById(this.rel).reset();
			}
			return false;
		} );

	// public

	// login form default values
	$('#login #txtLogin').blur( function() {
		if (this.value == '') {
			this.value = 'Email';
		}
	} ).focus( function() {
		if (this.value == 'Email') {
			this.value = '';
		}
	} )
	$('#login #txtPassword').blur( function() {
		if (this.value == '') {
			this.value = 'Password';
		}
	} )	.focus( function() {
		if (this.value == 'Password') {
			this.value = '';
		}
	} )

	// open rel="external" links in new window
	$('a[rel="external"],a[rel="document"]').click( function() {
		var newWin = window.open(this.href, 'newwin');
		newWin.focus();
		return false;
	 } );

	// add links to homepage
	$('.addLink').blur ( function() { 
		addLink(this);
	} );

	// max length for text area
	$().maxlength();

	// admin

	// invites
	$('.invite').click( function() {
		if ( ! $(this.parentNode).hasClass('true') ) {
			handleInvite(true, this.rel);
		}
		return false;
	} );
	$('.reject').click( function() {
		if ( ! $(this.parentNode).hasClass('true') ) {
			handleInvite(false, this.rel);
		}
		return false;
	} );

	// rfp
	$('.approve').click( function() {
		if ( ! $(this.parentNode).hasClass('true') ) {
			handleApprove(this.rel);
		}
		return false;
	} );

} );