/**
* @author Michael
*/
$j = jQuery.noConflict();
$j(document).ready(function()
{
	function trace (msg)
	{
		console.log(msg);
	}
	theme = 'http://onenterframe.de/wp-content/themes/onenterframe.de';
	url = 'http://onenterframe.de';
	ptype = 'page';
	fancyZoomOptions = {
		Speed:250,
		showoverlay:true,
		overlay:0.75, 
		overlayColor:'#000'
	};
	//
	function init()
	{
		var html = '<div id="RecentToggle" class="up"><img src="' + theme + '/img/tools/toggle_up.png" alt="" class="up" /><img src="' + theme + '/img/tools/toggle_down.png" alt="" class="down" /></div>';
		$j('#Menu').after(html);
		// check cookie
		var state = $j.cookie('recenttoggle');
		if (state == 'small')
		{
			hideRecent();
		}
		else
		{
			$j('#RecentToggle img.up').css({'opacity': '1'});
			$j('#RecentToggle img.down').css({'opacity': '0.5'});
			applyToggleUp();
		}
		applyPortfolioStyle();
		applyFancyIframe();
		ifix ();
		$j.localScroll();
	}
	function ifix ()
	{
		if($j.browser.msie && $j.browser.version == "6.0")
		{
			$j.ifixpng(theme + '/js/ressources/blank.gif');
			$j('img[@src$=.png]').ifixpng();
		}
	}
	//
	function hideRecent()
	{
		$j('#RecentToggle img.up').css({'opacity': '0.5'});
		$j('#RecentToggle img.down').css({'opacity': '1'});
		applyToggleDown();
		$j('#Recent').hide();
		$j('#Header').css({'height':'150px'});
		$j('body').css({'background-position': '0 -216px'});
		$j('#Recent').css({'opacity':'0'});
		var href = $j('#Recent a').attr('href');
		var html = '<div id="RecentSmall"><a href="' + href + '"><img src="' + theme + '/img/featured/proj_like_small.png" alt="" /></a></div>';
		$j('#Recent').before(html)
		$j('#RecentSmall').css({top:'20px'});
		
		/*
		html = '<img id="Heart" src="' + theme + '/img/featured/heart.png" alt="" />'
		$j('#RecentSmall').prepend(html);
		$j('#Heart').hide().fadeIn('slow').animate({width:'48px', height:'48px', top:'-80px', left:'50px'}, 550);
		*/
	}
	//
	function applyToggleDown()
	{
		$j('#RecentToggle').bind('click', function ()
		{
			$j('#RecentSmall').animate({top:'-120px'}, 450, 'swing', function ()
			{
				$j('#Header').animate({'height':'366px'}, 850, 'swing', function ()
				{
					$j('#Recent').animate({'opacity':'1'}, 300)
					$j('#Recent').slideDown(300, function (){});
				});
				$j('body').animate({backgroundPosition:'(0 0)'}, 850, 'swing', function ()
				{
					$j.cookie('recenttoggle', 'big');
					applyToggleUp();
					$j('#RecentSmall').remove();
				});
				$j('#RecentToggle img.up').animate({'opacity': '1'}, 500);
				$j('#RecentToggle img.down').animate({'opacity': '0.5'}, 500);
				$j('#RecentToggle').unbind('click');
				
			})
			
		});
	}
	//
	function applyToggleUp()
	{
		$j('#RecentToggle').bind('click', function ()
		{
			$j('#Recent').animate({'opacity':'0'}, 300);
			$j('#Recent').slideUp(300, function ()
			{
				//$j('#RecentToggle').animate({'bottom':'113px'}, 850);
				$j('#RecentToggle img.up').animate({'opacity': '0.5'}, 500);
				$j('#RecentToggle img.down').animate({'opacity': '1'}, 500);
				//
				$j('#Header').animate({'height':'150px'}, 850);
				$j('body')
					.css({'background-position': '0 0'})
					.animate({backgroundPosition:'(0 -216px)'}, 850, 'swing', function ()
					{
						$j.cookie('recenttoggle', 'small');
						applyToggleDown();
						var href = $j('#Recent a').attr('href');
						var html = '<div id="RecentSmall"><a href="' + href + '"><img src="' + theme + '/img/featured/proj_like_small.png" alt="" /></a></div>';
						$j('#Recent').before(html)
						$j('#RecentSmall').css({top:'-110px'}).animate({top:'20px'}, 450, 'swing', function ()
						{
							html = '<img id="Heart" src="' + theme + '/img/featured/heart.png" alt="" />'
							$j('#RecentSmall').prepend(html);
							$j('#Heart').hide().fadeIn('slow').animate({width:'48px', height:'48px', top:'-80px', left:'50px'}, 450, 'swing');
						});
					});
			});
			$j(this).unbind('click');
		});
	}
	//
	function addCycleFunctions (rel)
	{
		if (rel == undefined )
			rel = 'a.fancy';
		//
		$j('.close, .overlay').bind('click', function (){$j('li.current').removeClass('current');});
		// bind an event to each a.fancy:
		$j(rel).bind('click', function (e)
		{
			trace(this)
			var ul = $j(this).parent().parent();
			// make sure there's no 'current' li
			$j('#Portfolio li.current').removeClass('current');
			// only if there's more than one image
			if (ul.children().length > 1)
			{
				var li = $j(this).parent();
				li.addClass('current');
				var html = '<div id="Cycler"></div>';
				$j('.zoomImage').wrap(html);
				html = '<div class="next"></div><div class="prev"></div>';
				$j('#Cycler').append(html);
				//
				$j('#Cycler .prev, #Cycler .next')
					.data('ul', ul)
					.css({
						height:$j('#Cycler img').height(),
						opacity:0.5,
						cursor:'pointer'
					})
					.hover(
						function ()
						{
							$j(this).fadeTo('fast', 1);;
						},
						function ()
						{
							$j(this).fadeTo('fast', 0.5);
						}
					);
				//
				function splitUrl(s)
				{
					var o = {path:'', file:'', filename:'', type:''};
					var lio = s.lastIndexOf('/');
					o.path = s.substr(0, lio + 1);
					o.file = s.substr(lio + 1, s.length);
					o.filename = o.file.substr(0, o.file.lastIndexOf('.'));
					o.type = o.file.substr(o.file.lastIndexOf('.') + 1, o.file.length);
					return o;
				}
				function prevImage()
				{
					var cur = $j('li.current a').attr('href');
					var o = splitUrl(cur);
					var curnr = parseInt(o.filename.split('_')[1]) - 1 == 0 ? $j('li.current').parent().children().length : (o.filename.split('_')[1]-1);
					var newfile = o.filename.split('_')[0] + '_' + curnr + '.' + o.type;
					//var html = '<img class="zoomImage" src="' + o.path + newfile + '" style="z-index:101; margin-top:15px; margin-right:15px;" />';
					var nImg = $j('#Cycler img.zoomImage').attr('src', o.path + newfile);
					//$j('#Cycler img').fadeOut()
					var p = $j('li.current').prev();
					if (p.length == 0)
					{
						p = $j('li.current').parent().children()[$j('li.current').parent().children().length - 1];
					}
					$j('li.current').removeClass('current');
					$j(p).addClass('current');
					
				}
				function nextImage()
				{
					var cur = $j('li.current a').attr('href');
					var o = splitUrl(cur);
					var curnr = parseInt(o.filename.split('_')[1]) + 1 > $j('li.current').parent().children().length ? 1 : parseInt(o.filename.split('_')[1]) + 1;
					var newfile = o.filename.split('_')[0] + '_' + curnr + '.' + o.type;
					//var html = '<img class="zoomImage" src="' + o.path + newfile + '" style="z-index:101; margin-top:15px; margin-right:15px;" />';
					var nImg = $j('#Cycler img.zoomImage').attr('src', o.path + newfile);
					//$j('#Cycler img').fadeOut()
					var p = $j('li.current').next();
					if (p.length == 0)
					{
						p = $j('li.current').parent().children()[0];
					}
					$j('li.current').removeClass('current');
					$j(p).addClass('current');
				}
				$j('#Cycler .prev').bind('click', function ()
				{
					prevImage();
				});
				$j('#Cycler .next').bind('click', function ()
				{
					nextImage();
				});
			} // endif more than one image
		});
	}
	function addNiceRollOver(rel)
	{
		if (rel == undefined)
		{
			$j.each ($j('a.fancy'), function (i, val)
			{
				var zoom = '<div class="zoom"></div>';
				$j(this).append(zoom);
				var z = $j(this).find('.zoom');
				z.css({
					top:-$j(this).find('img').height() + 20 + 'px'
				});
				z.hide();
				z.parent().hover(
				function ()
				{
					$j(this).find('.zoom').fadeIn();
				},
				function ()
				{
					$j(this).find('.zoom').fadeOut();
				});
			});
		}
		else
		{
			var rel = $j(rel).find('a');
			var zoom = '<div class="zoom"></div>';
			$j(this).append(zoom);
			var z = $j(rel).find('.zoom');
			z.hide();
			z.parent().hover(
			function ()
			{
				$j(this).find('.zoom').fadeIn();
			},
			function ()
			{
				$j(this).find('.zoom').fadeOut();
			});
		}
		//$j('.portfolio a.fancy .zoom').css({top:-$j('.portfolio a.fancy').find('img').height() + 'px', left:'5px'})
	}
	//
	function applyFancyZoom ()
	{
		// adding picture preview
		$j.fn.fancyzoom.defaultsOptions.imgDir = theme + "/js/ressources/";
		var as = $j('a img');
		$j.each(as, function (i, val)
		{
			var href = $j(this).parent().attr('href');
			if (href.substr(href.length - 3) == ('jpg' || 'png' || 'gif'))
			{
				$j(this).parent().addClass('fancy'); // 'a'
			}
		});
		$j('a.fancy:not(a.fancy.iframe)').fancyzoom(fancyZoomOptions);
		var fi = $j('a img.size-full');
		$j.each(fi, function (i, val)
		{
			$j(this).clone().insertAfter($j(this).parent());
			$j(this).parent().remove();
		});
		//
		// add some nice rollover style:
		addNiceRollOver();
		// adding video preview:
		var html = '<script src="' + theme + '/js/swfobject.js" type="text/javascript"></script>';
		html += '<script src="' + theme + '/js/jquery.dimensions.min.js" type="text/javascript"></script>';
		$j('head').append(html);
		var videos = $j('a.flv');
		$j.each(videos, function ()
		{
			$j(this).bind('click', function ()
			{
				function closeVideoBox()
				{
					$j('#Videopreview').remove();
					$j('#VideopreviewBg').remove();
				}
				var html = '<div id="VideopreviewBg"></div>';
				$j('body').prepend(html);
				html = '<div id="Videopreview"><h2>click to play</h2><p id="swf">Video should be here</p></div>';
				$j('body').prepend(html);
				$j('#VideopreviewBg').css({opacity:'0.7'});
				//
				$j('#Videopreview').css({
					left:($j('body').width() / 2) - 256,
					top:($j('body').height() / 2) - 192
				})
				var flv = $j(this).attr('href');
				var img = $j(this).children().attr('src').replace('125x80', '320x240');
				var s1 = new SWFObject(theme + '/js/player.swf','flvplayer','512','384','9');
				s1.addParam('allowfullscreen','true');
				s1.addParam('allowscriptaccess','always');
				s1.addParam('flashvars','file=' + flv + '&volume=30&controlbar=over&displayclick=play&stretching=none&autostart=false&image=' + img + '&skin=' + theme + '/js/stylish.swf' + '&frontcolor=ffffff&lightcolor=A1D3EC');
				s1.write('swf');
				html = '<div id="VideopreviewClose"><img src="' + theme + '/js/ressources/closebox.png" alt="" /></div>'
				$j('#Videopreview').append(html);
				
				$j('#VideopreviewBg, #VideopreviewClose').bind('click', function ()
				{
					closeVideoBox();
				});
				//
				return false;
			})
		});
	}
	//
	function applyFancyIframe()
	{
		$j('a.fancy.iframe').bind('click', function ()
		{
			var src = $j(this).attr('href');
			var iframe = '<div id="Iframepreview"><h2><a href="' + src + '">Original (Fullscreen)</a></h2><iframe src="' + src + '" width="100%" height="100%"></iframe></div>';
			var close = '<div id="IframepreviewClose"><img src="' + theme + '/js/ressources/closebox.png" alt="" /></div>';
			var bg = '<div id="IframepreviewBg"></div>';
			$j('body').prepend(bg);
			$j('#IframepreviewBg').css({opacity:'0.7'});
			$j('body').prepend(iframe);
			$j('#Iframepreview').append(close);
			$j('#Iframepreview').css({
				left:($j('body').width() / 2) - ($j('#Iframepreview').width()/2),
				top:($j('body').height() / 2) - ($j('#Iframepreview').height()/2)
			});
			$j('#IframepreviewBg, #IframepreviewClose').bind('click', function ()
			{
				$j('#Iframepreview').remove();
				$j('#IframepreviewBg').remove();
			});
			//
			return false;
		});
	} 
	//
	function applyPortfolioStyle()
	{
		var ps = $j('.portfolio');
		var plen = ps.length;
		if (ptype == 'page' && plen > 0)
		{
			// collapse header
			if ($j.cookie('recenttoggle') != 'small')
			{
				hideRecent();
				$j('#RecentToggle').unbind('click');
				applyToggleDown();
			}
			var list = $j('.portfolio ul');
			$j.each(list, function (i, val)
			{
				// only link css if neccessary
				if ($j('#jsportfolio').length == 0)
				{
					var link = '<link href="' + theme + '/css/js.portfolio.css" rel="stylesheet" media="screen, projection" type="text/css" id="jsportfolio" />'
					$j('head').append(link);
				}
				var t = $j(this).parent().attr('class');
				var id = t.substr(t.indexOf('id_'));	
				var prev = '<img src="' + theme + '/img/icons/previous.png" alt="" class="previous" id="p_' + id + '" />';			
				var next = '<img src="' + theme + '/img/icons/next.png" alt="" class="next" id="n_' + id + '" />';
				//
				applySlideFunctions(
					this,
					id,
					prev,
					next
				);
				
				// fix css
				var lis = $j('.portfolio li');
				$j.each(lis, function (i, val)
				{
					var html = $j(this).html();
					var parent = $j(this).parent();
					$j(this).parent().append('<li>' + html + '</li>');
					$j(this).remove();
				});
			});
		}
		applyFancyZoom();
	}
	//
	function applySlideFunctions(ul, id, p, n)
	{
		$j(ul).wrap('<div class="slider"></div>')
		$j(ul).parent().easySlider({prevText:p, nextText:n, prevId:'p_' + id, nextId:'n_' + id, speed:150});
		if ($j.browser.safari){
			
		}
	}
	//$j('.portfolio').css({opacity:0});
	$j('.wp-caption').next().addClass('clearfix');
	//
	function fancyTweety()
	{
		$j('#Twitterlink').hover(
			function(){
				$j('#Fatbird').fadeOut(200);
				$j('#Fatbird_Sign').fadeIn(200);
			}, 
			function(){
				$j('#Fatbird').fadeIn(100);
				$j('#Fatbird_Sign').fadeOut(100);
			}
		);
	}
	fancyTweety();
	//
	function betterTwitterLinks (){
		
		var twl = $j('#Tweets .twitter-link');
		$j.each(twl, function(i, val)
		{
			$j(this).attr('href').indexOf('http://twitter.com/#search?') > -1 ? $j(this).addClass('tweet') : null; 
		});
	}
	betterTwitterLinks();
	//
	function fancyTwitterScroller()
	{
		$j('#Tweets ul.twitter').css('overflow', 'hidden');
		var images = '<img class="up button deactivated" alt="" src="' + theme + '/img/tools/toggle_up.png" />' 
		+ '<img class="down button" alt="" src="' + theme + '/img/tools/toggle_down.png" />';		
		$j('#Tweets').append('<div id="Scroller">' + images + '</div>');
		$j('#Tweets ul.twitter').data('scrollPositions', new Array());
		$j('#Tweets ul.twitter').data('scrollPosition', 0);
		$j.each($j('#Tweets li.twitter-item'), function(i, el)
		{
			$j(this)
			.attr('id', 'Tweet-'+(i+1))
			.css('opacity', 1/(i+1));
			$j(this).parent().data('scrollPositions').push( (i+1)* $j(this).height() ); 
		});
		$j('#Scroller img').bind('click', function()
		{
			var index = parseInt( $j('#Tweets ul.twitter').data('scrollPosition') );
			var positions = $j('#Tweets ul.twitter').data('scrollPositions');
			if ( $j(this).hasClass('deactivated') )
				return false;
			if ($j(this).hasClass('up'))
			{
				$j('#Tweets ul.twitter').scrollTo($j('#Tweets ul.twitter').data('scrollPositions')[(index-1)] - 8);
				(index > 0) ? $j('#Tweets ul.twitter').data('scrollPosition', (index--)) : null;
			}
			if ($j(this).hasClass('down'))
			{
				$j('#Tweets ul.twitter').scrollTo(positions[(index+1)] - 8);
				(index < positions.length - 1) ? $j('#Tweets ul.twitter').data('scrollPosition', (index++)) : null;
			}
		});
		//
		$j('.button.deactivated').css(
		{
			opacity:0.5,
			cursor:'normal'
		});
	}
	fancyTwitterScroller();
	//
	
	//
	init();
});





