/* Copyright (c) 2010 by Jessie A. Morris
 *
 * Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */




	KOBJ.selectorReturned = '';

	if(typeof(KOBJ.formData) == "undefined"){
			KOBJ.formData = [];
	}


	
	KOBJ.detectSelector = function(passedElement){

		//Begin setup and such things

		var element = $K(passedElement);

		var tagName = $K(element)[0].tagName.toLowerCase();

		//End setup and such things


		if($K(element).attr('name')){
			return '[name='+$K(element).attr('name')+']';
		} else if($K(element).attr('id')){
			return  '#'+$K(element).attr('id');
		} else if($K(element).parent().children(tagName).length == 1){
			var parentSelector = KOBJ.detectSelector($K(element).parent());
			return parentSelector + '>' + tagName;
		} else if($K(element).attr('class')){
			var elementClass = $K(element).attr('class');
			if($K('.'+elementClass).length == 1){
				return '.'+elementClass;
			} else {
				var counter = 0;
				var tmpReturn = '';

				$K('.'+elementClass).each(function(){
					var data = $K(this);
					if($K(element)[0] == $K(data)[0]){
						tmpReturn =  '.' + $K(element).attr('class') + ':eq(' + counter + ')';
						return false;
					}
					counter++;
				});
				if(tmpReturn){
					return tmpReturn;
				}
			}
		} else {
			var parentSelector = KOBJ.detectSelector($K(element).parent());
			var counter = 0;
			var tmpReturn = '';

			$K(parentSelector + '>' + tagName).each(function(){
				var data = $K(this);
				if($K(element)[0] == $K(data)[0]){
					tmpReturn = parentSelector + '>' + tagName + ':eq(' + counter + ')';
					return false;
				}
				counter++;
			});
			if(tmpReturn){
				return tmpReturn;
			}
		}
	};

	
	$K('img,a,div,span,cite,input,p,h1,h2,h3,h4,h5,select').bind('click',function(){
		$K(KOBJ.selectorReturned).removeClass('jqueryDetector');
		var dataElement = this;
		KOBJ.selectorReturned = KOBJ.detectSelector(dataElement);
		KOBJ.log('Object you just clicked:');
		KOBJ.log(KOBJ.selectorReturned);
		$K('#jquerySelector').html(KOBJ.selectorReturned);
		$K(KOBJ.selectorReturned).addClass('jqueryDetector');
		return false;
	});

