$(document).ready(function(){ 

	// --- soluciones page ----
	$('div.frames a').mousedown(function(){ enableToggle = false; });
	$('div.marco').click(function(){ if (enableToggle) toggleImageBackground($(this).children('img.back')); });
	
	// --- soluciones details ----
	$('.sol .image').mouseover(function(){ toggleSolucionBackground($(this), true); });
	$('.sol .image').mouseout(function(){ toggleSolucionBackground($(this), false); });
	$('.sol .image').click(function(){  setPressed($(this)); getProductos($(this)); });
	
	// --- soluciones list -----
	$('.head').live('click', function(){ toggleContent($(this)); });
	
	// --- styles correction -----
	if (nav.code=='ff') 	$('head').append("<style type='text/css'>div.back .soluciones .sol .selected{margin-left:-10px}</style>");
	if (nav.code=='ie' && nav.version<=7) $('head').append("<style type='text/css'>.marco .option{left:81px !important;}</style>");
	//if (nav.platform=='Macintosh') $('head').append("<style type='text/css'>.marco .option{left:2px !important;}</style>");
    $('.solucion, .productos, .acabado').click(function(){
      getProductos($(this).parent().parent());
    });
});//ready
var enableToggle = true;
// -------- soluciones page -------- 

function toggleImageBackground(img) {
	var src = img.attr('src');
	if (img.attr('id')=='interior') {
		var newSrc = (src.indexOf('interior_inverse')>-1) ? src.replace('interior_inverse', 'interior') : src.replace('interior', 'interior_inverse');
		var controls = (src.indexOf('interior_inverse')>-1) ? false : 'option.int';
		if ($('.marco.exterior img.back').attr('src').indexOf('inverse')>-1) toggleImageBackground($('.marco.exterior img.back'));
	}else{ 
		var newSrc = (src.indexOf('exterior_inverse')>-1) ? src.replace('exterior_inverse', 'exterior') : src.replace('exterior', 'exterior_inverse');
		var controls = (src.indexOf('exterior_inverse')>-1) ? false : 'option.ext';
		if ($('.marco.interior img.back').attr('src').indexOf('inverse')>-1) toggleImageBackground($('.marco.interior img.back'));
	}
	$('div.option').hide(0);
	var controls = (newSrc.indexOf('inverse')>-1) ? (newSrc.indexOf('interior')>-1) ? 'div.option.int' : 'div.option.ext' : false;
	img.animate({ width:'1px', height:'405px' }, 'fast', function(){ 
		$(this).attr('src', newSrc);
		$(this).animate({ width:'392px', height:'405px' }, function(){ 
			if (controls) $(controls).show(0);			
		});		
	}); 
}//function



//--- soluciones details ----

function toggleSolucionBackground(img, on) {
	var style = img.attr('style');
	var newStyle = (on) ? (img.children('#tipo').val()=='interior') ? style.replace('black', 'red') : style.replace('black', 'green')    :    (img.children('#tipo').val()=='interior') ? style.replace('red', 'black') : style.replace('green', 'black');
	// --- this patch is used in setPressed() ---
	if (!on && solucionClicked==img.parent('.sol').attr('id')) newStyle = style;
	// -----
	img.attr('style', newStyle);
}//function

function setPressed(img) { 
	var originalBack = img.attr('style').replace('red', 'black'); originalBack = originalBack.replace('green', 'black');
	$('.sol .image').attr('style', originalBack);
	$('img.selected').hide(0);
	img.parent().children('.selected').show(0);
	toggleSolucionBackground(img, true); 
	solucionClicked = img.parent('.sol').attr('id');
}//function
var solucionClicked;


// ---------- soluciones list -----------

function getProductos(img) {
	$id = img;
	$desde = $id.attr('id').length;
	$numero = $id.attr('id').substring($desde - 1, $desde);
	$.post('solucionesList', { pd:img.children('#idsProductos').val(), type:img.children('#tipo').val(), idC:$('#idC').val(), idM:$('#idM').val() }, function(result){ 
		if (result.length>0) {
			if($('#soluciones'+$numero+' #list'+$numero).css('display')!='none'){$('#soluciones'+$numero+' #list'+$numero).toggle('slow');}
			else{
			  $('.soluciones .list').hide();
			  $('#list'+$numero).html(result);
			  $('#list'+$numero).toggle('slow');
			}
			//toggleContent($('#circle1')); // init step 1 by default
		}else{$('#list'+$numero).html('');$('#list'+$numero).toggle();}
	});
}//function

function toggleContent(row) {
	$('.producto .cont').each(function(){
		var tipo =$(this).parent('.producto').children('.head').children('#tipo').val();
		var src = $(this).parent('.producto').children('.head').children('.circle').attr('src');
		
		if ($(this).parents('.producto').children('#rand').val()==row.parents('.producto').children('#rand').val()) { //element clicked 
			$(this).slideDown('fast'); //show			
			$(this).parent('.producto').children('.head').css('color', (tipo=='interior') ? '#ec262d' : '#00a44e' ); //color			
			$(this).parent('.producto').children('.head').children('.circle').attr('src', src.replace('texto', tipo)); //icon
		}else{ //rest of elements
			$(this).slideUp('fast'); //show
			$(this).parent('.producto').children('.head').css('color', '#55514e'); //color
			$(this).parent('.producto').children('.head').children('.circle').attr('src', src.replace(tipo, 'texto')); //icon
		}
	});
}//function



