// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

function changeSex(dropDown) {
	var sex = dropDown.value;
	if (sex == 'F') {
		Element.hide($('manData'));	
		Element.show($('womanData'));
	}
	else if (sex == 'M') {
		Element.show($('manData'));	
		Element.hide($('womanData'));
	}
	else {
		Element.hide($('manData'));	
		Element.hide($('womanData'));
	}	
}


function findWindowDimensions() {
    var myWidth = 0, myHeight = 0;
    if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
    } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
    } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
    }

    var scrOfX = 0, scrOfY = 0;
    if( typeof( window.pageYOffset ) == 'number' ) {
        scrOfY = window.pageYOffset; scrOfX = window.pageXOffset;
    } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
        scrOfY = document.body.scrollTop; scrOfX = document.body.scrollLeft;
    } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
        scrOfY = document.documentElement.scrollTop; scrOfX = document.documentElement.scrollLeft;
    }
    totalWindowHeight=scrOfY + myHeight;
    totalWindowWidth=scrOfX + myWidth;
    
    return [totalWindowHeight,totalWindowWidth];
};



function findPosX(obj) {
    var curleft=0;
    if(obj.offsetParent) {
        while(true) {
            curleft += obj.offsetLeft;
            if(!obj.offsetParent || obj.offsetParent.style.position == 'relative'){
                break;
            }
            obj = obj.offsetParent;
        }
    } else if(obj.x) {
        curleft += obj.x;
    }
    return curleft;
};

/**
 * Function: findPosY
 *
 * Calculate the y (vertical) position of a given
 * element, relative to either the body, or the 
 * first parent element with position:relative.
 *
 * Parameters: obj - Any DOM element
 * Returns: The y-coordinate
 * See Also: <findPosX>
 */
function findPosY(obj) {
    var curtop=0;
    if(obj.offsetParent) {
        while(true) {
            curtop += obj.offsetTop;
            if(!obj.offsetParent || obj.offsetParent.style.position == 'relative') {
                break;
            }
            obj = obj.offsetParent;
        }
    } else if(obj.y) {
        curtop += obj.y;
    }
    return curtop;
};

function showError () {
	alert('problem with ajax request');
}  

function goAjax(url,params) {
	params +=  '&authenticity_token=' + window._token;
	var myAjax = new Ajax.Request(
		url, 
		{
			method: 'post', 
			parameters: params, 
			onFailure: showError
		});
	
}
function remove_from_queue(id) {}

function drawQueueItem(id, firstname, icon_src) {
	/*
	We need to create a <table> for every single image.  Crazy, I know, but it's the only way to horizontally render the icons with a delete
	function beneath
	*/
	var newModIconTable = document.createElement('table');
	newModIconTable.className = 'model_icon_table';
	newModIconTable.id = 'queue_model_table' + id;
	newModIconTable.cellPadding = 0;
	newModIconTable.cellSpacing = 0;
	
	//i.e. requires a tbody element to create rows
	var tBody = document.createElement('tbody');
	
	//the TR/TD that holds the image and href
	var newModIconTableRow = document.createElement('tr');
	var newModIconTableTD = document.createElement('td');
	newModIconTableTD.width = 25;
	
	//the TR/TD that holds the 'remove' functionality
	var newModRemoveTableRow = document.createElement('tr');
	var newModRemoveTableTD = document.createElement('td');
	newModRemoveTableTD.width = 25;
	newModRemoveTableTD.className = "tinyThumbRemove"
	
	//first we create the models image icon
	var newModIcon = document.createElement('img');
	newModIcon.src = icon_src;
	newModIcon.width = 25;
	newModIcon.height = 35;
	newModIcon.title = firstname + '_' + id + ' ';
	newModIcon.id = 'queue_model_' + id;
	newModIcon.border = '0';
		
	//now create the href for the models icon
	var newModHref = document.createElement('a');
	newModHref.href = icon_src;
	newModHref.rel='lightbox[queue]';


	//create the href to remove the model
	var newModXCLHref = document.createElement('a');
	js_method = "populate_queue(0, " + id + ", '" + firstname + "','" + icon_src + "')"
	newModXCLHref.href = 'javascript:' + js_method + ';';

	//create the text to click on to remove
	var XCL_text = document.createTextNode('del');
	var BR = document.createElement('br');
	
	//attach the xcl text to the xcl href
	newModXCLHref.appendChild(XCL_text);
	
	var spacer = document.createTextNode(' ');

	//assemble the model icon href
	newModHref.appendChild(newModIcon);

	//Assemble table and attach the whole schebang to it
	newModIconTableTD.appendChild(newModHref);
	newModIconTableTD.appendChild(BR);
	
	newModRemoveTableTD.appendChild(newModXCLHref);
	
	//assemble row for image
	newModIconTableRow.appendChild(newModIconTableTD);
	newModRemoveTableRow.appendChild(newModRemoveTableTD);

	//assemble row for 'del'
	newModIconTable.appendChild(tBody);

	tBody.appendChild(newModIconTableRow);
	tBody.appendChild(newModRemoveTableRow);
	
	//add the combined href/image to the queueList area of the site
	var queueList = $('queueList');
	queueList.appendChild(newModIconTable);
}

function removeQueueItem (id) {
	
	var Node = $('queue_model_table' + id);
	Node.parentNode.removeChild(Node);
	
	if(! $('queueList').hasChildNodes()) {
		slideUpDiv("userSelect",1)
	}

}

function populate_queue(checked, id, firstname, icon_src, paginate_page) {
	var queue = $('queue');

	if ($('queue_model_' + id)) {
		
		if ($('modelSelect_' + id)) {
			$('modelSelect_' + id).className = 'modelSelect';
		}
		
		var pars = 'person_id=' + id + '&page_id=' + paginate_page;
		var url = '/person_queue/remove_from_queue';

		goAjax(url,pars);
		removeQueueItem(id);
		
	}
	else {

		$('modelSelect_' + id).className = 'modelSelectOn';

		//first we add the values to the session
		var pars = 'person_id=' + id + '&firstname=' + firstname + '&icon=' + icon_src + '&page_id=' + paginate_page;
		var url = '/person_queue/add_to_queue';

		goAjax(url,pars);
		drawQueueItem(id,firstname,icon_src);
		
		slideDownDiv("userSelect",1)
		
	}

}
    
//scriptaculous wrappers
function slideDownDiv (element_id,duration) {
	if ($(element_id).style.display=='none') {
		// <![CDATA[
		Effect.SlideDown(element_id,{duration:duration});
		// ]]>
	}	
}

function slideUpDiv (element_id,duration) {
	if ($(element_id).style.display !='none') {
		// <![CDATA[
		Effect.SlideUp(element_id,{duration:duration});
		// ]]>
	}	
}
