if(typeof bsc === 'undefined'){
	var bsc = {};
}

bsc.showProjectForm = function(mode){
	var overlay  = $('#overlay_' + mode);
	overlay.show();
	$('#project-item-url').focus();
}

bsc.openOverlays = function(){
	if ($('body').attr('class')) {
		var bodyClasses = $('body').attr('class').split(' ');
		$(bodyClasses).each(function(index, overlayName){
			overlay = $('#' + overlayName);
			overlay.show();
			var inputFirstFocus = overlay.find('input.first-focus');
			if(inputFirstFocus.length > 0){
				inputFirstFocus.focus();
			}
		});
	}
}

function repos() {
	$('#flash').css('top', 0);
	$('#login_box_container').css('margin-top', 0);
	var d_height = $(document).height();
	var f_height = $('#flash').height();
	var l_height = $('#login_box_container').height();
	var top1 = (d_height > f_height) ? Math.floor((d_height-f_height)/2) : 0;
	var top2 = (d_height > l_height) ? Math.floor((d_height-l_height)/2) : 0;
	$('#flash').css('top', top1);
	$('#login_box_container').css('margin-top', top2);
}

$(document).ready(function() {
	
	repos();
	$(window).resize(repos);
	
	$(window).resize(function() {
		$('#dp-popup').remove();
	});
	$(window).scroll(function() {
		$('#dp-popup').remove();
	});
	 
	$('.overlay_content').each(function() {
		var margin_top = Math.round($(this).height()*-0.5);
		$(this).css('margin-top', margin_top);
	});
	
	$('.overlay_background').click(function() {
		$(this).parent().hide();
	});
	$('.overlay .close').click(function() {
		$(this).parent().parent().hide();
		return false;
	});
	
	$('.overlay').each(function() {
		$(this).css('visibility', 'visible');
		$(this).hide();
	});
	
	$('.product_details').hide();
	
	var move = false
	
	$('.product_estimation .open_close').click(function() {
		if (!move) {
			move = true;
			$(this).toggleClass('close');
			$(this).parent().next().slideToggle(500, function() {
				move = false;
			});
		}
		return false;
	});
	
	$('#pa_invite_it a').click(function() {
		$('#overlay_invite').show();
		return false;
	});
	
	$('#pa_edit_it a').click(function() {
		bsc.showProjectForm('edit');
		return false;
	});
	$('#add_item a').click(function() {
		bsc.showProjectForm('add');
		return false;
	});
	
	$('#pa_cancel_it a').click(function() {
		return confirm('Are you sure you want to cancel this item?');
	});
	
	$('#cancel_participation').click(function() {
		return confirm('Are you sure you want to cancel your participation?');
	});
	
	$('.cancel_participation_for').click(function() {
		return confirm('Are you sure you want to cancel this participation?');
	});
	
	$('#close_item').click(function() {
		return confirm('Are you sure you want to finalize this order? There is no turning back.');
	});
	
	$('a[rel="screencast"]').click(function() {
		$('#flash').hide();
		$('#overlay_screencast').show();
		$('#screencast').flash({
			swf: '/assets/_swf/screencast_player.swf',
			wmode: 'transparent',
			width: 975,
			height: 548,
			base: '/assets/_swf/'
		});
		return false;
	});
	
	$('#overlay_screencast .close').click(function() {
		$('#flash').show();
	});
	
	$('#overlay_screencast .overlay_background').click(function() {
		$('#flash').show();
	});
	
	$('#add_first_item .button').click(function() {
		$('#overlay_add').show();
		$('#add_url').focus();
		return false;
	});
	
	$('#pa_contact_in a').click(function() {
		$('#overlay_email_initiator').show();
		$('#email_initiator_message').focus();
		return false;
	});
	
	$('#forgot').click(function() {
		$('#overlay_password').show();
		$('#password_email').focus();
		return false;
	});
	
	$('#subscribe').click(function() {
		$('#overlay_subscribe').show();
		$('#subscribe_username').focus();
		return false;
	});
	
	$('#product_people ul a.participation_user').click(function() {
		var username = $(this).text();
		$('#overlay_email_participant h3').text('Contact participant » '+username);
		$('#email_participant_username').val(username);
		$('#overlay_email_participant').show();
		$('#email_participant_message').focus();
		return false;
	});
	
	if ($('#dashboard_follow').length==0) {
		$('#dashboard_own').css('border', 0);
	}
	
	if ($('#action-dashboard').length==0) {
		$('#user p').css('border', 0);
	}
	
	$('.target_blank').click(function() {
		window.open($(this).attr('href'));
		return false;
	});
	
	if ($('#login_container').length > 0) {
		$('#flash').flash({
			swf: bsc.baseurl + 'assets/_swf/intro_home.swf',
			wmode: 'transparent',
			width: 508,
			height: 626
		});
	}
	else {
		Date.format = 'mm/dd/yyyy';
		$('.project-delivery-date').datePicker();
		
		var position = $('#bookmarklet_link').position();
		$('#bookmarklet_arrow').css('top',position.top-280).css('left',position.left+150);
		$('#bookmarklet_arrow').hide();
		
		if (!$.browser.msie) {
			$('#bookmarklet_link').mouseover(function() {
				$('#bookmarklet_arrow').show();
			});
			$('#bookmarklet_link').mouseout(function() {
				$('#bookmarklet_arrow').hide();
			});
			$('#bookmarklet_link').click(function() {
				alert('Drag this to your Bookmark Bar to install. Once installed, you can instantly add items to your SocialShop from any website!');
				return false;
			});
		}
		else {
			$('#bookmarklet_link').click(function() {
				alert('Right-click this and choose "Add to Favorites" to quickly post items to your SocialShop.');
				return false;
			});
		}
	}
	
	/*** MAKE SURE CORRECT OVERLAYS ARE OPEN WHEN ERROR IN FORMS ***/
	
	bsc.openOverlays();
	
	/*** SHARE ***/
	
	$('.share_facebook_wall').click(function() {
		var url = window.location.href;
		window.open('http://www.facebook.com/sharer.php?u='+url,'sharer','toolbar=0,status=0,resizable=1,width=626,height=436');
		return false;
	});
	
	function showDialog() {
		var dialog = new FB.UI.FBMLPopupDialog('Invite your friends to participate!', '');
		var fbml = '<fb:fbml>' +
		'<fb:request-form ' +
		'method="GET" ' +
		'action="'+window.location.href+'" ' +
		'type="SocialShop" ' +
		'content="Participate with me to buy &laquo; '+document.title+' &raquo; on SocialShop!' +
		'<fb:req-choice url=\''+window.location.href+'\' label=\'Confirm\' />"' +
		'>' +
		'<fb:multi-friend-selector actiontext="SocialShop" cols="4" rows="4" max="35" bypass="cancel" />' +
		'</fb:request-form>' +
		'</fb:fbml>';

		dialog.setFBMLContent(fbml);
		dialog.setContentWidth(625);
		dialog.setContentHeight(580);
	
	  	dialog.show();
	}
	
	try {
		$('.share_facebook_friends').click(function() {
			FB.init('c33c09c262bab94cbca590ec06ccd6fc', bsc.fbwallurl);
			FB.ensureInit(function() {
				FB.Connect.get_status().waitUntilReady(function(status) {
					if (status === FB.ConnectState.connected) {
						showDialog();
					}
					else if (status === FB.ConnectState.appNotAuthorized || status === FB.ConnectState.userNotLoggedIn) {
						FB.Connect.requireSession(function() {
							showDialog();
						});
					}
				});
			});
			return false;
		});
	}
	catch(e) {
		alert(e);
	}
	
	/*** END SHARE ***/
	
	if ($('#ie6').length > 0) {
		
		var h = $('#ie6').height();
		var mt = Math.round((h/2)*-1);
		
		$('#ie6').css('margin-top', mt);
		
		$('#ie6').click(function() {
			$('#ie6').hide();
		});
	}
	
});