/*
 * jPalette  v0.1.1
 * http://palette.seanmccambridge.com/
 *
 * Copyright (c) 2009 Sean McCambridge
 *
 * Date: 2009-10-14 04:02:00 -0500 (Wed, 14 October 2009)
 * First beta version
 */

$(document).ready(function() {
	
	var activeExample = 'first';

	// Example code begins here
	
	$('.swatch').click(function() {
		if (activeExample == 'first') {
			var color = $(this).css('background-color');
			var htmlNode = $('#firstInput').attr('value');
			var selected = $('#firstSelect option:selected').attr('value');
			if (selected == 'bg') {
				target = 'background-color';
			} else if (selected == 'text') {
				target = 'color';
			} else if (selected == 'border') {
				target = 'border';
				color = color + ' solid 3px';
			}
			if (htmlNode.indexOf('.swatch') == -1 && htmlNode.indexOf('#swatch') == -1) {
				$(htmlNode).css(target, color);
			}
		}
	});
	
	// END example code
	
	// set double click
	
	$('*').dblclick(function (e) {
		e.stopPropagation();
		var thisNode = $(this).get(0);
		var thisId = $(this).attr('id');
		if (thisId.length == 0) {
			thisId = '(no id)';
		} else {
			thisId = '#' + thisId;
		}
		var thisClass = $(this).attr('class');
		if (thisClass.length == 0) {
			thisClass = '(no class)';
		} else {
			thisClass = '.' + thisClass;
		}
		var nodeMessage = '<em>You double-clicked: ' + thisNode.tagName + ' ' + thisId + ' ' + thisClass + '</em>';
		$('#clickedNode').html(nodeMessage);
		var fieldString;
		fieldString = thisNode.tagName;
		fieldString = (thisId[0] == '#') ? ' ' + thisId : fieldString;
		fieldString = (thisClass[0] == '.' && thisId[0] != '#') ? thisClass : fieldString;
		$('#firstInput').val(fieldString);
	});
	
	// Toggle Palette sizes
	var paletteSize = $('#palette').width(); // takes from your CSS
	var smallPaletteSize = 90; // set total width of small palette
	
	var originalText = $('#togglePalette').html(); // taken from your HTML
	var newText = 'Show large palette'; // provide new text after toggle to small
	
	$('#togglePalette').click(function() {
		var currentSize = $('#palette').width();
		if (currentSize == paletteSize) {
			var newSize = smallPaletteSize;
		} else {
			var newSize = paletteSize;
		}
		var swatchSize = newSize / 15;
		$('.swatch').css({
			width: swatchSize,
			height: swatchSize + 1
		}, 400).parent().css({
			width: newSize
		}, 400);
		
		var currentText = $('#togglePalette').html();
		if (currentText == originalText) {
			$('#togglePalette').html(newText);
		} else {
			$('#togglePalette').html(originalText);
		}
		return false;
	});
	
	$('#activateFirst').click(function() {
		$('div.border').removeClass('active');
		$('#anything').addClass('active');
		activeExample = 'first';
		return false;
	});
	
	$('.next').click(function() {
		alert('Sorry, this is the only example right now.');
		return false;
	});
	
	// Get random color for background
	var random = Math.floor(Math.random()*179);
	var randomColor = $('#swatch' + random).attr('rel');
	$('h1, h2').css('color','#' + randomColor);
		
});
