var pictures_gallery_loaded = false;
var photos_gallery_loaded = false;

$(document).ready(function() {
	$('#twitter').load(home_url + '?ajax=twitter');
});

function shop_effects(type)
{
	//Navigation der shops
	$('#shop_' + type + '2').cycle({
		fx: 'scrollHorz',
		speed: 1000, 
		timeout: 0,
		next: '#shop_' + type + '_next', 
		prev: '#shop_' + type + '_prev',
	});
	
	//Fadeeffect der Shopartikel
	$('.img').mouseover(function() {
		$(this).fadeTo(100, 0);
	});
	$('.img').mouseout(function() {
		$(this).fadeTo(100, 1);
	});
}

function loadpost(url, in_shop)
{
	if(in_shop == true)
	{
		var content_id = '#left_shop_content';
	}
	else
	{
		var content_id = '#left_content';
	}
	
	//ID ändern, wenn shopartikel aufgerufen wird
	if (in_shop == true && $("#left_content").length > 0)
	{
		$('#left_content').attr("id", 'left_shop_content');
	}
	
	if (in_shop == false && $("#left_shop_content").length > 0)
	{
		$('#left_shop_content').attr("id", 'left_content');
	}
	
	$(content_id).html('<div style="text-align: center;"><img src="' + template_direction + '/images/load.gif" /><br />Article is loading...</div>');
	
	$(content_id).load(url);
}

function toggle_mini_nav(name)
{
	document.getElementById(name + '_button').className = 'button_active';
	
	if(name != 'about')
	{
		document.getElementById('about_content').style.display = 'none';
		document.getElementById(name + '_content').style.display = 'block';
	}
	
	resize_textarea(document.getElementById('contact_form'));
	
	//MM_effectBlind('about_content', 0, '100%', '0%', false);
	//MM_effectBlind('pictures_content', 0, '100%', '0%', false);
	//MM_effectBlind('photos_content', 0, '100%', '0%', false);
	//MM_effectBlind('contact_content', 0, '100%', '0%', false);
}

function MM_effectBlind(targetElement, duration, from, to, toggle)
{
	Spry.Effect.DoBlind(targetElement, {duration: duration, from: from, to: to, toggle: toggle});
}

function open_mail()
{
	var mail = 'mailto';
	var name_at = ':info@';
	var domain = 'rubin-design.com';
	 
	location.href = mail + name_at + domain;
}

function resize_textarea(area)
{
	var content = area.value;

	var divided = content.split('\n');
	var distance = 1;
	var old_position = -1;
	for(var i = 0; i < divided.length; i++)
	{
		var position = divided[i].search(/ |-/);
		
		while(position != old_position)
		{
			if(parseInt(position / area.cols) != parseInt(old_position / area.cols))
			{
				distance += Math.abs(old_position%area.cols - (area.cols-1));
			}
			
			old_position = position;
			position++;
			position = divided[i].slice(position).search(/ |-/) + position;
		}
		
		distance += Math.abs((divided[i].length-1) % area.cols - area.cols) + divided[i].length-1;
	}

	distance = distance / area.cols * 17 + 28;
	if(distance > 560)
	{
        distance = 560;
	}
    area.style.height = distance+'px';
}


function toggleMe(a, b)
{
	var e = document.getElementById(a);
	if(!e)return true;
	
	if(b == null)
	{
		if(e.style.display == "none")
		{
			e.style.display = "block";
		}
		else
		{
			e.style.display = "none";
		}
	}
	else
	{
		if(b == 'off')
		{
			e.style.display = "none";
		}
		else
		{
			e.style.display = "block";
		}
	}
	
	return true;
}

function mini_nav_control(name)
{
	buttons = new Array("about", "pictures", "photos", "contact");
	
	current_classname = document.getElementById(name + '_button').className;
	
	if(current_classname == 'button_inactive')
	{
		c = buttons.length;
		i = 0;
		
		while(i < c)
		{
			//Ermittelt den Klassenamen
			classname = document.getElementById(buttons[i] + '_button').className;
			
			if(classname == 'button_active')
			{
				document.getElementById(buttons[i] + '_button').className = 'button_inactive';
				document.getElementById(buttons[i] + '_content').style.display = 'none';
				//MM_effectBlind(buttons[i] + '_content', 0, '100%', '0%', false);
			}
			
			i = i + 1;
		}
		
		document.getElementById(name + '_button').className = 'button_active';
		document.getElementById(name + '_content').style.display = 'block';
		//MM_effectBlind(name + '_content', 1000, '0%', '100%', false);
	}
	
	if(name == "pictures" && pictures_gallery_loaded == false)
	{
		$('#picture_gallery').load(home_url + '?ajax=gallery&id=pictures', function() {
			$('#picture_gallery').cycle({
				fx: 'scrollHorz',
				speed: 1000,
				timeout: 0,
				next: '#pictures_next_thumbs',
				prev: '#pictures_prev_thumbs',
			});
		});
		
		pictures_gallery_loaded = true;
	}
	
	if(name == "photos" && photos_gallery_loaded == false)
	{
		$('#photo_gallery').load(home_url + '?ajax=gallery&id=photos', function() {
			$('#photo_gallery').cycle({
				fx: 'scrollHorz',
				speed: 1000, 
				timeout: 0,
				next: '#photos_next_thumbs', 
				prev: '#photos_prev_thumbs',
			});
		});
		
		photos_gallery_loaded = true;
	}
}


function img_hover(id, img)
{
	url = 'url("' + img + '")';
	
	document.getElementById(id).style.backgroundImage = url;
}
