/**

IEDImages v1.0

parametricdesign.js

*/

var columnWidth = 224;
var tweening = false;
var immediate = false;


function setTweenOpts(element) {
	if(immediate) {
		element.set('tween', {duration: 0, onStart: function(){tweening=true;}, onComplete: function(){tweening=false;}});
	} else {
		element.set('tween', {duration: 375, onStart: function(){tweening=true;}, onComplete: function(){tweening=false;}});
	}
}


function setMorphOpts(element) {
	if(immediate) {
		element.set('morph', {duration: 0, onStart: function(){tweening=true;}, onComplete: function(){tweening=false;}});
	} else {
		element.set('morph', {duration: 375, onStart: function(){tweening=true;}, onComplete: function(){tweening=false;}});
	}
}


function incColumn(element,property,columnWidth) {
	if(document.getElementById(element)!=null) {
		setTweenOpts($(element));
		$(element).tween(property, $(element).getStyle(property).toInt()+columnWidth+'px');
		return(true);
	}
	return(false);
}


function decColumn(element,property,columnWidth) {
	if(document.getElementById(element)!=null) {
		setTweenOpts($(element));
		$(element).tween(property, $(element).getStyle(property).toInt()-columnWidth+'px');
		return(true);
	}
	return(false);
}


function updateHrefs(columns) {
	var anchors = document.getElementsByTagName("a");
	for(var idx = 0; idx < anchors.length; idx++) {
		var href = anchors[idx].href;
		if( href.indexOf('http://www.iedimages.it') == 0 ) {
			if( href.indexOf('&cols=') == -1 && href.indexOf('?cols=') == -1) {
				if( href.indexOf('?') == -1) {
					anchors[idx].href += '?cols='+columns;
				} else {
					anchors[idx].href += '&cols='+columns;
				}
			} else {
				if( href.indexOf('?cols=') == -1) {
					anchors[idx].href = anchors[idx].href.replace(/&cols=\d*/,'&cols='+columns);
				} else {
					anchors[idx].href = anchors[idx].href.substring(0,anchors[idx].href.indexOf('?cols='))+'?cols='+columns;
				}
			}
		}
	}
	if(document.getElementById("colsInput") != null) {
		document.getElementById("colsInput").value=columns;
	}
	return;
};


function getDesignWidth() {
	return ($('siteHeader').getStyle('width').toInt()/columnWidth).toInt();
}


function setDesignWidth(columns) {
	var actualWidth = getDesignWidth();
	var steps = Math.abs(columns-actualWidth);

	var affectWidth=['siteHeader','main','browser','topPageNav','images','imageList','bottomPageNav','helpContent','siteFooter'];
//	var affectLeft=['keywordsInfo'];

	if(columns>actualWidth){  //increment width
		for(i in affectWidth) {
			incColumn(affectWidth[i],'width',columnWidth*steps);
		}
/*		for(i in affectLeft) {
			incColumn(affectLeft[i],'left',columnWidth*steps);
		}*/
		if(actualWidth<4 && columns>=4) {
			if(document.getElementById("image")!=null) {
				incColumn('breadcrumbs','width',columnWidth*(steps-1));
				incColumn('image','width',columnWidth*(steps-1));
			} else {
				incColumn('breadcrumbs','width',columnWidth*steps);
			}	
		} else {
			incColumn('breadcrumbs','width',columnWidth*steps);
			incColumn('image','width',columnWidth*steps);
		}
		if(actualWidth<3 && columns>=3) {
			incColumn('footerFotografia','left',columnWidth*(steps-1));
			//$('footerIed').setStyle('display', 'block');
			incColumn('footerIed','left',columnWidth*(steps-1));
/*			if(document.getElementById("keywordsInfo") != null) {
				setTweenOpts($('keywordsInfo'));
				$('keywordsInfo').tween('width', '196px');
			}*/
			if(document.getElementById("detailedInfo") != null) {
				setMorphOpts($('detailedInfo'));
				$('detailedInfo').morph({'margin-left': 0, width: 196});
			}
		} else {
			incColumn('footerFotografia','left',columnWidth*steps);
			incColumn('footerIed','left',columnWidth*steps);
		}
		if(actualWidth==2 && document.getElementById("image")!=null) {
			setMorphOpts($$('#image img')[0]);
			$$('#image img')[0].morph({height: $$('#image img')[0].getProperty('height'),width: $$('#image img')[0].getProperty('width')});
		}
	}

	if(document.getElementById("keywordsInfo")!=null) {
		if(columns>=5) {
			setTweenOpts($('keywordsInfo'));
			$('keywordsInfo').tween('width', 196+columnWidth*(columns-5)+'px');
		} else if(columns<=2) {
			setTweenOpts($('keywordsInfo'));
			$('keywordsInfo').tween('width', '168px');
		} else {
			setTweenOpts($('keywordsInfo'));
			$('keywordsInfo').tween('width', '196px');
		}
	}

	if($$('#helpContent section') != null) {
		if(columns==4 || columns>6) {
			setTweenOpts($$('#helpContent section'));
			$$('#helpContent section').tween('width',616);
		} else if(columns<3) {
			setTweenOpts($$('#helpContent section'));
			$$('#helpContent section').tween('width',168);
		} else {
			setTweenOpts($$('#helpContent section'));
			$$('#helpContent section').tween('width',392);
		}
	}

	/*if(columns>3) {
		$('footerFotografia').setStyle('position', 'relative');
		$('footerIed').setStyle('position', 'relative');
	} else {
		$('footerFotografia').setStyle('position', 'inherit');
		$('footerIed').setStyle('position', 'inherit');
	}*/

	if(columns<actualWidth && columns>1) {  //decrement width
		for(i in affectWidth) {
			decColumn(affectWidth[i],'width',columnWidth*steps);
		}
/*		for(i in affectLeft) {
			decColumn(affectLeft[i],'left',columnWidth*steps);
		}*/
		if(actualWidth>=4 && columns<4) {
			if(document.getElementById("image")!=null) {
				decColumn('breadcrumbs','width',columnWidth*(steps-1));
				decColumn('image','width',columnWidth*(steps-1));
			} else {
				decColumn('breadcrumbs','width',columnWidth*steps);
			}	
		} else {
			decColumn('breadcrumbs','width',columnWidth*steps);
			decColumn('image','width',columnWidth*steps);
		}
		if(actualWidth>=3 && columns<3) {
			decColumn('footerFotografia','left',columnWidth*(steps-1));
			decColumn('footerIed','left',columnWidth*(steps-1));
			//$('footerIed').setStyle('display', 'none');
/*			if(document.getElementById("keywordsInfo") != null) {
				setTweenOpts($('keywordsInfo'));
				$('keywordsInfo').tween('width', '168px');
			}*/
			if(document.getElementById("detailedInfo") != null) {
				setMorphOpts($('detailedInfo'));
				$('detailedInfo').morph({'margin-left': 28, width: 168});
			}
		} else {
			decColumn('footerFotografia','left',columnWidth*steps);
			decColumn('footerIed','left',columnWidth*steps);
		}
		if(columns==2 && document.getElementById("image")!=null) {
			if($$('#image img')[0].width>=columnWidth){
				var newWidth = columnWidth-56;
				setMorphOpts($$('#image img')[0]);
				$$('#image img')[0].morph({height: Math.round($$('#image img')[0].height/$$('#image img')[0].width*newWidth),width: Math.round(newWidth)});
			}
		}
	}
}


function getViewportWidth() {
	return document.documentElement.clientWidth || document.body.clientWidth || window.innerWidth;
}


function fitDesignToViewport(viewportWidth) {
	if(document.getElementById("main")!=null) {
		if(viewportWidth<952) {
			setDesignWidth(2);
		} else {
			setDesignWidth(4);
		}
	} else {
		if(viewportWidth<728) {
			setDesignWidth(2);
		} else if(viewportWidth<952) {
			setDesignWidth(3);
		} else if(viewportWidth<1176) {
			setDesignWidth(4);
		} else if(viewportWidth<1400) {
			setDesignWidth(5);
		} else if(viewportWidth<1624) {
			setDesignWidth(6);
		} else if(viewportWidth<1848) {
			setDesignWidth(7);
		} else if(viewportWidth<2072) {
			setDesignWidth(8);
		} else if(viewportWidth<2296) {
			setDesignWidth(9);
		} else if(viewportWidth<2520) {
			setDesignWidth(10);
		} else if(viewportWidth<2744) {
			setDesignWidth(11);
		} else if(viewportWidth>=2744) {
			setDesignWidth(12);
		} else { return false; }
	}
	return true;
}


function updateDesign() {
	/*if($$('nav.categories') != null) {
		columnWidth = $$('nav.categories')[0].getSize().x;
	}*/
	viewportWidth = getViewportWidth();

	if(viewportWidth>0) {
		fitDesignToViewport(viewportWidth);
		return true;
	}
	return false;
}


var fincDesignWidth = function(incDesignWidth) {
	incDesignWidth.stop();
	if(!tweening) { setDesignWidth(getDesignWidth()+1); }
}

var fdecDesignWidth = function(decDesignWidth) {
	decDesignWidth.stop();
	if(!tweening) { setDesignWidth(getDesignWidth()-1); }
}

if(Browser.Engine.trident) { var tridentTimer; }

var fupdateDesign = function() {
	if(!tweening) {
		if(Browser.Engine.trident) {
			$clear(tridentTimer);
			tridentTimer = (function(){updateDesign();}).delay(50);
		} else {
			updateDesign();
		}
	}
};


window.addEvent('domready', function() {

	if(document.getElementById("+col") != null) {
		$('+col').addEvent('click', fincDesignWidth);
	}
	if(document.getElementById("-col") != null) {
		$('-col').addEvent('click', fdecDesignWidth);
	}
	window.addEvent('resize', fupdateDesign);

	immediate = true;
	updateDesign();
	immediate = false;

});
