$.each(['backgroundColor', 'borderBottomColor', 'borderLeftColor',
    'borderRightColor', 'borderTopColor', 'borderColor', 'color', 'outlineColor'],
function(i, attr) {
    $.fx.step[attr] = function(fx) {
        if (!fx.colorInit) {
            fx.start = getColor(fx.elem, attr);
            fx.end = getRGB(fx.end);
            fx.colorInit = true;
        }

        fx.elem.style[attr] = 'rgb(' +
            Math.max(Math.min(parseInt((fx.pos * (fx.end[0] - fx.start[0])) + fx.start[0], 10), 255), 0) + ',' +
            Math.max(Math.min(parseInt((fx.pos * (fx.end[1] - fx.start[1])) + fx.start[1], 10), 255), 0) + ',' +
            Math.max(Math.min(parseInt((fx.pos * (fx.end[2] - fx.start[2])) + fx.start[2], 10), 255), 0) + ')';
    };
});

// Color Conversion functions from highlightFade
// By Blair Mitchelmore
// http://jquery.offput.ca/highlightFade/

// Parse strings looking for color tuples [255,255,255]
function getRGB(color) {
        var result;

        // Check if we're already dealing with an array of colors
        if ( color && color.constructor == Array && color.length == 3 )
                return color;

        // Look for rgb(num,num,num)
        if (result = /rgb\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*\)/.exec(color))
                return [parseInt(result[1],10), parseInt(result[2],10), parseInt(result[3],10)];

        // Look for rgb(num%,num%,num%)
        if (result = /rgb\(\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*\)/.exec(color))
                return [parseFloat(result[1])*2.55, parseFloat(result[2])*2.55, parseFloat(result[3])*2.55];

        // Look for #a0b1c2
        if (result = /#([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})/.exec(color))
                return [parseInt(result[1],16), parseInt(result[2],16), parseInt(result[3],16)];

        // Look for #fff
        if (result = /#([a-fA-F0-9])([a-fA-F0-9])([a-fA-F0-9])/.exec(color))
                return [parseInt(result[1]+result[1],16), parseInt(result[2]+result[2],16), parseInt(result[3]+result[3],16)];

        // Look for rgba(0, 0, 0, 0) == transparent in Safari 3
        if (result = /rgba\(0, 0, 0, 0\)/.exec(color))
                return colors['transparent'];

        // Otherwise, we're most likely dealing with a named color
        return colors[$.trim(color).toLowerCase()];
}

function getColor(elem, attr) {
        var color;

        do {
                color = $.curCSS(elem, attr);

                // Keep going until we find an element that has color, or we hit the body
                if ( color != '' && color != 'transparent' || $.nodeName(elem, "body") )
                        break;

                attr = "backgroundColor";
        } while ( elem = elem.parentNode );

        return getRGB(color);
};

function getCurrentScreen() {
	if (currentScreen == "0") {
		return $('#screen0');
	} else {
		return $('#screen1');
	}
}

function getPrevScreen() {
	if (currentScreen == "0") {
		return $('#screen1');
	} else {
		return $('#screen0');
	}
}

function toggleScreen() {
	if (currentScreen == "0") {
		currentScreen = "1";
	} else {
		currentScreen = "0";
	}
}

function removePageOne() {
	if (currentStatus == 0)  {
		$('#heroSpace').animate({top: -400}, {duration: 350});
		$('#mainNav').animate({top: -350}, {duration: 350});
		$('#greybar').animate({top: 28}, {duration: 350});
		currentStatus = 1;
	}
}

function showPageOne() {
	if (currentStatus == 1) {
		$('#heroSpace').animate({top: 0}, {duration: 350});
		$('#mainNav').animate({top: 0}, {duration: 350});
		$('#greybar').animate({top: 378}, {duration: 350});
		currentStatus = 0;
	}
}

function loadAddress(href) {
	console.log(language);
 	$.ajax({
	      type: "POST",
	      url: 'http://icon-worldwide.com/'+language+href,
	      data: {ajax:'true'},
	      success: function (data) {
	        if (data) {
				pageHandler(nextScreen, data);
				
					

	        }
	      },
	      error: function (xmlhttp) {
	        //alert('An error occured: ' + xmlhttp.status);
	      }
	    });

}

function loadLanguage(href) {
    console.log("getting language "+'http://icon-worldwide.com/'+language+href);
 	$.ajax({
	      type: "POST",
	      url: 'http://icon-worldwide.com/'+language+href,
	      data: {lang:'true'},
	      success: function (data) {
	        if (data) {
				$('#language-switcher').replaceWith(data);
				function setHref( index, attributeValue ) {
				    return (this.href );
				}
				$('a.menu-item').attr( 'mylink', setHref );
				$('a.menu-item').attr('href', "#");
				$('a.menu-item').click(function() {

						window.location.href=$(this).attr("mylink").replace('//icon-worldwide.com/','//icon-worldwide.com/#/').replace('/#/es/','/es/#/').replace('/#/de/','/de/#/');
				});
	        }
	      },
	      error: function (xmlhttp) {
	        //alert('An error occured: ' + xmlhttp.status);
	      }
		});
}

function pageHandler(e, data) {
	filter = e.split("?filter");
	e = filter[0];
	filter = filter[filter.length-1];
	filter = filter.substr(1,9999); 
	
	e = e.split("/");
	target = e[e.length-1];
	if ((e[e.length-2]) == "intern-blog") target = "intern-blog";
	if ((e[e.length-2]) == "icon-news") target = "icon-news";
	if ((e[e.length-2]) == "blog") target = "blog";
	if ((e[e.length-2]) == "blog-2") target = "blog";
	if ((e[e.length-2]) == "blog-3") target = "blog";
	if ((e[e.length-2]) == "blog-es") target = "blog";
	if ((e[e.length-2]) == "culture-2") target = "culture";
	if ((e[e.length-2]) == "culture-3") target = "culture";
	if ((e[e.length-2]) == "work-3") target = "work";
	if ((e[e.length-2]) == "work-2") target = "work";
	if ((e[e.length-2]) == "intern-blog-de") target = "intern-blog";
	if ((e[e.length-2]) == "icon-news-de") target = "icon-news";
	if ((e[e.length-2]) == "intern-blog-es") target = "intern-blog";
	if ((e[e.length-2]) == "icon-news-es") target = "icon-news";

	//icon-news-de
	console.log(target)
	switch(target)
	{
	case 'clients':
	case 'about':
	  toggleScreen();
	  removePageOne();
	  getPrevScreen().animate({top: -2000}, 500, function() {getPrevScreen().hide()});
	  getCurrentScreen().show();
	  getPrevScreen().show();
	  getCurrentScreen().css('top', getPrevScreen().height() );
	  getCurrentScreen().animate({top: 68}, {duration: 500});
	  getCurrentScreen().html(data);
	 // $('#mainFooter').css('top', getCurrentScreen().find('#main').height() + 60);
	  if (prevScreen = "culture") $('body').animate({ backgroundColor: "#181b1f" }, '500');
	  if (prevScreen = "culture") $('#language-switcher a').animate({ color: "#fff" }, '500');
	  if (prevScreen = "culture") $('#language-switcher a.active').animate({ color: "#B81C24" }, '500');
	  $('#menu-about-us .menu-item a').address(function(event) {
			target = $(this).attr('href').replace('http:/icon-worldwide.com/', '').replace('http://icon-worldwide.com/', '');
			plang = target.substr(0,4);
			switch (plang) {
				case '/es/':
				case '/de/':
				case '/en/':
					target = target.substr(3,99999);
					break;
			}
			return target;
			//return $(this).attr('href').replace('http:/icon-worldwide.com/', '').replace('http://icon-worldwide.com/', '');
      });
	  break;
	case 'contact':
	  toggleScreen();
	  removePageOne();
	  getPrevScreen().animate({top: -2000}, 500, function() {getPrevScreen().hide()});
	  getCurrentScreen().show();
	  getPrevScreen().show();
	  getCurrentScreen().css('top', getPrevScreen().height() );
	  getCurrentScreen().animate({top: 68}, {duration: 500});
	  getCurrentScreen().html(data);
	 // $('#mainFooter').css('top', getCurrentScreen().find('#main').height() + 130);
	  if (prevScreen = "culture") $('body').animate({ backgroundColor: "#181b1f" }, '500');
	  if (prevScreen = "culture") $('#language-switcher a').animate({ color: "#fff" }, '500');
	  if (prevScreen = "culture") $('#language-switcher a.active').animate({ color: "#B81C24" }, '500');
	  $('#menu-about-us .menu-item a').address(function(event) {
			target = $(this).attr('href').replace('http:/icon-worldwide.com/', '').replace('http://icon-worldwide.com/', '');
			plang = target.substr(0,4);
			switch (plang) {
				case '/es/':
				case '/de/':
				case '/en/':
					target = target.substr(3,99999);
					break;
			}
			return target;
			//return $(this).attr('href').replace('http:/icon-worldwide.com/', '').replace('http://icon-worldwide.com/', '');
      });
	  break;
	case 'work':
	  toggleScreen();
	  removePageOne();
	  getPrevScreen().animate({top: -2000}, 500, function() {getPrevScreen().hide()});
	  getCurrentScreen().show();
	  getPrevScreen().show();
	  getCurrentScreen().css('top', getPrevScreen().height() );
	  getCurrentScreen().animate({top: 68}, {duration: 500});
	  getCurrentScreen().html(data);
		//$('#mainFooter').css('top', getCurrentScreen().find('#main').height() + 80);
	  if (prevScreen = "culture") $('body').animate({ backgroundColor: "#181b1f" }, '500');
	  if (prevScreen = "culture") $('#language-switcher a').animate({ color: "#fff" }, '500');
	  if (prevScreen = "culture") $('#language-switcher a.active').animate({ color: "#B81C24" }, '500');
	  workSort();
	  initWork();
	  console.log(filter);
		if (filter) $('#work').isotope( {filter:'.'+filter});      
	  break;
	case 'work-2':
	  toggleScreen();
	  removePageOne();
	  getPrevScreen().animate({top: -2000}, 500, function() {getPrevScreen().hide()});
	  getCurrentScreen().show();
	  getPrevScreen().show();
	  getCurrentScreen().css('top', getPrevScreen().height() );
	  getCurrentScreen().animate({top: 68}, {duration: 500});
	  getCurrentScreen().html(data);
		//$('#mainFooter').css('top', getCurrentScreen().find('#main').height() + 80);
	  if (prevScreen = "culture") $('body').animate({ backgroundColor: "#181b1f" }, '500');
	  if (prevScreen = "culture") $('#language-switcher a').animate({ color: "#fff" }, '500');
	  if (prevScreen = "culture") $('#language-switcher a.active').animate({ color: "#B81C24" }, '500');
	  workSort();
	  initWork();
	  console.log(filter);
		if (filter) $('#work').isotope( {filter:'.'+filter});      
	  break;
	case 'work-3':
	  toggleScreen();
	  removePageOne();
	  getPrevScreen().animate({top: -2000}, 500, function() {getPrevScreen().hide()});
	  getCurrentScreen().show();
	  getPrevScreen().show();
	  getCurrentScreen().css('top', getPrevScreen().height() );
	  getCurrentScreen().animate({top: 68}, {duration: 500});
	  getCurrentScreen().html(data);
		//$('#mainFooter').css('top', getCurrentScreen().find('#main').height() + 80);
	  if (prevScreen = "culture") $('body').animate({ backgroundColor: "#181b1f" }, '500');
	  if (prevScreen = "culture") $('#language-switcher a').animate({ color: "#fff" }, '500');
	  if (prevScreen = "culture") $('#language-switcher a.active').animate({ color: "#B81C24" }, '500');
	  workSort();
	  initWork();
	  console.log(filter);
		if (filter) $('#work').isotope( {filter:'.'+filter});      
	  break;
	case 'culture':
	  toggleScreen();
	  removePageOne();
	  getPrevScreen().animate({top: -2000}, 500, function() {getPrevScreen().hide()});
	  getCurrentScreen().show();
	  getPrevScreen().show();
	  getCurrentScreen().css('top', getPrevScreen().height() );
	  getCurrentScreen().animate({top: 68}, {duration: 500});
	  $('body').animate({ backgroundColor: "#fff" }, '500');
	  $('#language-switcher a').animate({ color: "#000" }, '500');
	  $('#language-switcher a.active').animate({ color: "#B81C24" }, '500');
	  getCurrentScreen().html(data);
	  //$('#mainFooter').css('top', getCurrentScreen().find('#main').height() + 60);
	  break;
	case 'culture-2':
	  toggleScreen();
	  removePageOne();
	  getPrevScreen().animate({top: -2000}, 500, function() {getPrevScreen().hide()});
	  getCurrentScreen().show();
	  getPrevScreen().show();
	  getCurrentScreen().css('top', getPrevScreen().height() );
	  getCurrentScreen().animate({top: 68}, {duration: 500});
	  $('body').animate({ backgroundColor: "#fff" }, '500');
	  $('#language-switcher a').animate({ color: "#000" }, '500');
	  $('#language-switcher a.active').animate({ color: "#B81C24" }, '500');
	  getCurrentScreen().html(data);
	  //$('#mainFooter').css('top', getCurrentScreen().find('#main').height() + 60);
	  googlePlus();
	  break;
	case 'culture-3':
	  toggleScreen();
	  removePageOne();
	  getPrevScreen().animate({top: -2000}, 500, function() {getPrevScreen().hide()});
	  getCurrentScreen().show();
	  getPrevScreen().show();
	  getCurrentScreen().css('top', getPrevScreen().height() );
	  getCurrentScreen().animate({top: 68}, {duration: 500});
	  $('body').animate({ backgroundColor: "#fff" }, '500');
	  $('#language-switcher a').animate({ color: "#000" }, '500');
	  $('#language-switcher a.active').animate({ color: "#B81C24" }, '500');
	  getCurrentScreen().html(data);
	  //$('#mainFooter').css('top', getCurrentScreen().find('#main').height() + 60);
	  googlePlus();
	  break;
	case 'intern-blog':
	  toggleScreen();
	  removePageOne();
	  getPrevScreen().animate({top: -2000}, 500, function() {getPrevScreen().hide()});
	  getCurrentScreen().show();
	  getPrevScreen().show();
	  getCurrentScreen().css('top', getPrevScreen().height() );
	  getCurrentScreen().animate({top: 68}, {duration: 500});
	  $('body').animate({ backgroundColor: "#fff" }, '500');
	  $('#language-switcher a').animate({ color: "#000" }, '500');
	  $('#language-switcher a.active').animate({ color: "#B81C24" }, '500');
	  getCurrentScreen().html(data);
	  //$('#mainFooter').css('top', getCurrentScreen().find('#main').height() + 60);
	  googlePlus();
	  break;
	case 'icon-news':
	  toggleScreen();
	  removePageOne();
	  getPrevScreen().animate({top: -2000}, 500, function() {getPrevScreen().hide()});
	  getCurrentScreen().show();
	  getPrevScreen().show();
	  getCurrentScreen().css('top', getPrevScreen().height() );
	  getCurrentScreen().animate({top: 68}, {duration: 500});
	  $('body').animate({ backgroundColor: "#fff" }, '500');
	  $('#language-switcher a').animate({ color: "#000" }, '500');
	  $('#language-switcher a.active').animate({ color: "#B81C24" }, '500');
	  getCurrentScreen().html(data);
	  //$('#mainFooter').css('top', getCurrentScreen().find('#main').height() + 60);
	  googlePlus();
	  break;
	case 'blog':
	  toggleScreen();
	  removePageOne();
	  getPrevScreen().animate({top: -2000}, 500, function() {getPrevScreen().hide()});
	  getCurrentScreen().show();
	  getPrevScreen().show();
	  getCurrentScreen().css('top', getPrevScreen().height() );
	  getCurrentScreen().animate({top: 68}, {duration: 500});
	  $('body').animate({ backgroundColor: "#fff" }, '500');
	  blogSort();
	  $('#language-switcher a').animate({ color: "#000" }, '500');
	  $('#language-switcher a.active').animate({ color: "#B81C24" }, '500');
	  getCurrentScreen().html(data);
	  //$('#mainFooter').css('top', getCurrentScreen().find('#main').height() + 60);
	  googlePlus();
	  break;
	case '': //home
	  showPageOne();
	  getPrevScreen().animate({top: -1200}, 500, function() {getPrevScreen().hide();toggleScreen();});
	  getCurrentScreen().show();
	  getCurrentScreen().css('top', getPrevScreen().height() );
	  getCurrentScreen().animate({top: 410}, {duration: 500});
	  getCurrentScreen().html(data);
	  //$('#mainFooter').css('top', getCurrentScreen().find('#main').height() + 60);
	  if (prevScreen = "culture") $('body').animate({ backgroundColor: "#181b1f" }, '500');
	  if (prevScreen = "culture") $('#language-switcher a').animate({ color: "#fff" }, '500');
	  if (prevScreen = "culture") $('#language-switcher a.active').animate({ color: "#B81C24" }, '500');
	  break;
	default:
	  toggleScreen();
	  removePageOne();
	  getPrevScreen().animate({top: -2000}, 500, function() {getPrevScreen().hide()});
	  getCurrentScreen().show();
	  getPrevScreen().show();
	  getCurrentScreen().css('top', getPrevScreen().height() );
	  getCurrentScreen().animate({top: 68}, {duration: 500});
	 // $('#mainFooter').css('top', getCurrentScreen().find('#main').height());
	  getCurrentScreen().html(data);
	  if (prevScreen = "culture") $('body').animate({ backgroundColor: "#181b1f" }, '500');
	  if (prevScreen = "culture") $('#language-switcher a').animate({ color: "#fff" }, '500');
	  if (prevScreen = "culture") $('#language-switcher a.active').animate({ color: "#B81C24" }, '500');
	}

}

	var currentScreen = "0";
	var currentStatus = 0;
	var nextScreen = "";
	var prevScreen = "";
	var language = "";
	
	var currentI = 0;
	
	//$('#screen0').slideToggle("slow");
	
	$.address.init(function(event) {
        $('.menu-item a, a.blog-item').address(function(event) {
			target = $(this).attr('href').replace('http:/icon-worldwide.com/', '').replace('http://icon-worldwide.com/', '');
			plang = target.substr(0,3);
			switch (plang) {
				case 'es/':
				case 'de/':
				case 'en/':
					target = target.substr(3,99999);
					break;
			}
			return target;
        });
		
		$('a.menu-item').attr( 'mylink', setHref );
		function setHref( index, attributeValue ) {
		    return (this.href );
		}
		$('a.menu-item').attr('href', "#");
		$('a.menu-item').click(function() {
			window.location.href=$(this).attr("mylink")+"#"+nextScreen;
		});
    }).change(function(event) {
			plang = document.location.href.replace('http://icon-worldwide.com/','').substr(0,3);
			switch (plang) {
				case "de/":
				case "en/":
				case "es/":
					language = plang.substr(0,2);
					break;
			}
			
			target = event.value.replace('/http:/icon-worldwide.com/', '');
			plang = target.substr(0,4);
			switch (plang) {
				case '/es/':
				case '/de/':
				case '/en/':
					target = target.substr(3,99999);
					break;
			}
			prevScreen = nextScreen;
			nextScreen = target;
			loadAddress(target);
			loadLanguage(target);


		if (event.pathNames[0]) {
        var text = (event.value == '') ? 'Home' : 
            event.pathNames[0].substr(0, 1).toUpperCase() + 
            event.pathNames[0].substr(1);
		}
        $('.menu-item a').each(function() {
            $(this).toggleClass('selected', $(this).text() == text);
        });
    })	.internalChange(function(event) {
			//nextScreen = event.value;
    }).bind('externalChange', {msg: 'The value of the event is "{value}".'}, function(event) {
			//nextScreen = event.data.msg.replace(/\{value\}/, event.value);
            
    });
