function initPage()
{
	var login = document.getElementById("login-box");
	if (login)
	{
		var nodes = login.getElementsByTagName("input");
		for (var i = 0; i < nodes.length; i++)
		{
			nodes[i].onmouseover = function () 
			{
				if (this.className.indexOf("btn-login-hover") == -1)
				{
					this.className += " btn-login-hover";
				}
			}
			nodes[i].onmouseout = function ()
			{
				this.className = this.className.replace(" btn-login-hover", "");
			}
		}
	}
	var nav = document.getElementById("nav");
	if (nav)
	{
		var nodes = nav.getElementsByTagName("li");
		for (var i = 0; i < nodes.length; i++)
		{
			nodes[i].onmouseover = function () 
			{
				if (this.className.indexOf("hover") == -1)
				{
					this.className += " hover";
				}
			}
			nodes[i].onmouseout = function ()
			{
				this.className = this.className.replace(" hover", "");
			}
		}
	}
	var nav = document.getElementById("modify-buttons");
	if (nav)
	{
		var nodes = nav.getElementsByTagName("input");
		for (var i = 0; i < nodes.length; i++)
		{
			nodes[i].onmouseover = function () 
			{
				if (this.className.indexOf("hover") == -1)
				{
					this.className += " hover";
				}
			}
			nodes[i].onmouseout = function ()
			{
				this.className = this.className.replace(" hover", "");
			}
		}
	}
}
if (window.attachEvent && !window.opera)
	window.attachEvent("onload", initPage);

function socialDiary()
{
	// dummy 
}

function backToTop()
{
	$('html, body').animate({scrollTop:0}, 'slow');
}


$(document).ready(function()
{
	$("#frm_top_username").click(function(){
		$(this).attr("value", $(this).attr("value") == "Email" ? "" : $(this).attr("value"));
	});
	$("#frm_top_username").blur(function(){
		$(this).attr("value", $(this).attr("value") == "" ? "Email" : $(this).attr("value"));
	});

	$("#frm_top_password").click(function(){
		$(this).attr("value", $(this).attr("value") == "Password" ? "" : $(this).attr("value"));
	});
	$("#frm_top_password").blur(function(){
		$(this).attr("value", $(this).attr("value") == "" ? "Password" : $(this).attr("value"));
	});

	$(".char-counter #frm_description").charCounter(500, {
		container: "#description-counter"
	});
	
	// add supplier to directory 
	$(".add-supplier-counter #frm_description").charCounter(60 , {
		container: "#description-counter"
	});

	// member offers "if you win..."
	$(".member-offers #frm_address").charCounter(500, {
		container: "#member-offer-address-counter"
	});
	
	// submit party pics WOW
	$("#frm_wow").charCounter(500, {
		container: "#submit-party-pic-counter"
	});
	
	// submit POTW descriptiont
	$(".submit-potw #frm_description").charCounter(400, {
		container: "#submit-potw-description-counter"
	});
	
	

	// submit news -> select type
	$("#submit_news_type").change(function(){
			var _type = $(this).attr("value");

			$("#regular-item").css("display", (_type == 1 ? "" : "none"));
			$("#product-call").css("display", (_type == 2 ? "" : "none"));

			$("#modify-buttons").css("display", (_type == 1 || _type == 2 ? "" : "none"));
	});

	// auto load home first 3 photo galleries
	if($("#home-hottest-parties"))
	{
		WBM_homepageHottestParties(0);
	}
	
	// auto fade alphabet for sidebar search for brand / supplier
	WBM_alphabetLetter_initFade()

	// alphabet-clear
	$("#alphabet-clear").fadeTo(0, 0.5);
	$("#alphabet-clear").hover(function(){ $(this).fadeTo(0, 1); }, function(){ $(this).fadeTo(0, 0.5); });

	// add supplier form 
	$(".add-supplier-form input, .add-supplier-form select").change(function(){
			WBM_updateAddSupplierPreview();
	});


	WBM_showSidebarTicker("member-offers");

});

function WBM_showSidebarTicker(item)
{
	var e_class;

	if(item == "member-offers") 
	{
		e_class = "member-offers";
		next_class = "sidebar-blog";
	}
	if(item == "sidebar-blog")
	{
		e_class = "sidebar-blog";
		next_class = "member-offers";
	}

	var e_selector = "#sidebar-ticker ." + e_class;
	var next_selector = "#sidebar-ticker ." + next_class;

	$(next_selector).hide();
	$(e_selector).show();
	setTimeout("WBM_showSidebarTicker('" + next_class + "');", 3000);
}

function WBM_stripHTML(string)
{ 
    return string.replace(/<(.|\n)*?>/g, ''); 
}

function WBM_updateAddSupplierPreview()
{
	// name
	if($("#frm_name").val() != "") {
		$("#spc-company-name").show();
		$("#spv-company-name").html(WBM_stripHTML($("#frm_name").val()));
	}
	else {
		$("#spc-company-name").hide();
	}

	// direct contact name
	if($("#frm_contact_name").val() != "") {
		$("#spc-contact-name").show();
		$("#spv-contact-name").html(WBM_stripHTML($("#frm_contact_name").val()));
	}
	else {
		$("#spc-contact-name").hide();
	}

	// direct contact email
	if($("#frm_contact_email").val() != "") {
		$("#spc-contact-email").show();
		$("#spv-contact-email").html(WBM_stripHTML($("#frm_contact_email").val()));
		$("#spv-contact-email").attr("href", "mailto:" + WBM_stripHTML($("#frm_contact_email").val()));
	}
	else {
		$("#spc-contact-email").hide();
	}

	// direct contact phone
	if($("#frm_contact_phone_prefix").val() != "" || $("#frm_contact_phone").val() != "") {
		$("#spc-contact-phone").show();
		$("#spv-contact-phone").html(WBM_stripHTML($("#frm_contact_phone_prefix").val()) + " " + WBM_stripHTML($("#frm_contact_phone").val()));
	}
	else {
		$("#spc-contact-phone").hide();
	}

	// address
	var address_conf = [$("#frm_address").val(), $("#frm_suburb").val(), $("#frm_state").val(), $("#frm_post_code").val()];
	var address_conf_items = address_conf.length; 
	var address_text = "";

	for(i=0; i<address_conf_items; i++)
	{
		if(address_conf[i] != "" && address_conf[i] != "undefined")
		{
			address_text += WBM_stripHTML(address_conf[i]);
			if(i < address_conf_items - 1)
				address_text += ", ";
		}
	}

	if(address_text != "") {		
		$("#spc-address").show();
		$("#spv-address").html(WBM_stripHTML(address_text));
	}
	else {
		$("#spc-address").hide();
	}

	// web
	if($("#frm_web").val() != "") {
		web_url = WBM_stripHTML($("#frm_web").val());
		web_www_url = web_url.replace("http://", "");
		web_full_url = "http://" + web_www_url;

		$("#spc-web").show();
		$("#spv-web").html(web_www_url);
		$("#spv-web").attr("href", web_full_url);
	}
	else {
		$("#spc-web").hide();
	}



	// pr agency
	if($("#frm_pr_agency").val() != "") {
		$("#spc-pr-agency").show();
		$("#spv-pr-agency").html(WBM_stripHTML($("#frm_pr_agency").val()));
	}
	else {
		$("#spc-pr-agency").hide();
	}
	// pr contact name
	if($("#frm_pr_contact_name").val() != "") {
		$("#spc-pr-contact-name").show();
		$("#spv-pr-contact-name").html(WBM_stripHTML($("#frm_pr_contact_name").val()));
	}
	else {
		$("#spc-pr-contact-name").hide();
	}

	// pr contact email
	if($("#frm_pr_contact_email").val() != "") {
		$("#spc-pr-contact-email").show();
		$("#spv-pr-contact-email").html(WBM_stripHTML($("#frm_pr_contact_email").val()));
		$("#spv-pr-contact-email").attr("href", "mailto:" + WBM_stripHTML($("#frm_pr_contact_email").val()));
	}
	else {
		$("#spc-pr-contact-email").hide();
	}

	// pr contact phone
	if($("#frm_pr_contact_phone_prefix").val() != "" || $("#frm_pr_contact_phone").val() != "") {
		$("#spc-pr-contact-phone").show();
		$("#spv-pr-contact-phone").html(WBM_stripHTML($("#frm_pr_contact_phone_prefix").val()) + " " + WBM_stripHTML($("#frm_pr_contact_phone").val()));
	}
	else {
		$("#spc-pr-contact-phone").hide();
	}

	// description
	if($("#frm_description").val() != "") {
		$("#spc-description").show();
		$("#spv-description").html(WBM_stripHTML($("#frm_description").val()));
	}
	else {
		$("#spc-description").hide();
	}

}

function WBM_alphabetClear_click()
{
	WBM_alphabetLetter_resetClasses();
	$("#frm_letter").val("");
}

function WBM_alphabetLetter_initFade()
{
	$(".sidebar-brand-supplier-container .alphabet-container a").each(function(){
		if(!$(this).hasClass("active"))
			$(this).fadeTo(0, 0.5);
	});
}

function WBM_alphabetLetter_resetClasses()
{
	$(".sidebar-brand-supplier-container .alphabet-container a").removeClass("active");
	WBM_alphabetLetter_initFade();
}

function WBM_alphabetLetter_click(letter)
{
	WBM_alphabetLetter_resetClasses();
	$("#alphabet-letter-" + letter).addClass("active");
	$("#alphabet-letter-" + letter).fadeTo(0, 1);
	$("#frm_letter").val(letter);
}
function WBM_alphabetLetter_mouseover(letter)
{
	if($("#alphabet-letter-" + letter).hasClass("active"))
	{
		// nothing
	}
	else
	{
		$("#alphabet-letter-" + letter).fadeTo(0, 1);
	}
}
function WBM_alphabetLetter_mouseout(letter)
{
	if($("#alphabet-letter-" + letter).hasClass("active"))
	{
		// nothing
	}
	else
	{
		$("#alphabet-letter-" + letter).fadeTo(0, 0.5);
	}
}
function WBM_ajaxLoading(_id)
{
	$("#" + _id).html("<span class=\"loading\"></span>");
}

function WBM_sidebarDiaryEvents(_date, _location_id)
{
	WBM_ajaxLoading("sidebar");

	$.ajax({
	   type: "POST",
	   url: "public_panel/includes/_diary_action.php",
	   data: "action=sidebar_events&load_auth=1&d=" + _date + "&l=" + _location_id,
	   success: function(response){
		   $("#sidebar").html(response);
	   }
	 });
}

function WBM_sidebarPhotoGalleries(_start)
{
	$('html, body').animate({scrollTop:0}, 'slow');
	WBM_ajaxLoading("sidebar");

	$.ajax({
	   type: "POST",
	   url: "public_panel/includes/_photo_gallery_action.php",
	   data: "gallery_action=sidebar&load_auth=1&start=" + _start,
	   success: function(response){
		   $("#sidebar").html(response);
	   }
	 });
}

function WBM_sidebarHottestParties(_start)
{
	$('html, body').animate({scrollTop:0}, 'slow');
	WBM_ajaxLoading("sidebar");

	$.ajax({
	   type: "POST",
	   url: "public_panel/includes/_hottest_party_action.php",
	   data: "hottest_party_action=sidebar&load_auth=1&start=" + _start,
	   success: function(response){
		   $("#sidebar").html(response);
	   }
	 });
}
function WBM_membershipShowPaymentContainer(display)
{
	$("#membership-payment-container").css("display", (display == true ? "" : "none"));
}
function WBM_homepageHottestParties(_start)
{
	WBM_ajaxLoading("home-hottest-parties");

	$.ajax({
	   type: "POST",
	   url: "public_panel/includes/_hottest_party_action.php",
	   data: "hottest_party_action=home&load_auth=1&start=" + _start,
	   success: function(response){
		   $("#home-hottest-parties").html(response);
	   }
	 });
}