// JavaScript Document

/*

mewmedia:

javascript

*/

/*

selectAtt(item id, attribute type);

*/

var attType = '0'; // attribute_type (link level of 0 in attributes table)
var attId = '0'; // attribute_type (first level down in link level in attributes table)
var productId = '0'; // attribute_type (product selected)
var groupId = '0';
var groupName = '';
var selectAllVar = "0"; // select all products

var filterTickArray = Array(); // array of all the filters ticked
var filterCrossArray = Array(); // array of all the filters crossed

var filter_group = 'true';
var withinGroup = '';

// product array - for adding to groups
var productArray = Array();
var addProductArray = Array();
var deleteProductArray = Array();

var searchTotal = 0; // search total variable


function openAttPublic(att_id, c, url, type) {
	
	//tickString = filterTickArray.join(", ");
	
	replaceElement('attributeList_'+att_id, BASE+'ajaxAttributes.php', 'listType='+type+'&type=attributeList&attribute='+att_id+'&'+url, 'showAttributes', 'menu_block'+c);

}


function filterGroupFn(url) {
	urlr = url;
	if (url.indexOf('filter_group=true') > 0) {
		urlr = url.replace('filter_group=true', 'filter_group=false')
		window.location = urlr;
		return;
	}
	if (url.indexOf('filter_group=false') > 0) {
		urlr = url.replace('filter_group=false', 'filter_group=true');
		window.location = urlr;
		return;
	}
	
}

function winLocation(url) {
	window.location = url;
}

var productDetail = 'spec';
function productDetailsFn(d) {
	
	document.getElementById('full_'+productDetail+'_title').className = '';
	document.getElementById('full_'+productDetail).className = 'hidden';
	productDetail = d;
	document.getElementById('full_'+productDetail+'_title').className = 'current';
	document.getElementById('full_'+productDetail).className = '';

}


function selectAtt(att_id, att_type, prod_id) {
	
	attId = top.window.attId;
	attType = top.window.attType;
	top.window.productId = prod_id;
	productId = top.window.productId;
	
	switch(att_type) {
		
		case"refreshAttributeEditorPanel":
			replaceElement('attributeEditorPanel', 'ajaxAttributes.php', 'type=attributeEditorPanel&attribute='+attType+'&product='+prod_id);
		break;
	
		case"attributeEditorPanel":
			
			// hide edit and delete attribute buttons
			hideAttributeButtons();
			
			showAddAttributeButton();
			// show edit and delete attribute type buttons
			showAttributeTypeButtons();
			// clear attributes
			
			
			s = document.getElementById('modAttribute_edit');
			if (s) {
				r = s.href.replace("ref="+attId ,"ref=0" );
				document.getElementById('modAttribute_edit').href = r;
			}
			
			s = document.getElementById('modAttribute_delete');
			if (s) {
				r = s.href.replace("ref="+attId ,"ref=0" );
				document.getElementById('modAttribute_delete').href = r;
			}

			attId = '0';
			
			if (attType != att_id && att_id != '') {
				
				
				
				// new global attribute type is set
				if (attType != '0') {
					// clear the previous attType
					e = document.getElementById('attType_'+attType);
					e.className = "";
				}
				
				// to be done before updating attType
				
				// replace ajax links
				
				// ----- modAttributeType ------- //
				
				
				
				s = document.getElementById('modAttributeType_edit').href;
				r = s.replace("ref="+attType ,"ref="+att_id );
				document.getElementById('modAttributeType_edit').href = r;
				
				s = document.getElementById('modAttributeType_delete').href;
				r = s.replace("ref="+attType ,"ref="+att_id );
				document.getElementById('modAttributeType_delete').href = r;
				
				// ----- modAttribute ------- //
				
				s = document.getElementById('modAttribute_add').href;
				r = s.replace("ref="+attType ,"ref="+att_id );
				document.getElementById('modAttribute_add').href = r;
				
			
				
				
				// ----- END ------- //
				
				attType = att_id;
				e = document.getElementById('attType_'+att_id);
				e.className = "current";
				
				// show element
				e = document.getElementById('attributeEditorPanel').style;
				e.display = 'block';
				
				// show edit panels
				e = document.getElementById('modAttribute').style;
				e.display = 'block';
				
				
				replaceElement('attributeEditorPanel', 'ajaxAttributes.php', 'type=attributeEditorPanel&attribute='+attType+'&product='+prod_id);
				replaceElement('attributeDetails_container', 'ajaxAttributes.php', 'type=attributeDetails_container_clear&product='+prod_id);
				

				
			} else {
				
				
				replaceElement('attributesPanel_container', 'ajaxAttributes.php', 'type=attributesPanel_container&product='+prod_id);
				hideAllAttributeButtons();
				attType = 0;
				
				
			}
		
		break;
		case"attribute":
			
			// if global attribute is not equal to funtion variable
			
			
			// new global attribute type is set
			if (top.window.attId != '0') {
				// clear the previous attType
				e = document.getElementById('att_'+top.window.attId);
				e.className = "";
				
			}
			
			if (attId != att_id) {
			
				
				
				showAttributeButtons();
				
				old_attId = top.window.attId;
				
				
				e = document.getElementById('att_'+att_id);
				e.className = "current";
				
				s = document.getElementById('modAttribute_edit').href;
				r = s.replace("ref="+old_attId ,"ref="+att_id );
				document.getElementById('modAttribute_edit').href = r;
				
				s = document.getElementById('modAttribute_delete').href;
				r = s.replace("ref="+old_attId ,"ref="+att_id );
				document.getElementById('modAttribute_delete').href = r;
				
				top.window.attId = att_id;
				
				
				// load ajax descriptions and synonyms
				replaceElement('attributeDetails_container', 'ajaxAttributes.php', 'type=attributeDetails_container&attribute='+att_id+'&product='+prod_id);
				
				
			} else {
				
				// hide attribute edit + delete buttons
				hideAttributeButtons();
				// replace edit boxes
				replaceElement('attributeDetails_container', 'ajaxAttributes.php', 'type=attributeDetails_container_clear&product='+prod_id);
				// clear global var
				top.window.attId = '0';
			
			}
			
			
		
		break;
	
	}
}


function selectProduct(id, selectType) {
	// move the filter panel up
	
	slidePanelUp('panel_block1', '0.7');
	
	// load the product details in
	replaceElement('productPanel', 'ajaxAttributes.php', 'type='+selectType+'Panel&'+selectType+'='+id, 'true');
	
	
}


function cleanDatabase() {
	// clean up tables in database
	replaceElement('saving', 'ajaxAttributes.php', 'type=cleanDatabase', 'true');
}

function allowDate() {
	replaceElement('updateSession', 'ajaxAttributes.php', 'type=updateSession');
	t = window.setTimeout(allowDate, 120000);
}


function filterPanel(id, type) {
	/*
	
	set the filter arrays
	
	*/
	
	// value from search bar
	
	
	
	s = document.getElementById('search');
	s = (s) ? s.value : '';
	
	// find search type
	search_type = findName('search_type');
	top.window.productArray = Array();
	
	
	
		
		switch(type) {
			
			case"group":
				// define a new group if required
				tickString = filterTickArray.join(',');
				setId = id != ''?'&group='+id:'';
				top.window.withinGroup = 'true';
				top.window.filter_group = 'true';
	
				replaceElement('resultsContainer', 'ajaxAttributes.php', 'type=resultsContainer'+setId+'&tick='+tickString+'&search='+s+'&search_type=&withinGroup=true&filter_group='+top.window.filter_group, 'true');
			
			break;
			
			case"filter_group":
			// define a new group if required
				tickString = filterTickArray.join(',');
				setId = id != ''?'&group='+id:'';
				top.window.withinGroup = 'true';
				if (document.getElementById('filter_group'))
					top.window.filter_group = document.getElementById('filter_group').checked;
				
				replaceElement('resultsContainer', 'ajaxAttributes.php', 'type=resultsContainer'+setId+'&tick='+tickString+'&search='+s+'&search_type=&withinGroup=true&filter_group='+top.window.filter_group, 'true');
			break;
			
			case"tick":
				
				// if product or group buttons are pressed, deselect filter inside group
				if (id == '') {
					top.window.withinGroup = '';
				} else {
					top.window.filter_group = 'true';
				}
				
				selectAllBtn('clear');
				
				if (inArray(id, filterTickArray)) {
					filterTickArray = spliceArray(id, filterTickArray);
					v = document.getElementById('tick'+id);
					if (v) v.className = "tick icon";
				} else {
					if (id != '') {
						filterTickArray.push(id);
						v = document.getElementById('tick'+id);
						if (v) v.className = "untick icon";
						
						// remove from crossArray if set
						if (inArray(id, filterCrossArray)) {
							filterCrossArray = spliceArray(id, filterCrossArray);
							v = document.getElementById('cross'+id);
							if (v) v.className = "cross icon";
							
						}
					}
					
				}
				
				tickString = filterTickArray.join(',');
				//crossString = filterTickArray.join(',');
				
				// update the results panel with ajax
				//replaceElement('resultsContainer', 'ajaxAttributes.php', 'type=resultsContainer&tick='+tickString+'&search='+s+'&search_type='+search_type+'&withinGroup='+top.window.withinGroup+'&filter_group='+top.window.filter_group, 'true');
				
				
				
			break;
			case"cross":
				if (inArray(id, filterCrossArray)) {
					filterCrossArray = spliceArray(id, filterCrossArray);
					document.getElementById('cross'+id).className = "cross icon";
				} else {
					filterCrossArray.push(id);
					document.getElementById('cross'+id).className = "uncross icon";
					
					// remove from tickArray if set
					if (inArray(id, filterTickArray)) {
						filterTickArray = spliceArray(id, filterTickArray);
						document.getElementById('tick'+id).className = "tick icon";
					}
					
				}
				
				tickString = filterTickArray.join(',');
				crossString = filterCrossArray.join(',');
				
				// update the results panel with ajax
				replaceElement('resultsContainer', 'ajaxAttributes.php', 'type=resultsContainer&cross='+crossString+'&tick='+tickString, 'true');
				
				
				
			break;
		}
	
}

function searchFilter(type, url) {
	/*
	nav for search filter
	*/
	
	
	switch(type) {
		case"next":
			// get results form next button
			replaceElement('resultsContainer', 'ajaxAttributes.php', 'type=resultsContainer&'+url, 'true');
		break;
		case"previous":
			// get results form next button
			replaceElement('resultsContainer', 'ajaxAttributes.php', 'type=resultsContainer&'+url, 'true');
		break;
		case"limit":
			limit = document.getElementById('searchLimit').value;
			// get results form next button
			replaceElement('resultsContainer', 'ajaxAttributes.php', 'type=resultsContainer&'+url+'&limit='+limit, 'true');
		break;
	}
}


// check if an item is in an array
function inArray(v, a) {
	for (i=0;i<a.length;i++) {
		if (a[i]==v) {
			return true;
		}
	}
	
	return false;
}



// delete from array
function spliceArray(v, a) {
	
	for (i=0;i<a.length;i++) {
		if (a[i]==v) {
			
			a.splice(i, 1);
			
		}
	}
		
		return a;
}

/* slide panels

sets a variable to fix the slide bug */

// which slide is relevant
var playSlide = Array();
// set to 1 when moving
var movingSlide = Array();

function slidePanelDown(panel, duration, type) {
	
	// used with scriptaculous effects.js
	
	
	
	if (!inArray(panel+'down', playSlide) && !inArray(panel, movingSlide)) {
		
		playSlide = spliceArray(panel+"up", playSlide);
		playSlide.push(panel+'down');
		movingSlide.push(panel);
		new Effect.SlideDown(panel, duration);
		displayArrows(panel, 'down');
		
		
		
	}
	
	switch(type) {
		case"resetAttributes":
			
				selectAtt('', 'attributeEditorPanel', productId);
			
		break;
	}
	
	
	
}

function slidePanelUp(panel, duration) {
	
	// used with scriptaculous effects.js
	
	
	if (!inArray(panel+'up', playSlide) && !inArray(panel, movingSlide)) {
		
		playSlide = spliceArray(panel+"down", playSlide);
		playSlide.push(panel+'up');
		movingSlide.push(panel);
		new Effect.SlideUp(panel, duration);
		displayArrows(panel, 'up');
		
	}
	
	
	
}


function displayArrows(panel, method) {
	
	if (document.getElementById(panel+'_'+method)) {
		
		if (method == "up") {
			
			document.getElementById(panel+'_up').style.display = 'none';
			document.getElementById(panel+'_down').style.display = 'inline';
		} else {
			document.getElementById(panel+'_down').style.display = 'none';
			document.getElementById(panel+'_up').style.display = 'inline';
		}
			
	
	}
}



function alertDelete(url) {
	
	if (window.confirm('Are you sure you want to Delete this item?')) {
		//alert("ok");
		location = url;
	}
}

function findName(v) {
	// array, string
	// to be used with getElementsByName
	// findName('radio');
	
	for(i=0;document.getElementsByName(v)[i];i++) {
		
		if (document.getElementsByName(v)[i].checked == true) return document.getElementsByName(v)[i].value;
	
	}


}


function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function gbCenterMew(url) {
	
	/*
	
	fix for gb_script
	
	*/

	ajaxHideElement();
	var sp=url.split(/, ?/);
	
	
	/*
	
	mewmedia edit:
	was
	GB_showCenter(_17.caption,_17.url,parseInt(sp[1]),parseInt(sp[0]));
	
	*/
	
	GB_showCenter('test'.caption,url,parseInt(sp[1]),parseInt(sp[0]));

}

function loadingBox(url) {
	var sp=url.split(/, ?/);
	GB_load('test', parseInt(sp[0]));

}


function enterFilterPanel(t) {
	if (!t) {
		if (top.window.withinGroup == 'true') {
			filterPanel('', 'group');
		} else {
			filterPanel('', 'tick');
		}
	}
}


// form input prefill clear on focus, and reset on blur (if empty)

function prefillClear(field, dValue) {

if (field.value==dValue) {field.value = '';}

else if (field.value == '') {field.value = dValue;}



}
