/**
 * jQuery.ScrollTo - Easy element scrolling using jQuery.
 * Copyright (c) 2007-2008 Ariel Flesler - aflesler(at)gmail(dot)com | http://flesler.blogspot.com
 * Dual licensed under MIT and GPL.
 * Date: 9/11/2008
 * @author Ariel Flesler
 * @version 1.4
 *
 * http://flesler.blogspot.com/2007/10/jqueryscrollto.html
 */
;(function(h){var m=h.scrollTo=function(b,c,g){h(window).scrollTo(b,c,g)};m.defaults={axis:'y',duration:1};m.window=function(b){return h(window).scrollable()};h.fn.scrollable=function(){return this.map(function(){var b=this.parentWindow||this.defaultView,c=this.nodeName=='#document'?b.frameElement||b:this,g=c.contentDocument||(c.contentWindow||c).document,i=c.setInterval;return c.nodeName=='IFRAME'||i&&h.browser.safari?g.body:i?g.documentElement:this})};h.fn.scrollTo=function(r,j,a){if(typeof j=='object'){a=j;j=0}if(typeof a=='function')a={onAfter:a};a=h.extend({},m.defaults,a);j=j||a.speed||a.duration;a.queue=a.queue&&a.axis.length>1;if(a.queue)j/=2;a.offset=n(a.offset);a.over=n(a.over);return this.scrollable().each(function(){var k=this,o=h(k),d=r,l,e={},p=o.is('html,body');switch(typeof d){case'number':case'string':if(/^([+-]=)?\d+(px)?$/.test(d)){d=n(d);break}d=h(d,this);case'object':if(d.is||d.style)l=(d=h(d)).offset()}h.each(a.axis.split(''),function(b,c){var g=c=='x'?'Left':'Top',i=g.toLowerCase(),f='scroll'+g,s=k[f],t=c=='x'?'Width':'Height',v=t.toLowerCase();if(l){e[f]=l[i]+(p?0:s-o.offset()[i]);if(a.margin){e[f]-=parseInt(d.css('margin'+g))||0;e[f]-=parseInt(d.css('border'+g+'Width'))||0}e[f]+=a.offset[i]||0;if(a.over[i])e[f]+=d[v]()*a.over[i]}else e[f]=d[i];if(/^\d+$/.test(e[f]))e[f]=e[f]<=0?0:Math.min(e[f],u(t));if(!b&&a.queue){if(s!=e[f])q(a.onAfterFirst);delete e[f]}});q(a.onAfter);function q(b){o.animate(e,j,a.easing,b&&function(){b.call(this,r,a)})};function u(b){var c='scroll'+b,g=k.ownerDocument;return p?Math.max(g.documentElement[c],g.body[c]):k[c]}}).end()};function n(b){return typeof b=='object'?b:{top:b,left:b}}})(jQuery);

/* =========================================================
 *
 * jquery.reorder.js - version 0.1.1
 * 2007-12-11
 *
 * jquery plugin to randomly reorder 
 * first-child elements of selected elements 
 * by rebecca murphey
 * http://blog.rebeccamurphey.com/ 
 *
 * ========================================================= */

(function($){
$.fn.reorder = function() {

  function randOrd() { return(Math.round(Math.random())-0.5); }

  return($(this).each(function() {
    var $this = $(this);
    var $children = $this.children();
    var childCount = $children.length;

    if (childCount > 1) {
      $children.remove();

      var indices = new Array();
      for (i=0;i<childCount;i++) { indices[indices.length] = i; }
      indices = indices.sort(randOrd);
      $.each(indices,function(j,k) { $this.append($children.eq(k)); });

    }
  }));
} 
})(jQuery);

/* Aplication Functions */

function elementSupportsAttribute(element, attribute) {
	var test = document.createElement(element);
	if (attribute in test) {
		return true;
	} else {
		return false;
	}
}

function inputSupportsType(test) {
	var input = document.createElement('input');
	input.setAttribute('type',test);
	if (input.type == 'text') {
		return false;
	} else {
		return true;
	}
}

var filters = {
    required: function(el) {return ($(el).val() != '' && $(el).val() != -1);},
    email: function(el) {return /^[A-Za-z][A-Za-z0-9_]*@[A-Za-z0-9_]+\.[A-Za-z0-9_.]+[A-za-z]$/.test($(el).val());},
    phone: function(el){return /^[0-9]*$/.test($(el).val());}};
	
$.extend({
		stop: function(e){
        if (e.preventDefault) e.preventDefault();
        if (e.stopPropagation) e.stopPropagation();
    }, 
    alert: function(str) { alert(str);
	}
});

function externalLinks() {
 	if (!document.getElementsByTagName) return;
	var anchors = document.getElementsByTagName("a");
	for (var i=0; i<anchors.length; i++) { var anchor = anchors[i];
	if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") anchor.target = "_blank";}
} window.onload = externalLinks;

jQuery(function($) {
	$.easing.easeOut = function (x, t, b, c, d) {
	return c*((t=t/d-1)*t*t*t*t + 1) + b;
	}
});

$(document).ready(function (){
	/*if (!elementSupportsAttribute('input','required')) {*/
		$("form.validable").bind("submit", function(e){
			if (typeof filters == 'undefined') return;
			$(this).find("input, textarea, select").each(function(x,el){ 
				if ($(el).attr("className") != 'undefined') { 
					$(el).removeClass("error");
					$.each(new String($(el).attr("className")).split(" "), function(x, klass){
					if ($.isFunction(filters[klass])) if (!filters[klass](el))
						$(el).addClass("error");
					});
				}
			});
			if ($(this).find(".error").size() > 0) {
				$.stop(e || window.event); return false;
			} return true;
		});/*}*/
	
	var colores = ["#FFFF33","#00FFFF","#FFCCFF","#D9E1E9","#C9E7ED","#E1ECE2","#0000FF","#000000"];
	var color = colores[ Math.floor( Math.random() * colores.length) ];
	$('#form input,textarea, input, .enviar').css("border-color", color);
	$('hr,.confirm,.color').css("background-color", color);
	$('hr').css("color", color);
	$('#gallery, img, .text.desc, footer').hide().stop();
	$('#promsite').fadeIn();
	$('#gallery').reorder();
	$(window).bind('load', function() {
		$('#gallery, img, .text.desc, footer, #share').fadeIn('fast');
	});
	$('a.scroll').click(function(e){
		var target = this.href.match(/\#[^\#]+$/)[0];
		$.scrollTo(target, 2000, {easing:'easeOut'});
		e.preventDefault();
	});
	if ($('#navi').length){
		$(document).keydown(function(e){
			var target = $('.next a').attr("href");
			if(e.which == 39){ window.location = target;} else if(e.which == 37){window.history.back(); }
		});
	}
});


