$(document).ready(function(){
 //isSearchBox = false;
 isLogInBox = false;
 isJoinBox = false;
 if($('#mshomebox').length > 0){
  var mt = (($(window).height() - $('#mshomebox').outerHeight())/2) - 100;
  $('#mshomebox').css({'margin-top': mt<0 ? 0 : mt});
  $(window).resize(function(){ var mt = (($(window).height() - $('#mshomebox').outerHeight())/2) - 100;
                               $('#mshomebox').css({'margin-top': mt<0 ? 0 : mt}); });
  $('#msabout').css('opacity',0.8).msAbout();
 }
 $('.addthis_button_google_plusone').css('opacity',0.7);
 $('.mscopy').msCopyPos();
 $(window).resize(function(){ $('.mscopy').msCopyPos(); });
 
 if(skybg != ''){
  $('<img>').mSkyBG(false);
  $(window).resize(function(){ $('<img>').mSkyBG(true); });
 }
 $('#mslogin').css('opacity',caton=='home'?0.8:0.7).msLogIn();
 $('#msignup').css('opacity',caton=='home'?0.8:0.7).msJoin();
 
 if(caton=='search'){
  $('#msearch').css({'background-image':'url(/img/blends/bgtslight30.png)'});
  $('#mswhat,#mswhataval').msSearchWhat();
  $('#mswall,#mswpeoples,#mswsouls').css('opacity',0.7).msSearchWhatSel();
 }
 else $('#msearchbtn').css('opacity',caton=='home'?1:0.7);
 $('#msearchbtn').msSearch();
 $('.msclbsoul').hover(function(){ $(this).attr('class','msclbsoul msclbsoulov'); },function(){ $(this).attr('class','msclbsoul'); });
 $('.msclbpeople').hover(function(){ $(this).attr('class','msclbpeople msclbpeopleov'); },function(){ $(this).attr('class','msclbpeople'); });
 $('.mscomntmore').showComnts();
 
 if(catsub == 'image'){
  $('#mscgprev,#mscgnext').hover(function(){ $('#mscgprev,#mscgnext').css({'display':'block'}); },function(){ $('#mscgprev,#mscgnext').css({'display':'none'}); }).click(function(){ window.location.href = $(this).children('a').attr('href'); });
  $('.mscg').children('img:first').hover(function(){ var pos = $(this).position(); $('#mscgprev').css({'left':pos.left+1,'display':'block'}); $('#mscgnext').css({'right':$(this).parent().innerWidth() - (pos.left+$(this).width()+1),'display':'block'}); },function(){ $('#mscgprev,#mscgnext').css({'display':'none'}); });
 }
});

jQuery.fn.msCopyPos = function() {
	//alert($(window).height()+'|'+$('.body').innerHeight());
 var wH = $(window).height();
 var bH = $('.body').innerHeight();
 return this.css({'position': (wH <= bH ? 'absolute' : 'relative'),'top':(wH <= bH ? (bH-$('.mscopy').innerHeight()) : '0')+'px','left':'0px','width':'100%'});
}

jQuery.fn.msInpBG = function() {
 return this.focusin(function(){ $(this).css({'background-image':'url(nix)'}); }).focusout(function(){ if($(this).val() == '') $(this).css({'background-image':''});  })
}

jQuery.fn.msLogIn = function() {
 return this.hover(function(){ $(this).css({'opacity':1}); },function(){ $(this).css({'opacity':caton=='home'?0.8:0.7}); })
            .click(function(){ if(isSearchBox){ isSearchBox = false; $('#msearchbox').outSearchBox(); }
                               if(isJoinBox){ isJoinBox = false; $('#msignup').outJoinBox(); }
                               if(isLogInBox == false){
                                isLogInBox = true;
                                $(this).animate({'width':'190px'},'fast',function(){
                            	    var pos = $(this).offset();
                                    $('<div>').attr('id','msloginbox')
                                              .css({'position':'absolute','top':pos.top,'left':pos.left,'width':'190px','min-height':'200px','background-image':'url(/img/bglogin.png)','border-radius':'2px','box-shadow':'1px 2px 8px 0px #101214','z-index':'110'})
                                              .append( $('<div>').css({'padding':'20px 0px 20px 0px','font-size':'15px','color':'#191f23','text-align':'center'}).html("Loading...") )
                                              .appendTo('body');
                                    $.get('/index.php', { dyn: 'mscl', 'mscl': 'ms', 'msclsub': 'userlogin', 'nocache' : Math.random() }, 
                                            function(res){ $('#msloginbox').html( $(res) );
                                                           $('input[name=xdusername],input[name=xdpassword]').msInpBG();
                                                           $('#msliclose').click(function(){ $('#mslogin').outLogInBox(); });
                                                           
                                                         });
                                });
                               }
                               
                             });
}

jQuery.fn.msJoin = function() {
 return this.hover(function(){ $(this).css({'opacity':1}); },function(){ $(this).css({'opacity':caton=='home'?0.8:0.7}); })
            .click(function(){ if(isSearchBox){ isSearchBox = false; $('#msearchbox').outSearchBox(); }
                               if(isLogInBox){ isLogInBox = false; $('#mslogin').outLogInBox(); }
                               if(isJoinBox == false){
                            	isJoinBox = true;
                                $(this).animate({'width':'305px'},'fast',function(){
                            	    var pos = $(this).offset();
                                    $('<div>').attr('id','msjoinbox')
                                              .css({'position':'absolute','top':pos.top,'left':pos.left,'width':'305px','min-height':'200px','background-image':'url(/img/bgjoin.png)','border-radius':'2px','box-shadow':'1px 2px 8px 0px #101214','z-index':'110'})
                                              .append( $('<div>').css({'padding':'20px 0px 20px 0px','font-size':'15px','color':'#191f23','text-align':'center'}).html("Loading...") )
                                              .appendTo('body');
                                    $.get('/index.php', { dyn: 'mscl', 'mscl': 'ms', 'msclsub': 'userjoin', 'nocache' : Math.random() }, 
                                            function(res){ $('#msjoinbox').html( $(res) );
                                                           $('#msjoclose').click(function(){ $('#msignup').outJoinBox(); });
                                                           $('input[name=signup_nickname],input[name=signup_email],input[name=signup_password],input[name=signup_password1]').msInpBG();
                                                           $('#userjoin').msJoinNow();
                                                         });
                                });
                               }
                             });
}

jQuery.fn.msAbout = function() {
	 return this.hover(function(){ $(this).css({'opacity':1}); },function(){ $(this).css({'opacity':0.8}); })
	            .click(function(){ $('#mshomebox').animate({'margin-top': 0},'normal',function(){ $('#msaboutc').css({'opacity':0,'display':'block'}); $('.mscopy').msCopyPos(); $('#msaboutc').fadeTo('normal',1); }); });
}

jQuery.fn.msJoinNow = function(id) {
 return this.submit(function(data){ $('#msjoinbox').loadSign(true);
                                    $('#userjoin').css('display','none');
                                    var frameName = ('ujo'+(new Date()).getTime());
	                                $(this).attr('target', frameName);
	                                var submitFrame = $('<iframe>').attr('name',frameName).css("display", "none")
	                                                               .load(function(data){ setTimeout(function(){ var result = submitFrame.contents().find('body').html();
	                                                                                                            submitFrame.remove();
	                                                                                                            $('#loadsign').remove();
	                                                                                                            $('#msjoinbox').append($(result));
	                                                                                                            $('#ujoconfirm').click(function(){ $(this).parent().remove(); $('#userjoin').css('display','block'); });
	                                                                                                          },1000);
	                                                                                   });
                                    $('body').append(submitFrame);
	                              }); 
}

jQuery.fn.msSearch = function() {
 return this.hover(function(){ $(this).css({'opacity':1}).parent().css({'background-image':'url(/img/blends/bgtslight30.png)'}); },function(){ if(isSearchBox == false) $(this).css({'opacity':0.7}).parent().css({'background-image':''}); })
            .click(function(){ if(isLogInBox){ isLogInBox = false; $('#mslogin').outLogInBox(); }
                               if(isJoinBox){ isJoinBox = false; $('#msignup').outJoinBox(); }
                               if(isSearchBox == false){
                            	isSearchBox = true;
                            	$(this).css({'opacity':1,'color':'#ffffff'}).parent().css({'background-image':'url(/img/blends/bgtslight30.png)'});
                                $('#msearchbox').css({'width':'1px','display':'block'}).animate({'width':'200px'},'fast',function(){ $('#msearchbox,#mswhat,input[name=term]').css('display','block');
                                                                                                                                     $('#mswhat,#mswhataval').msSearchWhat();
                                                                                                                                     $('#mswall,#mswpeoples,#mswsouls').css('opacity',0.7).msSearchWhatSel();
                                                                                                                                   });
                               }
                               else{
                            	if($('#msearchterm').val() == ''){
                            	 return false;
                            	}
                            	else{
                            	 $('form[name=msearch]').submit();
                            	}
                               }
                             });
}

jQuery.fn.msSearchWhat = function() {
 return this.hover(function(){ $('#mswhataval').css({'display':'block'}); }
                  ,function(){ $('#mswhataval').css({'display':'none'}); });
}

jQuery.fn.msSearchWhatSel = function() {
 return this.hover(function(){ $(this).css({'opacity':1}); }
                  ,function(){ $(this).css({'opacity':0.6}); })
            .click(function(){ var what = $(this).xoexid(3);
                               if(what != 'hat'){
               	                $('input[name=what]').val(what);
               	                $('#mswhataval').css({'display':'none'});
               	                $('#mswhat').attr('class','msw'+what).children('img:first').attr({'src':'/img/msw'+what+'.png'});
                               }
                });
}

jQuery.fn.outSearchBox = function() {
 return this.children('div,input').css('display','none').parent().animate({'width':'1px'},'fast',function(){ $(this).css('display','none'); $('#msearchbtn').css({'opacity':0.7,'color':''}).parent().css({'background-image':''}); isSearchBox = false; });
}

jQuery.fn.outLogInBox = function() {
 $('#msloginbox').remove();
 return this.animate({'width':'60px'},'fast',function(){ isLogInBox = false; });
}

jQuery.fn.outJoinBox = function() {
 $('#msjoinbox').remove();
 return this.animate({'width':'60px'},'fast',function(){ isJoinBox = false; });
}


jQuery.fn.mSkyBG = function(rz) {
 return this.attr('src',skybg)
            .css('display','none')
            .load(function(){ $(this).appendTo('body');
                              $('#mskybg').css({'background-image':'url('+skybg+')','opacity':!rz?0:1});
                              var skyH = $(this).height();
                              var skyW = $(this).width();
                              $('#mskybg,#mskybgb').css('height',skyH+15);
                              if( skyW < $('#mskybg').width() ){
                               $('#mskybgl').css({'background-image':'url(/styles/'+style+'/img/bgskybl.png)'});
                               $('#mskybgr').css({'width':skyW+30,'height':skyH+15,'margin-left': (($('#mskybg').width()-skyW-30)/2),'background-image':'url(/styles/'+style+'/img/bgskybr.png)'});
                              }
                              else{
                               $('#mskybgl,#mskybgr').css({'background-image':'url()'});
                              }
                              if(!rz) $('#mskybg').fadeTo('normal',1);
                            }).error(function(){});
}





jQuery.fn.showComnts = function() { // hide option noch hinzu
 return this.click(function(){ $(this).css('display','none').parent().children('.mscomnti').css('display','block'); });
}


jQuery.fn.xoexid = function(pos) {
 return this.attr('id').substr(pos);
}


jQuery.fn.loadSign = function(apnd) {
 var ih = $(this).innerHeight();
 var iw = $(this).innerWidth();
 ih = ih != 'undefined' && ih > 41 ? ih : 41;
 iw = iw != 'undefined' && iw > 41 ? iw : 41;
 if(iw > 200){
  var lSw = 110;
 }
 else{
  var lSw = 41;
 }
 var lSign = $('<div>').attr('id','loadsign')
                       .css({'padding':Math.floor( (ih-41) / 2)+'px '+Math.floor( (iw-lSw) / 2)+'px','text-align':'center'})
                       .append( $('<div>').css({'width':lSw+'px','height':'41px','background-image':'url(/img/loadbg'+(lSw==41?'s':'')+'.png)','background-repeat':'no-repeat','padding':'0px','background-color':''})
                                          .append( $('<img>').attr({'src':'/img/load.gif','align':'right'}).css({'margin':'5px 5px','border-width':'0px'}) ) );
 if(apnd) return this.append( lSign );
 else return this.html( lSign );
}

jQuery.fn.stopEnter = function() {
 return this.keypress(function(e){ if((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) return false; })
}

function msgPop(style,title,cont,confirmbtn) {
 var pop = $('<div>').attr({'id':'msgpopb','class':(style=='ok'?'box_ok':'box_error')}).css({'position':'absolute','width':'auto','display':'none','z-index':'100'});
 pop.append( $('<p>').html(title) ).append( $('<div>').html( $('<div>'+cont+'</div>') ) );
 if(style=='confirm' && confirmbtn!='') pop.append( confirmbtn );
 pop.append( $('<button type="button">').css({'width':'100px','height':'35px'}).html( (style=='confirm'?'Cancel':'OK') ).click(function(){ $('#msgpopb').remove(); $('#msgpop').remove(); return false; }) );
 $('body').append( $('<div>').attr('id','msgpop').css({'position' : 'absolute', 'top' : 0, 'left' : 0, 'width' : $(document).width(), 'height' : $(document).height(), 'zIndex' : 100}).append( pop ) );
 $('#msgpopb').css({'top':( ( $(window).height() - $('#msgpopb').outerHeight() ) / 2 ) + f_scrollTop(),'left':( ( $(window).width() - $('#msgpopb').outerWidth() ) / 2 ),'display':'block'});
}

function f_scrollTop() {
	return f_filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}
function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}
