/**
 * Some common functions to handle dynamic content and effect
 * Oskryptowanie zapewniające obsługę dla dynamiczne zawartości i efektów
 */
function log(str){
	try{
		console.log(str);
	}catch(e){ 
		//alert(str); 
		}
}

var news = {
		box: null,
		current:0,
		countList:0,
		run: false,
		slideshow:true,
		slideshowTimer:null,
		slideshowDuration:8110,
		duration: 500,
		durationSlow:800,
		init: function(){
			news.box = $('#hslider_carousel');
			if(news.box.size() == 0)
				return false;
			$('#hslider').css('position','relative').mouseenter(function(){ news.slideshow=false; }).mouseleave(function(){ news.slideshow=true; });
			
			news.box.css({ 'width':'100000px','position':'absolute', 'left':'0','top':'0' });
			this.countList = $('#hslider_carousel li').size();
			if(this.countList <= 3)
				return false;

			$( "#hslider_carousel li:lt(3)" ).clone().appendTo('#hslider_carousel');
			this.countList+=3; 
			//log(this.countList);	
			$('#nPrevBtn').click(news.prev);
			$('#nNextBtn').click(news.next);
			
			news.current = 0;
			news.slideshowTimer = setTimeout( news.play , news.slideshowDuration);			
		},
		prev: function(){
			news.show(news.current-1,false);
		},
		next: function(){
			news.show(news.current+1,false);
		},
		show: function(idx,slow)
		{
			if (news.slideshowTimer) clearTimeout(news.slideshowTimer);
			news.slideshowTimer = setTimeout( news.play , slow == true ? news.slideshowDuration : news.slideshowDuration );
			
			if( this.slideshow == false || this.run == true )
				return false;
			
			this.run = true;
			var m = 262*idx;
			
			if(idx == -1){
				idx = news.countList -4; 
				m = 262*idx;
				news.box.css('left', '-' + (262*(idx+1)).toString() + 'px');
			} 
			
			//log('m:' + m + 'current:' + news.current );	
			
			news.box.animate({'left': '-' +  m.toString() + 'px' }, slow == true ? news.durationSlow : news.duration,'jswing', function(){ 

				if(news.countList - idx -4 >= 0 ){
					news.current = idx;
				} else {
					news.current = 0;
					news.box.css('left',0);
				}
				//log('current next:' + news.current);	
				news.run = false;
				
				//$("#hslider_carousel li:nth-child(" + idx + ")").clone().appendTo('#hslider_carousel'); 
			});
			
				
		},
		play: function(){
			//log('play:' + news.current + ' ' + (news.current+1));
			news.show((news.current+1), true );
		}		

	}

//$('#menu .te').live('load',function(){ log('load'); });
//log($('#menu .te').size()); 

$(document).ready(function () {
		var debuggin = false;
		var d1 = new Date(); 
		var d2 = new Date(2010,11,7,0,1);
		
		//alert(d2 + ' ' + d1);
		if(d1.getTime()<d2.getTime())
			$('#menu .te').css('background-image','url(images\/layout-logo-teo-christmas.gif)');

		$('<a />').attr({'href':'http://www.facebook.com/Era', 'target':'_blank' }).css({
				background : 'url(images/sidebar_facebook.gif) right top no-repeat',
				'display': 'block',
				height: '99px',
				left:'0',
				position: $.browser.msie &&  $.browser.version < 7 ? 'absolute' : 'fixed',
				top:'150px',
				width:'78px',
				'z-index':'9999999',
				outline: 'none'
			}).appendTo('body');	
			
		$('.addthis_toolbox  .addthis_button_facebook').attr('title','Podziel się');
		$('.addthis_toolbox  .addthis_button_twitter').attr('title','Podziel się');
		$('.addthis_toolbox  .addthis_button_wykop').attr('title','Podziel się');
		$('.addthis_toolbox  .addthis_button_blip').attr('title','Podziel się');
		$('.addthis_toolbox  .addthis_button_favorites').attr('title','Dodaj do ulubionych');

		//embedowanie wmode z youtube flasha
		// $("object").ifE(function (item) {
		// 	$(item).attr("wmode", "opaque").wrap('<div>');
		// });
		
		//embedowanie wmode z youtube flasha
		// $("object").ifE(function (item) {
		// 	/* Firefox*/
		// 	$(item).attr("wmode", "opaque");
		// 	/* IE */
		// 	var embedTag;
		// 	$("embed").each(function(i) {
		// 		embedTag = $(this).attr("outerHTML");
		// 		if ((embedTag != null) && (embedTag.length > 0)) {
		// 			embedTag = embedTag.replace(/embed /gi, "embed wmode=\"opaque\" ");
		// 			$(this).attr("outerHTML", embedTag);
		// 		}
		// 	});
		// 
		// });
		
		/*powiekszanie tekstu*/	
		$('.extend_text a').click(function(){
			var data = $(this).attr('rel').split('|');
			$('.'+data[0]).find('p').css('font-size', data[1]);
			$('.selected_size').removeClass('selected_size');
			$(this).addClass('selected_size');
			if (this.basicSize == '') {

			}
			return false;
		});
		
			var slide_num = $('.slide').length;
			var slide = $('.slide').get();
			for (var s in slide)
			{
				console.log("Jestem w slajdzie: "+ s + ' ' + slide[s]);
				var fade = slide[s];
				if(fade.id(':animated')) 
				{
					fade.stop().fadeTo(250,1);
				}
				else
				{
					fade.fadeIn(250);
				}
				
			}
			
		
		// $('div.fade').hover(function() {
		//           // on hovering over find the element we want to fade *up*
		//           var fade = $('> div', this);
		// 
		//           // if the element is currently being animated (to fadeOut)...
		//           if (fade.is(':animated')) {
		//               // ...stop the current animation, and fade it to 1 from current position
		//               fade.stop().fadeTo(250, 1);
		//           } else {
		//               fade.fadeIn(250);
		//           }
		//       }, function () {
		//           var fade = $('> div', this);
		//           if (fade.is(':animated')) {
		//               fade.stop().fadeTo(3000, 0);
		//           } else {
		//               fade.fadeOut(3000);
		//           }
		//       });
		
		//  $('#slides').hide();
		//  $('#slides').fadeIn();
		// 
		// setInterval(function() {
		//     var active = $('#slides li IMG.active');
		//     var next = active.next();
		//     next.addClass('active').fadeIn();
		//     active.removeClass('active').fadeOut();
		// 	console.log("Active: "+$(active)+" next: "+$(next));
		// }, 50);

		
		
		/* wmode fix */
		// $("object").ifE(function (item) {
		// 	/* Firefox*/
		// 	$(item).attr("wmode", "opaque");
		// 	/* IE */
		// 	var embedTag;
		// 	$(item).each(function(i) {
		// 		embedTag = $(this).attr("outerHTML");
		//         if ((embedTag != null) && (embedTag.length > 0)) {
		//             embedTag = embedTag.replace(/embed /gi, "object wmode=\"opaque\" ");
		//             $(this).attr("outerHTML", embedTag);
		//         }
		//         else {
		//             $(this).wrap("<div></div>");
		//         }
		//     });
		// });
		
		/* obsługa dynamiczna inputów formularzy w panelu bocznym */
		$('.rcontent .input.empty').ifE(function (item) {
			$(item).each(function(i) {
				var obj = $(this);
				obj.val(obj.attr('title'));
				obj.data('empty', $(this).val());
			
				obj.focus(function () {
					if($(this).val() == $(this).data('empty')) {
						$(this).val('');
						$(this).toggleClass('empty');
					}
				});
			
				obj.blur(function () {
					if($(this).val() == '') {
						$(this).val($(this).data('empty'));
						$(this).toggleClass('empty');
					}
				});
			});
		});


		/* easySlider dla listy youtube */
		$('#slider').ifE(function (item) {
			$(item).easySlider({
					vertical: true,
					controlsShow: false,
					addExtraToH: 6
			});
		});

		/* easySlider dla listy newsów */
		
		/*
		$('#hslider').ifE(function (item) {
			$(item).easySlider({
					controlsShow: false,
					prevId: 'nPrevBtn',
					nextId: 'nNextBtn',
					vertical: false,
					auto: true,
					continuous: true,
					speed: 1000,
					pause: 4600
			});
		});
		*/
		
		news.init();
		
		/* Nadanie zdarzenia onClick polom wyboru typu video */
		$('#youtube').ifE(function (item) {
			$('ul.chanel_type li a', item).click(function () {
				if(!$(this).parent().hasClass('active'))
					YoutubeToggle($(this));
				return false;
			});

			/* Wybór domyślnego typu video youtube */
			YoutubeToggle($('ul.chanel_type li a[href=pub]', item));
		});

		/* Helper obsługujący podmianę type video youtube */
		function YoutubeToggle(obj) {
			var id = obj.attr('href');
			var active_href = $('#youtube ul.chanel_type li.active a')

			$('#youtube #slider ul').css('margin-top', '0px');
			$('#' + active_href.attr('href')).slideUp();
			$('#' + id).slideDown();
			active_href.parent().toggleClass('active');
			obj.parent().toggleClass('active');

		}

		$('#propose_topic').ifE(function (item) {
			$(item).click(function () {
				if($('#propose_topic_form:visible').length == 0) {
					
					var offset = $(this).offset();
					$('#propose_topic_form').css({
						left: (offset.left + 100),
						top: (offset.top - 457)
					}).slideDown();
				} else {
					$('#propose_topic_form').slideUp(250);
				}
				$('#p_cancel').click(function () { $('#propose_topic_form').slideUp(250); });
			});
		});

		$('#hc_groups > li').ifE({
			okCallback: function (item) {
				$('> a', item).click(function() {
					if($('.open').length > 0 && $(this).parent().hasClass('open') === true) {
						$('.open > ul').ifE(
							{ 	okCallback: function (innerItem) {
									$(innerItem).slideUp(function () {
										$('.open').toggleClass('open');
									});
								},
								noCallback: function () {
									$('.open').toggleClass('open');
								}
							}
						);
					} else if($('.open').length == 0 && $(this).parent().hasClass('open') === true) {
						$('.open > ul').ifE(
								{ 	okCallback: function (innerItem) {
										$(innerItem).slideUp(function () {
											$('.open').toggleClass('open');
										});
									},
									noCallback: function () {
										$('.open').toggleClass('open');
									}
							}
						);
					} else if($('.open').length > 0 && $(this).parent().hasClass('open') === false) {
						$('.open > ul').ifE(
								{ 	okCallback: function (innerItem) {
										$(innerItem).slideUp(function () {
											$('.open').toggleClass('open');
											$(item).toggleClass('open');
											$('.open > ul').slideDown();
										});
									},
									noCallback: function () {
										$('.open').toggleClass('open');
									}
							}
						);
					} else {
						$(item).toggleClass('open');
						$('.open > ul').ifE(function (item) { $(item).slideDown(); });
					}
				});
			}
		});

		/* Nadajemy zdarzenie chowające i pokazujące formularz komentarza i ukrywamy sam formularz */
		if(!debuggin) {
			$('#add_comment').ifE(function (item) {
					// $(item).hide();
					$(item).show();
		
					// $('#c_quit').ifE(function (item) {
					// 	$(item).click(function() { $('#add_comment').slideUp(); });
					// });
					// 		
					// $('#toggle_comment').ifE(function (item) {
					// 	$(item).click(function() { $('#add_comment').slideDown(); });
					// });
			});

			$('#toggle_comment').hide();
			$('#c_quit').hide();

			return;
			/* Dynamiczne załadowanie skryptu facebook i jego wywołanie */
			try{
				$('#facebook').ifE(function (item) {

					$(item).html(' <fb:fan profile_id="124229942463" stream="0" connections="10" logobar="1" width="300"></fb:fan>');
					/* IE hack - sporadycznie api facebook generuje błąd - to odpowiada za jego przechwycenie */
					$('body').append('<div id="FB_HiddenContainer"  style="position:absolute; top:-10000px; width:0px; height:0px;" ></div>');

					$.getScript("http://static.ak.connect.facebook.com/connect.php/pl_PL", function() {					
						FB.init("6ea0d396b1d80ae160c0c8ecc302dedc");
					});
				});
			} catch (e) {}
		}

});
