
(function($) {

	var visible = false;
    var currentModel = false;
    var camlistLoaded = false;

	$.fn.inlineProfile = function() {
        var $this = $(this);

//        if(!camlistLoaded){
//            camlistLoaded = true;
//            var models = {};
//            $('.cam a.start_chat').each(function(i,e){
//                if(models[$(this).attr('rel')]==undefined)
//                    models[$(this).attr('rel')] = $(this).attr('rel');
//            });
//            $.post('/inline/prevnext/camlist.php',models,function(data){},'json');
//        }

        $('.prevprofile, .nextprofile').die('click');
        $('.prevprofile, .nextprofile').live('click',function(e){
            e.preventDefault();

            $.post('/inline/prevnext/camlist.php',{
                current:currentModel,
                direction:$(this).attr('class')
            },
            function(data){
                $this.showProfile(data.next_model);
            },'json');
        });

		$('.back').live('click', function (e) {
			e.preventDefault();
			$this.hideProfile();
		});

		$this.showProfile = function(name) {
			$(document).scrollTop(0);

			$.post(
				'/inline/profile/',
				{ m: name },
				function (data) {
					if(data == null || data.model == null) return alert('We hebben geen modellen met de gekeuzde naam.');
                    currentModel = data.model.modelnaam;
					$('.prof-modelnaam').html(data.model.modelnaam);
					$('#profiel .start_chat').attr('rel', data.model.modelnaam);
					$('.leeftijd').html(data.model.leeftijd);
					$('#img1').attr('src', $('#img1').attr('src').replace(/^(.+\/)[^\/]+(\/[^\/]+)$/i, '$1'+data.model.modelnaam+'$2'));
					$('#img2').attr('src', $('#img2').attr('src').replace(/^(.+\/)[^\/]+(\/[^\/]+)$/i, '$1'+data.model.modelnaam+'$2'));
					$('#img3').attr('src', $('#img3').attr('src').replace(/^(.+\/)[^\/]+(\/[^\/]+)$/i, '$1'+data.model.modelnaam+'$2'));
					$('#img4').attr('src', $('#img4').attr('src').replace(/^(.+\/)[^\/]+(\/[^\/]+)$/i, '$1'+data.model.modelnaam+'$2'));
					$('.prof-leeftijd').html(data.model.leeftijd);
					$('.prof-woonplaats').html(data.model.woonplaats);


					$("#profiel .start_chat").attr('href', '/chat.html?m=' + data.model.modelnaam);

					//
					$('.prof-rating').html('');
					var rating = Math.ceil(data.model.waardering / 2),
						counter = 0;

					for (var i = 0; i < 5; i++) {
						if(rating > i)
							$('.prof-rating').append($('<img />').attr('src', custom + 'img/star_heel.jpg').width(23).height(40).css('border', 'none'));
						else $('.prof-rating').append($('<img />').attr('src', custom + 'img/star_leeg.jpg').width(23).height(40).css('border', 'none'));
					}


					$('.prof-talen').empty().append(
						$('<img />').attr('src', custom + 'img/flags/' + data.model.taal + '.jpg')
					);

					$('.prof-land').html(data.model.land);
					$('.prof-relatie').html(data.model.relatie);
					$('.prof-geaardheid').html(data.model.geaardheid);
					$('.prof-haar').html(data.model.haar);
					$('.prof-ogen').html(data.model.ogen);
					$('.prof-aanuit').html((data.model.onoffline == 'online')? 'ja': 'nee');
					$('.prof-video').html(data.model.video);
					$('.prof-beschrijving').html(data.model.beschrijving);

					$('#videopreview_container').removeClass('videoyes videono').addClass('video'+data.model.video);
					$('#videopreview_text').removeClass('videooffyes videooffno').addClass('videooff'+data.model.video);

					$('#profiel').slideDown(400, function () {
						if(data.model.video != 'yes') return;
						var s1 = new SWFObject("/swf/player.swf","ply","240","180","9","#FFFFFF");
		                s1.addParam("allowfullscreen","true");
		                s1.addParam("allowscriptaccess","always");
		                s1.addParam("flashvars","bufferlength=5&icons=true&controlbar=none&autostart=true&image=http://images."+domain+"/"+data.model.modelnaam+"/multimedia/promotie/standaard.jpg&file=http://images."+domain+"/"+data.model.modelnaam+"/multimedia/promotie/"+data.model.modelnaam+"_promotie.flv");
		                s1.write("videopreview");
					});
					visible = true;
				},
				'json'
				);
		};

		$this.hideProfile = function() {
			$('#videopreview_container').html('<div id="videopreview" />');
			$('#profiel').slideUp('fast');
		};

		$this.switchProfile = function(name) {
			$('#videopreview_container').html('<div id="videopreview" />');
			$('#profiel').slideUp('fast', function () {
				$this.showProfile(name);
			});
		};

		return $this.live('click', function(evt) {
			evt.preventDefault();

			var selected = $(this).attr('rel');

			if(selected.replace(/^\s+(.+?)\s+$/i, '$1') == '') return;

			if(visible) {
				$this.switchProfile(selected);
			}
			else $this.showProfile(selected);
		});
	};

})(jQuery);
