var timers = new Array();
var mediaBlockSettings = {
		'width'				: 940,
		'height'			: 520,
		'autoScale'			: true,
		'transitionIn'		: 'fade',
		'transitionOut'		: 'fade',
		'type'				: 'iframe'	
	}; 

$(document).ready(function(){
	
	$('a.newWindow').click(function(){
		window.open($(this).attr('href'));
		return false;
	});
	
	$('#outfit.clickable').click(function(){
		var _url = 'http://'+$('#outfit.clickable a:first').attr('href');
		if($(this).hasClass('newWindow'))
			window.open(_url);
		else
			window.location = _url;
		return false;
	});
	
	$('ul.tabNav a').click(function() {
		var _id = $(this).parent().parent().parent().attr('id');
		timers[_id] = false;
		
		scrollNavTabToSlide($(this));
		return false;								
	});
	
	scollNavTab();
	
	
	$("#login_now").tooltip({ });									  	
	$("#vip_now").tooltip({ position: 'center right'});	
	
	
	$(".costumized").fancybox(mediaBlockSettings);
	
	$(".reactionpopup").fancybox({
		'width'				: 550,
		'height'			: 410,
		'autoScale'			: true,
		'transitionIn'		: 'fade',
		'transitionOut'		: 'fade',
		'type'				: 'iframe'	
	});
	
	$(document).pngFix(); 
	
	$('#twitter-feed').tweet({
        username: user_twitter,
        join_text: "auto",
        avatar_size: 25,
        count: 5,
        relative_time: false,
        time_format: false,
        template: "{avatar}@{user}<br/>{join}{text}<br/>{time}",
        auto_join_text_default: "",
        auto_join_text_ed: "",
        auto_join_text_ing: "",
        auto_join_text_reply: "",
        auto_join_text_url: "",
        loading_text: "Tweets aan het laden ...",
        callback: function(){ $('#twitter-feed a').click(function(){
    		window.open($(this).attr('href'));
    		return false;
    	}); }
        
    });
	
	
	$('#facebook-wall').fbWall({
		 id: user_facebook
		,showGuestEntries:true
		,showComments:true
		,max:3
		,timeConversion:24
		,accessToken: api_facebook+'|'+key_facebook
	});

	$('input#subscribe-btn').click(function(){
		_val = $('input#subscription').val();
		if(_val == undefined || _val.length == 0)
			return false;
		
		$('#nieuwsbrief-box').animate({'opacity' : 0.05}, 500, 'linear', function(){
			$('#subscription-spinner').show().animate({'opacity' : 1}, 200, 'linear');
			
			$.ajax({
		    	type: "POST",
		        url:  baseUrl+'/ajax/subscribe',
				data: 'subscription='+_val,
		        complete: function(msg){
		        	var _parts = msg.responseText.split('##|##');
		        	
					$('#nieuwsbrief-box').hide(200, function(){
		        		$('#subscription-spinner').hide();
		        		$('#nieuwsbrief').append('<div class="'+_parts[0]+'">'+_parts[1]+'</div>');
					
						$('.'+_parts[0]+'').delay(2000).animate({'opacity' : 0.05}, 400, 'linear').hide('linear', function(){	
							
							$('#nieuwsbrief-box').show().animate({'opacity' : 1}, 200, 'linear');
							
						});
							
						
		        	});
		        }
			});
		});
	});
	
	$('a#react-btn').click(function(){
		_val = $('textarea#react-text').val();
		_url = $(this).attr('href');
		if(_val == undefined || _val.length == 0)
			return false;
		
		_rid = $('input#reaction_id').val();
		
		$('textarea#react-text').animate({'opacity' : 0.35}, 500, 'linear', function(){

			$.ajax({
		    	type: "POST",
		        url:  baseUrl+_url,
				data: 'text='+_val+'&rid='+_rid,
		        complete: function(msg){
		        	var _parts	= msg.responseText.split('##|##');
		        	var _url = $('input#gotoUrl').val()
		        	var _rec = '?reactions'+(_rid > 0 ? '='+_rid : '');
		        	var d = new Date();
		        	var t = d.getTime();

		        	$('textarea#react-text').slideUp(200, function(){
		        		$('#comment form').html('<div class="'+_parts[0]+'">'+_parts[1]+'</div>');
		        	});
		        	
		        	if(_parts[0] == 'succes'){
		        		if($('input#facebooksharecomment').is(':checked')){
	    					$('<a/>')
    						.attr('href', 'http://www.facebook.com/sharer/sharer.php?u='+urlencode(_url)+'&t='+urlencode(_val))
    						.click(function(){
    							wopen($(this).attr('href'), 'facebookShare'+t, 720, 380, false);
    							return false;
    						})
    						.trigger('click'); 		        			
		        		}
		        		
		        		if($('input#twittersharecomment').is(':checked')){
	    					$('<a/>')
    						.attr('href', 'http://twitter.com/intent/tweet?text='+urlencode(_val)+'&url='+urlencode(_url))
    						.click(function(){
    							wopen($(this).attr('href'), 'twitterShare'+t, 720, 380, false);
    							return false;
    						})
    						.trigger('click'); 		        			
		        		}		
		        		
		        		$('body').scrollTo('#comment', 500 );   
	        			setTimeout(function(){
		        			window.location = _url+_rec;
		        		}, 1500);
		        	}
		        }
			});
		});
		
		return false;
	});
	
	$('a.reaction-btn.reply').click(function(){
		$('#blog-reaction-reply').slideDown();
		$('#reaction_text').html($(this).parent().find('p:first').html());
		$('#reaction_id').val($(this).attr('rel'));
		$('body').scrollTo('#comment-box', 500 );
		return false;
	});
	
	$('#blog-reaction-reply-close').click(function(){
		$('#blog-reaction-reply').slideUp();
		$('#reaction_text').val('');
		$('#reaction_id').val('0');		
	});
	
	
	$('a.reaction-btn.comment').click(function(){
		$('#blog-reaction-reply-close').trigger('click');
		$('body').scrollTo('#comment-box', 500 );
		return false;
	});
	
	initPollInput();
	$('a#pollvote').click(function(){
		_val = $('input[@name=poll]:checked').val();
		
		if(_val == undefined || _val.length == 0){
			_val = $('input[name=poll]:first').val()
			if(_val == undefined || _val.length == 0)
				loadPollVote();

			return false;
		}
		
		_other = $('input#pollother').val();

		setTimeout(function(){
			$.ajax({
		    	type: "POST",
		        url:  baseUrl+'/ajax/votepoll',
				data: 'vote='+_val+'&other='+_other,
		        complete: function(msg){
		        	var _parts = msg.responseText.split('##|##');
		        	
		        	setTimeout(function(){
		        		loadPollView(_parts[1]);
		        	}, 	unloadingPoll(_parts[0])+1000);
		        }
			});
		},loadingPoll());
		return false;
	});
	$('a#pollview').click(function(){
		loadPollView($(this).attr('rel'));
		return false;
	});
	
	scollCover();
	
	$(".form .toChangeHolder p").click(function(){
		var _obj = $(this).next('div');
		var _input = _obj.next('input');
		
		if(_obj.is(':visible')){
			_input.val('0');
			_obj.slideUp();
		}else{
			_input.val('1');
			_obj.slideDown();
		}
	});
		
	$("#arrow-left").hover(function(){
		$("#arrow-left-tekst").fadeIn(500);
	},function(){ 
		$("#arrow-left-tekst").fadeOut('fast');
	});
	
	$('.holder-begin').click(function(){
		var url = $(this).find('.text-holder a').attr('href')+'/';		
		window.location = url;
		return false;
	});
	
});

function urlencode(str) {
	return escape(str).replace(/\+/g,'%2B').replace(/%20/g, '+').replace(/\*/g, '%2A').replace(/\//g, '%2F').replace(/@/g, '%40');
}

function wopen(url, name, w, h, focus){
	w += 32;
	h += 96;
	var win = window.open(url, name,
				'width=' + w + ', height=' + h + ', ' +
				'location=no, menubar=no, ' +
				'status=no, toolbar=no, scrollbars=no, resizable=no');
	win.resizeTo(w, h);
	
	if(focus)
		win.focus();
}

function scollCover(){
	if($('#mama-cover img').length > 1){
		setTimeout(function(){
			var aObj = $('#mama-cover img.active');
			$('#mama-cover img').fadeIn();
			$('#mama-cover img').addClass('active');
			aObj.removeClass('active');
			aObj.fadeOut();
			scollCover();
		},7500);
	}
}

function scollNavTab(){
	setTimeout(function(){
		$('ul.tabNav').each(function(){
		
			var _id		= $(this).parent().attr('id');
			if(timers[_id] == undefined)
				timers[_id] = true;
			
			if(timers[_id] == true){
				var max		= $(this).children('li').length-1;
				var aChild  = $(this).children('li.current');
				var aNum	= aChild.index();
				var nNum 	= (aNum == max)? 0 : aNum+1;
				scrollNavTabToSlide($(this).children('li:eq('+(nNum)+')').children('a:first'));
			}
		});
		
		scollNavTab();
	},10000);
}

function scrollNavTabToSlide(_navAObj){
	var curChildIndex = _navAObj.parent().prevAll().length + 1;
	_navAObj.parent().parent().children('.current').removeClass('current');
	_navAObj.parent().addClass('current');
	_navAObj.parent().parent().next('.tabContainer').children('.current').show('fast',function() {
		$(this).removeClass('current');
		$(this).parent().children('div:nth-child('+curChildIndex+')').hide('normal',function() {
			$(this).addClass('current');
		});
	});	
}

function initPollInput(){
	$('input[name=poll]').click(function(){
		$('#pollother').css({'color': '#A5AAAF'});
		if($(this).attr('id') === 'pollotherchk'){
			$('#pollother').focus();
		}else{
			$('#pollother').val($('#pollotherlabel').html());
		}
	});
	
	$('#pollother').focus(function(){
		$(this).css({'color': '#000000'});
		$('#pollotherchk').attr('checked', 'checked');
		if($('#pollother').val() == $('#pollotherlabel').html()){
			$('#pollother').val('');
		}
	});
}

function loadingPoll(){
	var speed = 200;
	
	$('#poll-spinner').height($('#poll-content').height());
	$('#poll-spinner').show();
	$('#poll-content-holder, #poll-btn-holder').animate({'opacity' : 0.4}, speed, 'linear');
	
	return speed;
}

function unloadingPoll(html){
	var speed = 100;
	
	$('#poll-content-holder').html(html);
	$('#poll-content-holder, #poll-btn-holder').animate({'opacity' : 1}, speed, 'linear', function(){
		$('#poll-spinner').hide();
	});
	
	return speed;
}

function loadPollVote(){
	setTimeout(function(){
		$.ajax({
	    	type: "POST",
	        url:  baseUrl+'/ajax/pollform',
	        complete: function(msg){
	        	unloadingPoll(msg.responseText);
	        	initPollInput();
	        }
		});
	},loadingPoll());
}

function loadPollView(pollId){
	if(pollId != undefined && pollId.length > 0){
		setTimeout(function(){
			$.ajax({
		    	type: "POST",
		        url:  baseUrl+'/ajax/pollresult',
				data: 'pollId='+pollId,
		        complete: function(msg){
		        	unloadingPoll(msg.responseText);
		        }
			});
		},loadingPoll());
	}
}

function showForm() {
	$('#searchimg').toggleClass('search-active');
	$('#search-type-box').slideToggle();
};



