var homeurl = "http://skinpoison.com";
var themepath = homeurl + '/wp-content/themes/skinpoison';

function strpos (haystack, needle, offset) {
    var i = (haystack+'').indexOf(needle, (offset ? offset : 0));
    return i === -1 ? false : i;
}

function substr (f_string, f_start, f_length) {
    f_string += '';

    if (f_start < 0) {
        f_start += f_string.length;
    }

    if (f_length == undefined) {
        f_length = f_string.length;
    } else if (f_length < 0){
        f_length += f_string.length;
    } else {
        f_length += f_start;
    }

    if (f_length < f_start) {
        f_length = f_start;
    }

    return f_string.substring(f_start, f_length);
}

function str_replace (search, replace, subject, count) {
    var i = 0, j = 0, temp = '', repl = '', sl = 0, fl = 0,
            f = [].concat(search),
            r = [].concat(replace),
            s = subject,
            ra = r instanceof Array, sa = s instanceof Array;
    s = [].concat(s);
    if (count) {
        this.window[count] = 0;
    }

    for (i=0, sl=s.length; i < sl; i++) {
        if (s[i] === '') {
            continue;
        }
        for (j=0, fl=f.length; j < fl; j++) {
            temp = s[i]+'';
            repl = ra ? (r[j] !== undefined ? r[j] : '') : r[0];
            s[i] = (temp).split(f[j]).join(repl);
            if (count && s[i] !== temp) {
                this.window[count] += (temp.length-s[i].length)/f[j].length;}
        }
    }
    return sa ? s : s[0];
}

$(document).ready(function() {
	
	// allows list-style to be a different color than the text itself
	$('.entry ol:not("#commentlist") li').each(function() {
		$(this).html('<span class="helper">'+$(this).html()+'</span>');
	});
	
	// since we've got JS, we can remove this button because we're going to hook the select onChange
	$('#galleryfilter .buttons').hide();

	// make sure GetInk'd opens in a new window
    $('#getinked a').click(function(){
        window.open(this.href);
        return false;
    });


	// Sidebar ad
	// $(".ad").load(homeurl + "/members/home.php .ad div",function() {
	// 	newsrc = homeurl + '/members/' + $('div.ad img').attr('src').replace('./', '');
	// 	$('div.ad img').attr('src',newsrc);
	// });
	
	// Just Signed Up
	$("#justsignedup>ul").load(homeurl + "/members/home.php #justsignedup li");
	
	// Popular Tattoos
	$("#popular>ul").load(homeurl + "/members/home.php #popular li");
	
	
	// ======================
	// = HOME PAGE BUSINESS =
	// ======================
	if( $('body#homepage').length != 0 ) {
		
		// Home page Flash
		var flashvars = {};
		flashvars.appurl = homeurl + "/get-inkd/";
		var params = {};
		params.scale = "noscale";
		params.wmode = "transparent";
		var attributes = {};
		attributes.id = "objf_getinked";
		swfobject.embedSWF(themepath + "/f/getinkd.swf", "flash", "580", "254", "10.0.0", themepath + "/f/expressInstall.swf", flashvars, params, attributes);
		
		// Latest Activity
		$("#latestactivity>.inner>ul").load(homeurl + "/members/home.php #recent-activity li:lt(6)", function() {
			$('#latestactivity li').find('a').each(function() {
				targetlink = $(this);
				targethref = targetlink.attr('href');
				if( !strpos(targethref,'members') ) {
					targetlink.attr('href','members/' + targethref);
				}
			});
		});
		
		// Sidebar content
		$.getJSON(homeurl + "/members/status.php", function(json) {
			if(json.status=="yes") {
				$('#sidebar').addClass('seloggedin');
				$('#loginsignup').empty().append('<p><a href="' + homeurl + '/members/user_home.php">Return to members area</a></p>');
				$('#loginsignupmini').empty().append('<p><a href="' + homeurl + '/members/user_home.php">Return to members area</a></p>');
			}
		});
		
	}
	
});