$(document).ready(function(){

$(".js_option").not(".hidden").show();

$("div.comment_container.below_threshold").hide();

// Fade out any element with the "fade_out" class
$(".fade_out").fadeOut(7000);

// Insert "search" text in search box, and remove it when clicked
$("div#search input#search_q").val("Search...");
$("div#search input#search_q").click(function(){
	$(this).val("");
});

// Auto-select text on click for copy/paste textareas
$(".auto-select").click(function(){
	$(this).select();
});

// Make the "ad" class open in new window
$("a.ad").attr("target","_blank");

// This for the for the options page when starting a new poll

max_options = 7;

i = 0;
$("form#form_startpoll2 div.option.field").each(function(){
	var field = $(this);
	i++;

	if( field.find("input.url").val() == "" ){
		field.find("div.url").hide();
	}else{
		field.find("a.add_url").hide();
	}
	
	if( field.find("input.option").val() == "" && i > 3 ){
		field.remove();
	}
	
});

$("form#form_startpoll2").find("a.add_option").click(function(){
	if( $("form#form_startpoll2 div.option.field").length < max_options+1 ){
		$("form#form_startpoll2 div.option.field:last").after( "<div class=\"option field\">"+$("form#form_startpoll2 div.option.field:first").html()+"</div>" );
		register_option_links( $("form#form_startpoll2 div.option.field:last") );
	}
});

function register_option_links( selector ){

	selector.find("a.add_url").click(function(){
		var field = $(this).parents("div.field");
		field.find("a.add_url").hide();
		field.find("div.url").show();
	});
	
	selector.find("a.remove_url").click(function(){
		var field = $(this).parents("div.field");
		field.find("div.url").hide();
		field.find("div.url").find("input").val("");
		field.find("a.add_url").show();
	});
	
	selector.find("a.remove_option").click(function(){
		var field = $(this).parents("div.field");
		field.remove();
	});
	
	selector.find("a.move_up").click(function(){
		var field = $(this).parents("div.field");
		var prev_field = field.prev("div.field.option").not("#option_template");
		if( prev_field.length == 1 ){
			$(prev_field).insertAfter(field);
		}
	});
	
	selector.find("a.move_down").click(function(){
		var field = $(this).parents("div.field");
		var next_field = field.next("div.field.option");
		if( next_field.length == 1 ){
			$(field).insertAfter(next_field);
		}
	});

}

register_option_links( $("form#form_startpoll2 div.option.field") );



// This creates the sub-category menus from the arrays placed on the page when selecting a category on start poll page 1

$("select#f_category").change(function(){
	$("select#f_subcategory").remove();
	$("span.sub_cat_arrow").remove();

	var cat_id = $(this).val();
	
	if( typeof(sub_categories[cat_id]) == "object" ){
		var subcathtml = new Array();
		
		for( key in sub_categories[cat_id] ){
			subcathtml[subcathtml.length] = '<option value="'+sub_categories[cat_id][key][0]+'">'+sub_categories[cat_id][key][1]+'</option>';
		}
		
		$(this).after('<span class="sub_cat_arrow"> &raquo; </span><select name="subcategory" id="f_subcategory"><option value="0">'+L_OPTIONAL_SUBCATEGORY+'</option>'+subcathtml.join()+'</select>');
	}
});

// Similar to above but for the "state" menus on the profile page


$("select#f_location_country").change(function(){
	$("select#f_location_state").remove();
	$("span.sub_cat_arrow").remove();
	
	var country_id = $(this).val();
	
	if( typeof(states[country_id]) == "object" ){
		var statehtml = new Array();
		
		for( key in states[country_id] ){
			statehtml[statehtml.length] = '<option value="'+states[country_id][key][0]+'">'+states[country_id][key][1]+'</option>';
		}
		
		$(this).after('<span class="sub_cat_arrow"><br /> &raquo; </span><select name="location_state" id="f_location_state"><option value="">'+L_STATE+'</option><option value=""></option>'+statehtml.join()+'</select>');
	}
});



// These functions handle poll show results/voting options

function register_poll_links(){
	$("a.show_results").click(function(){
		var poll = $(this).parents("div.poll");
		
		poll.find("div.poll_vote").hide();
		poll.find("div.poll_results").show();
		
		poll.find("a.show_results").hide();
		poll.find("a.show_vote").show();
		
		return(false);
	});
	
	$("a.show_vote").click(function(){
		var poll = $(this).parents("div.poll");
		
		poll.find("div.poll_vote").show();
		poll.find("div.poll_results").hide();
		
		poll.find("a.show_results").show();
		poll.find("a.show_vote").hide();
		
		return(false);
	});
}

register_poll_links();


// This functions shows the fader over a given poll
function show_fader( poll ){
	poll.find("div.poll_results").show().find("div.poll_container").fadeTo(1000, 0.4, function(){
		poll.find("div.make_it_count").fadeIn(500);
		poll.find("div.poll_results").find("div.poll_container, a.close_fader, div.make_it_count").click(function(){
			poll.find("div.poll_container").fadeTo(0, 1.0);
			poll.find("div.make_it_count").hide();
		})
	});
	poll.find("a.show_results").hide();
}



// This function is for voting...

function register_vote_links( selector ){

selector.click(function(){	
	var poll = $(this).parents("div.poll");
	
	poll.find("div.poll_vote").hide();
	
	/* This is the code for when a logged out user can't vote at all */
	if( logged_in == 0 && anon == 0 ){
		show_fader( poll );
		return(false);
	}
	
	poll.find("div.poll_stats").hide();
	
	poll.find("span.tallying_vote").show();

	var option_id = $(this).parents("div.vote_option").find("span.option_id").html();
	var poll_id = poll.find("span.poll_id").html();

	$.ajax({
		type: "POST",
		cache: false,
		dataType: "html",
		data: {
			"ajax": "vote",
			"poll_id": poll_id,
			"option_id": option_id,
			"s": sessi
		},
		success: function(response){
			if( response.substring(0,5) == "ERROR" ){
				//alert( response.split(":",2)[1] );
				poll.find("span.poll_fader_message").html( response.split(":",2)[1] );
				
				poll.find("div.poll_results").show();
				poll.find("div.poll_stats").show();
				poll.find("span.tallying_vote").hide();
				
				show_fader( poll );
			}else{
				poll.replaceWith(response);
				poll.show();
				register_poll_links();
			}
			
			run_censor( $("body") );
		}
	});
	
	return(false);

});

}

register_vote_links( $("a.vote") );




// Comments
$("form.form_post_comment").hide();

function register_comment_link(){

$("a.post_comment").click(function(){
	var comment_post = $(this).next("div.comment_post");
	$(this).hide();
	comment_post.find("form.form_post_comment").show();
	register_comment_post( comment_post.find("form.form_post_comment:first") );
	setbbCode( comment_post.find("a.bbCode") );
	return(false);
});

}

register_comment_link();


// function to show a reply OR edit box
// Pass a comment_container(:first) to it
function reply_edit_box( comment ){
	var comment_id = comment.find("span.comment_id:first").html();
	comment.find("div.clear:first").after("<div class=\"comment_post\" style=\"margin-left: 170px;\">"+$("div#comment_post_template").html()+"</div>");
	var comment_post = comment.find("div.comment_post:first");
	comment_post.find("form.form_post_comment").show();
	comment_post.find("a.post_comment").remove();
	setbbCode( comment_post.find("a.bbCode") );
	comment_post.find("input.f_parent_id").val(comment_id);

	return(comment_post);
}


// Reply link
function register_reply_link( selector ){
	selector.click(function(){
		var comment = $(this).parents("div.comment_container:first");
		comment.find("span.reply_to:first").hide();
		var comment_post = reply_edit_box( comment );
		register_comment_post( comment_post.find("form.form_post_comment") );
		
		return(false);
	});
}

register_reply_link( $("a.reply_link") );

// Comment posting
function register_comment_post( selector ){

selector.find("input.cancel_post_comment").click(function(){
	var comment_post = $(this).parents("div.comment_post:first");
	comment_post.find("form.form_post_comment textarea").val("");
	comment_post.find("form.form_post_comment").hide();
	comment_post.prev("a.post_comment").show();
	
	comment_post.parent("div.comment_container:first").find("span.reply_to:first").show();
	return(false);
});

selector.submit(function(){
	var comment_post = $(this).parents("div.comment_post:first");
	comment_post.find("form.form_post_comment").hide();
	comment_post.find("span.submitting_comment").show();
	
	var parent = comment_post.find("input.f_parent_id").val();
	
	$.ajax({
		type: "POST",
		cache: false,
		dataType: "html",
		data: {
			"ajax": "comment",
			"poll_id": comment_post.find("input.f_poll_id").val(),
			"parent": parent,
			"comment_entry": comment_post.find("textarea.f_comment_entry").val(),
			"s": comment_post.find("input.f_sess").val()
		},
		success: function(response){
			if( response.substring(0,5) == "ERROR" ){
				alert( response.split(":",2)[1] );
				comment_post.find("form.form_post_comment").show();
			}else{
				comment_post.after(response);
				if( parent != 0 ){comment_post.next().removeClass("indent-0");}
				register_reply_link( comment_post.next().find("a.reply_link") );
				register_edit_comment( comment_post.next().find("a.edit_comment") );
				
				comment_post.find("form.form_post_comment textarea").val("");
				comment_post.prev("a.post_comment").show();
				comment_post.parent("div.comment_container:first").find("span.reply_to:first").show();
				
				comment_post.next().find("span.reply_to").hide();
			}
			comment_post.find("span.submitting_comment").hide();
			run_censor( $("body") );
		}
	});
	
	return(false);
});

}

//register_comment_post( $("form.form_post_comment:first") );


// Comment rating
function register_comment_rate( selector ){
	selector.find("a.boost, a.veto").click(function(){
		var comment = $(this).parents("div.comment_container:first");
		comment_id = comment.find("span.comment_id:first").html();
		
		var comment_rate = $(this).parent("span.comment_rate");
		comment_rate.hide();
		
		comment.find("span.rate_comment_wait:first").show();
		
		if( $(this).hasClass("boost") ){
			rate = 1;
		}else{
			rate = -1;
		}
		
		$.ajax({
			type: "POST",
			cache: false,
			dataType: "html",
			data: {
				"ajax": "rate",
				"comment_id": comment_id,
				"rating": rate,
				"s": sessi
			},
			success: function(response){
				comment.find("span.rate_comment_wait:first").hide();
				comment_rate.after( response );
				if( rate == -1 ){
					comment.find("span.report_comment_options:first").show();
				}
			}
		});
		
		return(false);
	});
}

register_comment_rate( $("span.comment_rate") );


// Edit comment link
function register_edit_comment( selector ){
	selector.click(function(){
		var comment = $(this).parents("div.comment_container:first");
		comment.find("span.comment_edit:first").hide();
		var comment_post = reply_edit_box( comment );
		comment_post.find("form.form_post_comment textarea").val( comment.find("textarea.original_comment:first").val() );
		register_edit_post( comment_post.find("form.form_post_comment") );
		
		return(false);
	});
}

register_edit_comment( $("a.edit_comment") );


// Comment editing
function register_edit_post( selector ){

selector.find("input.cancel_post_comment").click(function(){
	var comment_post = $(this).parents("div.comment_post");
	comment_post.find("form.form_post_comment textarea").val("");
	comment_post.find("form.form_post_comment").hide();

	comment_post.parent("div.comment_container:first").find("span.comment_edit:first").show();
	return(false);
});

selector.submit(function(){
	var comment_post = $(this).parents("div.comment_post");
	comment_post.find("form.form_post_comment").hide();
	comment_post.find("span.submitting_comment").show();
	
	var parent = comment_post.find("input.f_parent_id").val();
	
	$.ajax({
		type: "POST",
		cache: false,
		dataType: "html",
		data: {
			"ajax": "edit_comment",
			"comment_id": parent,
			"comment_entry": comment_post.find("textarea.f_comment_entry").val(),
			"s": comment_post.find("input.f_sess").val()
		},
		success: function(response){
			if( response.substring(0,5) == "ERROR" ){
				alert( response.split(":",2)[1] );
				comment_post.find("form.form_post_comment").show();
			}else{
				comment = comment_post.parents("div.comment_container:first");
				comment.find("div.comment_text:first").html(response);
				comment.find("textarea.original_comment:first").val( comment_post.find("textarea.f_comment_entry").val() )
				comment.find("span.comment_edit:first").show();
			}
			comment_post.find("span.submitting_comment").hide();
			run_censor( $("body") );
		}
	});
	
	return(false);
});

}


// Report comment
function register_report_links( selector ){
	selector.find("a.report_comment").click(function(){
		var report_comment = $(this).parents("span.report_comment_options:first");
		report_comment.find("span.report_options").show();
		return(false);
	});
	
	selector.find("a.report_no").click(function(){
		var report_comment = $(this).parents("span.report_comment_options:first");
		report_comment.find("span.report_options").hide();
		return(false);
	});
	
	selector.find("a.report_yes").click(function(){
		var report_comment = $(this).parents("span.report_comment_options:first");
		report_comment.find("span.report_options").hide();
		report_comment.find("span.reporting").show();
		report_comment.find("a.report_comment").hide();
		
		var comment = report_comment.parents("div.comment_container:first");
		var comment_id = comment.find("span.comment_id:first").html();
		
		$.ajax({
			type: "POST",
			cache: false,
			dataType: "html",
			data: {
				"ajax": "report_comment",
				"comment_id": comment_id,
				"s": sessi
			},
			success: function(response){
				report_comment.find("span.reporting").hide();
				report_comment.find("span.reported").show();
			}
		});
		
		return(false);
	});
	
}

register_report_links( $("span.report_comment_options") );

// Show below threshold
function register_threshold_links( selector ){

selector.click(function(){
	var comment_container = $(this).parents("div.comment_container:first");
	comment_container.hide();
	comment_container.next().show();
	return(false);
});

}

register_threshold_links( $("a.show_below_threshold") );


// Fetch more comments!
function register_comment_fetch( selector ){
	selector.click(function(){
		var fetch = $(this).parent("div.fetch_more_comments");
		$(this).hide();
		fetch.find("span.fetching_comments").show();
		
		bits = fetch.find("span.more_comment_ids").html().split(" ");
		
		$.ajax({
			type: "POST",
			cache: false,
			dataType: "html",
			data: {
				"ajax": "fetch_comments",
				"poll_id": bits[0],
				"comment_ids": bits[1]
			},
			success: function(response){
				fetch.after(response);
				
				// Register all the new links on the page
				register_reply_link( fetch.nextAll().find("a.reply_link") );
				register_comment_rate( fetch.nextAll().find("span.comment_rate") );
				register_edit_comment( fetch.nextAll().find("a.edit_comment") );
				register_threshold_links( fetch.nextAll().find("a.show_below_threshold") );
				register_report_links( fetch.nextAll().find("span.report_comment_options") );
				fetch.nextAll().find(".js_option").not(".hidden").show();
				
				fetch.remove();
				register_comment_fetch( $("a.fetch_more_comments") );
				run_censor( $("body") );
			}
		});
		
	});
	
	return(false);
}

register_comment_fetch( $("a.fetch_more_comments") );



// bbCode Help popups
var bbCodeHelpBoxFix=false;
var bbCodeHelpBox=$("div#bbCodeHelpBox");

function setbbCode(on_element){

on_element.hover(function(e){
	if(bbCodeHelpBoxFix==false){
		bbCodeHelpBox.css("display","inline").css("top",(e.pageY-20-bbCodeHelpBox.height())+"px").css("left",(e.pageX-300)+"px");
	}
},function(){
	if(bbCodeHelpBoxFix==false){
		bbCodeHelpBox.css("display","none");
	}
});

on_element.click(function(){
	if(bbCodeHelpBoxFix==true){
		bbCodeHelpBoxFix=false;
		bbCodeHelpBox.css('border','1px solid #aaa').css('padding','1px');
		bbCodeHelpBox.css("display","none");
		bbCodeHelpBox.draggable('disable');
	}else{
		if(bbCodeHelpBox.css('display')=='none'){
			return(false);
		}
		bbCodeHelpBoxFix=true;
		bbCodeHelpBox.css('border','2px solid #888').css('padding','0px');
		bbCodeHelpBox.draggable();
		bbCodeHelpBox.draggable('enable');
	}
	return(false);
});
}

setbbCode( $("form#form_startpoll1 a.bbCode, form#form_edit_profile a.bbCode") );

bbCodeHelpBox.find("a.bbCodeHelpBoxClose").click(function(){
	bbCodeHelpBoxFix=false;
	bbCodeHelpBox.css('border','1px solid #aaa').css('padding','1px');
	bbCodeHelpBox.css("display","none");
	bbCodeHelpBox.draggable('disable');
	return(false);
});




// Let's censor some ****!

function run_censor( selector ){
	if( censor == 1 ){
		selector.find("span.censor").each(function(){
			$(this).replaceWith( $(this).html().replace(/./g, "*") );
		});
	}
}

run_censor( $("body") );



// This function finds a colour darker than (or lighter than if the colour is dark) the given colour
// It's used to provide contrast with a picked customisable colour
function get_contrasting_color( hex, direction ){
	vals = hex.match(/(.{2})(.{2})(.{2})/);
	
	r = parseInt(vals[1], 16);
	g = parseInt(vals[2], 16);
	b = parseInt(vals[3], 16);
	
	if( (r > 230 && g > 230 && b > 230) || direction == 1 ){
		r -= 16;
		g -= 16;
		b -= 16;
		
		if(r < 0){r=0;}
		if(g < 0){g=0;}
		if(b < 0){b=0;}
	}else{
		r += 16;
		g += 16;
		b += 16;
		
		if(r > 255){r=255;}
		if(g > 255){g=255;}
		if(b > 255){b=255;}
	}
	
	r = r.toString(16);
	g = g.toString(16);
	b = b.toString(16);
	
	if( r.length == 1){r = "0"+r}
	if( g.length == 1){g = "0"+g}
	if( b.length == 1){b = "0"+b}
	
	return(r+g+b);
}

// Stuff for customising the embed poll
// Beware: monstrous looking code but it's actually pretty simple.

if( typeof(embed_poll_id) == "number" ){
	$(".cpicker").pngFix();
	
	var embed_poll_vars = new Array();
	
	embed_poll_vars["comment_link"] = 1;
	embed_poll_vars["border"] = 1;
	embed_poll_vars["border_color"] = '000000';
	embed_poll_vars["background_color"] = 'ededed';
	embed_poll_vars["background_color_alt"] = 'dadada';
	embed_poll_vars["link_color"] = '467ac6';
	embed_poll_vars["text_color"] = '000000';
	embed_poll_vars["hr_color"] = 'dddddd';
	embed_poll_vars["bar_color"] = '0094d1';
	embed_poll_vars["bar_border_light_color"] = '3daddb';
	embed_poll_vars["bar_border_dark_color"] = '006f9d';
	embed_poll_vars["width"] = '160';
	
	$("#cpicker_bg_color").ColorPicker({
	color: '#'+embed_poll_vars["background_color"],
	onShow: function(cpicker){
		$(cpicker).find(".colorpicker_color").pngFix();
	},
	onChange: function (hsb, hex, rgb) {
		$("#cpicker_bg_color div").css('backgroundColor', '#' + hex);
		embed_poll_vars["background_color"] = hex;
		embed_poll_vars["hr_color"] = get_contrasting_color( embed_poll_vars["background_color"], 1 );
		embed_poll_vars["background_color_alt"] = get_contrasting_color( embed_poll_vars["background_color"], 1 );
		$("div#dap_poll_"+embed_poll_id+" .poll_inner, .poll_row_bg1").css("background-color", "#"+embed_poll_vars["background_color"]);
		$("div#dap_poll_"+embed_poll_id+" .poll_hr, .poll_row_bg2").css("background-color", "#"+embed_poll_vars["hr_color"]);
		update_get_embed_code();
	},
	onHide: function(cpicker){
		$(cpicker).hide();
		return false;
	}
	});
	
	$("#cpicker_link_color").ColorPicker({
	color: embed_poll_vars["link_color"],
	onShow: function(cpicker){
		$(cpicker).find(".colorpicker_color").pngFix();
	},
	onChange: function (hsb, hex, rgb) {
		$("#cpicker_link_color div").css('backgroundColor', '#' + hex);
		embed_poll_vars["link_color"] = hex;
		$("div#dap_poll_"+embed_poll_id+" a").css("color", "#"+embed_poll_vars["link_color"]);
		update_get_embed_code();
	},
	onHide: function(cpicker){
		$(cpicker).hide();
		return false;
	}	
	});

	$("#cpicker_text_color").ColorPicker({
	color: embed_poll_vars["text_color"],
	onShow: function(cpicker){
		$(cpicker).find(".colorpicker_color").pngFix();
	},
	onChange: function (hsb, hex, rgb) {
		$("#cpicker_text_color div").css('backgroundColor', '#' + hex);
		embed_poll_vars["text_color"] = hex;
		$("div#dap_poll_"+embed_poll_id+" .text_color").css("color", "#"+embed_poll_vars["text_color"]);
		update_get_embed_code();
	},
	onHide: function(cpicker){
		$(cpicker).hide();
		return false;
	}	
	});
	
	$("#cpicker_border_color").ColorPicker({
	color: embed_poll_vars["border_color"],
	onShow: function(cpicker){
		$(cpicker).find(".colorpicker_color").pngFix();
	},
	onChange: function (hsb, hex, rgb) {
		$("#cpicker_border_color div").css('backgroundColor', '#' + hex);
		embed_poll_vars["border_color"] = hex;
		$("div#dap_poll_"+embed_poll_id+" .border_color").css("border-color", "#"+embed_poll_vars["border_color"]);
		update_get_embed_code();
	},
	onHide: function(cpicker){
		$(cpicker).hide();
		return false;
	}	
	});
	
	$("#cpicker_bar_color").ColorPicker({
	color: embed_poll_vars["bar_color"],
	onShow: function(cpicker){
		$(cpicker).find(".colorpicker_color").pngFix();
	},
	onChange: function (hsb, hex, rgb) {
		$("#cpicker_bar_color div").css('backgroundColor', '#' + hex);
		embed_poll_vars["bar_color"] = hex;
		embed_poll_vars["bar_border_light_color"] = get_contrasting_color( embed_poll_vars["bar_color"], -1 );
		embed_poll_vars["bar_border_dark_color"] = get_contrasting_color( embed_poll_vars["bar_color"], 1 );
		$("div#dap_poll_"+embed_poll_id+" .poll_bar").css("background-color", "#"+embed_poll_vars["bar_color"]);
		$("div#dap_poll_"+embed_poll_id+" .poll_bar").css("border-top-color", "#"+embed_poll_vars["bar_border_light_color"]);
		$("div#dap_poll_"+embed_poll_id+" .poll_bar").css("border-right-color", "#"+embed_poll_vars["bar_border_dark_color"]).css("border-bottom-color", "#"+embed_poll_vars["bar_border_dark_color"]);
		update_get_embed_code();
	},
	onHide: function(cpicker){
		$(cpicker).hide();
		return false;
	}	
	});
	
	$("input[name=with_comments]").click(function(){
		if( $(this).is(":checked") == 1 ){
			embed_poll_vars["comment_link"] = 1;
			$(".comment_link").show();
		}else{
			embed_poll_vars["comment_link"] = 0;
			$(".comment_link").hide();
		}
		update_get_embed_code();
	});
	
	function update_get_embed_code(){
		$("textarea#embed_poll_code").val('<div id="dap_poll_'+embed_poll_id+'" class="poll-style-default"></div><script type="text/javascript" src="'+LINK_URL+'external_poll/'+embed_poll_id+'?style=2&comment_link='+embed_poll_vars["comment_link"]+'&border_color='+embed_poll_vars["border_color"]+'&background_color='+embed_poll_vars["background_color"] +'&background_color_alt='+embed_poll_vars["background_color_alt"]+'&link_color='+embed_poll_vars["link_color"]+'&text_color='+embed_poll_vars["text_color"]+'&hr_color='+embed_poll_vars["hr_color"]+'&bar_color='+embed_poll_vars["bar_color"]+'&bar_border_light_color='+embed_poll_vars["bar_border_light_color"]+'&bar_border_dark_color='+embed_poll_vars["bar_border_dark_color"]+'&width='+embed_poll_vars["width"]+'"></script>');
	}
	
	update_get_embed_code();

	
	// Resizer for the external poll customisation
	$("a#embed_poll_width_decrease").mousedown(function(){
		if( !parseInt($("input#embed_poll_width").val()) ){
			$("input#embed_poll_width").val(0);
		}
		embed_poll_width_change( parseInt($("input#embed_poll_width").val()) - 10 );
	});
	
	$("a#embed_poll_width_increase").mousedown(function(){
		if( !parseInt($("input#embed_poll_width").val()) ){
			$("input#embed_poll_width").val(0);
		}
		embed_poll_width_change( parseInt($("input#embed_poll_width").val()) + 10 );
	});
	
	function embed_poll_width_change( width ){
		if( width < 150 ){
			width = 150;
		}else if( width > 300 ){
			width = 300;
		}
		$("input#embed_poll_width").val( width );
		
		$("div#dap_poll_"+embed_poll_id).css("width", width+"px");
		$("div#dap_poll_"+embed_poll_id+" .top_bottom_borders").css("width", (width-2)+"px");
		$("div#dap_poll_"+embed_poll_id+" .poll_hr").css("width", (width-8)+"px");
		
		embed_poll_vars["width"] = width;
		update_get_embed_code();
	}
	
	$("input#embed_poll_width").keyup(function(e){
		if( parseInt( $("input#embed_poll_width").val() ) >= 150 && parseInt( $("input#embed_poll_width").val() ) <= 300 ){
			embed_poll_width_change( $("input#embed_poll_width").val() );
		}
	});


}




// Share link inline menu popup thingy trigger
$("div.share_link").hover(function(){
	$(this).find("div.share_menu").show();
},function(){
	$(this).find("div.share_menu").hide();
});



});



// Function to reload the subcategory menu on Start Poll page 1 when the user goes back to modigy the first page

function reload_subcat_menu( sub_cat_id ){
	$(document).ready(function(){
		$("select#f_category").change();
		$("select#f_subcategory").val(sub_cat_id);
	});
}

// Similar to above but for state menu when country is selected on profile edit page

function reload_subcat_menu( state_id ){
	$(document).ready(function(){
		$("select#f_location_country").change();
		$("select#f_location_state").val(state_id);
	});
}



