var toolOffset = 0;
var chosenType = 'backgrounds';
var view = 0;
var maincat = 1;
var kind = 0;

//Loads everything the first time the tool is loaded
window.onload = function() {
	showThumbs('', 'main', 3);
	showThumbs('', 'bg', 3);
	showThumbs('', 'acc', 3);
	
	//switchViews = document.getElementById('switchViews');
	//switchViews.onclick = function() {
		//switchView(view);
	//}
	
	navnext = document.getElementById('nav_next');
	navnext.onclick = function() {
		showThumbs('next', '', 9);
	};
	
	navback = document.getElementById('nav_back');
	navback.onclick = function() {
		showThumbs('back', '', 9);
	};
	
	toolBack = document.getElementById('tool_images_back');
	toolBack.onclick = function() {
		changeType('back');
	};
	
	toolNext = document.getElementById('tool_images_next');
	toolNext.onclick = function() {
		changeType('next');
	};
	
	bg = document.getElementById('image0');
	bg.onclick = function() {
		limpia_seleccion();
	};
	
	removeButton = document.getElementById('toolbar_delete');
	removeButton.onclick = function() {
		removeElement();
	};
	
	undoButton = document.getElementById('toolbar_undo');
	undoButton.onclick = function() {
		undo();
	};
	
	clearButton = document.getElementById('toolbar_clear');
	clearButton.onclick = function() {
		clearAll();
	};
	
	duplicateButton = document.getElementById('toolbar_duplicate');
	duplicateButton.onclick = function() {
		duplicate();
	};
	
	builder = document.getElementById('build');
	builder.onclick = function() {
		compileData();
	};
	
	tbMain = document.getElementById('tool_main_back');
	tbMain.onclick = function() {
		showThumbs('back', 'main', 3);
	};
	
	tnMain = document.getElementById('tool_main_next');
	tnMain.onclick = function() {
		showThumbs('next', 'main', 3);
	};
	
	tbcatMain = document.getElementById('tool_main_cat_back');
	tbcatMain.onclick = function() {
		maincat--;
		changetitle('back')
		showThumbs('back', 'main', 3, 1);
	};
	
	tncatMain = document.getElementById('tool_main_cat_next');
	tncatMain.onclick = function() {
		maincat++;
		changetitle('next')
		showThumbs('next', 'main', 3, 1);

	};
	
	tbBg = document.getElementById('tool_bg_back');
	tbBg.onclick = function() {
		showThumbs('back', 'bg', 3);
	};
	
	tnBg = document.getElementById('tool_bg_next');
	tnBg.onclick = function() {
		showThumbs('next', 'bg', 3);
	};
	
	tbAcc = document.getElementById('tool_acc_back');
	tbAcc.onclick = function() {
		showThumbs('back', 'acc', 3);
	};
	
	tnAcc = document.getElementById('tool_acc_next');
	tnAcc.onclick = function() {
		showThumbs('next', 'acc', 3);
	};
	
	screenwidth = document.getElementById('screenwidth');
	screenheight = document.getElementById('screenheight');
	
	screenwidth.value = window.screen.width;
	screenheight.value = window.screen.height;
	
		//CREATES THE SLIDER CONTROL

		demoSlider = new Control.Slider('handle1','track1', { axis:'horizontal', minimum: 50, maximum:100, alignX: 2, increment: 2, sliderValue: 1 });

		demoSlider.options.onSlide = function(value) {
			escala[seleccion] = value;
			if(sliderStart == 1) {
				accionUndo = 'resize';
				undoVars = seleccion + '-' + value;
				sliderStart = 0;
			}
			scaleIt(value);
		};

		demoSlider.options.onChange = function(value){
			scaleIt(value);
		};
	
};

function switchView() {
	if(view == 0) {
		$(multi_view).style.visibility = 'hidden';
		$(multi_view).style.display = 'none';
		$(single_view).style.visibility = 'visible';
		$(single_view).style.display = 'block';
		view = 1;
	}
	else {
		$(multi_view).style.visibility = 'visible';
		$(multi_view).style.display = 'block';
		$(single_view).style.visibility = 'hidden';
		$(single_view).style.display = 'none';
		view = 0;
	}
	showThumbs('', 'main', '9', 2);
}

function showThumbs(action, type, max, ccat) {
	if(type == 'bg') { chosenType = 'backgrounds' }
	if(type == 'main') { chosenType = 'pictures' }
	if(type == 'acc') { chosenType = 'accessories' }
	
	if(ccat == 1) { extracc = '&cc=1'; }
	else { extracc = '&cc=0'; }
	
	pars = 'action=' + action + '&type=' + chosenType + '&max=' + max + '&maincat=' + maincat + extracc;
	if(view == 0) {
		thumbs = new Ajax.Updater(type + '_images', 'showthumbs.php', { parameters: pars, method: 'post' });
	}
	else {
		$('images').innerHTML = '<img src="images/design/loader.gif" id="thumbsloader" />';
		thumbs = new Ajax.Updater('images', 'showthumbs.php', { parameters: pars, method: 'post' });
	}
};


function changetitle(action) {
	
	
	if(action == 'next') {
		if(maincat == 1) { $('tool_main_title').style.backgroundPosition = '-1016px 0'; }
		else if(maincat == 2) {  $('tool_main_title').style.backgroundPosition = '-1976px 0'; }
		else if(maincat == 3) { $('tool_main_title').style.backgroundPosition = '-632 0'; }
		else if(maincat == 4) { $('tool_main_title').style.backgroundPosition = '-2168 0'; }
		else if(maincat == 5) { $('tool_main_title').style.backgroundPosition = '-1592 0'; }
		else if(maincat == 6) { $('tool_main_title').style.backgroundPosition = '-1208 0'; }
		else if(maincat == 7) { $('tool_main_title').style.backgroundPosition = '-1400 0'; }
		else if(maincat == 8) { $('tool_main_title').style.backgroundPosition = '-440 0'; }
		else if(maincat == 9) { $('tool_main_title').style.backgroundPosition = '-824 0'; }
		else if(maincat == 10) { $('tool_main_title').style.backgroundPosition = '-1784 0'; }
		else if(maincat == 11) { $('tool_main_title').style.backgroundPosition = '-248 0'; }
		else if(maincat == 12) { maincat = 1; $('tool_main_title').style.backgroundPosition = '-1016 0'; }
	}
	
	if(action == 'back') {
		if(maincat == 1) { $('tool_main_title').style.backgroundPosition = '-1016px 0'; }
		else if(maincat == 2) {  $('tool_main_title').style.backgroundPosition = '-1976px 0'; }
		else if(maincat == 3) { $('tool_main_title').style.backgroundPosition = '-632 0'; }
		else if(maincat == 4) { $('tool_main_title').style.backgroundPosition = '-2168 0'; }
		else if(maincat == 5) { $('tool_main_title').style.backgroundPosition = '-1592 0'; }
		else if(maincat == 6) { $('tool_main_title').style.backgroundPosition = '-1208 0'; }
		else if(maincat == 7) { $('tool_main_title').style.backgroundPosition = '-1400 0'; }
		else if(maincat == 8) { $('tool_main_title').style.backgroundPosition = '-440 0'; }
		else if(maincat == 9) { $('tool_main_title').style.backgroundPosition = '-824 0'; }
		else if(maincat == 10) { $('tool_main_title').style.backgroundPosition = '-1784 0'; }
		else if(maincat == 11) { $('tool_main_title').style.backgroundPosition = '-248 0'; }
		else if(maincat == 0) { maincat = 11; $('tool_main_title').style.backgroundPosition = '-248 0'; }
	}
	
};

function changeType(action) {
	
	
	if(action == 'next') {
		if(chosenType == 'backgrounds') { chosenType = 'pictures'; $('tool_img_title2').style.backgroundPosition = '-248px 0'; }
		else if(chosenType == 'pictures') { chosenType = 'accessories'; $('tool_img_title2').style.backgroundPosition = '-2360px 0'; }
		else if(chosenType == 'accessories') { chosenType = 'backgrounds'; $('tool_img_title2').style.backgroundPosition = '0 0'; }
		showThumbs('next', '', 9);
	}
	
	if(action == 'back') {
		if(chosenType == 'backgrounds') { chosenType = 'accessories'; $('tool_img_title2').style.backgroundPosition = '-2360px 0'; }
		else if(chosenType == 'pictures') { chosenType = 'backgrounds'; $('tool_img_title2').style.backgroundPosition = '0 0'; }
		else if(chosenType == 'accessories') { chosenType = 'pictures'; $('tool_img_title2').style.backgroundPosition = '-248px 0'; }
		showThumbs('back', '', 9);
	}
	
};