var Scroller = { /* Smooth Scroller Script, version 1.0.1	(cc?) 2007 Dezinerfolio Inc. <midart@gmail.com>	For details, please check the website : http://dezinerfolio.com/ Tweaked by RSB Media */

	speed: 10,
	freq: 5,
	stdFactor: 260,
	brakeFactor: 3.6,
	informer: null, // for debugging
	
	init: function() {
		Scroller.add(window, 'load', Scroller.render);
		Scroller.informer = document.getElementById("navDiv");
	},
	
	gx: function(d) {
		gx = d.offsetLeft;
		if(d.offsetParent) while (d = d.offsetParent) gx += d.offsetLeft;
		return gx;
	},
	
	scrollLeft: function() {
		body = document.body;
		d = document.documentElement;
		if(body && body.scrollLeft) return body.scrollLeft;
		if(d && d.scrollLeft) return d.scrollLeft;
		if(window.pageYOffset) return window.pageYOffset;
		return 0;
	},
	
	add: function(event, body, d) {
		if(event.addEventListener) return event.addEventListener(body, d, false);
		if(event.attachEvent) return event.attachEvent('on' + body, d);
	},
	
	end: function(e) {
		if(window.event) {
			window.event.cancelBubble = true;
			window.event.returnValue = false;
			return;
		}
		if(e.preventDefault && e.stopPropagation) {
			e.preventDefault();
			e.stopPropagation();
		}
	},
	
	scroll: function(d) {
		w = document.body.scrollWidth;
		a = Scroller.scrollLeft();
		
		if(d > a) { 
			var endDistance = Math.round((d - a) / Scroller.brakeFactor);
			endDistance = Math.min(Math.round((d - a + w) / Scroller.brakeFactor), endDistance);
			var offset = Math.max(2, Math.min(endDistance, Scroller.stdFactor));
		}else { 
			var offset = - Math.min(Math.abs(Math.round((d - a) / Scroller.brakeFactor)), Scroller.stdFactor);
		}
		
		window.scrollTo(a + offset, 0);
		
		if(Math.abs(d - a) <= 1 || a == Scroller.scrollLeft()) {
			window.scrollTo(d, 0);
			clearInterval(Scroller.interval);
		}
	},
	
	info: function(a, b) {
		Scroller.informer.innerHTML = "a: " + a + ". b: " + b + ".";
	},
	
	render: function() {
		var pageLinks = document.getElementsByTagName('a');
		Scroller.end(this);
		window.onscroll;
		for(i = 0; i < pageLinks.length; i++) {
			if(pageLinks[i] != null) {
				pageLink = pageLinks[i];
				if(pageLink.href && pageLink.href.indexOf('#') != -1 && ((pageLink.pathname == location.pathname) || ('/' + pageLink.pathname == location.pathname))) {
					Scroller.add(pageLink, 'click', Scroller.end);
					pageLink.onclick = function() {
						Scroller.end(this);
						pageLink = this.hash.substr(1);
						divs = document.getElementsByTagName('div');
						for(i = 0; i < divs.length; i++) {
							if(divs[i].id == pageLink) {
								clearInterval(Scroller.interval);
								Scroller.interval = setInterval('Scroller.scroll(' + Scroller.gx(divs[i]) + ')', Scroller.freq);
							}//endif
						}//endfor
					}//endfn
				}//endif
			}//endif
		}//endfor
	}

}

var ScrollHandler = {
	init: function() {
		if(window.addEventListener) { //isFF?
			window.addEventListener('DOMMouseScroll', this.mouseScroll, false);
		}else { //isIE?
			document.attachEvent('onmousewheel', this.mouseScroll);
		}
	},
	
	mouseScroll: function(windowEvent) {
		clearInterval(Scroller.interval);
		
		Scroller.end(windowEvent);
		if(!windowEvent) var windowEvent = window.event;
		if(windowEvent.wheelDelta <= 0 || windowEvent.detail >= 0) {
			window.scrollBy(80, 0);
		}else {
			window.scrollBy(-80, 0);
		}
	}
	
}

var BoundsHandler = {
	endSpacerDivWidth: 70,
	minDocumentWidth: 1015,
	documentWidth: 0,
	noticiasElementWidth: 380,
	portfolioElementWidth: 655,
	section: "",
	nElements: 0,
	
	init: function() {
		BoundsHandler.section = "home";
		BoundsHandler.documentWidth = BoundsHandler.getDocumentWidth();
		
		switch(BoundsHandler.section) {
			case "home":
				BoundsHandler.setHomeWidth();
				break;
			
			case "portfolio":
				BoundsHandler.nElements = "";
				BoundsHandler.setPortfolioWidth();
				break;
			
			case "noticias":
				BoundsHandler.nElements = "";
				BoundsHandler.setNewsWidth();
				break;
			
			case "proyecto":
			case "noticia":
			case "nosotros":
			case "obras":
				BoundsHandler.setWhateverDetailWidth();
				break;
            case "ultimo":
				BoundsHandler.setWhateverDetailWidth();
				break;
		}
	},
	
	windowWidthChanged: function() {
		BoundsHandler.documentWidth = BoundsHandler.getDocumentWidth();
		
		switch(BoundsHandler.section) {
			case "home":
				BoundsHandler.setHomeWidth();
				break;
			
			case "proyecto":
			case "noticia":
			case "nosotros":
			case "obras":
				BoundsHandler.setWhateverDetailWidth();
				break;
            case "ultimo":
				BoundsHandler.setWhateverDetailWidth();
				break;
		}
	},
	
	getDocumentWidth: function(customWidth) {
		var widthValue = 0;
		
		if(customWidth) {
			widthValue = parseInt(customWidth);
		}else {
			if(typeof(window.innerWidth) == "number") {
				widthValue = window.innerWidth;
			}else {
				if(document.documentElement && document.documentElement.clientWidth) {
					widthValue = document.documentElement.clientWidth;
				}else {
					if(document.body && document.body.clientWidth) {
						widthValue = document.body.clientWidth;
					}
				}
			}
		}
		return widthValue;
	},
	
	setHomeWidth: function() {
		var mainDiv = document.getElementById("mainDiv");
		var contentDiv = document.getElementById("contentDiv");
		var pageStartDiv = document.getElementById("pageStart");
		var widthValue = 0;
		
		widthValue = parseInt(5500 + BoundsHandler.documentWidth); /*  ============= AQUÍ ES PARA AÑADIR MÁS ITEMS ========= */
		
		mainDiv.style.width = parseInt(widthValue + BoundsHandler.endSpacerDivWidth) + "px";
		contentDiv.style.width = widthValue + "px";
		pageStartDiv.style.width = BoundsHandler.documentWidth + "px";
	},
	
	setPortfolioWidth: function() {
		var mainDiv = document.getElementById("mainDiv");
		var contentDiv = document.getElementById("contentDiv");
		var widthValue = 0;
		
		widthValue = parseInt((Math.ceil(BoundsHandler.nElements / 2) * BoundsHandler.portfolioElementWidth) + 130); // 130 = pageStartDiv.style.paddingLeft
		
		mainDiv.style.width = parseInt(widthValue + BoundsHandler.endSpacerDivWidth) + "px";
		contentDiv.style.width = parseInt(widthValue) + "px";
	},
	
	setNewsWidth: function() {
		var mainDiv = document.getElementById("mainDiv");
		var contentDiv = document.getElementById("contentDiv");
		var widthValue = 0;
		
		widthValue = parseInt((Math.ceil(BoundsHandler.nElements / 3) * BoundsHandler.noticiasElementWidth) + 130); // 130 = pageStartDiv.style.paddingLeft
		
		mainDiv.style.width = parseInt(widthValue + BoundsHandler.endSpacerDivWidth) + "px";
		contentDiv.style.width = parseInt(widthValue) + "px";
	},
	
	setWhateverDetailWidth: function() {
		var mainDiv = document.getElementById("mainDiv");
		var contentDiv = document.getElementById("contentDiv");
		var endSpacerDiv = document.getElementById("endSpacerDiv");
		//var showEndSpacer = false;
		var widthValue = 0;
		
		/*if(BoundsHandler.minDocumentWidth + BoundsHandler.endSpacerDivWidth <= BoundsHandler.documentWidth) {
			widthValue = BoundsHandler.documentWidth;
			showEndSpacer = true;
		}else */
		if(BoundsHandler.minDocumentWidth < BoundsHandler.documentWidth) {
			widthValue = BoundsHandler.documentWidth;
			//showEndSpacer = false;
		}else if(BoundsHandler.minDocumentWidth >= BoundsHandler.documentWidth) {
			widthValue = BoundsHandler.minDocumentWidth;
			//showEndSpacer = false;
		}
		/*
		if(showEndSpacer) {
			mainDiv.style.width = BoundsHandler.documentWidth + "px";
			contentDiv.style.width = BoundsHandler.documentWidth - BoundsHandler.endSpacerDivWidth + "px";
			endSpacerDiv.style.display = "block";
		}else { */
			mainDiv.style.width = contentDiv.style.width = widthValue + "px";
			endSpacerDiv.style.display = "none";
		//}
	}
	
}

var ContactForm = {
	checkEmail: function(formObjectName) {
		var ok = true;
		var formObject = document.getElementById(formObjectName);
		var value = formObject.value;
		
		if(value != ""){
			var a = value.indexOf("@");
			var p = value.indexOf(".", a);
			
			if(a > -1 && p > -1) {
				ContactForm.clearError(formObjectName);
			}else {
				ContactForm.showError(formObjectName);
				ok = false;
			}
		}
		return ok;
	},
	
	checkMandatoryFields: function() {
		var ok = true;
		
		ok = !ContactForm.isEmptyTextField("contactFormMail") && ok;
		return ok;
	},
	
	validate: function() {
		var ok = true;
		
		ok = ContactForm.checkMandatoryFields() && ok;
		ok = ContactForm.checkEmail("contactFormMail") && ok;
		return ok;
	},
	
	isEmptyTextField: function(formObjectName) {
		var empty = false;
		var formObject = document.getElementById(formObjectName);
		ContactForm.clearError(formObjectName);
		if(formObject) {
			if(formObject.value == "") {
				empty = true;
				ContactForm.showError(formObjectName);
			}
		}
		return empty;
	},
	
	showError: function(formObjectName) {
		var formObject = document.getElementById(formObjectName);
		
		formObject.style.backgroundColor = "#e6e039";
	},
	
	clearError: function(formObjectName) {
		var formObject = document.getElementById(formObjectName);
		
		formObject.style.backgroundColor = "#ffffff";
	},
	
	gotoContacto: function() {
		var contactFormElement = document.getElementById("contactar");
		
		window.scrollTo(contactFormElement.offsetLeft + BoundsHandler.getDocumentWidth(), 0);
	}
	
}

var GoogleMap = {
	createMarker: function(point,html) {
	  var marker = new GMarker(point);
	  GEvent.addListener(marker, "click", function() {
	    marker.openInfoWindowHtml(html);
	  });
	  return marker;
	},
	
	init: function() {
		if(GBrowserIsCompatible()) {
			var map = new GMap2(document.getElementById("map"));
			map.addControl(new GSmallMapControl());
			map.setCenter(new GLatLng(41.402397,2.155563), 17);
			
		  var point = new GLatLng(41.402397,2.155563);
		  var marker = GoogleMap.createMarker(point,"<div style=\"position: relative; top: 20px; color: #000;\"><img src=\"/images/rsbAdress.gif\" alt=\"C/ Montseny, 30. 08012, Barcelona, Spain\" /></div>");
	    map.addOverlay(marker);
	   	return true;
		}
		return false;
	}
	
}

var HomeScripts = {
	init: function() {
		HomeScripts.dhtmlLoadScript("http://twitter.com/javascripts/blogger.js");
		HomeScripts.dhtmlLoadScript("http://twitter.com/statuses/user_timeline/rsbmedia.json?callback=twitterCallback2&count=6");
	},
	
	dhtmlLoadScript: function(scriptUrl) {
		var e = document.createElement("script");
		e.src = scriptUrl;
		e.type = "text/javascript";
		document.getElementsByTagName("head")[0].appendChild(e);
	}
	
}

/* --------------------------------------------------------------------------------------------------------------------------------------- MAIN 	- - - -  - - - - - - - - - - - - - -
*/
var RsbScript = {
	init: function() {
		BoundsHandler.init();
		Scroller.init();
		if(BoundsHandler.section == "home") {
			document.getElementById("mainMenu").style.display = "block";
		}
		window.onresize = BoundsHandler.windowWidthChanged;
	}
	
}

ScrollHandler.init();
