//var base = 'http://devil.p-c.pl/y4m/';

function komunikat(txt) {
	$(document).ready(function() {
        $('.komunikat').html(txt + '<span class="closeKomunikat"> (zamknij)</span>').fadeIn('slow').click(function() {
            $(this).fadeOut();
        });
    });
}

function usunMsg(base, id) {
	if(confirm('Usunąć wiadomość?')) {
		$.ajax({
			type:	"GET",
			url:	base+"messages/delmsg/"+id,
			success: function(){
				$('#usunMsg'+id).parent('td').parent('tr').fadeOut('100');
			}
		});
		tdColor();
	}
}

function usunMsgPermanent(base, id) {
	if(confirm('Usunąć wiadomość na stałe?')) {
		$.ajax({
			type:	"GET",
			url:	base+"messages/delPermanent/"+id,
			success: function(){
				$('#usunMsg'+id).parent('td').parent('tr').fadeOut('100');
			}
		});
		tdColor();
	}
}

function usunSmile(base, id) {
	if(confirm('Usunąć uśmiech??')) {
		$.ajax({
			type:	"GET",
			url:	base+"messages/delSmile/"+id,
			success: function(){
				$('#usunSmile'+id).parent('td').parent('tr').fadeOut('100');
			}
		});
		tdColor();
	}
}

function usunSmilePermanent(base, id) {
	if(confirm('Usunąć uśmiech na stałe?')) {
		$.ajax({
			type:	"GET",
			url:	base+"messages/delSmilePermanent/"+id,
			success: function(){
				$('#usunSmile'+id).parent('td').parent('tr').fadeOut('100');
			}
		});
		tdColor();
	}
}

function usunMsgs(base) {
	if(confirm('Usunąć wiadomości?')) {
		$('#messagesForm').attr('action', base+'messages/delmsg/');
		$('#messagesForm').submit();
	}
}

function msgArch(base) {
	$('#messagesForm').attr('action', base+'messages/msgArch/');
	$('#messagesForm').submit();
}

function usunMsgsPermanent(base) {
	if(confirm('Usunąć wiadomości na stałe?')) {
		$('#messagesForm').attr('action', base+'messages/delPermanent/');
		$('#messagesForm').submit();
	}
}

function markMsg(status) {
	var messagesMultiActions = $('.messagesMultiActions').html();
	if($('.messagesMultiActions input').attr('name') == 'status') {
		$('.messagesMultiActions input').attr('value', status);
	} else {
		$('.messagesMultiActions').html('<input type="hidden" name="status" value="'+status+'" />'+messagesMultiActions);
	}
	$('#messagesForm').submit();
}

function tdColor() {
	$('.msgTable tr:odd').addClass('msgTableOdd');
	$('.smileTable tr:odd').addClass('msgTableOdd');

	$('body .msgBoxMiddle').css('height', 'auto');
	$('body .smileBoxMiddle').css('height', 'auto');
	if($('body .msgBoxMiddle').height() > $('body .smileBoxMiddle').height()) {
		$('body .smileBoxMiddle').css('height', $('body .msgBoxMiddle').height()+'px');
	} else {
		$('body .msgBoxMiddle').css('height', $('body .smileBoxMiddle').height()+'px');
	}
}

function changePhoto() {
	//var url = $(this).attr('href');
	//alert(url);
	//$('.profileImage a img').attr('src', img).toggleIn('slow');
}

function switchVal(id, txt) {
	$(document).ready(function() {
		if($('#'+id).val() == '') {
			$('#'+id).val(txt);
		}
	});
	$('#'+id).focus(function() {
		if($(this).val() == txt) {
			$(this).val('');
		}
	});
	$('#'+id).blur(function() {
		if($(this).val() == '') {
			$(this).val(txt);
		}
        $(this).bind('focus', function() {
            if($(this).val() == txt) {
                $(this).val('');
            }
        });
	});
	$('#'+id).keyup(function() {
		$(this).unbind('focus');
	});
}

function showForm(id) {
	$('#partnerForm div#profilDiv1').hide();
	$('#partnerForm div#profilDiv2').hide();
	$('#partnerForm div#profilDiv3').hide();
	$('#partnerForm div#profilDiv4').hide();
	$('#partnerForm div#profilDiv5').hide();
	$('#partnerForm div#profilDiv6').hide();
	$('#partnerForm div#profilDiv7').hide();
	$('#profileEditInfoMenu li').removeClass('active');
	$('#menuProfileEdit'+id).addClass('active');
	$('#partnerForm div#profilDiv'+id).show();
}

function checkAll() {
	$('.msgTable td input').each(function() {
		if($(this).attr('name') == 'message_id[]' && $(this).attr('checked') != 'checked') {
			$(this).attr('checked', 'checked');
		}
	});
}

function ajaxTopLayer(){
	$('body').append('<div id="msg_box"><div class="layer_top"><a href="javascript:ajaxTopLayerClose();" class="pomocBtn"></a></div><div id="msg_boxMainContent" class="layer_middle smiles"></div><div class="layer_bottom"></div></div><div id="overlay"></div>');	
	$("#msg_box").hide().css({
	    position: 'absolute',
	    zIndex: 99
	});
	$("#overlay").hide();
	$("#msg_box").makeCenterScreen();
}

function ajaxTopLayerBig(){
	$('body').append('<div id="msg_boxBig"><div class="layer_top"><a href="javascript:ajaxTopLayerClose();" class="pomocBtn"></a></div><div id="msg_boxMainContentBig" class="layer_middle smiles"></div><div class="layer_bottom"></div></div>');
	$("#msg_boxBig").hide().css({
	    position: 'absolute',
	    zIndex: 99
	});
	$("#overlay").hide();
	$("#msg_boxBig").makeCenterScreen();
}

function prezentacja(){
	//$('body').append('<div id="overlay"></div>');	
	$("#layer_prezentacja2").show().css({
	    position: 'absolute',
	    zIndex: 999
	});
	$("#overlay").height($(document).height()).width($(document).width()).show();
	$("#layer_prezentacja2").makeCenterScreenPresentation();
}

function ajaxTopLayerLoad(url, data, colback){

	data=data!=''?data:'';
	$("#msg_box").find('#msg_boxMainContent').html('Wczytywanie danych...');
	$("#overlay").height($(document).height()).width($(document).width()).css('opacity', 0.8);

	if(!$("#msg_box").hasClass("loading")){
		$("#msg_box").fadeIn("slow");
		$("#overlay").fadeIn();
		$("#msg_box").addClass('loading')

	}
	
	$.ajax({
		type: "POST",
		url: url,
		data: data,
		success: function(msg){
			$("#msg_box").find('#msg_boxMainContent').html(msg);
			//$("body *").css('border', '1px solid #ffffff');
			$('#msg_boxMainContent form').submit(function(){
				ajaxTopLayerLoad($(this).attr('action')!=''?$(this).attr('action'):url, $(this).serialize())
				
				return false;
			});
		}
	});
}

function ajaxTopLayerLoadBig(url, data, colback){

	data=data!=''?data:'';
	$("#msg_box").attr('id', 'msg_boxBig');
	$("#msg_boxMainContent").attr('id', 'msg_boxMainContentBig');
	$("#msg_boxBig").find('#msg_boxMainContentBig').html('Wczytywanie danych...');
	$("#overlay").height($(document).height()).width($(document).width()).css('opacity', 0.8);

	if(!$("#msg_boxBig").hasClass("loading")){
		$("#msg_boxBig").fadeIn("slow");
		$("#overlay").fadeIn();
		$('#closeBox').fadeIn();
		$("#msg_boxBig").addClass('loading').makeCenterScreenPresentation();
	}

	$.ajax({
		type: "POST",
		url: url,
		data: data,
		success: function(msg){
			$("#msg_boxBig").find('#msg_boxMainContentBig').html(msg);
			//$("body *").css('border', '1px solid #ffffff');
			$('#msg_boxMainContentBig form').submit(function(){
				ajaxTopLayerLoadBig($(this).attr('action')!=''?$(this).attr('action'):url, $(this).serialize())
				return false;
			});
		}
	});
}

function ajaxTopLayerStatic(msg){
	$("#msg_box").find('#msg_boxMainContent').html('Wczytywanie danych...')
	$("#overlay").height($(document).height()).width($(document).width()).css('opacity', 0.8).fadeIn();
	$("#msg_box").fadeIn("slow").addClass('loading').makeCenterScreen();
	$("#msg_box").find('#msg_boxMainContent').html(msg);
	$('#msg_boxMainContent').find('form').submit(function(){
		ajaxTopLayerLoad(url, $(this).serialize())
	});
}

function ajaxTopLayerClose(){
	$("#overlay").fadeOut();
	//$("#overlay").fadeOut();
	$('#msg_box').fadeOut().removeClass('loading');
	$('#layer_prezentacja2').fadeOut();
}

function ajaxTopLayerCloseBig(){
	$("#overlay").fadeOut();
	//$("#overlay").fadeOut();
	$('#msg_boxBig').fadeOut().removeClass('loading');
	$('#closeBox').fadeOut();
	$('#layer_prezentacja2').fadeOut();
}

function messageFillProfile() {
	ajaxTopLayerStatic('Wypełnij bieżącą zakładkę żeby móc przejść do kolejnej!');
}

jQuery.fn.makeCenterScreen = function(loaded) {
	var obj = this;
	if(!loaded) {
		obj.css('top',$(window).height()/2-this.height()/2+$(window).scrollTop()-80);
		obj.css('left', $(window).width()/2-this.width()/2);
		obj.css('margin', 0);
		$(window).resize(function(){ obj.makeCenterScreen(!loaded); });
		$(window).scroll(function(){ obj.makeCenterScreen(!loaded); });
	} else {
		obj.stop(false,true);
		$("#overlay").height($(document).height()).width($(document).width());
		obj.animate({
			margin:0,
			top: (($(window).height()/2-this.height()/2+$(window).scrollTop())-80),
			left: ($(window).width()/2-this.width()/2)
		}, 200, 'linear');
	}
    }

jQuery.fn.makeCenterScreenPresentation = function(loaded) {
	var obj = this;
	if(!loaded) {
		obj.css('top',$(window).height()/2-this.height()/2+$(window).scrollTop());
		obj.css('left', $(window).width()/2-this.width()/2);
		obj.css('margin', 0);
		$(window).resize(function(){ obj.makeCenterScreenPresentation(!loaded); });
		$(window).scroll(function(){ obj.makeCenterScreenPresentation(!loaded); });
	} else {
		obj.stop(false,true);
		$("#overlay").height($(document).height()).width($(document).width());
		obj.animate({
			margin:0,
			top: (($(window).height()-this.height())/2+$(window).scrollTop()),
			left: ($(document).width()/2-this.width()/2)
		}, 200, 'linear');
	}
}



function jsChat(user, pass, isAv, room)
{
	alert('jsChat');
	//w = window.open('chat.html', '', 'width=1024,height=700');
	//w.focus();
}

function jsChatInvite(user1, user2)
{
	if(!$('#msg_box').attr('id')){
		ajaxTopLayer();
	}
	ajaxTopLayerStatic('<div id="chatSound"></div><h1>Użytkownik '+user1+' zaprasza cię do czata</h1><div>Czy chcesz rozpocząc rozmowę?</div><div><a href="javascript:acceptInvitation();">Akceptuj zaproszenie</a>&nbsp;|&nbsp;<a href="javascript:chatCencel();">Odrzuć</a></div>');
	var so = new SWFObject(base + "swf/snd.swf", "chat", "1", "1", "9", "#29074A");
	so.addParam("wmode", "window");
	so.addParam("quality", "high");
	so.addParam("scale", "noscale");
	so.write("chatSound");
}


function acceptInvitation(){
	//$('#fc').cancelRequest();
	thisMovie('fc').acceptInvitation();
	ajaxTopLayerClose();
}

function chatCencel(){
	//$('#fc').cancelRequest();
	thisMovie('fc').rejectInvitation();
	ajaxTopLayerClose();
}

function jsChatRequest(user, isAv){
	if(!$('#msg_box').attr('id')){
		ajaxTopLayer();
	}
	ajaxTopLayerStatic('<h1>Zaproszenie zostało wysłane <br />do użytkownika '+user+'</h1><div><br /><a href="javascript:cancelRequest();">Anuluj zaproszenie</a></div>');

}

function cancelRequest(){
	thisMovie('fc').cancelRequest();
	ajaxTopLayerClose()
}

function jsInvitationRejected(){
	if(!$('#msg_box').attr('id')){
		ajaxTopLayer();
	}
	ajaxTopLayerStatic('<h1>Twoje zaproszenie zostało odrzucone.</h1>');
}


function jsInvitationCancelled(){
	if(!$('#msg_box').attr('id')){
		ajaxTopLayer();
	}
	ajaxTopLayerStatic('<h1>Zaproszenie zostało anulowane.</h1>');
}
var w=false;
function jsChatStart( user, pass, isAv, room ){
	w = window.open('/users/chat/'+room+'/'+isAv+'/'+user, room.replace(/-/i, ""), 'width=1024,height=700');
	if(_hasPopupBlocker(w)){
		ajaxTopLayerStatic('<div style="margin: 8px 0px;">Najprawdopodobniej Twoja przeglądarka blokuje wyskakujące okienka.</div><div style="font-size: 11px;">Na przyszłość włącz możliwość otwierania nowych okien przez serwis you4me.pl</div><div>Aby otworzyć okienko z czatem <br /><a id="openChat" href="javascript:;">kliknij tutaj</a></div>');
		$('#openChat').unbind('click').live('click', function(){
			jsChatStart( user, pass, isAv, room );
		});
	}else{
		w.focus();
		ajaxTopLayerClose();
	}
	
}

$('openChat').live('click', function(){

});

function chatStart( user, pass, isAv, room )
{
	jsChatStart( user, pass, isAv, room )
}

function sendEvent(typ,prm) {
	thisMovie('fc').sendEvent(typ,prm);
}

function sendEventCard(nr, colour, txt) {
	thisMovie('cardFlashView').changeCard(nr, colour, txt);
}

function getUpdate(typ,pr1,pr2,swf) {
}

function thisMovie(swf) {
	if(navigator.appName.indexOf("Microsoft") != -1) {
		return window[swf];
	} else {
		return document[swf];
	}
}

function links2Rel() {
	$('div.paginationMsgs p.pagination a').each(function(i) {
		$(this).attr('rel', $(this).attr('href'));
		$(this).removeAttr('href');
	});
	$('div.paginationSmiles p.pagination a').each(function(i) {
		$(this).attr('rel', $(this).attr('href'));
		$(this).removeAttr('href');
	});

	$('div.paginationMsgs p.pagination a').click(function() {
		$('#msgContentWrapper').load($(this).attr('rel')+' #msgContentWrapper', function() { tdColor(); links2Rel(); });
	});
	$('div.paginationSmiles p.pagination a').click(function() {
		$('#smilesContentWrapper').load($(this).attr('rel')+' #smilesContentWrapper', function() { tdColor(); links2Rel(); });
	});
}

function selectAll(id) {
	$('#'+id).find('input[type="checkbox"]').each(function() {
		$(this).attr('checked', 'checked');
	});
}

function deselectAll(id) {
	$('#'+id).find('input[type="checkbox"]').each(function() {
		$(this).removeAttr('checked');
	});
}


function _hasPopupBlocker(poppedWindow) {
	var result = false;

	try {
		if (typeof poppedWindow == 'undefined') {
			result = true;
		}
		else if (poppedWindow && poppedWindow.closed) {
			result = false;
		}
		else if (poppedWindow && poppedWindow.test) {
			result = false;
		}
		else {
			result = true;
		}

	} catch (err) {
		if (console) {
			console.warn("Could not access popup window", err);
		}
	}

	return result;
}

