var address = {};
var conversation = {};

$(document).ready(init);

function init () {
	loadGeo();
	activateScrollInForm();
	activateRefinePopup();
	/* Disabled so flash works
	fadeInOut('#profileInfoColumn #screenName #onlineStatus');
	fadeInOut('#imContainer dt .title');
	*/
}

function fadeInOut(id) {
    var badBrowser = (/MSIE ((5\.5)|6)/.test(navigator.userAgent) && navigator.platform == "Win32");
    if (badBrowser) {
        //-- Popup is disabled in IE 5.5 and 6
    } else {
        $(id).fadeTo(750, .20, function() {
            $(this).fadeTo(800, .80, function() {
                $(this).fadeTo(400, 1.00, function() {
                fadeInOut(id);
                });
            });
        });
    }
}

/*** Start geoLoad ***/
function loadGeo() {
    var url= "http://textad.xxxmatch.com/if/1/2213/0/";
    $.getJSON( url+'?jquery=?');
}

function geoLocation(json) {
    address.city = json.city;
    address.city_region = json.city_region,
    address.country_code = json.country_code;
    $('.profileLocation .location').html(address.city +', '+ address.city_region +', '+ address.country_code);
    $('#imContainer .location').html(address.city +', '+ address.city_region +', '+ address.country_code);
}    
/*** Start geoLoad ***/
    
/*** Start bb_off -- for footers ***/
function bb_off () {
    document.domain = document.domain.substring(document.domain.indexOf('.')+1);
    if ( top.shut )
    {
        top.shut = false;
    }
    top.location = 'http://www.google.com';
}    
/*** End bb_off -- for footers ***/    


/*** Start scrollToForm ***/
function activateScrollInForm() {
    $("a:not(.tabs a, #profileImageNav a, #imContainer dt .close, .footer_2257 a, .pagefooter a, #myPhotos a.hasImage, .refine_search a, #popup_body .close), #imContainer .send").click( function() {
		scrollInForm();
		return false;
	});
}

function scrollInForm() {
    addOverlay('#overlay');
    $('#overlay').fadeIn(400, function() {
        scrollWindow(0, 350);  
       	$('#formContainer')
    	    .css({'position': 'absolute', 'top': '50px', 'left': (($(window).width() - $('#formContainer').width()) / 2) +'px'})
            .fadeIn(350); 
    });
    
    //-- Apply close functionality
   	$('#form .close').click(function() {
       	$('#formContainer').fadeOut(350);
       	removeOverlay("", 350);
   	})
}

function scrollWindowToElem(element, time) {
    if ("string" == typeof(element)) {
        element = $(element);
    }
    scrollWindow(element.position().top ,time);
}

function scrollWindow(scrolltop, time) {
    var elem = 'html';
	
	if ($('html').scrollTop() == 0 && $('body').scrollTop() == 0) {
		elem = 'html, body';
	} else if ($('body').scrollTop() > 0) {
		elem = 'body';
	}
	$(elem).animate({"scrollTop": scrolltop }, time);
}
/*** End scrollToForm ***/

/*** Start IM Converstation ***/
function activateIM(screenname) {
    var badBrowser = (/MSIE ((5\.5)|6)/.test(navigator.userAgent) && navigator.platform == "Win32");
    if (badBrowser) {
        //-- Popup is disabled in IE 5.5 and 6
    } else {
        scrollInIM();
        conversation = new IMConversation(screenname);
        activateIMClose();
        //-- Uncomment this line to enable chat 'bot'
        //-- activateIMConversation();
        $('#imContainer .txtImMessage').focus( function() {
            $(this).val('');
        });
    }
}

function scrollInIM() {
    $('#imContainer').css({'top': $(window).height() - 100 });
    $('#imContainer').show();
    $('#imContainer').animate({'top': $(window).height() - $('#imContainer').height() }, 1000, 'linear', function() {
        $(this).css({'bottom': '0px', 'top': 'auto'});
    });
}

function activateIMClose(screenname) {
    $('#imContainer dt .close').click( function() {
        $('#imContainer').css({'top': ($(window).height() - $('#imContainer').height()) });
        $('#imContainer').animate({'top': $(window).height() }, 350, 'linear', function() {
            $(this).remove();
        });
        return false;
    });
}

function IMConversation (screenname) {
    this.screenname = screenname;
    this.i = 0;
    this.test = "test";
    this.min_delay = 7000;
    this.max_delay = 20000;
    this.convo = new Array(
        "hey baby...",
        "are you there?",
        "hello??",
        "you wanna chat?",
        "what's so interesting?",
        "maybe I should come back later...",
        "hey there, I'm waiting! ;)",
        "tick tock tick tock, I'm looking for your cock",
        "what's going on?",
        "are you too busy for me? :("
    );

    this.getNextConvo = function() {
        var text = '<span class="screenname">xx'+ this.screenname +'</span>: '+ this.convo[this.i++] +'<br />';
        if ( this.i < this.convo.length ) {
            setTimeout("activateIMConversation()", Math.floor((Math.random()*(this.max_delay-this.min_delay))+this.min_delay));
        }
        return text;
    };
}

function activateIMConversation() {
    $('#imContainer .convo').append( conversation.getNextConvo() );
    $('#imContainer .convo').scrollTop($('#imContainer .convo').height());
}
/*** End IM Converstation ***/

/*** Start bb_off -- for footers ***/
function bb_off () {
    top.location = 'http://www.google.com';
}    
/*** End bb_off -- for footers ***/



/*** Start popup Refine ***/
function activateRefinePopup() {
    $(".refine_search a").click(popupRefine);
}

function popupRefine(e) {
	if( ! $("#popupRefine").is(':visible') ) {
		$("#popupRefine").fadeIn(200);
	} else {
		closeRefine();
	}
	return false;
}
	
function closeRefine() {
    $("#popupRefine").fadeOut(200);
}
/*** END popup Refine ***/

