/*
sfHover = function() {
	var sfEls = document.getElementById("nav").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);
*/

$(document).ready(function(){

	$("#toolbar #toggle.loggedout > a").click( function() {
		$("#toolbar.closed").animate({top: "0px"}, 50, 'swing', function(){
			$(this).removeClass('closed').addClass('open');
		});
		$("#toolbar.open").animate({top: "-26px"}, 100, 'swing', function(){
			$(this).removeClass('open').addClass('closed');
		});
		return false;
	}); 
	
	$('form div.input div.error-message').mouseenter(function(){
		$(this).fadeOut('fast');
	});
	$('form div.input input').focus(function(){
		$(this).parent().find('.error-message').fadeOut('fast');
	});	
	
	$('.phone-area input').keyup(function(){
		if($(this).val().length == 3){
			$('.phone-prefix input').focus();
		}
	});
	
	$('.phone-prefix input').keyup(function(){
		if($(this).val().length == 3){
			$('.phone-line input').focus();
		}
	});
	
	$('#UserPreferenceFacebookAuto').click(function(){
		if($(this).attr('checked') == true){
			$('#facebooksettings').show();
		}else{
			$('#facebooksettings').hide();
		}
	});
	$('#UserPreferenceFacebookAuto').each(function(){
		if($(this).attr('checked') == true){
			$('#facebooksettings').show();
		}else{
			$('#facebooksettings').hide();
		}
	});
	
	$('.dialog').click(function(){
		$('#dialogmodal').remove();
		var dialog = $('<div id="dialogmodal"></div>').appendTo('body');
		$('#dialogmodal').load($(this).attr('href'),{},function(response, status, xhr){
			//var successaction = $('#dialogmodal').find('#successaction').val();
			var title = $('#dialogmodal').find('.title').html();
			bindModal($(this));
			$('#dialogmodal').dialog({
				title: title, 
				width: 600,
				modal: true
			});
			
		});
		return false;
	});
	
	var fieldarray = new Array();
	
	$('select.productoptions').each(function(n){
		var field = $(this).parent();
		var newfield = $(field).clone();
	
		var options = Array();
		var optionarray = Array();
		var optionsn = Array();


		$(newfield).find('.alert').remove();
		$(field).find('option').each(function(){
			optionarray = $(this).text().split(' - ');
			selectname = optionarray[0];
			if(selectname != ''){
				options[selectname] = selectname;
			}
		});
		var i = 0;
		for(var key in options){
			
			if(i == 0){
				$(newfield).find('label').text(key);
				$(newfield).find('option').each(function(){
					if($(this).text().indexOf(key) > -1){
						$(this).remove();
					}
				});
			}else{
				$(newfield).find('label').text(key);
				$(field).find('option').each(function(){
					if($(this).text().indexOf(key) > -1){
						$(this).remove();
					}else{
						$(this).text($(this).text().replace('(NO LONGER AVAILABLE)', ''));
					}
				});
			}
			i++;
		}
		$(field).find('option').each(function(){
			var text = $(this).text().split(' - ');
			$(this).text(text[1]);
		});
		if($(newfield).find('option').length >= 1){
			$(field).parent().append($(newfield));
			$(newfield).addClass($(field).find('select').attr('id') + 'Alternate alternate');
			$(newfield).hide();
			$(newfield).find('option').each(function(){
				var text = $(this).text().split(' - ');
				$(this).text(text[1]);
				if($(this).val() != '' && $(this).attr('selected') == true){
					$(newfield).show();
				}
			});
			$(newfield).find("option:first").remove();
			$(newfield).find("select").prepend("<option value='" + $(field).find('option:selected').val() + "'></option>");
		}
	});
	$(':not(.alternate) > select.productoptions').change(function(){
		var fieldid = $(this).attr('id') + 'Alternate';
		$(this).parent().parent().find(".alternate select option").removeAttr('selected');
		
		var defaultmessage = $(this).parent().find('.alert').html();
		
		if($.inArray($(this).find(':selected').val(), soldout) > -1  && typeof(soldoutmessages[$(this).find(':selected').val()]) != undefined){			
			$(this).parent().find('.alert').html(soldoutmessages[$(this).find(':selected').val()]);
		}else{
			$(this).parent().find('.alert').html(defaultmessage);
		}
		
		if($.inArray($(this).find(':selected').val(), soldout) > -1 && $(this).parent().parent().find('.' + fieldid).find('option').length > 1){
			$(this).parent().parent().find('.' + fieldid).show();
			//$(this).parent().find('.alert').html(soldoutmessages[$(this).val()]);
			$(this).parent().find('.alert').show();
			$('.' + fieldid + " select option:first").val($(this).val()).attr('selected', 'selected');
			
		}else{
			$(this).parent().parent().find('.' + fieldid).hide();
			$(this).parent().find('.alert').hide();
			$('.' + fieldid + " select option:first").val($(this).val()).attr('selected', 'selected');

		}
		
		if($.inArray($(this).find(':selected').val(), soldout) > -1  && typeof(soldoutmessages[$(this).find(':selected').val()]) != undefined){			
			$(this).parent().find('.alert').show();
		}else{
			$(this).parent().find('.alert').hide();
		}
	});
	
	$('.alternate > select.productoptions').change(function(){
		$(this).parent().parent().find(':not(.alternate) > select.productoptions').val('');
	});
	
	//uniform();	
});

function uniform(){
	//$("select, input[type=text], input[type=password], input[type=checkbox], input[type=radio], input[type=submit], a.button, button").uniform();	
}

function bindModal(){
	$('#dialogmodal').find('.title').remove();
	$('#dialogmodal form').submit(function(){
		$.post(
			$(this).attr('action'),
			$(this).serialize(),
			function(data){ 
				var successaction = $(data).find('#successaction').val();
				if(successaction != '' && successaction != undefined){
					window.location = successaction;
				}else{
					$('#dialogmodal').html(data);
					bindModal();
				}		
			}
		);
		return false;
	});
	$('.cancel').click(function(){
		if($(this).attr('href') == window.location.pathname){
			$('#dialogmodal').remove();
			return false;
		}
	});
	
	$('form div.input div.error-message').mouseenter(function(){
		$(this).fadeOut('fast');
	});
	$('form div.input input').focus(function(){
		$(this).parent().find('.error-message').fadeOut('fast');
	});	
	
	$('.phone-area input').keyup(function(){
		if($(this).val().length == 3){
			$('.phone-prefix input').focus();
		}
	});
	
	$('.phone-prefix input').keyup(function(){
		if($(this).val().length == 3){
			$('.phone-line input').focus();
		}
	});
	
	$('#dialogmodal #UserPreferenceFacebookAuto').click(function(){
		if($(this).attr('checked') == true){
			$('#dialogmodal #facebooksettings').show();
		}else{
			$('#dialogmodal #facebooksettings').hide();
		}
	});
	$('#dialogmodal #UserPreferenceFacebookAuto').each(function(){
		if($(this).attr('checked') == true){
			$('#dialogmodal #facebooksettings').show();
		}else{
			$('#dialogmodal #facebooksettings').hide();
		}
	});
	
	$('form div.input input').focus(function(){
		$(this).parent().find('.error-message').fadeOut('fast');
	});
	
//	$('.wysiwyg').wysiwyg({controls: {insertImage : {visible: false}, createLink : {visible: false}}});
	//$("#dialogmodal select, #dialogmodal input[type=text], #dialogmodal input[type=password], #dialogmodal input[type=checkbox], #dialogmodal input[type=radio], #dialogmodal input[type=submit], #dialogmodal a.button, #dialogmodal button").uniform();
/*
	$('.dialog').click(function(){
		$('#dialogmodal').remove();
		var dialog = $('<div id="dialogmodal"></div>').appendTo('body');
		$('#dialogmodal').load($(this).attr('href'),{},function(response, status, xhr){
			//var successaction = $('#dialogmodal').find('#successaction').val();
			var title = $('#dialogmodal').find('.title').html();
			bindModal($(this));
			$('#dialogmodal').dialog({
				title: title, 
				width: 600,
				modal: true
			});
			
		});
		return false;
	});
	*/
}

var modalWindow={
	close:function(){
		$('#modal').fadeOut('fast', function(){
			$('#modal').remove();
		});
	},
	open:function(content){									
		var modalContent = '<div id="modal">';
			modalContent += '<div id="overlay-container" class="loading"></div>';
			modalContent += '</div>';
		$('body').append(modalContent);
		loadTopMargin = ($(window).height() - 40) / 2;
		$('.loading').css({marginTop:loadTopMargin});
		$('#modal').fadeIn('fast', function(){
			$('#overlay-container').html(content);
			$('#overlay-container').css({marginTop:"0"});
			$('#overlay-container #content').css({marginTop:'50px'});
			$('#overlay-container').show();
			$('#modal-close').click(function(){
				modalWindow.close();
				return false;
			});
		});
	}
};

