jQuery('html').addClass('js');

function loadConversation(rowId, show, type) {
	if(type=='sp') {
		var posturl = '/service-provider/messages/conversation';
	}
	else {
		var posturl = '/customer/messages/conversation';
	}	
	$.post(posturl, { id: rowId }, function (data) {
		if($(".conversation-"+rowId).length) {
			$(".conversation-"+rowId).remove();
			$(".toggle-conversation-"+rowId).remove();
		}
		$(data).insertBefore($(".conversation-item-"+rowId));
		if(show) {
			$(".conversation-item-"+rowId).hide();
			$(".conversation-"+rowId).show();
			$(".toggle-conversation-"+rowId+" a").text('Hide full conversation');
		}
		toggleConversation();
	});
}

function hideMessageParts() {
	$(".conversation").hide();
	$(".conversation-item").show();
	$(".toggle-conversation a").text('View full conversation');
	$(".message-details").hide();
	$(".message-headline").removeClass("open-message");	
}

function landingToggleHeaderContent(thisItem) {
	if(thisItem.hasClass('closed-section')) {
		thisItem.removeClass('closed-section');
		thisItem.parent().removeClass('rounded-title-box-full');
		thisItem.parent().parent().addClass('rounded-shadow-box-full');
		$(".services-overview").removeClass('services-overview-closed');
	}
	else {
		thisItem.addClass('closed-section');
		thisItem.parent().addClass('rounded-title-box-full');
		thisItem.parent().parent().removeClass('rounded-shadow-box-full');
		$(".services-overview").addClass('services-overview-closed');
	}
}

function toggleConversation() {
	$(".toggle-conversation a").unbind('click').click(function() {
		conv = $(this).parent().siblings(".conversation");
		curr = $(this).parent().siblings(".conversation-item");
		if(conv.is(':visible')) {
			conv.hide();
			$(this).text('View full conversation');
			curr.show();
		}
		else {
			conv.show();
			$(this).text('Hide full conversation');
			curr.hide();
		}
		return false;
	}); 
}

function showQuoteRequestPage() {
	
	$("#step-two-content").html('');
	$(".errormsg").hide();
	$("#loading-consignment").show();
	$("#submitter-field").hide();
	$("#quote-request-form").attr('action', '/customer/quoterequest/category-selection/');
	
	if($("#sub_select_3_element").val()==15 || $("#sub_select_3_element").val()==16) {
		$("#loading-consignment").hide();
		$("#submitter-field").show();
		return;
	}
	
	$.ajax({
		type: "POST", 
		dataType: "html", 
		url: "/customer/quoterequest/category-selection/", 
		data: $("#quote-request-form").serialize(), 
		success: function(data) {
			$("#sidebar-collectiondelivery").hide();		
			$("#step-two-content").html(data);
			$("#loading-consignment").hide();
			questionAction();	
			sortTabIndex();
		}, 
		error: function(data) {
			$("#loading-consignment").hide();
			$("#submitter-field").show();
			$("#sidebar-collectiondelivery").show();
			$("#sidebar-steptwo").hide();
		}
	});
}

function sortTabIndex() { 
	var tabindex = 1;
	$('input,select').each(function() {
		if(this.type != "hidden" && !$(this).hasClass('shortwide')) {
			$(this).attr("tabindex", tabindex);
			tabindex++;
		}
	});
}

function questionAction() {
	$(".question").click(function() {
		var currentAnswerState = $(this).next().is(':visible');
		$('.answer').hide();
		if(currentAnswerState==true) {
			$(this).next().hide();
		}
		else {
			$(this).next().show();
		}
		return false;
	});
}

function updateMessage(rowId) {
	$.post('/customer/messages/update-message', { id: rowId }, 
		function(data) {
			
	});
}

function feedbackFader() {
	var current = $(".feedback-content li.current");
	current.removeClass("current");
	if(current.next().length) {
		var next = current.next();
	}
	else {
		var next = $(".feedback-content li").first();
	}
	var next = next.addClass("current");
	
	current.fadeOut(300, function() {
		next.fadeIn(300, function() {
			
		});
	});
	setTimeout(feedbackFader, 8000);
}

function serviceOverviewTitleAction(thisItem) {
	landingToggleHeaderContent(thisItem);
	if(thisItem.hasClass('closed-section')) {
		$("div.services-overview div.copy").hide();
		if($("div.services-overview div.foot").length) {
			$("div.services-overview div.foot").hide();
		}
	}
	else {
		$("div.services-overview div.copy").show();
		if($("div.services-overview div.foot").length) {
			$("div.services-overview div.foot").show();
		}
	}
	return false;
}

$(document).ready(function() {
	
	// get browser type and version
	var browser;
	jQuery.each(jQuery.browser, function(i) {
	  if($.browser.msie){	    
	    browser = 'ie' + jQuery.browser.version.substr(0,1);	    	    
	  } else if($.browser.opera){
	  	browser = 'opera';
	  } else if($.browser.mozilla){
	  	browser = 'mozilla';
	  } else if($.browser.safari){
	  	browser = 'safari';
	  }
	});
	$('body').addClass(browser);
	
	if(browser=='ie6') {
		$('ul#menu').appendTo('div#compare-menu');
	}
	
	if($("#content-third").length) {
		$("#content-third").insertBefore($("#content-first"));
	}
	if($("#content-first").length) {
		$("#content-first").insertAfter($("#content-fourth"));
	}
	
	if(!$("table.recently-completed").length && 
	   $("#landing-content-first-inner #service-overview-title").length) {
		serviceOverviewTitleAction($("#service-overview-title"));
	}
	
	if($("#landing-content-first").length) {
		if($("#landing-content-third").length) {
			$("#landing-content-first").insertAfter($("#landing-content-third"));
		}		
		else {
			$("#landing-content-first").insertAfter($("#landing-content-second"));
		}
		if($("table.recently-completed").length) {
			$("table.recently-completed").insertBefore($("div.services-overview"));
		}
		if($("table.services-provided").length) {
			$("table.services-provided").insertBefore($("div.services-overview"));
		}
		if($("div.how-it-works").length) {
			$("div.how-it-works").insertBefore($("div.services-overview"));
		}
	}
	
	if(!$(".js body.landing div#holder").is(':visible')) {
		$(".js body.landing div#holder").show();
	}
	
	questionAction();
	toggleConversation();
	
	if($(".feedback-content").length) {
		$(".feedback-content li").first().addClass("current").fadeIn(300);
		setTimeout(feedbackFader, 8000);
	}
	
	if($("#quote-request-form").length) {
		sortTabIndex();
	}
	
	if($(".tooltip-link").length) {
		$(".tooltip-link").tooltip({
			showURL: false
		}).unbind('click').click(function() {
			return false;
		});
	}
	
	$("#how-it-works-title").click(function() {
		landingToggleHeaderContent($(this));
		if($(".how-it-works .step").length) {
			if($(".how-it-works .step").is(':visible')) {
				$(".how-it-works .step").hide();
			}
			else {
				$(".how-it-works .step").show();
			}
		}
		else {
			$.ajax({
				type: "GET", 
				dataType: "html", 
				url: "/index/read-more/", 
				success: function(data) {
					$(data).insertAfter($(".how-it-works p.heading"));
				}
			});	
		}
		return false;
	});
	
	$("#service-overview-title").click(function() {
		serviceOverviewTitleAction($(this));
		return false;
	});
	
	$(".show-closed-bids").click(function() {
		if($(this).text()=="Show closed bids") {
			$(".replaced-bid").show();
			$(this).text("Hide closed bids");
		}
		else {
			$(".replaced-bid").hide();
			$(".replaced-bid-details").hide();
			$(this).text("Show closed bids");
		}
		return false;
	});
	
	$(".delete-message").click(function() {
		if(confirm("Are you sure you want to delete this message?")) {
			$(this).parent().submit();
		}
		else {
			return false;
		}
	});
	
	$(".delete-account").click(function() {
		if(confirm("Are you sure you want to delete your account?\n\nYou will be logged out and will no longer have access to the system.")) {
			return true;
		}
		else {
			return false;
		}
	});
	
	$(".open-help-section").click(function() {
		$(".help-section").hide();
		$("#section-"+$(this).prop("id")).show();
	});
	
	$("#main_select_home").click(function() {
		$(".selector-ele-sub-home option:selected").each(
			function() {
				$(this).parent().prop('selectedIndex', '');
			}
		);		
		if($("#main_select_home option[value='"+$(this).val()+"']").hasClass("sub-selector")) {
			$("#sub_select_disabled").hide();
			$(".selector-ele-sub-home").hide();
			$("#sub_select_" + $(this).val() + "_element").show();
		}
		else {
			$(".selector-ele-sub-home").hide();
			$("#sub_select_disabled").show();
		}
	});
	
	$(".category-panel").hover(function() {
		$(this).css("cursor", "pointer");
	});
	
	$(".category-panel").click(function() {
		window.location = $(this).find("a").attr("href");
		return false;
	});
	
	$("table.recently-completed a.heading-link").click(function() {
		if($(this).hasClass('closed-section')) {
			$(this).removeClass('closed-section');
			$("table.recently-completed tbody").show();
			$("table.recently-completed tr#headings").show();
			$("table.recently-completed td.table-title").removeClass('rounded-title-box-full');
			$("table.recently-completed").addClass('rounded-shadow-box-full');
			$("table.recently-completed").removeClass('recently-completed-closed');
		}
		else {
			$(this).addClass('closed-section');
			$("table.recently-completed tbody").hide();
			$("table.recently-completed tr#headings").hide();
			$("table.recently-completed td.table-title").addClass('rounded-title-box-full');
			$("table.recently-completed").removeClass('rounded-shadow-box-full');
			$("table.recently-completed").addClass('recently-completed-closed');
		}
		return false;
	});
	
	$("#how-it-works-title-landing").click(function() {
		landingToggleHeaderContent($(this));
		if($(".how-it-works .step").length) {
			if($(".how-it-works .step").is(':visible')) {
				$(".how-it-works .step").hide();
			}
			else {
				$(".how-it-works .step").show();
			}
		}
		else {
			$.ajax({
				type: "GET", 
				dataType: "html", 
				url: "/index/landing-read-more/id/"+$(".how-it-works p.heading").attr('id')+"/", 
				success: function(data) {
					$(data).insertAfter($(".how-it-works p.heading"));
				}
			});	
		}
		return false;
	});
	
	$("#main_select").bind("click change", function() {
		if(!$("#main_select").val()) {
			return false;
		}
		$(".selector-field-sub").hide();
		$(".selector-ele-sub option:selected").each(
			function() {
				$(this).parent().prop('selectedIndex', '-1');
			}
		);		
		if($("#main_select option[value='"+$(this).val()+"']").hasClass("sub-selector")) {
			
			$("#sub_select_" + $(this).val() + "_div").show();
			
			$("#step-two-content").html('');
			$(".errormsg").hide();
			$("#submitter-field").show();
			$("#loading-consignment").hide();
			$("#sidebar-collectiondelivery").show();
			$("#sidebar-steptwo").hide();
			$("#quote-request-form").attr('action', '/customer/quoterequest/category-selection/');
		}
		else {
			showQuoteRequestPage();
		}
	});
	
	$(".selector-ele-sub").bind("click change", function() {
		showQuoteRequestPage();
	});
	
	$('input[type="text"]').each(function(){
		var cssClass = $(this).attr('type');
		$(this).addClass(cssClass);
	});
	
	$("#open-full-details").click(function() {
		var currentState = $("#full-load-details-content").is(':visible');
		if(currentState==true) {
			$("#full-load-details-content").hide();
			$("#open-full-details").text("View full delivery listing details");
		}
		else {
			$("#full-load-details-content").show();
			$("#open-full-details").text("Hide full delivery listing details");
			initialize();
		}
	});
	
	$(".open-bid").click(function() {
		var rowId = $(this).parent().parent().attr("id");
		var detailRow = rowId + '-details';
		if($("#"+detailRow).is(":visible")) {
			$("#"+detailRow).hide();
			$("#"+rowId).removeClass("open-bid");
		}
		else {	
			$(".bid-headline").removeClass("open-bid");
			$(".bid-details").hide();
			$("#"+detailRow).show();
			$("#"+rowId).addClass("open-bid");
		}		
		return false;
	});
		
	$(".showmessage").click(function() {
		var rowId = $(this).parent().parent().attr("id");
		var detailRow = rowId + '-details';
		if($("#"+rowId).hasClass("open-message")) {
			hideMessageParts();
			return false;
		}
		$("td.bottom-right").removeClass("bottom-right-remove");
		$("td.bottom-left").removeClass("bottom-left-remove");
		hideMessageParts();
		$("#"+rowId).addClass("open-message");
		$("#"+detailRow).show();
		$("#"+rowId+" td.bottom-right").addClass("bottom-right-remove");
		$("#"+rowId+" td.bottom-left").addClass("bottom-left-remove");
		if($("#"+rowId).hasClass("cust-mark-as-read")) {
			$.post('/customer/messages/mark-as-read', { id: rowId }, function (data) {
				$("#"+data.rowId).removeClass("messageunread");
				$("#"+data.rowId).removeClass("cust-mark-as-read");
				$(".inbox-unread").text(data.inboxUnreadCount);
			}, "json");
		}
		else if($("#"+rowId).hasClass("sp-mark-as-read")) {
			$.post('/service-provider/messages/mark-as-read', { id: rowId }, function (data) {
				$("#"+data.rowId).removeClass("messageunread");
				$("#"+data.rowId).removeClass("mark-as-read");
				$(".inbox-unread").text(data.inboxUnreadCount);
			}, "json");
		}
		if($(".cust-conversation").length) {
			loadConversation(rowId, false, 'cust');
		}
		else if($(".sp-conversation").length) {
			loadConversation(rowId, false, 'sp');
		}
		return false;
	}); 
	
	$(".toggle-new-question-form").click(function() {
		$(this).parent().siblings(".form-display").toggle();
		return false;
	});	
	
	$(".new-question-form").submit(function() {
		$.post($(this).attr('action'), $(this).serialize(),  
			function(data) {
				if(data!=null && data.area!='') {
					$("#new-form-"+data.area).html('');
					loadConversation('row-'+data.area, true, data.type);
				}
			}, 
		"json");
		return false;
	});
	
	$(".cust-response-form").submit(function() {
		$.post('/customer/messages/update-message', $(this).serialize(),   
			function(data) {
				if(data.message_content!=='') {
					$("span#"+data.update_area).html(data.message_content);
				}
		}, "json");
		
		return false;
	});
	
	$(".sp-response-form").submit(function() {
		$.post('/service-provider/messages/update-message', $(this).serialize(),   
			function(data) {
				if(data.message_content!=='') {
					$("span#"+data.update_area).html(data.message_content);
				}
		}, "json");
		
		return false;
	});
}); 

