	var mybrandz = function(){};
	
	
	mybrandz.profileEditor = function()
	{
		this.edit_mode = false;
		
		this.show_edit = function(obj)
		{
			if(this.edit_mode)
			{
				document.getElementById('edit_person').style.display = 'none';
				this.edit_mode = false;
			}
			else
			{
				document.getElementById('edit_person').style.display = 'block';
				this.edit_mode = true;
			}
		}
	};

	mybrandz.brandsSelection = function()
	{
		this.selectedBrands = false;
		this.hash_signature = false;	
		this.next_var = false;
		
		this.setSelectedBrands = function(ids_arr, hash_signature, next_var)
		{
			this.selectedBrands = ids_arr;
			this.hash_signature = hash_signature;	
			this.next_var = next_var;
		}

		this.makeSelectBrands = function()
		{
			ids_arr = this.selectedBrands;

			var request_get = '';
			
			for(var i in ids_arr)
			{
				request_get += 'entities['+ i +'][id]=' + ids_arr[i] + '&';
			}
			
			request_get += 'hash_signature=' + this.hash_signature;
			request_get += '&next=' + this.next_var;
			request_get += '&method=multi_join_request';
			
			alert(request_get);
		}
		
	};	
	
	mybrandz.user_withnoposts_onchange = function(obj, num_of_posts)
	{
		
		brand_id = obj[obj.selectedIndex].value;
		obj.form.action = '/brand/' + brand_id + '/post/';
		obj.form.parentid.value = brand_id;
		if(num_of_posts == 0)
		{
			obj.form.name.value = 'My favorite brand is ' + obj[obj.selectedIndex].text + ' because...';
		}
		else if(num_of_posts == 1)
		{
			obj.form.name.value = 'I think ' + obj[obj.selectedIndex].text + ' should...';
		}
		
	}
	
	mybrandz.textSwitcher = function(str_msg)
	{
		this.init_text = str_msg;
		
		this.call_blur = function(obj) 
		{
			if(obj.value == "")
			{
				obj.value = this.init_text;
			}
		}
		this.call_select = function(obj)
		{
			if(obj.value == this.init_text)
			{
				obj.value = "";
			}
		}
	};
	
	mybrandz.mybrandz_dropdown = function(sub_holder, select_box, call_event)
	{
		this.sub_holder = sub_holder; 
		this.select_box = select_box; 
	
		this.event_name = call_event;
	
		this.sub_holder = new YAHOO.widget.Overlay(this.sub_holder, {context:[this.select_box,"tl","bl", ["beforeShow", "windowResize"]],visible:false} );
		this.sub_holder.render();
		
		this.toggle_popup = function(show)
		{
			if(show == 1)
			{
				this.sub_holder.show();
			}
			else
			{
				this.sub_holder.hide();
			}
		}
		
		this.set_order = function(obj, event_name)
		{
			if(this.event_name)
			{
				new_value = obj.name;
				
				var hold_value = document.getElementById(this.select_box).name;
				document.getElementById(this.select_box).name = obj.name; 
				obj.name = hold_value;
				
				var hold_caption = document.getElementById(this.select_box).innerHTML;
				document.getElementById(this.select_box).innerHTML = obj.innerHTML; 
				obj.innerHTML = hold_caption;
				
				this.event_name(new_value);
				this.toggle_popup();
			}
		}	
	}
	
	
	mybrandz.story_popup = function(obj)
	{
		this.overlay3 =  
			new YAHOO.widget.Overlay("overlay3", {xy:[350,50],  
				visible:false,  
				zIndex:1000, 
				width:"425px", 
				height:"344px", 
				effect:[{effect:YAHOO.widget.ContainerEffect.FADE,duration:0.5}, 
				        {effect:YAHOO.widget.ContainerEffect.SLIDE,duration:0.5}]} ); 
		this.show_movie = function(form, force_login)
		{		
			document.getElementById('overlay3').style.display = 'inline';
			this.overlay3.render("example");
			this.overlay3.show();
		}
		this.hide_movie = function(form, force_login)
		{		
			this.overlay3.hide();
		}
	}
	

	
	function show_form(form, force_login, brand_ids, sharezp)
	{		
		var config = 
		{																				
			dialog_id:'loginsignup_dialog',						
			width:'590px',										
			autoclose: true,										
			button: 'submit',										
			result_true: '',										
			header_text: 'Login / Signup',
			need_validation:true,						
//			use_form_submit:true,									
			use_form_button: false,
			use_form_submit: true
		};				
	    this.dialog = new dex.dialog(this, config);					
	    var url = '/portal/900/module/ajax_login_signup/';	
	    url = url + '?email=';
	    if(form)
	    {
	    	url = url + form.email_input.value;			
	    }
	    if(force_login)
	    {
	    	url = url + '&force_login=1';			
	    }
	    if(brand_ids)
	    {
	    	url = url + '&brands=' + brand_ids;
	    }
	    if(sharezp)
		   url += '&sharez=1';
	   	this.dialog.load_form(url);									
	}
	
	
	function signup_dialog(form, force_login, brand_ids, sharezp)
	{		
		var config = 
		{																				
			dialog_id:'signup_dialog',						
			width:'700px',										
			autoclose: true,										
			button: 'submit',										
			result_true: '',										
			header_text: 'Signup',
			need_validation:true,						
			use_form_button: false,
			use_form_submit: true
		};				
		this.dialog = new dex.dialog(this, config);
		var url = '/portal/900/module/ajax_signup/?';
		if(brand_ids)
			url += '&brands=' + brand_ids;
		if(sharezp)
			url += '&sharez=1';

		this.dialog.load_form(url);
	}

	function signup_dialog_fsb_only(brand_ids)
	{
		var config =
		{
			dialog_id:'signup_dialog',
			width:'185px',
			autoclose: true,
			result_true: '',
			header_text: 'Signup',
			need_validation:true,
			use_form_button: false,
			use_form_submit: false
		};
		this.dialog = new dex.dialog(this, config);
		var url = '/portal/900/module/ajax_signup/?&brands=' + brand_ids+'&sharez=1&fsb_only=1';

		this.dialog.load_form(url);
	}
	
	
	function show_login_form()
	{
		show_form(null, true);
	}
	
	function username_update(obj)
	{
		var spacer = " ";
		if(obj.fname.value == "")
		{
			spacer = ""
		}
		obj.name.value = obj.fname.value + spacer + obj.lname.value; 
		return true;
	}
	
	
	mybrandz.form_submit = function(obj)
	{
		if(dex.forms.checks.email(obj.email_input))
		{
			show_form(obj, false);
		}				
	}
	
	
	
	mybrandz.select_onchange = function(obj, signature) 											
	{
		var item_id = mybrandz.get_itemid_by_index(obj.selectedIndex);
		var item_type_name = mybrandz.get_itemtype_name_by_index(obj.selectedIndex);
		mybrandz.set_new_post_form(obj.form, "post", item_id, item_type_name);
		mybrandz.populate_select(obj.form.dependant_controller, item_id, signature);
		mybrandz.set_new_post_form(obj.form, "post", item_id, item_type_name);
	};
		
	mybrandz.populate_select = function(select_obj, selected_brand, signature)
	{
		var sUrl =  '/brand/'+selected_brand+'/json/get_products';
		var callback =
		{
			success: function(o) {
			select_obj.length = 0; // clear options
			// add default option
			select_obj[select_obj.length] = new Option('Post to brand', 0);
			var myJSONtext = o.responseText;
//			//alert(o.responseText);
			var myObject = eval('(' + myJSONtext + ')');
			for (var i=0; i<myObject.messages.length; i++) 
			{
				select_obj[select_obj.length] = new Option(myObject.messages[i].name, myObject.messages[i].id);
			}
			},
			failure: function(o) {
				//alert("F")
				}
			//argument: [argument1, argument2, argument3]
		}
		
       YAHOO.util.Connect.initHeader('X-Signature', signature);
		var transaction = YAHOO.util.Connect.asyncRequest('POST', sUrl, callback, '');
	};
	
	mybrandz.dependant_onchange = function(obj)
	{
		var item_id = obj.options[obj.selectedIndex].value;
		if(item_id == 0)
		{
			item_id = obj.form.select_controller.value;
			item_type_name = "brand";
		}
		else
		{
			item_type_name = "product";
		
		}
		mybrandz.set_new_post_form(obj.form, "post", item_id, item_type_name, null);
	};
	
	function order_by(param, url)
	{
		YAHOO.util.Connect.asyncRequest("GET",url+param,{
		success:function(o) {
			document.getElementById('feed_holder').innerHTML = o.responseText;
		}});
	};
	
  	mybrandz.create_post = function(post_type) 										
	{
		this.process_answer = function(answer) 
		{
			answer = answer.replace(/\n/g,"");
			answer = answer.replace(/\r/g,"");
			var matches = (new RegExp(/<script[^>]*>(.*?)<\/script>/ig)).exec(answer);
			if (matches && matches[1]) 
			{
				eval(matches[1]);
			}
			return answer;
		}													
		this.create_post = function(current_user, brand_id, product_id, after_ihave)
		{
			var url = '/user/'+current_user+'/module/ajax_post/?type='+this.post_type;

			if(product_id)
			{
					url=url+'&brand_id=' + brand_id;
					url=url+'&product_id=' + product_id;
			}
			else
			{
				url=url+'&brand_id=' + brand_id;
			}
			if (after_ihave)
				url += '&after_ihave=1';

			this.dialog.load_form(url);
			return false;
		}
		var config = {												
			dialog_id:'createpost_dialog',
			width:'600px',
			autoclose: true,
			result_true: '',
			use_form_button:true,
			use_form_submit:true,
			need_validation:true,
			header_text: 'Create new '+post_type
    		};		
		this.post_type = post_type;
		this.dialog = new dex.dialog(this, config);
	}
	function verification_error()
	{
		document.getElementById("unverified_email").style.display = "block";
	}
	function verification_error_hide()
	{
		document.getElementById("unverified_email").style.display = "none";
	}
    
    
	function join_brand(obj, brand_name, signature, brand_id, user_id, url)
	{
		YAHOO.util.Connect.initHeader('X-Signature', signature);
		YAHOO.util.Connect.asyncRequest('POST', '/brand/'+brand_id+'/json/request/', {
		success:function(o) {
			this.src = '/templates/mybrandz_tmp/images/joined_brand.jpg';
		},
		scope: obj}
		,'friendid='+user_id);
		
		add_new_brand_to_list(brand_name, url);
		
		return false;
	}
	
	function vote_brand_mybrandz(brand_id, user_id, signature) 
	{
	
		YAHOO.util.Connect.initHeader('X-Signature', signature);
		YAHOO.util.Connect.asyncRequest('POST', '/brand/'+brand_id+'/json/request/', {
		success:function(o) {
			
		}}
		,'friendid='+user_id);
		
		return false;
	}
	

	function leave_brand(obj, brand_id, user_id, signature)
	{
		document.getElementById(obj).innerHTML = "Removing brand...";

		YAHOO.util.Connect.initHeader('X-Signature', signature);
		YAHOO.util.Connect.asyncRequest('POST', '/brand/'+brand_id+'/json/leave/', {
		success:function(o) {
			document.getElementById(obj).style.display = "none";
			document.getElementById("brand_list_" + brand_id).style.display = "none";
			
			var child = document.getElementById(obj);
	        var parent = document.getElementById("brand_holder");
	        parent.removeChild(child);

			if(!document.getElementById("brand_holder").hasChildNodes())
			{
				document.getElementById("brand_holder").innerHTML = '<li>You don\'t have any brands yet</li>';
//				document.getElementById("iwant_header").style.display = "none";
			}			
			
		}}
		,'friendid='+user_id);
		
//		leave_brand();
		
		return false;
	}

	function leave_ihave(obj, ihave_id, user_id, signature)
	{
		document.getElementById(obj).innerHTML = "Removing product...";

		YAHOO.util.Connect.initHeader('X-Signature', signature);
		YAHOO.util.Connect.asyncRequest('POST', '/ihave/'+ihave_id+'/json/remove/', {
		success:function(o) {
			var child = document.getElementById(obj);
	        var parent = document.getElementById("ihave_holder");
	        parent.removeChild(child);

			if(!document.getElementById("ihave_holder").hasChildNodes())
			{
				document.getElementById("ihave_header").style.display = "none";
				
			}
		}}
		,'friendid='+user_id);
		return false;
	}

	function leave_iwant(obj, iwant_id, user_id, signature)
	{
		document.getElementById(obj).innerHTML = "Removing product...";

		YAHOO.util.Connect.initHeader('X-Signature', signature);
		YAHOO.util.Connect.asyncRequest('POST', '/iwant/'+iwant_id+'/json/remove/', {
		success:function(o) {
			var child = document.getElementById(obj);
	        var parent = document.getElementById("iwant_holder");
	        parent.removeChild(child);

			if(!document.getElementById("iwant_holder").hasChildNodes())
			{
				document.getElementById("iwant_header").style.display = "none";
				
			}
		}}
		,'friendid='+user_id);
		return false;
	}
	
	
	function add_new_brand_to_list(new_name, new_url)
	{
		var new_brand_t = document.getElementById('new_brand_t');
		new_brand_t.style.display = "block";
		new_brand_t.innerHTML = new_name;
		new_brand_t.href = new_url;
	}
	
	mybrandz.create_iwant = function(product_id, current_user, signature, type_iwant)
	{
		var uploadHandler;
		var uploadHandler = {
			success: function(o) 
			{
				document.getElementById("join_want_holder").innerHTML = "<span class=\'joined\'>Thanks!</span>";
			}
		};
		var params;
		params='parentid='+product_id;
		params=params+'&ownerid='+current_user;
		params=params+'&hash_signature='+signature;
		params=params+'&typeid='+type_iwant;
		YAHOO.util.Connect.asyncRequest('POST', '/product/'+product_id+'/json/create/', uploadHandler, params);
	}
	mybrandz.create_iwant_list = function(product_id, current_user, signature, type_iwant, obj_id)
	{
		var uploadHandler;
		var uploadHandler = {
			success: function(o) 
			{
				document.getElementById(obj_id).innerHTML = "I WANT";
			}
		};
		var params;
		params='parentid='+product_id;
		params=params+'&ownerid='+current_user;
		params=params+'&hash_signature='+signature;
		params=params+'&typeid='+type_iwant;
		YAHOO.util.Connect.asyncRequest('POST', '/product/'+product_id+'/json/create/', uploadHandler, params);
	}

	mybrandz.create_ihave = function(post_type)
	{
		this.save_cur_user = 0;
		this.save_cur_product = 0;
		
		this.process_answer = function(answer)
		{
			answer = answer.replace(/\n/g,"");
			answer = answer.replace(/\r/g,"");
			var matches = (new RegExp(/<script[^>]*>(.*?)<\/script>/ig)).exec(answer);
			if (matches && matches[1])
			{
				eval(matches[1]);
			}
			return answer;
		}
		this.create_ihave = function(current_user, product_id)
		{
			this.save_cur_user = current_user;
			this.save_cur_product = product_id;
			var url = '/user/' + current_user + '/module/ajax_ihave/?';
			url=url+'product_id=' + product_id;
			this.dialog.load_form(url);
			return false;
		}
		this.create_ihave_list = function(current_user, product_id, obj_id)
		{
			this.save_cur_user = current_user;
			this.save_cur_product = product_id;
			this.obj_id = obj_id;
			this.from_list = true;
			var url = '/user/' + current_user + '/module/ajax_ihave/?';
			url=url+'product_id=' + product_id;
			this.dialog.load_form(url);
			document.getElementById(this.obj_id).innerHTML = "I HAVE";
			return false;
		}
		this.callback = function()
		{
			if(this.from_list)
			{
				document.getElementById(this.obj_id).innerHTML = "I HAVE";
			}
			else
			{
				document.getElementById("join_have_holder").innerHTML = "<span class='joined'>Thanks!</span>";
			}
			var mb2 = new mybrandz.create_post('image');
			mb2.create_post(this.save_cur_user,0,this.save_cur_product,true);
		}
		var config = {
			dialog_id:'createihave_dialog',
			width:'600px',
			autoclose: true,
			result_true: '',
			use_form_submit:true,
			need_validation:true,
			header_text: 'I Have',
			use_callback:true
		};
		this.post_type = post_type;
		this.dialog = new dex.dialog(this, config);
	}

	mybrandz.get_gmail_list = function(user_id, allow_unreg)
	{
		var email = document.getElementById('g_mail').value;
		var pass = document.getElementById('g_pass').value;
		var domain, pos;

		var uploadHandler = {
			success: function(o)
			{
				var rezz = eval("("+o.responseText+")");
				document.getElementById('gmail_submit').style.display = '';
				document.getElementById('gmail_loading').style.display = 'none';
				if (rezz.error)
				{
					alert(rezz.error);
				} else {
					for (var i=0; i<rezz.data.length; i++)
					{
						m_adder(rezz.data[i].email,rezz.data[i].name);
					}
					document.getElementById('mails_output').style.display = '';

					var from_email = document.getElementById('from_email');
					if (from_email)
						from_email.value = document.getElementById('g_mail').value;
				}
			}
		};

		if (!validate_email(email.trim(),1))
			return false;

		//Get domain
		pos = email.indexOf('@');
		domain = email.slice(pos+1);
		if (domain.toLowerCase()!='gmail.com')
		{
			alert('Not a Gmail mail!');
			return false;
		}
		if (pass.trim()=='')
		{
			alert('Password field is empty!');
			return false;
		}

		document.getElementById('gmail_submit').style.display = 'none';
		document.getElementById('gmail_loading').style.display = '';

		var params = 'subact=get&gmail='+email+'&gpass='+pass;
		if (allow_unreg)
			params += '&unreg=1';

		YAHOO.util.Connect.asyncRequest('POST', '/user/'+user_id+'/module/page_gmail/', uploadHandler, params);
	}

	mybrandz.gvf_gmail_sender = function()
	{
		var params = 'subact=send';
		
		var uploadHandler = {
			success: function(o)
			{
				var rezz = eval("("+o.responseText+")");
				document.location.href = rezz.redirect;
			}
		};

		//Build parameters
		var formobj = document.getElementById('gmail_send');
		for (var i = 0; i < formobj.length; i++)
			if (formobj[i].type=='checkbox')
			{
				if (formobj[i].checked && formobj[i].id!='allcheck')
					params += '&mlist[]='+formobj[i].value;
			}
		params += '&email='+document.getElementById('from_email').value;

		document.getElementById('gvf_gmail_sender').style.display = 'none';
		document.getElementById('gmail_sending').style.display = '';

		YAHOO.util.Connect.asyncRequest('POST', '/portal/900/module/google_vs_facebook/', uploadHandler, params);
	}

	mybrandz.make_rate_list = function(content_type,content_id,call_obj)
	{
		var key = content_type+'_'+content_id;
		if (top.content_loaded && top.content_loaded[key])
		{
			call_obj.innerHTML = top.content_loaded[key];
			return;
		} else {
			top.content_loaded = [];
			call_obj.innerHTML = 'Loading...';
		}

		var uploadHandler = {
			success: function(o)
			{
				var res = eval("("+o.responseText+")");
				var gui = '';
				if (res.result)
				{
					for (var i=0; i<res.data.length; i++)
					{
						gui += '<img src="/templates/mybrandz_tmp/images/star.gif" /> ';
						gui += '<a href="/g/user/'+res.data[i].id+'">'+res.data[i].name+'</a><br />';
					}
				} else {
					gui = res.message;
				}
				top.content_loaded[key] = gui;
				call_obj.innerHTML = gui;
			}
		};
	
		YAHOO.util.Connect.asyncRequest('POST', '/'+content_type+'/'+content_id+'/module/ajax_get_rates/', uploadHandler, '');
	}

	mybrandz.show_rates = function(content_type,content_id,obj,cur_count)
	{
		if (cur_count==0) return;
		
		var positioner = function(e_obj) {
			var iReturnX = 0, iReturnY = 0;
			while (e_obj!=null)
			{
				iReturnX += e_obj.offsetLeft;
				iReturnY += e_obj.offsetTop;
				e_obj = e_obj.offsetParent;
			}
			return [iReturnX,iReturnY];
		}

		var v_obj = document.getElementById('get_rates_box');
		if (v_obj.style.display=='')
			v_obj.style.display = 'none';
		else {
			var c_pos = positioner(obj);
			v_obj.style.left = c_pos[0]+'px';
			v_obj.style.top = (c_pos[1]+20)+'px';
			v_obj.style.display = '';
			this.make_rate_list(content_type,content_id,v_obj);
		}
	}

	mybrandz.shareon_save = function(ctype,cobj)
	{
		var data;
		switch (ctype)
		{
			case 1:
				if (cobj.checked)
				{
					data = 1;
					on_twt_check();
				}
				else
				{
					data = 0;
					//on_twt_uncheck();
				}
				setCookie("share_twt", data, 1209600, "/");
				break;
			case 2:
				if (cobj.checked)
				{
					data = 1;
					on_fb_check();
				}
				else
				{
					data = 0;
					//on_fb_uncheck();
				}
				setCookie("share_fbk", data, 1209600, "/");
				break;
		}
	}

	function validate_email(email,show_alert)
	{
		var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
		if (reg.test(email) == false)
		{
			if (show_alert)
				alert('Invalid Email Address');
			return false;
		}
		return true;
	}

	function showHide(oid)
	{
		var list = oid.split(',');
		for (var i=0; i<list.length; i++)
		{
			var cobj = document.getElementById(list[i]);
			if (cobj.style.display=='')
				cobj.style.display = 'none';
			else
				cobj.style.display = '';
		}
	}

	function change_chk_state(obj,formid)
	{
		var state = obj.checked;
		var formobj = document.getElementById(formid);
		for (var i = 0; i < formobj.length; i++)
			if (formobj[i].type=='checkbox')
				formobj[i].checked = state;
	}

	function drawTwitter(t_text,t_url)
	{
		if (t_url=='') t_url = window.location.href;
		if (t_text=='') t_text = document.title;
		document.write('<span class="retwetter"><a href="/ext/twitter/twitter_redirect.php?text='+escape(t_text));
		document.write('&url='+escape(t_url)+'" target="_blank">Tweet</a></span>');
	}

	function pop_gmail_inviter(user_id)
	{
		var config =
		{
			dialog_id:'gmail_invite',
			width:'290px',
			autoclose: true,
			button: '',
			result_true: '',
			header_text: 'Gmail Invite',
			need_validation: false,
			use_form_button: true,
			use_form_submit: true
		};
		this.dialog = new dex.dialog(this, config);
		var url = '/user/'+user_id+'/module/gmail_inviter/?pop=1';
		this.dialog.load_form(url);
	}

	function ajax_ihave_slider(butn)
	{
		document.getElementById('show_off_box').style.display = (butn==1) ? '' : 'none';
		document.getElementById('review_box').style.display = (butn==1) ? 'none' : '';
		document.getElementById('ihave_butn_1').className = (butn==1) ? 'ihave_switcher_off' : 'ihave_switcher_on';
		document.getElementById('ihave_butn_2').className = (butn==1) ? 'ihave_switcher_on' : 'ihave_switcher_off';
	}