$(document).ready(init);

//-- Global Variables
var profiles;

function init () {
    loadData();
    loadProfile( findProfile(findUrlScreenname()) );
}

///Replace ASK Btn
$.fn.replaceText = function (text) {
    return this.each (function () {
        if ( typeof(text) == "undefined" || text == "" || text.toLowerCase() == "tell you later") {
	    	$(this).html('Tell you later <a class="askIconDiv" href="#">ASK</a>');
	    } else {
		    $(this).html(text);
	    }
    });
}

function loadData() {
    $.ajax({
        type: "POST",
        async: false,
        data: 'action=json',
        url: getAjaxBaseURL(),
        dataType: "json",
        success:  function(json) {
            profiles = json.profiles;
        }
    });    
}

function findUrlScreenname() {
    urlChunks = location.href.split('/');
    screenname = urlChunks[urlChunks.length-2];
    return screenname;
}

function findProfile(sn) {
    for ( var i=0; i< profiles.length; i++ ) {
        if ( sn.toUpperCase() == profiles[i].screenname.toUpperCase() ) {
            return i;
        }
    }
    return 0;
}

function loadProfile(profileId) {
    if ( typeof(profileId) == "undefined" || profileId == "") {
		profileId = 0;
	}
	
	/// Profile Id Content
    $('#screenName a:not(#onlineStatus), #flirt_s1 p b, #zodiacDisplay h3').replaceText(profiles[profileId].screenname);
    $('#innerProfileWrapper h2.status span').replaceText(profiles[profileId].profileAboutMe);
	$('#profileRating .value').replaceText ('Profile Rating: '+ profiles[profileId].profileRating);
	$('#profileAboutMe strong').replaceText( profiles[profileId].profileAboutMe);
	$('#profileLookingFor strong').replaceText(profiles[profileId].profileookingyou +' for a ' + profiles[profileId].profileookingfor +' for a ' + profiles[profileId].profileookingwith );

	$('.profileLocation .gender').replaceText(profiles[profileId].individualcontent[0].profilelocation.gender);
	$('.profileLocation .age').replaceText(profiles[profileId].individualcontent[0].profilelocation.age);
/*
	$('.profileLocation .location').replaceText(profiles[profileId].individualcontent[0].profilelocation.city +', '+ profiles[profileId].individualcontent[0].profilelocation.state +', '+ profiles[profileId].individualcontent[0].profilelocation.country);
*/
    
	$('.profileInfo dl dd.birthday').replaceText (profiles[profileId].individualcontent[0].profileinfo.birthday);
	$('.profileInfo dl dd.zodiac').replaceText (profiles[profileId].individualcontent[0].profileinfo.zodiac);

	$('#zodiacDisplay .zodiacIcon').addClass(profiles[profileId].individualcontent[0].profileinfo.zodiac.toLowerCase());

	$('.profileInfo dl dd.height').replaceText (profiles[profileId].individualcontent[0].profileinfo.height);
	$('.profileInfo dl dd.bodyType').replaceText (profiles[profileId].individualcontent[0].profileinfo.bodytype);
	$('.profileInfo dl dd.sexualOrientation').replaceText (profiles[profileId].individualcontent[0].profileinfo.sexualorientation);
	$('.profileInfo dl dd.ethnicity').replaceText (profiles[profileId].individualcontent[0].profileinfo.ethnicity);
	$('.profileInfo dl dd.last').replaceText (profiles[profileId].individualcontent[0].profileinfo.maritalStatus);
	$('#aboutMe p').replaceText (profiles[profileId].aboutme);
	$('#aboutMyMatch p').replaceText (profiles[profileId].aboutmymatch);
	$('#myIdealMatchQuickStats ul li.hair span').replaceText (profiles[profileId].myidealmatchquickstats[0].hair);
	$('#myIdealMatchQuickStats ul li.eyeColor span').replaceText (profiles[profileId].myidealmatchquickstats[0].eyecolor);
	$('#myIdealMatchQuickStats ul li.tattoos span').replaceText (profiles[profileId].myidealmatchquickstats[0].tattoos);
	$('#myIdealMatchQuickStats ul li.piercings span').replaceText (profiles[profileId].myidealmatchquickstats[0].piercings);
	$('#myIdealMatchQuickStats ul li.education span').replaceText (profiles[profileId].myidealmatchquickstats[0].education);
	$('#myIdealMatchQuickStats ul li.myInterests span').replaceText (profiles[profileId].myidealmatchquickstats[0].myinterests);
	$('#myIdealMatchQuickStats ul li.exerciseHabits span').replaceText (profiles[profileId].myidealmatchquickstats[0].exercisehabits);
	$('#myIdealMatchQuickStats ul li.sportsLike span ').replaceText (profiles[profileId].myidealmatchquickstats[0].sportslike);
	$('#myIdealMatchQuickStats ul li.occupation span').replaceText (profiles[profileId].myidealmatchquickstats[0].occupation);
	$('#myIdealMatchQuickStats ul li.income span').replaceText (profiles[profileId].myidealmatchquickstats[0].income);
	$('#myIdealMatchQuickStats ul li.haveKids span').replaceText (profiles[profileId].myidealmatchquickstats[0].havekids);
	$('#myIdealMatchQuickStats ul li.wantKids span').replaceText (profiles[profileId].myidealmatchquickstats[0].wantkids);
	$('#myIdealMatchQuickStats ul li.languages span').replaceText (profiles[profileId].myidealmatchquickstats[0].languages);
	$('#myIdealMatchQuickStats ul li.politics span').replaceText (profiles[profileId].myidealmatchquickstats[0].politics);
	$('#myIdealMatchQuickStats ul li.religiousViews span').replaceText (profiles[profileId].myidealmatchquickstats[0].religiousviews);
	$('#myIdealMatchQuickStats ul li.personalStyle span').replaceText (profiles[profileId].myidealmatchquickstats[0].personalstyle);
	$('#myIdealMatchQuickStats ul li.myCar span').replaceText (profiles[profileId].myidealmatchquickstats[0].mycar);
	$('#myIdealMatchQuickStats ul li.drink span').replaceText (profiles[profileId].myidealmatchquickstats[0].drink);
	$('#myIdealMatchQuickStats ul li.smoke span').replaceText (profiles[profileId].myidealmatchquickstats[0].smoke);
	$('#myIdealMatchQuickStats ul li.drugs span').replaceText (profiles[profileId].myidealmatchquickstats[0].drugs);
	$('#myIdealMatchQuickStats ul li.eatingHabits span').replaceText (profiles[profileId].myidealmatchquickstats[0].eatinghabits);
	$('#myIdealMatchQuickStats ul li.myPets span').replaceText (profiles[profileId].myidealmatchquickstats[0].mypets);
	$('#myIdealMatchQuickStats ul li.petsILike span').replaceText (profiles[profileId].myidealmatchquickstats[0].petsilike);
	$('#myIdealMatchQuickStats ul li.highSchool span').replaceText (profiles[profileId].myidealmatchquickstats[0].highschool);
	$('#myIdealMatchQuickStats ul li.college span').replaceText (profiles[profileId].myidealmatchquickstats[0].college);
	$('#myIdealMatchQuickStats ul li.escort span').replaceText (profiles[profileId].myidealmatchquickstats[0].escort);
	$('#whatImLookingFor ul li.ageRange span').replaceText (profiles[profileId].whatimlookingfor[0].agerange);
	$('#whatImLookingFor ul li.sexualOrientation span').replaceText (profiles[profileId].whatimlookingfor[0].sexualorientation);
	$('#whatImLookingFor ul li.maritalStatus span').replaceText (profiles[profileId].whatimlookingfor[0].maritalstatus);
	$('#whatImLookingFor ul li.ethnicity span').replaceText (profiles[profileId].whatimlookingfor[0].ethnicity);
	$('#whatImLookingFor ul li.bodyType span').replaceText (profiles[profileId].whatimlookingfor[0].bodytype);
	$('#whatImLookingFor ul li.heightRange span').replaceText (profiles[profileId].whatimlookingfor[0].heightrange);
	$('#whatImLookingFor ul li.hairColor span').replaceText (profiles[profileId].whatimlookingfor[0].haircolor);
	$('#whatImLookingFor ul li.eyeColor span').replaceText (profiles[profileId].whatimlookingfor[0].eyecolor);
	$('#whatImLookingFor ul li.bodyHair span').replaceText (profiles[profileId].whatimlookingfor[0].bodyhair);
	$('#whatImLookingFor ul li.tattoos span').replaceText (profiles[profileId].whatimlookingfor[0].tattoos);
	$('#whatImLookingFor ul li.piercings span').replaceText (profiles[profileId].whatimlookingfor[0].piercings);
	$('#whatImLookingFor ul li.circumcised span').replaceText (profiles[profileId].whatimlookingfor[0].circumcised);
	$('#whatImLookingFor ul li.education span').replaceText (profiles[profileId].whatimlookingfor[0].education);
	$('#whatImLookingFor ul li.languages span').replaceText (profiles[profileId].whatimlookingfor[0].languages);
	$('#whatImLookingFor ul li.income span').replaceText (profiles[profileId].whatimlookingfor[0].income);
	$('#whatImLookingFor ul li.hasKids span').replaceText (profiles[profileId].whatimlookingfor[0].haskids);
	$('#whatImLookingFor ul li.wantKids span').replaceText (profiles[profileId].whatimlookingfor[0].wantskids);
	$('#whatImLookingFor ul li.interests span').replaceText (profiles[profileId].whatimlookingfor[0].interests);
	$('#whatImLookingFor ul li.exerciseHabits span').replaceText (profiles[profileId].whatimlookingfor[0].exercisehabits);
	$('#whatImLookingFor ul li.eatingHabits span').replaceText (profiles[profileId].whatimlookingfor[0].eatinghabits);
	$('#whatImLookingFor ul li.personalStyle span').replaceText (profiles[profileId].whatimlookingfor[0].personalstyle );
	$('#whatImLookingFor ul li.politics span').replaceText (profiles[profileId].whatimlookingfor[0].politics);
	$('#whatImLookingFor ul li.religiousViews span').replaceText (profiles[profileId].whatimlookingfor[0].religiousviews);
	$('#whatImLookingFor ul li.drink span').replaceText (profiles[profileId].whatimlookingfor[0].drink);
	$('#whatImLookingFor ul li.smoke span').replaceText (profiles[profileId].whatimlookingfor[0].smoke);
	$('#whatImLookingFor ul li.drugs span').replaceText (profiles[profileId].whatimlookingfor[0].drugs);
	$('#whatImLookingFor ul li.hasPets span').replaceText (profiles[profileId].whatimlookingfor[0].haspets);
	$('#whatImLookingFor ul li.wantsPets span').replaceText (profiles[profileId].whatimlookingfor[0].wantspets);
	$('#detailsAboutMe p .forFun').replaceText (profiles[profileId].detailsaboutme[0].forfun);
	$('#detailsAboutMe p .perfectEvening').replaceText (profiles[profileId].detailsaboutme[0].perfectevening);
	$('#detailsAboutMe p .favMovies').replaceText (profiles[profileId].detailsaboutme[0].favmovies);
	$('#detailsAboutMe p .musicalTastes').replaceText (profiles[profileId].detailsaboutme[0].musicaltastes);
	$('#detailsAboutMe p .petPeeves').replaceText (profiles[profileId].detailsaboutme[0].petpeeves);
	$('#detailsAboutMe p .favPlaces').replaceText (profiles[profileId].detailsaboutme[0].favplaces);
	$('#detailsAboutMe p .favThings').replaceText (profiles[profileId].detailsaboutme[0].favthings);
	$('#detailsAboutMe p .booksILike').replaceText (profiles[profileId].detailsaboutme[0].booksilike);
	$('#detailsAboutMe p .talentSkillls').replaceText (profiles[profileId].detailsaboutme[0].talentskillls);
	$('#detailsAboutMe p .explainJob').replaceText (profiles[profileId].detailsaboutme[0].explainjob);
	$('#surveysDiv ul.content li.love span').replaceText (profiles[profileId].surveysdiv[0].love);
	$('#surveysDiv ul.content li.hate span').replaceText (profiles[profileId].surveysdiv[0].hate);
	$('#surveysDiv ul.content li.favPositions span').replaceText (profiles[profileId].surveysdiv[0].favpositions);
	$('#surveysDiv ul.content li.senses span').replaceText (profiles[profileId].surveysdiv[0].senses);
	$('#surveysDiv ul.content li.sexyHair span').replaceText (profiles[profileId].surveysdiv[0].sexyhair);
	$('#surveysDiv ul.content li.penisSize span').replaceText (profiles[profileId].surveysdiv[0].penissize);
	$('#surveysDiv ul.content li.oneNightStand span').replaceText (profiles[profileId].surveysdiv[0].onenightstand);
	$('#surveysDiv ul.content li.fakeOrgasm span').replaceText (profiles[profileId].surveysdiv[0].fakeorgasm);
	$('#surveysDiv ul.content li.pornMovies span').replaceText (profiles[profileId].surveysdiv[0].pornmovies);
	$('#surveysDiv ul.content li.virginity span').replaceText (profiles[profileId].surveysdiv[0].virginity);
	$('#surveysDiv ul.content li.bodyTurnOn span').replaceText (profiles[profileId].surveysdiv[0].bodyturnon);
	$('#surveysDiv ul.content li.rateYourself span').replaceText (profiles[profileId].surveysdiv[0].rateyourself);
	$('#surveysDiv ul.content li.ageGroup span').replaceText (profiles[profileId].surveysdiv[0].agegroup);
	$('#surveysDiv ul.content li.afterSex span').replaceText (profiles[profileId].surveysdiv[0].aftersex);
	$('#surveysDiv ul.content li.placesSex span').replaceText (profiles[profileId].surveysdiv[0].placessex);
	$('#surveysDiv ul.content li.partners span').replaceText (profiles[profileId].surveysdiv[0].partners);
	$('#surveysDiv ul.content li.masturbate span').replaceText (profiles[profileId].surveysdiv[0].masturbate);
	
	//// My Photos Profile Id 
	if ( profiles[profileId].myPhotos.length > 0 ) {
		var i = 0;
		$('#profilePic img' ).attr ('src', profiles[profileId].myPhotos[i].src);
		$.each($('#myPhotos').find('img'), function() {			
		    if ( i< profiles[profileId].myPhotos.length ) {
				$(this).attr('src', profiles[profileId].myPhotos[i].thumb);
    			$(this).parent()
    			    .val(i)
    			    .addClass('hasImage')
    			    .unbind()                    
    			    .click( function() {
                        featuredcontentglider.glide(glideconfig, $(this).val(), false);
                        return false;
                    });
				i++;
			} else {
			    $(this).removeClass('hasImage')
			    $(this).attr('src', 'images_placeholders_photo.gif');
			}
		});
		$('#profileImages').html('');
		for ( i=0; i< profiles[profileId].myPhotos.length; i++ ) {
            $('#profileImages').append(
    			'<div class="image">' +
                '    <a href="" title="Profile pic" id="profilePic">'+
                '        <img class="imageBorder" width="224" height="300" src="'+ profiles[profileId].myPhotos[i].src +'" alt="Profile Picture"/>'+
                '    </a>' +
                '</div>'
            );
        }
	}

	/// My Videos Profile Id
	/*
	
	
	var i = 0;
	$.each($('#myVideos').find('img'), function() {			
		if ( i< profiles[profileId].myVideos.length ) {
			$(this).attr('src', profiles[profileId].myVideos[i].src);
			i++;
		}
	});
	*/
	
	// Similar Profile Matches Img
	var i = 0;
	var simProfiles = Array();
	$.each($('#similarProfiles').find('img'), function() {	
	    var done = false;
	    while ( !done && i<(profiles.length-1) ) {
		    randProfile = Math.floor( Math.random()*profiles.length );
			if ( -1 == jQuery.inArray(randProfile, simProfiles) ) {
				$(this).attr('src', profiles[randProfile].myPhotos[0].thumb);
				$(this).parent().parent().find('a.userName').html(profiles[randProfile].screenname);
				simProfiles.push(randProfile);
				i++;
				done = true;
			}
		}
	});
	
	//-- IM info
	$('#imContainer img').attr('alt', profiles[profileId].screenname);
	$('#imContainer img').attr('src', profiles[profileId].myPhotos[0].thumb);
	$('#imContainer .imInfo .screenname').replaceText(profiles[profileId].screenname);
    $('#imContainer .age').replaceText(profiles[profileId].individualcontent[0].profilelocation.age);
    
    //-- Link lightbox to any new <a href tags
    activateScrollInForm();	
    featuredcontentglider.init(glideconfig);        
    
    
    //-- Add the IM
	activateIM(profiles[profileId].screenname);
}

function arrayToObject(a) {
  var o = {};
  for(var i=0;i<a.length;i++)
  {
    o[a[i]]='';
  }
  return o;
}
