



function twitter( obj ) {
    var output = [];
	var twit;
    for (var i=0; i < 1; i++) {
        twit = obj[i].text;
        output.push( linkify_plain(twit, obj[i].created_at) );
    };
	if ( twit && twit.length ) {
		$(document).ready(function () {
		   $('#tweet').html(output.join(""));
		   $('#retweet-link').html('<a href="http://twitter.com/?status=RT %40RentJuice – '+twit+'" target="_blank" >Retweet Post<div class="retweet-icon"></div></a>');
	   });
	}
}

function relative_time(time_value) {
	var values = time_value.split(" ");
	time_value = values[1] + " " + values[2] + ", " + values[5] + " " + values[3];
	var parsed_date = Date.parse(time_value);
	var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
	var delta = parseInt((relative_to.getTime() - parsed_date) / 1000);
	delta = delta + (relative_to.getTimezoneOffset() * 60);

	var r = '';
	if (delta < 60) {
		r = 'a minute ago';
	} else if (delta < 120) {
		r = 'couple of minutes ago';
	} else if (delta < (45 * 60)) {
		r = (parseInt(delta / 60)).toString() + ' minutes ago';
	} else if (delta < (90 * 60)) {
		r = 'an hour ago';
	} else if (delta < (24 * 60 * 60)) {
		r = '' + (Math.ceil(delta / 3600)).toString() + ' hours ago';
	} else if (delta < (48 * 60 * 60)) {
		r = '1 day ago';
	} else {
		r = (parseInt(delta / 86400)).toString() + ' days ago';
	}

	return r;
}


function linkify_plain(text, created_at) {
	if( !text ) return text;

	text = text.replace(/((https?\:\/\/|ftp\:\/\/)|(www\.))(\S+)(\w{2,4})(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/gi,function(url){
	nice = url;
	if( url.match('^https?:\/\/') ) {
		nice = nice.replace(/^https?:\/\//i,'');
	} else {
		url = 'http://'+url;
	}


	return '<a target="_blank" rel="nofollow" href="'+ url +'">'+ nice.replace(/^www./i,'') +'</a>';

	});

	var to_return = text.replace( /#([a-zA-Z0-9]+)/g, '<a href="http://twitter.com/search?q=%23$1" target="_blank">#$1</a>' ).replace( /@([a-zA-Z0-9]+)/g, '<a href="http://twitter.com/$1" target="_blank">@$1</a>' );
	return to_return + '<div style="font-weight:bold; color:#666; font-size:11px;">' + relative_time(created_at) + '</div>';
}



function clear_blue_dot(carousel_num) {
	var cn = carousel_num+1;
	var tb = $('.text_box' + cn);
	tb.hide();
	$kids = $('#opt' + cn + 'holder').children();
	$kids.removeClass('active');
	return $kids;
}

function click_dot(carousel,carousel_num,obj,pic_num) {
	var cn = carousel_num+1;
	var tb = $('.text_box' + cn);
	$kids = clear_blue_dot(carousel_num);
	$.rjcarousel.sel[carousel_num] = pic_num;
        carousel.scroll(jQuery.jcarousel.intval(pic_num+1));
	$(obj).addClass('active');
	$(tb[pic_num]).show();
        return false;
}

function click_prev(carousel,carousel_num) {
	$kids = clear_blue_dot(carousel_num);
	var cn = carousel_num +1;
	var tb = $('.text_box' + cn);
	$.rjcarousel.sel[carousel_num]--;
	if ( $.rjcarousel.sel[carousel_num] < 0 ) {
		$.rjcarousel.sel[carousel_num] = 3;
		click_dot(carousel,carousel_num,$kids[3],3);
	} else {
		carousel.prev();
		$(tb[$.rjcarousel.sel[carousel_num]]).show();
		$($kids[$.rjcarousel.sel[carousel_num]]).addClass('active');
	}
}

function click_next(carousel,carousel_num) {
	$kids = clear_blue_dot(carousel_num);
	var cn = carousel_num +1;
	var tb = $('.text_box' + cn);
	$.rjcarousel.sel[carousel_num]++;
	if ( $.rjcarousel.sel[carousel_num] > 3 ) {
		$.rjcarousel.sel[carousel_num] = 0;
		click_dot(carousel,carousel_num,$kids[0],0);
	} else {
		carousel.next();
		$(tb[$.rjcarousel.sel[carousel_num]]).show();
		$($kids[$.rjcarousel.sel[carousel_num]]).addClass('active');
	}
}


function mycarousel_initCallback(carousel) {
    $('#mycarousel-next1').bind('click', function() {
	click_next(carousel,0);
        return false;
    });

    $('#mycarousel-prev1').bind('click', function() {
	click_prev(carousel,0);
        return false;
    });

    $('#mycarousel1-opt1').bind('click', function() {
	click_dot(carousel,0,this,0);
        return false;
    });

    $('#mycarousel1-opt2').bind('click', function() {
	click_dot(carousel,0,this,1);
        return false;
    });

    $('#mycarousel1-opt3').bind('click', function() {
	click_dot(carousel,0,this,2);
        return false;
    });

    $('#mycarousel1-opt4').bind('click', function() {
	click_dot(carousel,0,this,3);
        return false;
    });

};

function mycarousel_initCallback2(carousel) {
    $('#mycarousel-next2').bind('click', function() {
	click_next(carousel,1);
        return false;
    });

    $('#mycarousel-prev2').bind('click', function() {
	click_prev(carousel,1);
        return false;
    });

    $('#mycarousel2-opt1').bind('click', function() {
	click_dot(carousel,1,this,0);
        return false;
    });

    $('#mycarousel2-opt2').bind('click', function() {
	click_dot(carousel,1,this,1);
        return false;
    });

    $('#mycarousel2-opt3').bind('click', function() {
	click_dot(carousel,1,this,2);
        return false;
    });

    $('#mycarousel2-opt4').bind('click', function() {
	click_dot(carousel,1,this,3);
        return false;
    });

};

// Ride the carousel...
$(document).ready(function() {
    $.rjcarousel = { 
        sel : [0,0]
    }; 
    $("#mycarousel1").jcarousel({
        scroll: 1,
        initCallback: mycarousel_initCallback,
        // This tells jCarousel NOT to autobuild prev/next buttons
        buttonNextHTML: null,
        buttonPrevHTML: null
    });

    $("#mycarousel2").jcarousel({
        scroll: 1,
        initCallback: mycarousel_initCallback2,
        // This tells jCarousel NOT to autobuild prev/next buttons
        buttonNextHTML: null,
        buttonPrevHTML: null
    });

	$('#seo-header-wrap a').bind('click',function(k) {
		// handle headers
		$('#seo-header-wrap a').each(function(index) { $(this).parent().removeClass("active"); });
		$(this).parent().addClass("active");
		//handle contents
		$('.seo-content .summary').each(function(i) { $(this).hide(); });
		$('.seo-content .full').each(function(i) { $(this).hide(); });

		var tab_index = $(this).attr('id').split('_')[1];
		var cs = '.seo-content.tab' + tab_index;
		console.log(cs);
		$(cs).addClass("active");
		$(cs + ' > .summary').show()

		if ( tab_index == 1 ) {
			$('#seo-expand-trigger-container a').show().html('Read more').removeClass("expanded").addClass("notexpanded");
		} else {
			$('#seo-expand-trigger-container a').hide();
		}
	});

	$('#seo-expand-trigger-container a').bind('click',function(k) {

		if ( $(this).hasClass("notexpanded") ) {
			$($('.seo-content.active > .full')).show();
			$(this).html('Collapse').removeClass("notexpanded").addClass("expanded");
		} else if ( $(this).hasClass("expanded") ) {
			$($('.seo-content.active > .full')).hide();
			$(this).html('Read more').removeClass("expanded").addClass("notexpanded");
		}

	});


});

$(document).ready(function () {

	var options = {
		directory : '/common/images/fancy-zoom'
	};

	$('#video-1 a').fancyZoom(options);
	$('#video-2 a').fancyZoom(options);
	
	$('#one-btn a').fancyZoom(options);
	$('#free-btn a').fancyZoom(options);
	$('#pro-btn a').fancyZoom(options);
	$('#corp-btn a').fancyZoom(options);

	// Support link
	$('.zendesk').click(function() {
		Zenbox.render();
	});

});

