function downOpacity(id) {
    new Effect.Opacity('popupbutton' + id, { duration: 0.3, from: 1.0, to: 0.8 });
}

function upOpacity(id) {
    new Effect.Opacity('popupbutton' + id, { duration: 0.3, from: 0.8, to: 1.0 });
}

function drilldown(id) {
    $('sort_most_recent_off').style.display = 'inline';
    $('sort_most_favorited_off').style.display = 'none';
    $('sort_rating_off').style.display = 'none';

    $('sort_most_recent_on').style.display = 'none';
    $('sort_most_favorited_on').style.display = 'inline';
    $('sort_rating_on').style.display = 'inline';

    $('most_favorited_block').style.display = 'none';
    $('rating_block').style.display = 'none';

    $('sort_not_feeling_it_on').style.display = 'inline';
    $('sort_dig_it_on').style.display = 'inline';
    $('sort_nailed_it_on').style.display = 'inline';

    $('sort_today_on').style.display = 'none';
    $('sort_this_week_on').style.display = 'none';
    $('sort_this_month_on').style.display = 'inline';
    $('sort_all_time_on').style.display = 'inline';
    $('sort_nailed_it_off').style.display = 'none';
    $('sort_not_feeling_it_off').style.display = 'none';
    $('sort_dig_it_off').style.display = 'none';
    $('sort_today_off').style.display = 'none';
    $('sort_this_week_off').style.display = 'none';
    $('sort_this_month_off').style.display = 'none';
    $('sort_all_time_off').style.display = 'none';
}

function turnon(namer) {
    $(namer + '_off').style.display = 'inline';
    $(namer + '_on').style.display = 'none';

    if (namer != 'sort_most_recent') {
        $('sort_most_recent_off').style.display = 'none';
        $('sort_most_recent_on').style.display = 'inline';
    }
}

function clear_popup_user() {
    $('inner_create').style.display = 'none';
    $('inner_access').style.display = 'none';
    $('inner_confirm').style.display = 'none';
}

function add_new_tag(id, txt, isowner) {
    var element;

    if (isowner == 1) {
        element = Builder.node('div', { id: 'tag_' + id }, [
			Builder.node('span', { id: 'read_name_' + id, className: 'editable', onclick: "TabInline.SwitchEdit('_name_' + this.id.substring(10));" },
			[
				Builder.node('a', { id: 'tooltip_' + id, href: '#', title: "<div id='ToolTipTextWrap'>Click to edit</div>", className: 'addToolTip', style: 'color: #666666' }, [
				])
			])
			,
			Builder.node('span', { style: 'display:none', id: 'write_name_' + id, className: 'editable', onclick: "TabInline.SwitchEdit('_name_' + this.id.substring(11));" },
			[
				Builder.node('input', { id: 'input_' + id, value: txt, onchange: "ajax_request_update_tag(imagenumber, " + id + ");TabInline.SaveEdit('_name_' + this.id.substring(6)); TabInline.SwitchEdit('_name_' + this.id.substring(6));" }, [
				])
			])
			,
			' ['
			,
			Builder.node('a', { id: 'link_' + id, href: '#', onclick: "ajax_request_delete_tag(imagenumber, " + id + "); $('tag_' + this.id.substring(5)).style.display = 'none';" }, ['x'])
			,
			']'
			,
			Builder.node('input', { id: 'hidden_' + id, type: 'hidden', value: txt }, [
			]),
			Builder.node('br')
		]);

        $('tagset').insert(element);
        $('tooltip_' + id).innerHTML = '<!-- mp_trans_disable_start -->' + txt + '<!-- mp_trans_disable_end -->';
    }
    else {
        element = Builder.node('div', { id: 'tag_' + id }, [
		]),
		Builder.node('br');

        $('tagset').insert(element);
        $('tag_' + id).innerHTML = '<!-- mp_trans_disable_start -->' + txt + '<!-- mp_trans_disable_end -->';
    }

    // tooltip.innerHTML = '<!-- mp_trans_disable_start -->'

    if (isowner == 1) {
        $('tooltip_' + id).setAttribute("tooltiptext", $('tooltip_' + id).title);
        $('tooltip_' + id).removeAttribute("title");
        $('tooltip_' + id).onmouseover = function gomouseover() { ddrivetip(this.getAttribute("tooltiptext")) };
        $('tooltip_' + id).onmouseout = function gomouseout() { hideddrivetip(); };
    }
}

function ajax_request_update_tag(shellid, tagid) {
    var sessionid = read_cookie('sessionid');
    if (sessionid == '' || sessionid == null) sessionid = '0';

    var newtagphrase = $('input_' + tagid).value;
    var oldtagphrase = $('hidden_' + tagid).value;
    if (oldtagphrase == '(my new tag)') oldtagphrase = '';

    // alert('sessionid=' + sessionid + '&shellid=' + shellid + '&oldtagphrase=' + oldtagphrase + '&newtagphrase=' + newtagphrase);

    var request = new Ajax.Request(
		"json/update_tag.aspx",
		{
		    method: 'post',
		    parameters: 'sessionid=' + sessionid + '&tagid=' + tagid + '&shellid=' + shellid + '&oldtagphrase=' + oldtagphrase + '&newtagphrase=' + newtagphrase,
		    onComplete: callback_update_tag
		}
	);
}

function callback_update_tag(transport) {
    var response = transport.responseText;
    var jsonObj = eval("(" + response + ")");

    // alert(	jsonObj.tagid + jsonObj.tagphrase );
    $('input_' + jsonObj.tagid).value = jsonObj.tagphrase;
    $('hidden_' + jsonObj.tagid).value = jsonObj.tagphrase;
    $('read_name_' + jsonObj.tagid).innerHTML = '<!-- mp_trans_disable_start -->' + jsonObj.tagphrase + '<!-- mp_trans_disable_end -->';
}

function ajax_request_delete_tag(shellid, tagid) {
    var sessionid = read_cookie('sessionid');
    if (sessionid == '' || sessionid == null) sessionid = '0';

    var tagphrase = $('input_' + tagid).value;
    // alert('sessionid=' + sessionid + '&shellid=' + shellid + '&tagphrase=' + tagphrase);

    var request = new Ajax.Request(
		"json/delete_tag.aspx",
		{
		    method: 'post',
		    parameters: 'sessionid=' + sessionid + '&shellid=' + shellid + '&tagphrase=' + tagphrase,
		    onComplete: callback_delete_tag
		}
	);
}

function callback_delete_tag(transport) {
    var response = transport.responseText;
    var jsonObj = eval("(" + response + ")");
}

function ajax_request_add_to_favorites(shellid) {
    var sessionid = read_cookie('sessionid');
    if (sessionid == '' || sessionid == null) sessionid = '0';

    var request = new Ajax.Request(
		"json/add_to_favorites.aspx",
		{
		    method: 'post',
		    parameters: 'sessionid=' + sessionid + '&shellid=' + shellid,
		    onComplete: callback_add_to_favorites
		}
	);
}

function callback_add_to_favorites(transport) {
    var response = transport.responseText;
    var jsonObj = eval("(" + response + ")");

    if (mypage == 'My Gallery') reload_my_shells(profile);
}

function ajax_request_remove_from_favorites(shellid) {
    var sessionid = read_cookie('sessionid');
    if (sessionid == '' || sessionid == null) sessionid = '0';

    var request = new Ajax.Request(
		"json/remove_from_favorites.aspx",
		{
		    method: 'post',
		    parameters: 'sessionid=' + sessionid + '&shellid=' + shellid,
		    onComplete: callback_remove_from_favorites
		}
	);
}

function callback_remove_from_favorites(transport) {
    var response = transport.responseText;
    var jsonObj = eval("(" + response + ")");

    if (mypage == 'My Gallery') reload_my_shells(profile);
}

function add_new_record(id, shellObj, posLeft, posTop, imagename, container) {
    // alert('id: ' + id + ' shellname: ' + shellObj['shellname'] + ' posLeft: ' + posLeft + ' posTop: ' + posTop + ' imagename: ' + imagename + ' container: ' + container);	
    imagehash[shellObj.shellid] = id;

    var element = Builder.node('div', { id: 'record_' + id, style: 'position:absolute;top:' + posTop + 'px;left:' + posLeft + 'px;width:284px;height:295px;background-image:url(imageset__records/record_outer_shell.gif);' }, [
		Builder.node('div', { style: 'position:absolute;width:246px;height:25px;left:15px;top:16px;' }, [
			Builder.node('div', { style: 'position:absolute;width:215px;height:25px;left:3px;top:0px;' }, [
				Builder.node('a', { href: 'explore_collection.aspx?shelltarget=' + shellObj['shellid'] }, [
					Builder.node('h4', {}, [
						Builder.node('b', { id: 'shellname_' + id }, [
						])
					])
				])
			]),
			Builder.node('div', { id: 'popupbutton100' + id + '1', style: 'display:none;position:absolute;width:24px;height:25px;left:222px;top:0px;' }, [
				Builder.node('a', { id: 'tooltip100' + id + '1', href: '#', title: "<div id='ToolTipTextWrap'>Click to add to favorites</div>", className: 'addToolTip', onclick: "$('popupbutton100" + id + "1').style.display = 'none'; if (mypage != 'My Gallery') $('popupbutton100" + id + "2').style.display = 'block' ;ajax_request_add_to_favorites(" + imageset[id] + "); Effect.Appear('popup_favorites'); trackEvent(this); return false;" }, [
					Builder.node('img', { width: 24, height: 25, border: 0, src: "imageset__records/button_star.PNG", onmouseover: "javascript:downOpacity(100" + id + "1);", onmouseout: "javascript:upOpacity(100" + id + "1);" }, [
					])
				])
			]),
			Builder.node('div', { id: 'popupbutton100' + id + '2', style: 'display:none;position:absolute;width:24px;height:25px;left:222px;top:0px;' }, [
				Builder.node('a', { id: 'tooltip100' + id + '2', href: '#', title: "<div id='ToolTipTextWrap'>Click to remove from favorites</div>", className: 'addToolTip', onclick: "$('popupbutton100" + id + "2').style.display = 'none'; if (mypage != 'My Gallery') $('popupbutton100" + id + "1').style.display = 'block' ;ajax_request_remove_from_favorites(" + imageset[id] + "); Effect.Appear('popup_defave'); return false;" }, [
					Builder.node('img', { width: 24, height: 25, border: 0, src: "imageset__records/button_star_on.PNG", onmouseover: "javascript:downOpacity(100" + id + "2);", onmouseout: "javascript:upOpacity(100" + id + "2);" }, [
					])
				])
			]),
			Builder.node('div', { id: 'popupbutton100' + id + '3', style: 'display:none;position:absolute;width:24px;height:25px;left:222px;top:0px;' }, [
				Builder.node('a', { id: 'tooltip100' + id + '3', href: '#', title: "<div id='ToolTipTextWrap'>Sign in to select as a favorite.</div>", className: 'addToolTip' }, [
					Builder.node('img', { width: 24, height: 25, border: 0, src: "imageset__records/button_star.PNG" }, [
					])
				])
			])
		]),
		Builder.node('div', { style: 'position:absolute;width:112px;height:129px;left:18px;top:41px;' }, [
			Builder.node('b', {}, [
				Builder.node('a', { href: 'explore_collection.aspx?shelltarget=' + shellObj['shellid'], id: "view_details" + id }, [
				]),
				Builder.node('br'),
				Builder.node('br'),
				'Created by:',
				Builder.node('br'),
				Builder.node('a', { id: 'creator_' + id, href: 'my_gallery.aspx?profile=' + shellObj['userid'] }, [
				]),
				Builder.node('br'),
				Builder.node('br'),
				'View Count: ' + shellObj['viewcount'],
				Builder.node('br')
			])
		]),
		Builder.node('div', { style: 'position:absolute;width:149px;height:130px;left:130px;top:41px;' }, [
			Builder.node('a', { href: '#', onclick: "$('popup_show_phone_shell_image').src = 'http://cache.sidekick.com/gallery/pics/" + imageset[id] + "b.gif'; $('popup_show_phone_shell_name').innerHTML = '<!-- mp_trans_disable_start -->" + imagename.replace(/\'/gi, "&#39;").replace(/\"/gi, "&#34;") + "<!-- mp_trans_disable_end -->'; $('popup_show_phone_shell_id').value = " + imageset[id] + "; Effect.Appear('popup_show_phone', { from: 0, to: .9}); return false;" }, [
				Builder.node('img', { width: 130, height: 130, border: 0, src: "http://cache.sidekick.com/gallery/pics/" + imageset[id] + "c.gif" }, [
				]),
				Builder.node('img', { width: 19, height: 19, border: 0, style: "margin-left: -5px;", src: "imageset__records/plus_sign.gif" }, [
				])
			]),
		]),
		Builder.node('div', { style: 'position:absolute;width:246px;height:29px;left:15px;top:170px;' }, [
			Builder.node('img', { width: 246, height: 29, border: 0, src: "imageset__records/record_instructions.PNG" }, [
			])
		]),
		Builder.node('div', { style: 'position:absolute;width:246px;height:86px;left:11px;top:199px;' }, [
			Builder.node('div', { style: 'position:absolute;width:197px;height:15px;left:7px;top:0px;' }, [
				Builder.node('b', { style: 'color: #d8157e', id: "continuum" + id }, [
				])
			]),
			Builder.node('div', { style: 'position:absolute;width:194px;height:23px;left:10px;top:15px;' }, [
				Builder.node('div', { style: 'position:absolute; left: 3px; width: 160px; height: 3px; top: 5px;', id: "redfill" + id }, [
				]),
				Builder.node('div', { id: "track" + id, style: 'width:172px; height:9px; background: transparent url(imageset__slider/slider-images-track-right.png) no-repeat top right;' }, [
				 	Builder.node('div', { id: "track" + id + "-left", style: 'position: absolute; width: 5px; height: 9px; background: transparent url(imageset__slider/slider-images-track-left.png) no-repeat top left;' }, [
				 	]),
				 	Builder.node('div', { id: "handle" + id, style: 'width:19px; height:20px;' }, [
				 		Builder.node('img', { width: 19, height: 20, border: 0, src: "imageset__slider/slider-images-handle.gif", style: 'float: left;' }, [
				 		]),
				 		Builder.node('input', { type: 'hidden', id: 'shellrating' + id, value: shellObj['rating'], name: 'shellrating' + id }, [
				 		])
				 	])
				])
			]),
			Builder.node('div', { id: 'popupbutton100' + id + '4', style: 'position:absolute;width:50px;height:23px;left:204px;top:15px;' }, [
				Builder.node('a', { id: 'SK_COM_Gallery_vote_' + id, href: '#', onclick: "ajax_request_vote(" + imageset[id] + ", $('shellrating" + id + "').value); Effect.Fade('votebutton" + id + "'); Effect.Appear('popup_feedback_thank'); trackEvent(this); return false;" }, [
					Builder.node('img', { id: 'votebutton' + id, width: 50, height: 23, border: 0, src: "imageset__records/button_vote.PNG", onmouseover: "javascript:downOpacity(100" + id + "4);", onmouseout: "javascript:upOpacity(100" + id + "4);" }, [
					])
				])
			]),
			Builder.node('div', { id: 'votecount' + id, style: 'position:absolute;width:197px;height:15px;left:7px;top:43px;' }, [
					Builder.node('b', { style: 'color: #d8157e' }, [
						'Current rating '
					]),
					'(Total votes: ' + shellObj['votecount'] + ')'
			]),
			Builder.node('div', { style: 'position:absolute;width:182px;height:24px;left:5px;top:58px;' }, [
				Builder.node('img', { width: 182, height: 24, border: 0, src: "imageset__records/slider_gray.PNG", style: 'float:left;' }, [
				])
			]),
			Builder.node('div', { style: 'position:absolute; left: 12px; width: 160px; height: 5px; top: 68px;', id: "grayfill" + id }, [
			])
		])
	]);

    $(container).insert(element);
    var voted_on = get_cookie('voted_on');
    if (voted_on == null) voted_on = '';
    if (voted_on.indexOf('[' + imageset[id] + ']') >= 0) $('popupbutton100' + id + '4').style.display = 'none';

    $('view_details' + id).innerHTML = "View details &raquo;";
    $('continuum' + id).innerHTML = "Not feeling it &nbsp; &nbsp; Dig it &nbsp; &nbsp; Nailed it";
    $('redfill' + id).innerHTML = '<table width="160" height="3" cellspacing="0" cellpadding="0"><tr><td id="rewrap' + id + '" bgcolor="#d8157e" height="3" width="' + parseInt(parseInt(shellObj['rating']) * 160 / 100) + '"></td><td id="unwrap' + id + '" width="' + (160 - parseInt(parseInt(shellObj['rating']) * 160 / 100)) + '"></td></tr></table>';
    $('grayfill' + id).innerHTML = '<table width="160" height="5" cellspacing="0" cellpadding="0"><tr><td id="rewrapx' + id + '" bgcolor="#666666" height="3" width="' + parseInt(parseInt(shellObj['rating']) * 160 / 100 + 4) + '"></td><td id="unwrapx' + id + '" width="' + (160 - parseInt(parseInt(shellObj['rating']) * 160 / 100 + 4)) + '"></td></tr></table>';
    $('creator_' + id).innerHTML = '<!-- mp_trans_disable_start -->' + shellObj['creator'] + '<!-- mp_trans_disable_end -->';

    if (shellObj['shellname'].length > 20) {
        $('shellname_' + id).innerHTML = '<!-- mp_trans_disable_start -->' + shellObj['shellname'].substring(0, 18) + '...<!-- mp_trans_disable_end -->';
    }
    else {
        $('shellname_' + id).innerHTML = '<!-- mp_trans_disable_start -->' + shellObj['shellname'] + '<!-- mp_trans_disable_end -->';
    }

    $('tooltip100' + id + '1').setAttribute("tooltiptext", $('tooltip100' + id + '1').title);
    $('tooltip100' + id + '1').removeAttribute("title");
    $('tooltip100' + id + '1').onmouseover = function gomouseover() { ddrivetip(this.getAttribute("tooltiptext")) };
    $('tooltip100' + id + '1').onmouseout = function gomouseout() { hideddrivetip(); };

    $('tooltip100' + id + '2').setAttribute("tooltiptext", $('tooltip100' + id + '2').title);
    $('tooltip100' + id + '2').removeAttribute("title");
    $('tooltip100' + id + '2').onmouseover = function gomouseover() { ddrivetip(this.getAttribute("tooltiptext")) };
    $('tooltip100' + id + '2').onmouseout = function gomouseout() { hideddrivetip(); };

    $('tooltip100' + id + '3').setAttribute("tooltiptext", $('tooltip100' + id + '3').title);
    $('tooltip100' + id + '3').removeAttribute("title");
    $('tooltip100' + id + '3').onmouseover = function gomouseover() { ddrivetip(this.getAttribute("tooltiptext")) };
    $('tooltip100' + id + '3').onmouseout = function gomouseout() { hideddrivetip(); };

    make_slider(id, shellObj['rating'] / 100);
}

function make_slider(id, slider_value) {
    // <![CDATA[
    new Control.Slider('handle' + id, 'track' + id, {
        sliderValue: slider_value,
        onSlide: function(v) { $('rewrap' + id).style.width = parseInt(v * 160) + 'px'; $('unwrap' + id).style.width = (160 - parseInt(v * 160)) + 'px'; $('shellrating' + id).value = parseInt(v * 100); },
        onChange: function(v) { $('rewrap' + id).style.width = parseInt(v * 160) + 'px'; $('unwrap' + id).style.width = (160 - parseInt(v * 160)) + 'px'; $('shellrating' + id).value = parseInt(v * 100); }
    });
    // ]]>	
}

function read_cookie(ckname) {
    var nameEQ = ckname + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return null;
}

function get_cookie(ckname) {
    var nameEQ = ckname + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return null;
}

function set_greeting() {
    document.getElementById("greeting").innerHTML = 'Hello <!-- mp_trans_disable_start -->' + read_cookie('nickname') + '<!-- mp_trans_disable_end --> : ';
}

function set_gallery_title(nickname) {
    if (nickname.length > 0) {
        document.getElementById("gallery_title").innerHTML = '<!-- mp_trans_disable_start -->' + nickname + '<!-- mp_trans_disable_end -->\'s Gallery';
        document.getElementById("whose_gallery").innerHTML = '<b><!-- mp_trans_disable_start -->' + nickname + '<!-- mp_trans_disable_end -->\'s Gallery</b>';
    }
    else {
        document.getElementById("gallery_title").innerHTML = 'My Gallery';
        document.getElementById("whose_gallery").innerHTML = '<b>My Gallery</b>';
    }
}

function ajax_request_vote(shellid, rating) {
    var sessionid = read_cookie('sessionid');
    if (sessionid == '' || sessionid == null) sessionid = '0';

    // alert (shellid + '\n' + sessionid + '\n' + rating);
    var request = new Ajax.Request(
		"json/vote_on_shell.aspx",
		{
		    method: 'post',
		    parameters: 'shellid=' + shellid + '&sessionid=' + sessionid + '&vote=' + rating,
		    onComplete: callback_vote
		}
	);
}

function callback_vote(transport) {
    var response = transport.responseText;
    var jsonObj = eval("(" + response + ")");

    // alert('voted');
    refresh_vote_count(jsonObj.shellid, jsonObj.shellrating, jsonObj.shellvotecount);
}

function ajax_request_vote_main(rating) {
    var sessionid = read_cookie('sessionid');
    if (sessionid == '' || sessionid == null) sessionid = '0';

    // alert (shellid + '\n' + sessionid + '\n' + rating);
    var request = new Ajax.Request(
		"json/vote_on_shell.aspx",
		{
		    method: 'post',
		    parameters: 'shellid=' + imagenumber + '&sessionid=' + sessionid + '&vote=' + rating,
		    onComplete: callback_vote_main
		}
	);
}

function callback_vote_main(transport) {
    var response = transport.responseText;
    var jsonObj = eval("(" + response + ")");

    // alert('voted');
    refresh_vote_count_main(jsonObj.shellrating, jsonObj.shellvotecount);
}

function ajax_request_flag() {
    var sessionid = read_cookie('sessionid');
    if (sessionid == '' || sessionid == null) sessionid = '0';

    var complaint = 0;
    if ($('flag_technical').checked == true) complaint = complaint + 1;
    if ($('flag_offensive_imagery').checked == true) complaint = complaint + 2;
    if ($('flag_spam').checked == true) complaint = complaint + 4;
    if ($('flag_offensive_language').checked == true) complaint = complaint + 8;
    if ($('flag_copyrighted_material').checked == true) complaint = complaint + 16;

    var request = new Ajax.Request(
		"json/flag_shell.aspx",
		{
		    method: 'post',
		    parameters: 'shellid=' + imagenumber + '&complaint=' + complaint + '&sessionid=' + sessionid,
		    onComplete: callback_flag
		}
	);
}

function callback_flag(transport) {
    var response = transport.responseText;
    var jsonObj = eval("(" + response + ")");
}

function ajax_request_share_email() {
    var emailfrom = $('eshare_from').value;
    var emailto = $('eshare_to').value;
    var msg = $('eshare_message').value;

    var request = new Ajax.Request(
		"json/share_shell_via_email.aspx",
		{
		    method: 'post',
		    parameters: 'shellid=' + imagenumber + '&from=' + escape(emailfrom) + '&to=' + escape(emailto) + '&msg=' + escape(msg),
		    onComplete: callback_share_email
		}
	);
}

function callback_share_email(transport) {
    var response = transport.responseText;
    var jsonObj = eval("(" + response + ")");

    Effect.Fade('popup_share_email');
    Effect.Appear('popup_share_thanks');
}

function ajax_request_put_shell_name(shellid, shellname) {
    var request = new Ajax.Request
	(
		"json/put_shell_name.aspx",
		{
		    method: 'post',
		    parameters: 'shellid=' + shellid + '&shellname=' + shellname,
		    onComplete: callback_put_shell_name
		}
	);
}

function callback_put_shell_name(transport) {
    var response = transport.responseText;
    var jsonObj = eval("(" + response + ")");

    $('main_shell_name_input').value = jsonObj.shellname;
    $('read_name_0').innerHTML = '<!-- mp_trans_disable_start -->' + jsonObj.shellname + '<!-- mp_trans_disable_end -->';
    $('dropdown_share_menu').style.left = (17 + parseInt($('tag_0').offsetWidth)) + 'px';
}

function ajax_request_get_tags(shellid) {
    var request = new Ajax.Request
	(
		"json/get_tags.aspx",
		{
		    method: 'post',
		    parameters: 'shellid=' + shellid,
		    onComplete: callback_get_tags
		}
	);
}

function callback_get_tags(transport) {
    var response = transport.responseText;
    var jsonObj = eval("(" + response + ")");
    var shelltags = jsonObj.tags;
    var i;

    for (i = 0; i < shelltags.length; i++) {
        add_new_tag(nexttag, shelltags[i], jsonObj.isowner);
        nexttag = nexttag + 1;
    }
}

function ajax_request_all_my_shells(sessionid, profile) {
    $('viewall').style.display = 'none';
    if (sessionid == '' || sessionid == null) sessionid = '0';
    var request = new Ajax.Request
	(
		"json/my_shells_more.aspx",
		{
		    method: 'post',
		    parameters: 'sessionid=' + sessionid + '&profile=' + profile,
		    onComplete: callback_my_shells
		}
	);
}

function ajax_request_my_shells(sessionid, profile) {
    $('viewall').style.display = 'none';
    if (sessionid == '' || sessionid == null) sessionid = '0';
    var request = new Ajax.Request
	(
		"json/my_shells.aspx",
		{
		    method: 'post',
		    parameters: 'sessionid=' + sessionid + '&profile=' + profile,
		    onComplete: callback_my_shells
		}
	);
}

function callback_my_shells(transport) {
    var response = transport.responseText;
    var jsonObj = eval("(" + response + ")");

    // alert(response);

    var rec0 = jsonObj.shellinfo0.length;
    var rec1 = jsonObj.shellinfo1.length;
    var favrecords = jsonObj.favrecords;

    if (favrecords > 12) {
        $('viewall').style.display = 'inline';
    }

    records = parseInt(rec0) + parseInt(rec1);

    var i;
    var j;
    var k;
    var m;

    k = 0;
    m = 0;
    var shellObj = new Array();

    // alert(records);
    for (i = 0; i < records; i++) {
        j = 1 + i;

        if (i < rec0) {
            shellObj['shellname'] = jsonObj.shellinfo0[i].shellname;
            shellObj['creator'] = jsonObj.shellinfo0[i].nickname;
            shellObj['userid'] = jsonObj.shellinfo0[i].userid;
            shellObj['viewcount'] = jsonObj.shellinfo0[i].shellviewcount;
            shellObj['rating'] = jsonObj.shellinfo0[i].shellrating;
            shellObj['votecount'] = jsonObj.shellinfo0[i].shellvotecount;
            shellObj['shellid'] = jsonObj.shellinfo0[i].shellid;
            imageset[j] = jsonObj.shellinfo0[i].shellid;

            add_new_record(j, shellObj, (m % 3) * 300 + 15, parseInt(m / 3) * 300, jsonObj.shellinfo0[i].shellname, 'recordset');
            m++;
        }
        else {
            shellObj['shellname'] = jsonObj.shellinfo1[k].shellname;
            shellObj['creator'] = jsonObj.shellinfo1[k].nickname;
            shellObj['userid'] = jsonObj.shellinfo1[k].userid;
            shellObj['viewcount'] = jsonObj.shellinfo1[k].shellviewcount;
            shellObj['rating'] = jsonObj.shellinfo1[k].shellrating;
            shellObj['votecount'] = jsonObj.shellinfo1[k].shellvotecount;
            shellObj['shellid'] = jsonObj.shellinfo1[k].shellid;
            imageset[j] = jsonObj.shellinfo1[k].shellid;

            add_new_record(j, shellObj, (k % 3) * 300 + 15, parseInt(k / 3) * 300 + parseInt((m - 1) / 3) * 300 + 3, jsonObj.shellinfo1[k].shellname, 'slice_myfavs_content');
            k++;
        }

        if (m > 3) {
            $('slice_myfavs_cta').style.top = (544 + (parseInt((m - 1) / 3) * 300)) + 'px';
            $('slice_myfavs_header').style.top = (552 + (parseInt((m - 1) / 3) * 300)) + 'px';
            $('horizontal_rule').style.top = (502 + (parseInt((m - 1) / 3) * 300)) + 'px';
        }

        $('popupbutton100' + j + '3').style.display = 'none';
        $('popupbutton100' + j + '2').style.display = 'none';
        $('popupbutton100' + j + '1').style.display = 'none';

        if (read_cookie('sessionid') == null || read_cookie('sessionid') == '') {
            $('popupbutton100' + j + '3').style.display = 'block';
        }
        else {
            if (i < rec0) {
                if (jsonObj.shellinfo0[i].fav > 0) {
                    $('popupbutton100' + j + '2').style.display = 'block';
                }
                else {
                    $('popupbutton100' + j + '1').style.display = 'block';
                }
            }
            else {
                if (jsonObj.shellinfo1[k - 1].fav > 0) {
                    $('popupbutton100' + j + '2').style.display = 'block';
                }
                else {
                    $('popupbutton100' + j + '1').style.display = 'block';
                }
            }
        }

        favrecs = k;
    }

    if (m > 0) {
        set_gallery_title(jsonObj.shellinfo0[0].nickname);
        $('myshells_header_title_text').innerHTML = "<h4><b>" + jsonObj.shellinfo0[0].nickname + "\'s Shells</b></h4>";
        $('myfavs_header_title_text').innerHTML = "<h4><b>" + jsonObj.shellinfo0[0].nickname + "\'s Favorites</b></h4>";
    }
    else {
        set_gallery_title(get_cookie('nickname'));
        $('myshells_header_title_text').innerHTML = "<h4><b>" + get_cookie('nickname') + "\'s Shells</b></h4>";
        $('myfavs_header_title_text').innerHTML = "<h4><b>" + get_cookie('nickname') + "\'s Favorites</b></h4>";
    }

    $('slice_myshells_start').style.display = 'none';
    $('slice_myfavs_start').style.display = 'none';
    $('slice_myshells_cta').style.display = 'none';
    $('slice_myshells_header').style.display = 'none';
    $('recordset').style.display = 'none';
    $('horizontal_rule').style.display = 'none';
    $('slice_myfavs_cta').style.display = 'none';
    $('slice_yourfavs_cta').style.display = 'none';
    $('slice_myfavs_header').style.display = 'none';
    $('slice_myfavs_content').style.display = 'none';

    // alert ('k' + k + 'm' + m);

    if (k > 0 && m == 0) {
        $('slice_myshells_cta').style.display = 'block';
        $('horizontal_rule').style.display = 'block';
        $('slice_myfavs_header').style.display = 'block';
        $('slice_myfavs_content').style.display = 'block';
    }
    else if (k == 0 && m > 0) {
        $('slice_myshells_header').style.display = 'block';
        $('recordset').style.display = 'block';

        if (jsonObj.shellinfo0[0].nickname == get_cookie('nickname')) {
            $('slice_myfavs_cta').style.display = 'block';
        }
        else {
            $('slice_yourfavs_cta').style.display = 'block';
        }
    }
    else if (k > 0 && m > 0) {
        $('slice_myshells_header').style.display = 'block';
        $('recordset').style.display = 'block';
        $('horizontal_rule').style.display = 'block';
        $('slice_myfavs_header').style.display = 'block';
        $('slice_myfavs_content').style.display = 'block';
    }
    else {
        // alert('here');
        $('slice_myshells_start').style.display = 'block';
        $('slice_myfavs_start').style.display = 'block';
    }

    // if (k == 0) { k = 1; }
    // if (m == 0) { m = 1; }
    $('universal_nav_footer').style.top = (880 + (parseInt((k - 1) / 3) * 300) + (parseInt((m - 1) / 3) * 300)) + 'px';

    setHeight(300 * records / 3 * 300);
}

function ajax_request_search_shells(sortby, criteria, sessionid, resultpage) {
    $('nomatch').style.display = 'none';

    if (resultpage == '' || resultpage == null) resultpage = '0';
    if (sessionid == '' || sessionid == null) sessionid = '0';

    var request = new Ajax.Request
	(
		"json/search_shells.aspx",
		{
		    method: 'post',
		    parameters: 'sortby=' + sortby + '&criteria=' + criteria + '&sessionid=' + sessionid + '&page=' + resultpage,
		    onComplete: callback_search_shells
		}
	);
}

function callback_search_shells(transport) {
    var response = transport.responseText;
    var jsonObj = eval("(" + response + ")");

    // alert(jsonObj.shellinfo.length);
    records = jsonObj.shellinfo.length;

    if (records == 0) {
        $('nomatch').style.display = 'block';
    }
	setHeight(120 + (records * 100) + 100);
	
    $('of').innerHTML = '<b>' + (parseInt(jsonObj.page) + 1) + ' of ' + (1 + parseInt((jsonObj.cnt - 1) / 15)) + '</b>';
    $('bottom_of').innerHTML = '<b>' + (parseInt(jsonObj.page) + 1) + ' of ' + (1 + parseInt((jsonObj.cnt - 1) / 15)) + '</b>';
    maxpagenumber = (1 + parseInt((jsonObj.cnt - 1) / 15));

    var i;
    var j;
    var shellObj = new Array();
    for (i = 0; i < records; i++) {
        j = 1 + i;

        // alert(jsonObj.shellinfo[i].shellname);
        shellObj['shellname'] = jsonObj.shellinfo[i].shellname;
        shellObj['creator'] = jsonObj.shellinfo[i].nickname;
        shellObj['viewcount'] = jsonObj.shellinfo[i].shellviewcount;
        shellObj['rating'] = jsonObj.shellinfo[i].shellrating;
        shellObj['votecount'] = jsonObj.shellinfo[i].shellvotecount;
        shellObj['shellid'] = jsonObj.shellinfo[i].shellid;
        shellObj['userid'] = jsonObj.shellinfo[i].userid;

        imageset[j] = jsonObj.shellinfo[i].shellid;
        add_new_record(j, shellObj, (i % 3) * 300 + 15, parseInt(i / 3) * 300, jsonObj.shellinfo[i].shellname, 'recordset');

        $('popupbutton100' + j + '1').style.display = 'none';
        $('popupbutton100' + j + '2').style.display = 'none';
        $('popupbutton100' + j + '3').style.display = 'none';

        if (read_cookie('sessionid') == null || read_cookie('sessionid') == '') {
            $('popupbutton100' + j + '3').style.display = 'block';
        }
        else {
            if (jsonObj.shellinfo[i].fav > 0) {
                $('popupbutton100' + j + '2').style.display = 'block';
            }
            else {
                $('popupbutton100' + j + '1').style.display = 'block';
            }
        }
    }

    // if (i < 1) { i = 1; }
    $('bottom_pagination').style.top = (470 + (parseInt((i - 1) / 3) * 300)) + 'px';
    $('universal_nav_footer').style.top = (490 + (parseInt((i - 1) / 3) * 300)) + 'px';
    
}

document.observe("dom:loaded", function() {
    setHeight();
    //alert("hi");
});
var useSetHeight = true;
function setHeight(height) {
    if (useSetHeight) {
        //alert("setHeight::height = " + height);
        if (height == undefined) {
            height = Math.max(800, getPageSizeWithScroll()[1]);
        }
        parent.setIframeHeight(height);
    }
}

function getPageSizeWithScroll() {
    if (window.innerHeight && window.scrollMaxY) {// Firefox
        yWithScroll = window.innerHeight + window.scrollMaxY;
        xWithScroll = window.innerWidth + window.scrollMaxX;
    } else if (document.body.scrollHeight > document.body.offsetHeight) { // all but Explorer Mac
        yWithScroll = document.body.scrollHeight;
        xWithScroll = document.body.scrollWidth;
    } else { // works in Explorer 6 Strict, Mozilla (not FF) and Safari
        yWithScroll = document.body.offsetHeight;
        xWithScroll = document.body.offsetWidth;
    }
    arrayPageSizeWithScroll = new Array(xWithScroll, yWithScroll);
    //alert( 'The height is ' + yWithScroll + ' and the width is ' + xWithScroll );
    return arrayPageSizeWithScroll;
}



function ajax_request_get_shell_info_main(shellid) {
    var sessionid = read_cookie('sessionid');
    if (sessionid == '' || sessionid == null) sessionid = '0';

    var request = new Ajax.Request
	(
		"json/get_shell_info.aspx",
		{
		    method: 'post',
		    parameters: 'sessionid=' + sessionid + '&shellid=' + shellid,
		    onComplete: callback_get_shell_info_main
		}
	);
}

function callback_get_shell_info_main(transport) {
    var response = transport.responseText;
    var jsonObj = eval("(" + response + ")");
    var shelltags = jsonObj.tags;
    var i;

    var sessionid = read_cookie('sessionid');
    if (sessionid == '' || sessionid == null) sessionid = '0';

    // document.getElementById("main_shell_name").innerHTML = jsonObj.shellname; 	
    // document.getElementById("main_shell_name_public").innerHTML = jsonObj.shellname; 

    $('main_shell_name_input').value = jsonObj.shellname;
    $('main_shell_name').innerHTML = '<!-- mp_trans_disable_start -->' + jsonObj.shellname + '<!-- mp_trans_disable_end -->';
    $('whose_gallery').innerHTML = '<a href="my_gallery.aspx?profile=' + jsonObj.userid + '"><!-- mp_trans_disable_start -->' + jsonObj.nickname + '<!-- mp_trans_disable_end -->\'s Gallery</a>';
    $('whose_shells').innerHTML = '<h2><b><!-- mp_trans_disable_start -->' + jsonObj.nickname + '<!-- mp_trans_disable_end -->\'s Shells</b></h2>';
    $('which_shell').innerHTML = '<b><!-- mp_trans_disable_start -->' + jsonObj.shellname + '<!-- mp_trans_disable_end --></b>';
    $('main_shell_vote_count').innerHTML = '<b>Current rating</b> (Total votes: ' + jsonObj.shellvotecount + ')';
    $('main_shell_userid').innerHTML = '<a href="my_gallery.aspx?profile=' + jsonObj.userid + '"><!-- mp_trans_disable_start -->' + jsonObj.nickname + '<!-- mp_trans_disable_end --></a>';
    $('secondary_header_with_contents').innerHTML = "<h3><b>More shells from <!-- mp_trans_disable_start -->" + jsonObj.nickname + "<!-- mp_trans_disable_end --></b></h3>";
    $('secondary_header_without_contents').innerHTML = "<h3><b>More shells from <!-- mp_trans_disable_start -->" + jsonObj.nickname + "<!-- mp_trans_disable_end --> coming soon!</b></h3>";

    if (jsonObj.isowner) {
        // $('tag_0_public').style.display = 'none';
        $('dropdown').style.display = 'inline';
        $('addnewtag').style.display = 'inline';
        $owner = jsonObj.isowner;
    }
    else {
        $('tag_0').innerHTML = "<h2><b><!-- mp_trans_disable_start -->" + jsonObj.shellname + "<!-- mp_trans_disable_end --></b></h2>";
    }

    $('tag_0').style.display = 'inline';

    if (sessionid == 0 || sessionid == '0') {
        $('popupbutton4').style.display = 'none';
        $('popupbutton8').style.display = 'none';
        $('popupbutton9').style.display = 'block';
    }
    else {
        if (jsonObj.fav > 0) {
            $('popupbutton4').style.display = 'block';
            $('popupbutton8').style.display = 'none';
            $('popupbutton9').style.display = 'none';
        }
        else {
            $('popupbutton8').style.display = 'block';
            $('popupbutton4').style.display = 'none';
            $('popupbutton9').style.display = 'none';
        }
    }

    $('rewrap0').style.width = parseInt(jsonObj.shellrating / 100 * 160) + 'px';
    $('unwrap0').style.width = (160 - parseInt(jsonObj.shellrating / 100 * 160)) + 'px';
    $('rewrapx0').style.width = parseInt(jsonObj.shellrating / 100 * 160 + 4) + 'px';
    $('unwrapx0').style.width = (160 - parseInt(jsonObj.shellrating / 100 * 160 + 4)) + 'px';

    make_slider(0, jsonObj.shellrating / 100);

    // $('tooltip_0').setAttribute("tooltiptext", $('tooltip_0').title);
    // $('tooltip_0').removeAttribute("title");
    // $('tooltip_0').onmouseover = function gomouseover(){ddrivetip(this.getAttribute("tooltiptext"))};
    // $('tooltip_0').onmouseout = function gomouseout(){hideddrivetip();};

    for (i = 0; i < shelltags.length; i++) {
        add_new_tag(nexttag, shelltags[i], jsonObj.isowner);
        nexttag = nexttag + 1;
    }

    // alert((29 + parseInt($('tag_0').offsetWidth)) + 'px');
    $('dropdown_share_menu').style.left = (17 + parseInt($('tag_0').offsetWidth)) + 'px';

    records = jsonObj.shellinfo.length;
    if (records > 0) {
        $('ghostshells0').style.display = 'none';
        $('secondary_header_without_contents').style.display = 'none';
        $('secondary_header_with_contents').style.display = 'block';
    }

    if (records > 1) {
        $('ghostset').style.display = 'none';
        $('ghostshells1').style.display = 'none';
    }

    if (records > 2) {
        $('secondary_cta').style.display = 'none';
    }

    var j;
    var shellObj = new Array();
    for (i = 0; i < records; i++) {
        j = 1 + i;

        //  alert(jsonObj.shellinfo[i].shellname);

        shellObj['shellname'] = jsonObj.shellinfo[i].shellname;
        shellObj['creator'] = jsonObj.shellinfo[i].nickname;
        shellObj['viewcount'] = jsonObj.shellinfo[i].shellviewcount;
        shellObj['rating'] = jsonObj.shellinfo[i].shellrating;
        shellObj['votecount'] = jsonObj.shellinfo[i].shellvotecount;
        shellObj['shellid'] = jsonObj.shellinfo[i].shellid;
        shellObj['userid'] = jsonObj.shellinfo[i].userid;

        imageset[j] = jsonObj.shellinfo[i].shellid;
        add_new_record(j, shellObj, (i % 3) * 300, parseInt(i / 3) * 300, jsonObj.shellinfo[i].shellname, 'recordset');

        $('popupbutton100' + j + '1').style.display = 'none';
        $('popupbutton100' + j + '2').style.display = 'none';
        $('popupbutton100' + j + '3').style.display = 'none';

        if (read_cookie('sessionid') == null || read_cookie('sessionid') == '') {
            $('popupbutton100' + j + '3').style.display = 'block';
        }
        else {
            if (jsonObj.shellinfo[i].fav > 0) {
                $('popupbutton100' + j + '2').style.display = 'block';
            }
            else {
                $('popupbutton100' + j + '1').style.display = 'block';
            }
        }
    }

    // if (i < 1) { i = 1; }
    $('universal_nav_footer').style.top = (800 + (parseInt((i - 1) / 3) * 300)) + 'px';
	setHeight(300 * records / 3 * 300);
}

function ajax_request_register(nickname, email, es, password, password_confirm) {
    var pattern = new RegExp("@");

    // alert('nickname=' + nickname + '&email=' + email + '&es=' + es + '&password=' + password);

    if (nickname == '') {
        alert('Email cannot be blank.');
        return false;
    }

    if (email == '') {
        alert('Email cannot be blank.');
        return false;
    }

    if (!pattern.test(email)) {
        alert('Email address is invalid.');
        return false;
    }

    if (password == '') {
        alert('Password cannot be blank.');
        return false;
    }

    if (password.length < 5) {
        alert('Password must be at least 5 characters long.');
        return false;
    }

    if (password != password_confirm) {
        alert('Password and comfirm password do not match.');
        return false;
    }

    var request = new Ajax.Request
	(
		"json/register.aspx",
		{
		    method: 'post',
		    parameters: 'nickname=' + nickname + '&email=' + email + '&es=' + es + '&password=' + password,
		    onComplete: callback_register
		}
	);
}

function callback_register(transport) {
    var response = transport.responseText;

    // alert(response);

    var jsonObj = eval("(" + response + ")");

    if (jsonObj.used_nickname > 0 && jsonObj.used_email > 0) {
        alert('Sorry, but both the nickname and the email have already been taken.');
    }

    if (jsonObj.used_nickname > 0 && jsonObj.used_email == 0) {
        alert('Sorry, but that nickname has already been taken.');
    }

    if (jsonObj.used_nickname == 0 && jsonObj.used_email > 0) {
        alert('Sorry, but that email address has already been taken.');
    }

    if (jsonObj.used_nickname == 0 && jsonObj.used_email == 0) {
        Effect.Fade('popup_create');
        Effect.Appear('popup_confirm');
    }
}

function ajax_request_edit(nickname, email, es, password_current, password_new) {
    var pattern = new RegExp("@");

    if (nickname == '') {
        alert('Nickname cannot be blank.');
        return false;
    }

    if (email == '') {
        alert('Email cannot be blank.');
        return false;
    }

    if (!pattern.test(email)) {
        alert('Email address is invalid.');
        return false;
    }

    if (password_new != '') {
        if (password_new.length < 5) {
            alert('New password must be at least 5 characters long.');
            return false;
        }

        if (password_current == '') {
            alert('Current password cannot be blank.');
            return false;
        }
    }

    var request = new Ajax.Request
	(
		"json/edit_profile.aspx",
		{
		    method: 'post',
		    parameters: 'nickname=' + nickname + '&email=' + email + '&es=' + es + '&password_current=' + password_current + '&password_new=' + password_new + '&sessionid=' + read_cookie('sessionid'),
		    onComplete: callback_edit
		}
	);
}

function callback_edit(transport) {
    var response = transport.responseText;
    var jsonObj = eval("(" + response + ")");

    if (jsonObj.status == 1) {
        document.cookie = 'email=' + $('form_edit_email').value;
        document.cookie = 'nickname=' + $('form_edit_nickname').value;

        alert('Your profile has been updated');

        if (mypage == 'My Gallery') {
            window.location.reload();
        }
        else {
            window.location = 'my_gallery.aspx';
        }
    }
    else {
        alert(jsonObj.message);
    }
}

function ajax_request_forgot_password(email) {
    // alert(email);

    var pattern = new RegExp("@");

    if (email == '') {
        alert('We\'re sorry, but the e-mail address you entered was not formatted properly. Please verify the address and try again.');
        return false;
    }

    if (!pattern.test(email)) {
        alert('We\'re sorry, but the e-mail address you entered was not formatted properly. Please verify the address and try again.');
        return false;
    }

    var request = new Ajax.Request
	(
		"json/forgot_password.aspx",
		{
		    method: 'post',
		    parameters: 'email=' + email,
		    onComplete: callback_forgot_password
		}
	);

    return true;
}

function callback_forgot_password(transport) {
    var response = transport.responseText;
    var jsonObj = eval("(" + response + ")");

    if (jsonObj.found == 1) {
        alert('Thank you. Your password has been sent to the e-mail address you provided.');
    }
    else {
        alert('We\'re sorry, but the e-mail address you entered does not match any of our profiles. If you entered the wrong address, please try again.');
        Effect.Appear('popup_forgot');
    }
}

function ajax_request_access(email, password) {
    var pattern = new RegExp("@");

    if (email == '') {
        alert('Email cannot be blank.');
        return false;
    }

    if (!pattern.test(email)) {
        alert('Email address is invalid.');
        return false;
    }

    if (password == '') {
        alert('Password cannot be blank.');
        return false;
    }

    var request = new Ajax.Request(
		"json/login.aspx",
		{
		    method: 'post',
		    parameters: 'nickname=' + email + '&password=' + password,
		    onComplete: callback_access
		}
	);

    return true
}

function callback_access(transport) {
    var response = transport.responseText;
    var jsonObj = eval("(" + response + ")");

    if (jsonObj.sessionid != '0') {
        document.cookie = 'sessionid=' + jsonObj.sessionid;
        document.cookie = 'nickname=' + jsonObj.nickname;
        document.cookie = 'gallerystate=' + jsonObj.gallerystate;
        document.cookie = 'email=' + $('form_access_email').value;
        set_greeting();
        $('gallery_nav_guest').style.display = 'none';
        $('gallery_nav_user').style.display = 'block';
        Effect.Fade('popup_access');
        Effect.Fade('modal_overlay');
        window.location = 'my_gallery.aspx';
    }
    else {
        alert('We\'re sorry, but either your e-mail or password is incorrect. Please try again.');
    }
}

function ajax_request_availability_edit(nickname) {
    var request = new Ajax.Request(
		"json/check_availability.aspx",
		{
		    method: 'post',
		    parameters: 'nickname=' + nickname,
		    onComplete: callback_availability_edit
		}
	);
}

function callback_availability_edit(transport) {
    var response = transport.responseText;
    var jsonObj = eval("(" + response + ")");

    if (parseInt(jsonObj.available) > 0) {
        $('form_edit_taken').style.display = 'none';
        Effect.Appear('form_edit_avail');
    }
    else {
        Effect.Appear('form_edit_taken');
        $('form_edit_avail').style.display = 'none';
    }
}

function ajax_request_availability_create(nickname) {
    var request = new Ajax.Request(
		"json/check_availability.aspx",
		{
		    method: 'post',
		    parameters: 'nickname=' + nickname,
		    onComplete: callback_availability_create
		}
	);
}

function callback_availability_create(transport) {
    var response = transport.responseText;
    var jsonObj = eval("(" + response + ")");

    if (parseInt(jsonObj.available) > 0) {
        $('form_msg_taken').style.display = 'none';
        Effect.Appear('form_msg_avail');
    }
    else {
        Effect.Appear('form_msg_taken');
        $('form_msg_avail').style.display = 'none';
    }
}

function clear_cookies() {
    document.cookie = 'sessionid=';
    document.cookie = 'nickname=';
    document.cookie = 'language=';
    document.cookie = 'email=';
    document.cookie = 'gallerystate=';
}

function refresh_vote_count(shellid, rating, votes) {
    var loc = imagehash[shellid];
    $('rewrapx' + loc).style.width = parseInt(rating / 100 * 160 + 4) + 'px';
    $('unwrapx' + loc).style.width = (160 - parseInt(rating / 100 * 160 + 4)) + 'px';
    $('votecount' + loc).innerHTML = "<b style='color: #d8157e;'>Current rating</b> (Total votes: " + votes + ")";

    var existing_votes = get_cookie('voted_on');
    if (existing_votes == null) existing_votes = '';
    document.cookie = 'voted_on=[' + shellid + ']' + existing_votes;
}

function refresh_vote_count_main(rating, votes) {
    $('rewrapx0').style.width = parseInt(rating / 100 * 160 + 4) + 'px';
    $('unwrapx0').style.width = (160 - parseInt(rating / 100 * 160 + 4)) + 'px';
    $('main_shell_vote_count').innerHTML = "<b style='color: #d8157e;'>Current rating</b> (Total votes: " + votes + ")";

    var existing_votes = get_cookie('voted_on');
    if (existing_votes == null) existing_votes = '';
    document.cookie = 'voted_on=[' + shellid + ']' + existing_votes;
}

function reload_my_shells(profile) {
    $('recordset').innerHTML = '';
    $('slice_myfavs_content').innerHTML = '';
    ajax_request_my_shells(read_cookie('sessionid'), profile);
}

function reload_all_my_shells(profile) {
    $('recordset').innerHTML = '';
    $('slice_myfavs_content').innerHTML = '';
    ajax_request_all_my_shells(read_cookie('sessionid'), profile);
}

function make_sure_one_is_checked() {
    if ($('flag_offensive_language').checked == true) return true;
    if ($('flag_offensive_imagery').checked == true) return true;
    if ($('flag_spam').checked == true) return true;
    if ($('flag_copyrighted_material').checked == true) return true;
    if ($('flag_technical').checked == true) return true;

    alert('You must check at least one of the checkboxes to flag this shell.');
    return false;
}

function handlekeydown(e) {
    var keynum;

    if (window.event) // IE
    {
        keynum = e.keyCode;
    }
    else if (e.which) // Firefox
    {
        keynum = e.which;
    }

    if (keynum == 13) {
        reload_search_results();
    }
}

function jumponkeydown(e) {
    var keynum;

    if (window.event) // IE
    {
        keynum = e.keyCode;
    }
    else if (e.which) // Firefox
    {
        keynum = e.which;
    }

    // alert(keynum);

    if (keynum == 13) {
        window.location = 'explore_gallery.aspx?criteria=' + $('criteria').value;
    }
}

function myspace() {
    var T = 'My Sidekick Shell';
    var C = '<a href="http://www.sidekick.com/gallery/explore_collection.aspx?shelltarget=' + imagenumber + '"><img src="http://www.sidekick.com/gallery/pics/' + imagenumber + 'b.gif" /></a>';
    var U = 'http://www.sidekick.com/gallery/explore_collection.aspx?shelltarget=' + imagenumber;
    var L = 5;

    var targetUrl = 'http://www.myspace.com/index.cfm?fuseaction=postto&' + 't=' + encodeURIComponent(T)
    + '&c=' + encodeURIComponent(C) + '&u=' + encodeURIComponent(U) + '&l=' + L;
    window.open(targetUrl);
}

function facebook() {
    var targetUrl = 'http://www.facebook.com/sharer.php?u=http%3A//www.sidekick.com/gallery/explore_collection.aspx%3Fshelltarget%3D' + imagenumber + '&amp;t=My%20Sidekick';
    window.open(targetUrl);
}

function htmEncode(s) {
    var str = new String(s);
    str = str.replace(/&/g, "&amp;");
    str = str.replace(/</g, "&lt;");
    str = str.replace(/>/g, "&gt;");
    str = str.replace(/"/g, "&quot;");
    return str;
} 
